1 /*
2  * Copyright (C) 2019 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.permissioncontroller.permission.ui.auto;
18 
19 import androidx.fragment.app.FragmentTransaction;
20 import androidx.preference.Preference;
21 
22 import com.android.permissioncontroller.R;
23 import com.android.permissioncontroller.permission.model.legacy.PermissionGroup;
24 import com.android.permissioncontroller.permission.utils.Utils;
25 
26 import java.util.List;
27 
28 /** Shows the standard permissions that can be granted/denied. */
29 public class AutoManageStandardPermissionsFragment extends AutoManagePermissionsFragment {
30 
31     private static final String EXTRA_PREFS_KEY = "extra_prefs_key";
32 
33     /** Returns a new instance of {@link AutoManageStandardPermissionsFragment}. */
newInstance()34     public static AutoManageStandardPermissionsFragment newInstance() {
35         return new AutoManageStandardPermissionsFragment();
36     }
37 
38     @Override
getScreenHeaderRes()39     protected int getScreenHeaderRes() {
40         return R.string.app_permission_manager;
41     }
42 
43     @Override
updatePermissionsUi()44     protected void updatePermissionsUi() {
45         updatePermissionsUi(/* addSystemPermissions= */ true);
46 
47         // Check if we need an additional permissions preference
48         List<PermissionGroup> groups = getPermissions().getGroups();
49         int numExtraPermissions = 0;
50         for (PermissionGroup group : groups) {
51             if (!group.getDeclaringPackage().equals(AutoManagePermissionsFragment.OS_PKG)) {
52                 numExtraPermissions++;
53             }
54         }
55 
56         Preference additionalPermissionsPreference = getPreferenceScreen().findPreference(
57                 EXTRA_PREFS_KEY);
58         if (numExtraPermissions == 0) {
59             if (additionalPermissionsPreference != null) {
60                 getPreferenceScreen().removePreference(additionalPermissionsPreference);
61             }
62         } else {
63             if (additionalPermissionsPreference == null) {
64                 additionalPermissionsPreference = new Preference(
65                         getPreferenceManager().getContext());
66                 additionalPermissionsPreference.setKey(EXTRA_PREFS_KEY);
67                 additionalPermissionsPreference.setIcon(Utils.applyTint(getActivity(),
68                         R.drawable.ic_more_items,
69                         android.R.attr.colorControlNormal));
70                 additionalPermissionsPreference.setTitle(R.string.additional_permissions);
71                 additionalPermissionsPreference.setOnPreferenceClickListener(preference -> {
72                     AutoManageCustomPermissionsFragment frag =
73                             new AutoManageCustomPermissionsFragment();
74                     frag.setTargetFragment(AutoManageStandardPermissionsFragment.this,
75                             /* requestCode= */ 0);
76                     FragmentTransaction ft = getFragmentManager().beginTransaction();
77                     ft.replace(android.R.id.content, frag);
78                     ft.addToBackStack(null);
79                     ft.commit();
80                     return true;
81                 });
82 
83                 getPreferenceScreen().addPreference(additionalPermissionsPreference);
84             }
85 
86             additionalPermissionsPreference.setSummary(getResources().getQuantityString(
87                     R.plurals.additional_permissions_more, numExtraPermissions,
88                     numExtraPermissions));
89         }
90     }
91 }
92