• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.mDisallowSwitchUser
67                     && !mUserCapabilities.mIsGuest && mUserCapabilities.isAdmin());
68         }
69         mSwitchBar.setListener(this);
70     }
71 
72     @Override
onStart()73     public void onStart() {
74         mSwitchBar.startListening();
75     }
76 
77     @Override
onStop()78     public void onStop() {
79         mSwitchBar.stopListening();
80     }
81 
82     @Override
onSwitchToggled(boolean isChecked)83     public boolean onSwitchToggled(boolean isChecked) {
84         Log.d(TAG, "Toggling multi-user feature enabled state to: " + isChecked);
85         final boolean success = Settings.Global.putInt(mContext.getContentResolver(),
86                 Settings.Global.USER_SWITCHER_ENABLED, isChecked ? 1 : 0);
87         if (success && mListener != null) {
88             mListener.onMultiUserSwitchChanged(isChecked);
89         }
90         return success;
91     }
92 }
93