• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.profiles;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.os.UserManager;
22 
23 import androidx.annotation.StringRes;
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.PreferenceGroup;
26 import androidx.preference.SwitchPreference;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.ui.preference.CarUiSwitchPreference;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * Constructs and populates the permissions toggles for non admin profiles.
37  *
38  * <p>In order to add a new permission, it needs to be added to {@link
39  * android.car.userlib.UserHelper#OPTIONAL_NON_ADMIN_RESTRICTIONS} and the appropriate label needs
40  * to be added to {@link #PERMISSIONS_LIST}.
41  */
42 public class PermissionsPreferenceController extends
43         ProfileDetailsBasePreferenceController<PreferenceGroup> {
44 
45     private static class ProfilePermission {
46         private final String mPermissionKey;
47         @StringRes
48         private final int mPermissionTitle;
49 
ProfilePermission(String key, int title)50         ProfilePermission(String key, int title) {
51             mPermissionKey = key;
52             mPermissionTitle = title;
53         }
54 
getPermissionKey()55         public String getPermissionKey() {
56             return mPermissionKey;
57         }
58 
getPermissionTitle()59         public int getPermissionTitle() {
60             return mPermissionTitle;
61         }
62     }
63 
64     @VisibleForTesting
65     static final String PERMISSION_TYPE_KEY = "permission_type_key";
66     private static final List<ProfilePermission> PERMISSIONS_LIST = new ArrayList<>();
67 
68     // Add additional preferences to show here (in the order they should appear).
69     static {
PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_ADD_USER, R.string.create_user_permission_title))70         PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_ADD_USER,
71                 R.string.create_user_permission_title));
PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_OUTGOING_CALLS, R.string.outgoing_calls_permission_title))72         PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_OUTGOING_CALLS,
73                 R.string.outgoing_calls_permission_title));
PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_SMS, R.string.sms_messaging_permission_title))74         PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_SMS,
75                 R.string.sms_messaging_permission_title));
PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_INSTALL_APPS, R.string.install_apps_permission_title))76         PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_INSTALL_APPS,
77                 R.string.install_apps_permission_title));
PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_UNINSTALL_APPS, R.string.uninstall_apps_permission_title))78         PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_UNINSTALL_APPS,
79                 R.string.uninstall_apps_permission_title));
80     }
81 
82     private final List<SwitchPreference> mPermissionPreferences = new ArrayList<>();
83 
PermissionsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)84     public PermissionsPreferenceController(Context context, String preferenceKey,
85             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
86         super(context, preferenceKey, fragmentController, uxRestrictions);
87 
88         for (ProfilePermission permission : PERMISSIONS_LIST) {
89             SwitchPreference preference = new CarUiSwitchPreference(context);
90             preference.setTitle(permission.getPermissionTitle());
91             preference.getExtras().putString(PERMISSION_TYPE_KEY, permission.getPermissionKey());
92             preference.setOnPreferenceChangeListener((pref, newValue) -> {
93                 boolean granted = (boolean) newValue;
94                 UserManager.get(context).setUserRestriction(
95                         pref.getExtras().getString(PERMISSION_TYPE_KEY),
96                         !granted,
97                         getUserInfo().getUserHandle());
98                 return true;
99             });
100             mPermissionPreferences.add(preference);
101         }
102     }
103 
104     @Override
getPreferenceType()105     protected Class<PreferenceGroup> getPreferenceType() {
106         return PreferenceGroup.class;
107     }
108 
109     @Override
onCreateInternal()110     protected void onCreateInternal() {
111         super.onCreateInternal();
112         for (SwitchPreference switchPreference : mPermissionPreferences) {
113             getPreference().addPreference(switchPreference);
114         }
115     }
116 
117     @Override
updateState(PreferenceGroup preferenceGroup)118     protected void updateState(PreferenceGroup preferenceGroup) {
119         for (SwitchPreference switchPreference : mPermissionPreferences) {
120             UserManager userManager = UserManager.get(getContext());
121             switchPreference.setChecked(
122                     !userManager.hasUserRestriction(
123                             switchPreference.getExtras().getString(PERMISSION_TYPE_KEY),
124                             getUserInfo().getUserHandle()));
125         }
126     }
127 }
128