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