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.os.UserManager;
21 import android.provider.Settings;
22 
23 import androidx.lifecycle.DefaultLifecycleObserver;
24 import androidx.lifecycle.LifecycleOwner;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settings.slices.SliceData;
32 import com.android.settingslib.RestrictedLockUtils;
33 import com.android.settingslib.RestrictedLockUtilsInternal;
34 import com.android.settingslib.RestrictedSwitchPreference;
35 
36 import org.jetbrains.annotations.NotNull;
37 
38 public class ContactSearchPreferenceController extends TogglePreferenceController implements
39         Preference.OnPreferenceChangeListener, DefaultLifecycleObserver,
40         ManagedProfileQuietModeEnabler.QuietModeChangeListener {
41 
42     private final ManagedProfileQuietModeEnabler mQuietModeEnabler;
43     private final UserHandle mManagedUser;
44     private Preference mPreference;
45 
ContactSearchPreferenceController(Context context, String key)46     public ContactSearchPreferenceController(Context context, String key) {
47         super(context, key);
48         mManagedUser = Utils.getManagedProfile(context.getSystemService(UserManager.class));
49         mQuietModeEnabler = new ManagedProfileQuietModeEnabler(context, this);
50     }
51 
52     @Override
getAvailabilityStatus()53     public int getAvailabilityStatus() {
54         return mQuietModeEnabler.isAvailable() ? AVAILABLE : DISABLED_FOR_USER;
55     }
56 
57     @Override
updateState(Preference preference)58     public void updateState(Preference preference) {
59         super.updateState(preference);
60         if (preference instanceof RestrictedSwitchPreference) {
61             final RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
62             pref.setChecked(isChecked());
63             pref.setEnabled(!mQuietModeEnabler.isQuietModeEnabled());
64             if (mManagedUser != null) {
65                 final RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
66                         RestrictedLockUtilsInternal.checkIfRemoteContactSearchDisallowed(
67                                 mContext, mManagedUser.getIdentifier());
68                 pref.setDisabledByAdmin(enforcedAdmin);
69             }
70         }
71     }
72 
73     @Override
displayPreference(PreferenceScreen screen)74     public void displayPreference(PreferenceScreen screen) {
75         super.displayPreference(screen);
76         mPreference = screen.findPreference(getPreferenceKey());
77         updateState(mPreference);
78     }
79 
80     @Override
onStart(@otNull LifecycleOwner lifecycleOwner)81     public void onStart(@NotNull LifecycleOwner lifecycleOwner) {
82         lifecycleOwner.getLifecycle().addObserver(mQuietModeEnabler);
83     }
84 
85     @Override
onStop(@otNull LifecycleOwner lifecycleOwner)86     public void onStop(@NotNull LifecycleOwner lifecycleOwner) {
87         lifecycleOwner.getLifecycle().removeObserver(mQuietModeEnabler);
88     }
89 
90     @Override
isChecked()91     public boolean isChecked() {
92         if (mManagedUser == null || mQuietModeEnabler.isQuietModeEnabled()) {
93             return false;
94         }
95         return 0 != Settings.Secure.getIntForUser(mContext.getContentResolver(),
96                 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier());
97     }
98 
99     @Override
setChecked(boolean isChecked)100     public boolean setChecked(boolean isChecked) {
101         if (mManagedUser == null || mQuietModeEnabler.isQuietModeEnabled()) {
102             return false;
103         }
104         final int value = isChecked ? 1 : 0;
105         Settings.Secure.putIntForUser(mContext.getContentResolver(),
106                 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, value, mManagedUser.getIdentifier());
107         return true;
108     }
109 
110     @Override
onQuietModeChanged()111     public void onQuietModeChanged() {
112         if (mPreference != null) {
113             updateState(mPreference);
114         }
115     }
116 
117     @Override
118     @SliceData.SliceType
getSliceType()119     public int getSliceType() {
120         return SliceData.SliceType.SWITCH;
121     }
122 
123     @Override
getSliceHighlightMenuRes()124     public int getSliceHighlightMenuRes() {
125         return R.string.menu_key_accounts;
126     }
127 }
128