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 android.content.BroadcastReceiver;
17 import android.content.Context;
18 import android.content.Intent;
19 import android.content.IntentFilter;
20 import android.os.UserHandle;
21 import android.os.UserManager;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settings.slices.SliceData;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnStart;
34 import com.android.settingslib.core.lifecycle.events.OnStop;
35 
36 public class WorkModePreferenceController extends BasePreferenceController implements
37         Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop {
38 
39     private static final String TAG = "WorkModeController";
40 
41     private UserManager mUserManager;
42     private UserHandle mManagedUser;
43 
44     private Preference mPreference;
45     private IntentFilter mIntentFilter;
46 
WorkModePreferenceController(Context context, String key)47     public WorkModePreferenceController(Context context, String key) {
48         super(context, key);
49         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
50         mIntentFilter = new IntentFilter();
51         mIntentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
52         mIntentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
53     }
54 
setManagedUser(UserHandle managedUser)55     public void setManagedUser(UserHandle managedUser) {
56         mManagedUser = managedUser;
57     }
58 
59     @Override
onStart()60     public void onStart() {
61         mContext.registerReceiver(mReceiver, mIntentFilter);
62     }
63 
64     @Override
onStop()65     public void onStop() {
66         mContext.unregisterReceiver(mReceiver);
67     }
68 
69     @Override
getAvailabilityStatus()70     public int getAvailabilityStatus() {
71         return (mManagedUser != null) ? AVAILABLE : DISABLED_FOR_USER;
72     }
73 
74     @Override
displayPreference(PreferenceScreen screen)75     public void displayPreference(PreferenceScreen screen) {
76         super.displayPreference(screen);
77         mPreference = screen.findPreference(getPreferenceKey());
78     }
79 
80     @Override
getSummary()81     public CharSequence getSummary() {
82         return mContext.getText(isChecked()
83                 ? R.string.work_mode_on_summary
84                 : R.string.work_mode_off_summary);
85     }
86 
isChecked()87     private boolean isChecked() {
88         boolean isWorkModeOn = false;
89         if (mUserManager != null && mManagedUser != null) {
90             isWorkModeOn = !mUserManager.isQuietModeEnabled(mManagedUser);
91         }
92         return isWorkModeOn;
93     }
94 
setChecked(boolean isChecked)95     private boolean setChecked(boolean isChecked) {
96         if (mUserManager != null && mManagedUser != null) {
97             final boolean quietModeEnabled = !isChecked;
98             mUserManager.requestQuietModeEnabled(quietModeEnabled, mManagedUser);
99         }
100         return true;
101     }
102 
103     @Override
updateState(Preference preference)104     public void updateState(Preference preference) {
105         super.updateState(preference);
106         if (preference instanceof TwoStatePreference) {
107             ((TwoStatePreference) preference).setChecked(isChecked());
108         }
109     }
110 
111     @Override
onPreferenceChange(Preference preference, Object newValue)112     public final boolean onPreferenceChange(Preference preference, Object newValue) {
113         return setChecked((boolean) newValue);
114     }
115 
116     /**
117      * Receiver that listens to {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
118      * {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE}, and updates the work mode
119      */
120     @VisibleForTesting
121     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
122         @Override
123         public void onReceive(Context context, Intent intent) {
124             if (intent == null) {
125                 return;
126             }
127             final String action = intent.getAction();
128             Log.v(TAG, "Received broadcast: " + action);
129 
130             if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action)
131                     || Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
132                 if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
133                         UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
134                     updateState(mPreference);
135                 }
136                 return;
137             }
138             Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
139         }
140     };
141 
142     @Override
143     @SliceData.SliceType
getSliceType()144     public int getSliceType() {
145         return SliceData.SliceType.SWITCH;
146     }
147 }