1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.car.developeroptions.accounts; 15 16 import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH; 17 18 import android.content.Context; 19 import android.os.UserHandle; 20 import android.provider.Settings; 21 22 import androidx.preference.Preference; 23 24 import com.android.car.developeroptions.core.BasePreferenceController; 25 import com.android.car.developeroptions.slices.SliceData; 26 import com.android.settingslib.RestrictedLockUtils; 27 import com.android.settingslib.RestrictedLockUtilsInternal; 28 import com.android.settingslib.RestrictedSwitchPreference; 29 30 public class ContactSearchPreferenceController extends BasePreferenceController implements 31 Preference.OnPreferenceChangeListener { 32 33 private UserHandle mManagedUser; 34 ContactSearchPreferenceController(Context context, String key)35 public ContactSearchPreferenceController(Context context, String key) { 36 super(context, key); 37 } 38 setManagedUser(UserHandle managedUser)39 public void setManagedUser(UserHandle managedUser) { 40 mManagedUser = managedUser; 41 } 42 43 @Override getAvailabilityStatus()44 public int getAvailabilityStatus() { 45 return (mManagedUser != null) ? AVAILABLE : DISABLED_FOR_USER; 46 } 47 48 @Override updateState(Preference preference)49 public void updateState(Preference preference) { 50 super.updateState(preference); 51 if (preference instanceof RestrictedSwitchPreference) { 52 final RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 53 pref.setChecked(isChecked()); 54 if (mManagedUser != null) { 55 final RestrictedLockUtils.EnforcedAdmin enforcedAdmin = 56 RestrictedLockUtilsInternal.checkIfRemoteContactSearchDisallowed( 57 mContext, mManagedUser.getIdentifier()); 58 pref.setDisabledByAdmin(enforcedAdmin); 59 } 60 } 61 } 62 isChecked()63 private boolean isChecked() { 64 if (mManagedUser == null) { 65 return false; 66 } 67 return 0 != Settings.Secure.getIntForUser(mContext.getContentResolver(), 68 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier()); 69 } 70 setChecked(boolean isChecked)71 private boolean setChecked(boolean isChecked) { 72 if (mManagedUser != null) { 73 final int value = isChecked ? 1 : 0; 74 Settings.Secure.putIntForUser(mContext.getContentResolver(), 75 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, value, mManagedUser.getIdentifier()); 76 } 77 return true; 78 } 79 80 @Override onPreferenceChange(Preference preference, Object newValue)81 public final boolean onPreferenceChange(Preference preference, Object newValue) { 82 return setChecked((boolean) newValue); 83 } 84 85 @Override 86 @SliceData.SliceType getSliceType()87 public int getSliceType() { 88 return SliceData.SliceType.SWITCH; 89 } 90 }