jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.settings.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; |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 21 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 22 | import androidx.preference.Preference; |
| 23 | |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 24 | import com.android.settings.core.BasePreferenceController; |
| 25 | import com.android.settings.slices.SliceData; |
| 26 | import com.android.settingslib.RestrictedLockUtils; |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 27 | import com.android.settingslib.RestrictedLockUtilsInternal; |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 28 | import com.android.settingslib.RestrictedSwitchPreference; |
| 29 | |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 30 | public class ContactSearchPreferenceController extends BasePreferenceController implements |
| 31 | Preference.OnPreferenceChangeListener { |
| 32 | |
| 33 | private UserHandle mManagedUser; |
| 34 | |
| 35 | public ContactSearchPreferenceController(Context context, String key) { |
| 36 | super(context, key); |
| 37 | } |
| 38 | |
| 39 | public void setManagedUser(UserHandle managedUser) { |
| 40 | mManagedUser = managedUser; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public int getAvailabilityStatus() { |
| 45 | return (mManagedUser != null) ? AVAILABLE : DISABLED_FOR_USER; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 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 = |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 56 | RestrictedLockUtilsInternal.checkIfRemoteContactSearchDisallowed( |
jason_chiu | c69102d | 2018-04-03 11:37:09 +0800 | [diff] [blame] | 57 | mContext, mManagedUser.getIdentifier()); |
| 58 | pref.setDisabledByAdmin(enforcedAdmin); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 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 | |
| 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 |
| 81 | public final boolean onPreferenceChange(Preference preference, Object newValue) { |
| 82 | return setChecked((boolean) newValue); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | @SliceData.SliceType |
| 87 | public int getSliceType() { |
| 88 | return SliceData.SliceType.SWITCH; |
| 89 | } |
| 90 | } |