1 /* 2 * Copyright (C) 2018 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.car.settings.applications; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.ActivityNotFoundException; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.res.Resources; 24 import android.icu.text.ListFormatter; 25 import android.text.TextUtils; 26 27 import androidx.preference.Preference; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.FragmentController; 31 import com.android.car.settings.common.Logger; 32 import com.android.car.settings.common.PreferenceController; 33 import com.android.settingslib.applications.PermissionsSummaryHelper; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** Business logic for the permissions entry in the application details settings. */ 39 public class PermissionsPreferenceController extends PreferenceController<Preference> { 40 41 private static final Logger LOG = new Logger(PermissionsPreferenceController.class); 42 43 private String mPackageName; 44 private String mSummary; 45 PermissionsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46 public PermissionsPreferenceController(Context context, String preferenceKey, 47 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 48 super(context, preferenceKey, fragmentController, uxRestrictions); 49 } 50 51 @Override getPreferenceType()52 protected Class<Preference> getPreferenceType() { 53 return Preference.class; 54 } 55 56 /** 57 * Set the packageName, which is used on the intent to open the permissions 58 * selection screen. 59 */ setPackageName(String packageName)60 public void setPackageName(String packageName) { 61 mPackageName = packageName; 62 } 63 64 @Override checkInitialized()65 protected void checkInitialized() { 66 if (mPackageName == null) { 67 throw new IllegalStateException( 68 "PackageName should be set before calling this function"); 69 } 70 } 71 72 @Override onStartInternal()73 protected void onStartInternal() { 74 PermissionsSummaryHelper.getPermissionSummary(getContext(), mPackageName, 75 mPermissionCallback); 76 } 77 78 @Override updateState(Preference preference)79 protected void updateState(Preference preference) { 80 preference.setSummary(getSummary()); 81 } 82 83 @Override handlePreferenceClicked(Preference preference)84 protected boolean handlePreferenceClicked(Preference preference) { 85 Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS); 86 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName); 87 try { 88 getContext().startActivity(intent); 89 } catch (ActivityNotFoundException e) { 90 LOG.w("No app can handle android.intent.action.MANAGE_APP_PERMISSIONS"); 91 } 92 return true; 93 } 94 getSummary()95 private CharSequence getSummary() { 96 if (TextUtils.isEmpty(mSummary)) { 97 return getContext().getString(R.string.computing_size); 98 } 99 return mSummary; 100 } 101 102 private final PermissionsSummaryHelper.PermissionsResultCallback mPermissionCallback = 103 new PermissionsSummaryHelper.PermissionsResultCallback() { 104 @Override 105 public void onPermissionSummaryResult(int standardGrantedPermissionCount, 106 int requestedPermissionCount, int additionalGrantedPermissionCount, 107 List<CharSequence> grantedGroupLabels) { 108 Resources res = getContext().getResources(); 109 110 if (requestedPermissionCount == 0) { 111 mSummary = res.getString( 112 R.string.runtime_permissions_summary_no_permissions_requested); 113 } else { 114 ArrayList<CharSequence> list = new ArrayList<>(grantedGroupLabels); 115 if (additionalGrantedPermissionCount > 0) { 116 // N additional permissions. 117 list.add(res.getQuantityString( 118 R.plurals.runtime_permissions_additional_count, 119 additionalGrantedPermissionCount, 120 additionalGrantedPermissionCount)); 121 } 122 if (list.isEmpty()) { 123 mSummary = res.getString( 124 R.string.runtime_permissions_summary_no_permissions_granted); 125 } else { 126 mSummary = ListFormatter.getInstance().format(list); 127 } 128 } 129 refreshUi(); 130 } 131 }; 132 } 133