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