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 androidx.annotation.Nullable;
20 import androidx.annotation.StringRes;
21 import android.content.Context;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.graphics.drawable.Drawable;
26 
27 import com.android.documentsui.base.DocumentInfo;
28 
29 /**
30  * Abstract class for using ActionView with different actions.
31  */
32 public abstract class Action {
33 
34     public static final String APP_NAME_UNKNOWN = "unknown";
35     public static final String APP_NOT_CHOSEN = "android";
36 
37     protected Context mContext;
38     protected PackageManager mPm;
39     protected DocumentInfo mDoc;
40 
Action(Context context, PackageManager pm, DocumentInfo doc)41     public Action(Context context, PackageManager pm, DocumentInfo doc) {
42         assert context != null;
43         assert pm != null;
44         assert doc != null;
45         mContext = context;
46         mPm = pm;
47         mDoc = doc;
48     }
49 
getHeader()50     public abstract String getHeader();
51 
getButtonIcon()52     public abstract int getButtonIcon();
53 
canPerformAction()54     public abstract boolean canPerformAction();
55 
getPackageName()56     public abstract @Nullable String getPackageName();
57 
getButtonLabel()58     public abstract @StringRes int getButtonLabel();
59 
getAppIcon()60     public @Nullable Drawable getAppIcon() {
61 
62         String packageName = getPackageName();
63         Drawable icon = null;
64         if (packageName == null || APP_NOT_CHOSEN.equals(packageName)) {
65             return null;
66         }
67 
68         try {
69             icon = mPm.getApplicationIcon(packageName);
70         } catch(NameNotFoundException e) {
71             icon = null;
72         }
73         return icon;
74     }
75 
getAppName()76     public String getAppName() {
77 
78         String packageName = getPackageName();
79         if (packageName == null) {
80             return APP_NAME_UNKNOWN;
81         }
82 
83         ApplicationInfo appInfo = null;
84         String appName = "";
85 
86         if (APP_NOT_CHOSEN.equals(packageName)) {
87             return APP_NOT_CHOSEN;
88         }
89 
90         try {
91             appInfo = mPm.getApplicationInfo(packageName, 0);
92             appName = (String) mPm.getApplicationLabel(appInfo);
93         } catch (NameNotFoundException e) {
94             appName = APP_NAME_UNKNOWN;
95         }
96         return appName;
97     }
98 }