Home | History | Annotate | Download | only in actions
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.documentsui.inspector.actions;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.content.pm.ApplicationInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.graphics.drawable.Drawable;
     25 
     26 import com.android.documentsui.base.DocumentInfo;
     27 
     28 /**
     29  * Abstract class for using ActionView with different actions.
     30  */
     31 public abstract class Action {
     32 
     33     public static final String APP_NAME_UNKNOWN = "unknown";
     34     public static final String APP_NOT_CHOSEN = "android";
     35 
     36     protected Context mContext;
     37     protected PackageManager mPm;
     38     protected DocumentInfo mDoc;
     39 
     40     public Action(Context context, PackageManager pm, DocumentInfo doc) {
     41         assert context != null;
     42         assert pm != null;
     43         assert doc != null;
     44         mContext = context;
     45         mPm = pm;
     46         mDoc = doc;
     47     }
     48 
     49     public abstract String getHeader();
     50 
     51     public abstract int getButtonIcon();
     52 
     53     public abstract boolean canPerformAction();
     54 
     55     public abstract @Nullable String getPackageName();
     56 
     57     public @Nullable Drawable getAppIcon() {
     58 
     59         String packageName = getPackageName();
     60         Drawable icon = null;
     61         if (packageName == null || APP_NOT_CHOSEN.equals(packageName)) {
     62             return null;
     63         }
     64 
     65         try {
     66             icon = mPm.getApplicationIcon(packageName);
     67         } catch(NameNotFoundException e) {
     68             icon = null;
     69         }
     70         return icon;
     71     }
     72 
     73     public String getAppName() {
     74 
     75         String packageName = getPackageName();
     76         if (packageName == null) {
     77             return APP_NAME_UNKNOWN;
     78         }
     79 
     80         ApplicationInfo appInfo = null;
     81         String appName = "";
     82 
     83         if (APP_NOT_CHOSEN.equals(packageName)) {
     84             return APP_NOT_CHOSEN;
     85         }
     86 
     87         try {
     88             appInfo = mPm.getApplicationInfo(packageName, 0);
     89             appName = (String) mPm.getApplicationLabel(appInfo);
     90         } catch (NameNotFoundException e) {
     91             appName = APP_NAME_UNKNOWN;
     92         }
     93         return appName;
     94     }
     95 }