blob: 8b94ba1f8536531e37e08253091d84be2ab0c563 [file] [log] [blame]
jason_chiuc69102d2018-04-03 11:37:09 +08001/*
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 */
14package com.android.settings.accounts;
15
16import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH;
17
18import android.content.Context;
19import android.os.UserHandle;
20import android.provider.Settings;
jason_chiuc69102d2018-04-03 11:37:09 +080021
Fan Zhang23f8d592018-08-28 15:11:40 -070022import androidx.preference.Preference;
23
jason_chiuc69102d2018-04-03 11:37:09 +080024import com.android.settings.core.BasePreferenceController;
25import com.android.settings.slices.SliceData;
26import com.android.settingslib.RestrictedLockUtils;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070027import com.android.settingslib.RestrictedLockUtilsInternal;
jason_chiuc69102d2018-04-03 11:37:09 +080028import com.android.settingslib.RestrictedSwitchPreference;
29
jason_chiuc69102d2018-04-03 11:37:09 +080030public 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. Moltmanne3f72112018-08-28 15:01:43 -070056 RestrictedLockUtilsInternal.checkIfRemoteContactSearchDisallowed(
jason_chiuc69102d2018-04-03 11:37:09 +080057 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}