• 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 
17 package com.android.settings.applications.appinfo;
18 
19 import android.content.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.icu.text.ListFormatter;
24 import android.support.annotation.VisibleForTesting;
25 import android.support.v7.preference.Preference;
26 import android.util.Log;
27 
28 import com.android.settings.R;
29 import com.android.settingslib.applications.PermissionsSummaryHelper;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 public class AppPermissionPreferenceController extends AppInfoPreferenceControllerBase {
35 
36     private static final String TAG = "PermissionPrefControl";
37     private static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton";
38 
39     private String mPackageName;
40 
41     @VisibleForTesting
42     final PermissionsSummaryHelper.PermissionsResultCallback mPermissionCallback
43             = new PermissionsSummaryHelper.PermissionsResultCallback() {
44         @Override
45         public void onPermissionSummaryResult(int standardGrantedPermissionCount,
46                 int requestedPermissionCount, int additionalGrantedPermissionCount,
47                 List<CharSequence> grantedGroupLabels) {
48             if (mParent.getActivity() == null) {
49                 return;
50             }
51             final Resources res = mContext.getResources();
52             CharSequence summary = null;
53 
54             if (requestedPermissionCount == 0) {
55                 summary = res.getString(
56                         R.string.runtime_permissions_summary_no_permissions_requested);
57                 mPreference.setEnabled(false);
58             } else {
59                 final ArrayList<CharSequence> list = new ArrayList<>(grantedGroupLabels);
60                 if (additionalGrantedPermissionCount > 0) {
61                     // N additional permissions.
62                     list.add(res.getQuantityString(
63                             R.plurals.runtime_permissions_additional_count,
64                             additionalGrantedPermissionCount, additionalGrantedPermissionCount));
65                 }
66                 if (list.size() == 0) {
67                     summary = res.getString(
68                             R.string.runtime_permissions_summary_no_permissions_granted);
69                 } else {
70                     summary = ListFormatter.getInstance().format(list);
71                 }
72                 mPreference.setEnabled(true);
73             }
74             mPreference.setSummary(summary);
75         }
76     };
77 
AppPermissionPreferenceController(Context context, String key)78     public AppPermissionPreferenceController(Context context, String key) {
79         super(context, key);
80     }
81 
82     @Override
updateState(Preference preference)83     public void updateState(Preference preference) {
84         PermissionsSummaryHelper.getPermissionSummary(mContext, mPackageName, mPermissionCallback);
85     }
86 
87     @Override
handlePreferenceTreeClick(Preference preference)88     public boolean handlePreferenceTreeClick(Preference preference) {
89         if (getPreferenceKey().equals(preference.getKey())) {
90             startManagePermissionsActivity();
91             return true;
92         }
93         return false;
94     }
95 
setPackageName(String packageName)96     public void setPackageName(String packageName) {
97         mPackageName = packageName;
98     }
99 
startManagePermissionsActivity()100     private void startManagePermissionsActivity() {
101         // start new activity to manage app permissions
102         final Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS);
103         intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mParent.getAppEntry().info.packageName);
104         intent.putExtra(EXTRA_HIDE_INFO_BUTTON, true);
105         try {
106             mParent.getActivity().startActivityForResult(intent, mParent.SUB_INFO_FRAGMENT);
107         } catch (ActivityNotFoundException e) {
108             Log.w(TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS");
109         }
110     }
111 }
112