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