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 * com.android.car.internal.user.UserHelper#OPTIONAL_NON_ADMIN_RESTRICTIONS} and 40 * the appropriate label needs 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_INSTALL_APPS, R.string.install_apps_permission_title))74 PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_INSTALL_APPS, 75 R.string.install_apps_permission_title)); PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_UNINSTALL_APPS, R.string.uninstall_apps_permission_title))76 PERMISSIONS_LIST.add(new ProfilePermission(UserManager.DISALLOW_UNINSTALL_APPS, 77 R.string.uninstall_apps_permission_title)); 78 } 79 80 private final List<SwitchPreference> mPermissionPreferences = new ArrayList<>(); 81 PermissionsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)82 public PermissionsPreferenceController(Context context, String preferenceKey, 83 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 84 super(context, preferenceKey, fragmentController, uxRestrictions); 85 86 for (ProfilePermission permission : PERMISSIONS_LIST) { 87 SwitchPreference preference = new CarUiSwitchPreference(context); 88 preference.setTitle(permission.getPermissionTitle()); 89 preference.getExtras().putString(PERMISSION_TYPE_KEY, permission.getPermissionKey()); 90 preference.setOnPreferenceChangeListener((pref, newValue) -> { 91 boolean granted = (boolean) newValue; 92 UserManager.get(context).setUserRestriction( 93 pref.getExtras().getString(PERMISSION_TYPE_KEY), 94 !granted, 95 getUserInfo().getUserHandle()); 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 super.onCreateInternal(); 110 for (SwitchPreference switchPreference : mPermissionPreferences) { 111 getPreference().addPreference(switchPreference); 112 } 113 } 114 115 @Override updateState(PreferenceGroup preferenceGroup)116 protected void updateState(PreferenceGroup preferenceGroup) { 117 for (SwitchPreference switchPreference : mPermissionPreferences) { 118 UserManager userManager = UserManager.get(getContext()); 119 switchPreference.setChecked( 120 !userManager.hasUserRestriction( 121 switchPreference.getExtras().getString(PERMISSION_TYPE_KEY), 122 getUserInfo().getUserHandle())); 123 } 124 } 125 } 126