1 /* 2 * Copyright (C) 2022 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.settings.users; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.os.Bundle; 22 import android.os.UserManager; 23 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.core.TogglePreferenceController; 28 29 /** 30 * Controls the preference on the user settings screen which determines whether the guest user 31 * should have access to telephony or not. 32 */ 33 public class GuestTelephonyPreferenceController extends TogglePreferenceController { 34 35 private final UserManager mUserManager; 36 private final UserCapabilities mUserCaps; 37 GuestTelephonyPreferenceController(Context context, String preferenceKey)38 public GuestTelephonyPreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 mUserManager = context.getSystemService(UserManager.class); 41 mUserCaps = UserCapabilities.create(context); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 if (!mUserCaps.isAdmin() || !mUserCaps.mCanAddGuest) { 47 return DISABLED_FOR_USER; 48 } else { 49 return mUserCaps.mUserSwitcherEnabled ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 50 } 51 } 52 53 @Override isChecked()54 public boolean isChecked() { 55 return !mUserManager.getDefaultGuestRestrictions() 56 .getBoolean(UserManager.DISALLOW_OUTGOING_CALLS, false); 57 } 58 59 @Override setChecked(boolean isChecked)60 public boolean setChecked(boolean isChecked) { 61 Bundle guestRestrictions = mUserManager.getDefaultGuestRestrictions(); 62 guestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); 63 guestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, !isChecked); 64 mUserManager.setDefaultGuestRestrictions(guestRestrictions); 65 return true; 66 } 67 68 @Override getSliceHighlightMenuRes()69 public int getSliceHighlightMenuRes() { 70 return R.string.menu_key_system; 71 } 72 73 @Override updateState(Preference preference)74 public void updateState(Preference preference) { 75 super.updateState(preference); 76 mUserCaps.updateAddUserCapabilities(mContext); 77 preference.setVisible(isAvailable() && mUserCaps.mUserSwitcherEnabled 78 && mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)); 79 } 80 } 81