• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.documentsui.inspector.actions;
17 
18 import android.annotation.StringRes;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 
24 import com.android.documentsui.R;
25 import com.android.documentsui.base.DocumentInfo;
26 
27 import java.util.List;
28 
29 /**
30  * Model for clearing the default app that opens a file.
31  */
32 public final class ClearDefaultAppAction extends Action {
33 
ClearDefaultAppAction(Context context, PackageManager pm, DocumentInfo doc)34     public ClearDefaultAppAction(Context context, PackageManager pm, DocumentInfo doc) {
35         super(context, pm, doc);
36     }
37 
38     /**
39      * @return the header for this action. In English it would be "This file opens with"
40      */
41     @Override
getHeader()42     public String getHeader() {
43         return mContext.getString(R.string.handler_app_file_opens_with);
44     }
45 
46     @Override
getButtonIcon()47     public int getButtonIcon() {
48         return R.drawable.ic_action_clear;
49     }
50 
51     /**
52      * Checks if we can clear the default app to open a file.
53      *
54      * @return true if we can clear, false if we can't clear.
55      */
56     @Override
canPerformAction()57     public boolean canPerformAction() {
58         Intent intent = new Intent(Intent.ACTION_VIEW, mDoc.derivedUri);
59         List<ResolveInfo> list = mPm.queryIntentActivities(intent,
60             PackageManager.MATCH_DEFAULT_ONLY);
61 
62         if (list.size() > 1) {
63             String packageName = getPackageName();
64             if (packageName == null) {
65                 return false;
66             } else if (APP_NOT_CHOSEN.equals(packageName)) {
67                 return false;
68             } else {
69                 return true;
70             }
71         } else {
72             return false;
73         }
74     }
75 
76     @Override
getPackageName()77     public String getPackageName() {
78 
79         Intent intent = new Intent(Intent.ACTION_VIEW, mDoc.derivedUri);
80         ResolveInfo resolveInfo = mPm.resolveActivity(intent, 0);
81 
82         if (resolveInfo != null && resolveInfo.activityInfo != null) {
83             return resolveInfo.activityInfo.packageName;
84         } else {
85             return null;
86         }
87     }
88 
getButtonLabel()89     public @StringRes int getButtonLabel() {
90         return R.string.button_clear;
91     }
92 }