1 /* 2 * Copyright (C) 2016 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.packageinstaller.permission.ui.handheld; 17 18 import static com.android.packageinstaller.Constants.EXTRA_SESSION_ID; 19 20 import android.os.Bundle; 21 import android.view.MenuItem; 22 23 import androidx.fragment.app.FragmentTransaction; 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.packageinstaller.permission.model.PermissionGroup; 28 import com.android.packageinstaller.permission.utils.Utils; 29 import com.android.permissioncontroller.R; 30 31 import java.util.List; 32 33 /** 34 * Fragment that allows the user to manage standard permissions. 35 */ 36 public final class ManageStandardPermissionsFragment extends ManagePermissionsFragment { 37 private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; 38 private static final int MAXIMUM_APP_COUNT = 3; 39 40 /** 41 * @return A new fragment 42 */ newInstance(long sessionId)43 public static ManageStandardPermissionsFragment newInstance(long sessionId) { 44 ManageStandardPermissionsFragment fragment = new ManageStandardPermissionsFragment(); 45 Bundle arguments = new Bundle(); 46 arguments.putLong(EXTRA_SESSION_ID, sessionId); 47 fragment.setArguments(arguments); 48 return fragment; 49 } 50 51 @Override onCreate(Bundle icicle)52 public void onCreate(Bundle icicle) { 53 super.onCreate(icicle); 54 } 55 56 @Override onStart()57 public void onStart() { 58 super.onStart(); 59 60 getActivity().setTitle(com.android.permissioncontroller.R.string.app_permission_manager); 61 } 62 63 @Override onOptionsItemSelected(MenuItem item)64 public boolean onOptionsItemSelected(MenuItem item) { 65 if (item.getItemId() == android.R.id.home) { 66 getActivity().finish(); 67 return true; 68 } 69 return super.onOptionsItemSelected(item); 70 } 71 72 @Override updatePermissionsUi()73 protected void updatePermissionsUi() { 74 PreferenceScreen screen = updatePermissionsUi(true); 75 if (screen == null) { 76 return; 77 } 78 79 // Check if we need an additional permissions preference 80 List<PermissionGroup> groups = getPermissions().getGroups(); 81 int numExtraPermissions = 0; 82 for (PermissionGroup group : groups) { 83 if (!group.getDeclaringPackage().equals(ManagePermissionsFragment.OS_PKG)) { 84 numExtraPermissions++; 85 } 86 } 87 88 Preference additionalPermissionsPreference = screen.findPreference(EXTRA_PREFS_KEY); 89 if (numExtraPermissions == 0) { 90 if (additionalPermissionsPreference != null) { 91 screen.removePreference(additionalPermissionsPreference); 92 } 93 } else { 94 if (additionalPermissionsPreference == null) { 95 additionalPermissionsPreference = new Preference( 96 getPreferenceManager().getContext()); 97 additionalPermissionsPreference.setKey(EXTRA_PREFS_KEY); 98 additionalPermissionsPreference.setIcon(Utils.applyTint(getActivity(), 99 R.drawable.ic_more_items, 100 android.R.attr.colorControlNormal)); 101 additionalPermissionsPreference.setTitle(R.string.additional_permissions); 102 additionalPermissionsPreference.setOnPreferenceClickListener(preference -> { 103 long sessionId = getArguments().getLong(EXTRA_SESSION_ID); 104 ManageCustomPermissionsFragment frag = 105 ManageCustomPermissionsFragment.newInstance(sessionId); 106 frag.setTargetFragment(ManageStandardPermissionsFragment.this, 0); 107 FragmentTransaction ft = getFragmentManager().beginTransaction(); 108 ft.replace(android.R.id.content, frag); 109 ft.addToBackStack(null); 110 ft.commit(); 111 return true; 112 }); 113 114 screen.addPreference(additionalPermissionsPreference); 115 } 116 117 additionalPermissionsPreference.setSummary(getResources().getQuantityString( 118 R.plurals.additional_permissions_more, numExtraPermissions, 119 numExtraPermissions)); 120 } 121 } 122 } 123