1 /*
2  * Copyright (C) 2018 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 package com.android.settings.accounts;
17 
18 import android.content.Context;
19 import android.widget.CompoundButton;
20 import android.widget.CompoundButton.OnCheckedChangeListener;
21 
22 import androidx.lifecycle.DefaultLifecycleObserver;
23 import androidx.lifecycle.LifecycleOwner;
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.slices.SliceData;
30 import com.android.settingslib.widget.MainSwitchPreference;
31 
32 import org.jetbrains.annotations.NotNull;
33 
34 
35 /** Controller for "Work apps" toggle that allows the user to enable/disable quiet mode. */
36 public class WorkModePreferenceController extends BasePreferenceController
37         implements OnCheckedChangeListener, DefaultLifecycleObserver,
38         ManagedProfileQuietModeEnabler.QuietModeChangeListener {
39 
40     private final ManagedProfileQuietModeEnabler mQuietModeEnabler;
41     private MainSwitchPreference mPreference;
42 
WorkModePreferenceController(Context context, String key)43     public WorkModePreferenceController(Context context, String key) {
44         super(context, key);
45         mQuietModeEnabler = new ManagedProfileQuietModeEnabler(context, this);
46     }
47 
48     @Override
displayPreference(PreferenceScreen screen)49     public void displayPreference(PreferenceScreen screen) {
50         super.displayPreference(screen);
51         mPreference = screen.findPreference(getPreferenceKey());
52         mPreference.addOnSwitchChangeListener(this);
53     }
54 
55     @Override
getAvailabilityStatus()56     public int getAvailabilityStatus() {
57         return (mQuietModeEnabler.isAvailable()) ? AVAILABLE : DISABLED_FOR_USER;
58     }
59 
60     @Override
onStart(@otNull LifecycleOwner lifecycleOwner)61     public void onStart(@NotNull LifecycleOwner lifecycleOwner) {
62         lifecycleOwner.getLifecycle().addObserver(mQuietModeEnabler);
63     }
64 
65     @Override
onStop(@otNull LifecycleOwner lifecycleOwner)66     public void onStop(@NotNull LifecycleOwner lifecycleOwner) {
67         lifecycleOwner.getLifecycle().removeObserver(mQuietModeEnabler);
68     }
69 
70     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)71     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
72         mQuietModeEnabler.setQuietModeEnabled(!isChecked);
73         if (android.app.admin.flags.Flags.quietModeCredentialBugFix()) {
74             updateState(mPreference);
75         }
76     }
77 
78     @Override
updateState(Preference preference)79     public final void updateState(Preference preference) {
80         mPreference.updateStatus(!mQuietModeEnabler.isQuietModeEnabled());
81     }
82 
83     @Override
onQuietModeChanged()84     public void onQuietModeChanged() {
85         updateState(mPreference);
86     }
87 
88     @Override
89     @SliceData.SliceType
getSliceType()90     public int getSliceType() {
91         return SliceData.SliceType.SWITCH;
92     }
93 
94     @Override
getSliceHighlightMenuRes()95     public int getSliceHighlightMenuRes() {
96         return R.string.menu_key_accounts;
97     }
98 }
99