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.users; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.car.userlib.CarUserManagerHelper; 21 import android.content.Context; 22 import android.os.UserManager; 23 24 import androidx.annotation.StringRes; 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.PreferenceGroup; 27 import androidx.preference.SwitchPreference; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.FragmentController; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * Constructs and populates the permissions toggles for non admin users. 37 * 38 * <p>In order to add a new permission, it needs to be added to {@link 39 * CarUserManagerHelper#OPTIONAL_NON_ADMIN_RESTRICTIONS} and the appropriate label needs to be added 40 * to {@link #PERMISSIONS_LIST}. 41 */ 42 public class PermissionsPreferenceController extends 43 UserDetailsBasePreferenceController<PreferenceGroup> { 44 45 private static class UserPermission { 46 private final String mPermissionKey; 47 @StringRes 48 private final int mPermissionTitle; 49 UserPermission(String key, int title)50 UserPermission(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<UserPermission> PERMISSIONS_LIST = new ArrayList<>(); 67 68 // Add additional preferences to show here (in the order they should appear). 69 static { PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_ADD_USER, R.string.create_user_permission_title))70 PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_ADD_USER, 71 R.string.create_user_permission_title)); PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_OUTGOING_CALLS, R.string.outgoing_calls_permission_title))72 PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_OUTGOING_CALLS, 73 R.string.outgoing_calls_permission_title)); PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_SMS, R.string.sms_messaging_permission_title))74 PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_SMS, 75 R.string.sms_messaging_permission_title)); PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_INSTALL_APPS, R.string.install_apps_permission_title))76 PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_INSTALL_APPS, 77 R.string.install_apps_permission_title)); PERMISSIONS_LIST.add(new UserPermission(UserManager.DISALLOW_UNINSTALL_APPS, R.string.uninstall_apps_permission_title))78 PERMISSIONS_LIST.add(new UserPermission(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 (UserPermission permission : PERMISSIONS_LIST) { 89 SwitchPreference preference = new SwitchPreference(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 getCarUserManagerHelper().setUserRestriction(getUserInfo(), 95 pref.getExtras().getString(PERMISSION_TYPE_KEY), !granted); 96 return true; 97 }); 98 mPermissionPreferences.add(preference); 99 } 100 } 101 102 @Override getPreferenceType()103 protected Class<PreferenceGroup> getPreferenceType() { 104 return PreferenceGroup.class; 105 } 106 107 @Override onCreateInternal()108 protected void onCreateInternal() { 109 for (SwitchPreference switchPreference : mPermissionPreferences) { 110 getPreference().addPreference(switchPreference); 111 } 112 } 113 114 @Override updateState(PreferenceGroup preferenceGroup)115 protected void updateState(PreferenceGroup preferenceGroup) { 116 for (SwitchPreference switchPreference : mPermissionPreferences) { 117 switchPreference.setChecked(!getCarUserManagerHelper().hasUserRestriction( 118 switchPreference.getExtras().getString(PERMISSION_TYPE_KEY), getUserInfo())); 119 } 120 } 121 } 122