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