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 
17 package com.android.settings.users;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.os.UserManager;
22 import android.provider.Settings;
23 import android.util.Log;
24 
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.settings.widget.SwitchWidgetController;
28 import com.android.settingslib.RestrictedLockUtilsInternal;
29 import com.android.settingslib.core.lifecycle.LifecycleObserver;
30 import com.android.settingslib.core.lifecycle.events.OnStart;
31 import com.android.settingslib.core.lifecycle.events.OnStop;
32 
33 public class MultiUserSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener,
34         LifecycleObserver, OnStart, OnStop {
35     private static final String TAG = "MultiUserSwitchBarCtrl";
36 
37     interface OnMultiUserSwitchChangedListener {
onMultiUserSwitchChanged(boolean newState)38         void onMultiUserSwitchChanged(boolean newState);
39     }
40     @VisibleForTesting
41     final SwitchWidgetController mSwitchBar;
42 
43     private final Context mContext;
44     private final UserCapabilities mUserCapabilities;
45     private final OnMultiUserSwitchChangedListener mListener;
46 
47 
MultiUserSwitchBarController(Context context, SwitchWidgetController switchBar, OnMultiUserSwitchChangedListener listener)48     MultiUserSwitchBarController(Context context, SwitchWidgetController switchBar,
49             OnMultiUserSwitchChangedListener listener) {
50         mContext = context;
51         mSwitchBar = switchBar;
52         mListener = listener;
53         mUserCapabilities = UserCapabilities.create(context);
54         mSwitchBar.setChecked(mUserCapabilities.mUserSwitcherEnabled);
55 
56         if (mUserCapabilities.mDisallowSwitchUser) {
57             mSwitchBar.setDisabledByAdmin(RestrictedLockUtilsInternal
58                     .checkIfRestrictionEnforced(mContext, UserManager.DISALLOW_USER_SWITCH,
59                             UserHandle.myUserId()));
60 
61         } else if (mUserCapabilities.mDisallowAddUser) {
62             mSwitchBar.setDisabledByAdmin(RestrictedLockUtilsInternal
63                     .checkIfRestrictionEnforced(mContext, UserManager.DISALLOW_ADD_USER,
64                             UserHandle.myUserId()));
65         } else {
66             mSwitchBar.setEnabled(mUserCapabilities.mIsMain);
67         }
68         mSwitchBar.setListener(this);
69     }
70 
71     @Override
onStart()72     public void onStart() {
73         mSwitchBar.startListening();
74     }
75 
76     @Override
onStop()77     public void onStop() {
78         mSwitchBar.stopListening();
79     }
80 
81     @Override
onSwitchToggled(boolean isChecked)82     public boolean onSwitchToggled(boolean isChecked) {
83         Log.d(TAG, "Toggling multi-user feature enabled state to: " + isChecked);
84         final boolean success = Settings.Global.putInt(mContext.getContentResolver(),
85                 Settings.Global.USER_SWITCHER_ENABLED, isChecked ? 1 : 0);
86         if (success && mListener != null) {
87             mListener.onMultiUserSwitchChanged(isChecked);
88         }
89         return success;
90     }
91 }
92