• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.systemui.statusbar.phone;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.os.UserManager;
22 import android.provider.Settings;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.accessibility.AccessibilityEvent;
28 import android.view.accessibility.AccessibilityNodeInfo;
29 import android.widget.Button;
30 import android.widget.FrameLayout;
31 
32 import com.android.systemui.Dependency;
33 import com.android.systemui.Prefs;
34 import com.android.systemui.Prefs.Key;
35 import com.android.systemui.R;
36 import com.android.systemui.plugins.qs.DetailAdapter;
37 import com.android.systemui.qs.QSPanel;
38 import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
39 import com.android.systemui.statusbar.policy.UserSwitcherController;
40 
41 /**
42  * Container for image of the multi user switcher (tappable).
43  */
44 public class MultiUserSwitch extends FrameLayout implements View.OnClickListener {
45 
46     protected QSPanel mQsPanel;
47     private KeyguardUserSwitcher mKeyguardUserSwitcher;
48     private boolean mKeyguardMode;
49     private UserSwitcherController.BaseUserAdapter mUserListener;
50 
51     final UserManager mUserManager;
52 
53     private final int[] mTmpInt2 = new int[2];
54 
55     protected UserSwitcherController mUserSwitcherController;
56 
MultiUserSwitch(Context context, AttributeSet attrs)57     public MultiUserSwitch(Context context, AttributeSet attrs) {
58         super(context, attrs);
59         mUserManager = UserManager.get(getContext());
60     }
61 
62     @Override
onFinishInflate()63     protected void onFinishInflate() {
64         super.onFinishInflate();
65         setOnClickListener(this);
66         refreshContentDescription();
67     }
68 
setQsPanel(QSPanel qsPanel)69     public void setQsPanel(QSPanel qsPanel) {
70         mQsPanel = qsPanel;
71         setUserSwitcherController(Dependency.get(UserSwitcherController.class));
72     }
73 
hasMultipleUsers()74     public boolean hasMultipleUsers() {
75         if (mUserListener == null) {
76             return false;
77         }
78         return mUserListener.getUserCount() != 0
79                 && Prefs.getBoolean(getContext(), Key.SEEN_MULTI_USER, false);
80     }
81 
setUserSwitcherController(UserSwitcherController userSwitcherController)82     public void setUserSwitcherController(UserSwitcherController userSwitcherController) {
83         mUserSwitcherController = userSwitcherController;
84         registerListener();
85         refreshContentDescription();
86     }
87 
setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher)88     public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
89         mKeyguardUserSwitcher = keyguardUserSwitcher;
90     }
91 
setKeyguardMode(boolean keyguardShowing)92     public void setKeyguardMode(boolean keyguardShowing) {
93         mKeyguardMode = keyguardShowing;
94         registerListener();
95     }
96 
isMultiUserEnabled()97     public boolean isMultiUserEnabled() {
98         // Short-circuiting from UserManager. Needs to be extracted because of SystemUI boolean flag
99         // qs_show_user_switcher_for_single_user
100 
101         // The default in UserManager is to show the switcher. We want to not show it unless the
102         // user explicitly requests it in Settings
103         final boolean userSwitcherEnabled = Settings.Global.getInt(mContext.getContentResolver(),
104                 Settings.Global.USER_SWITCHER_ENABLED, 0) != 0;
105 
106         if (!UserManager.supportsMultipleUsers()
107                 || mUserManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH)
108                 || UserManager.isDeviceInDemoMode(mContext)
109                 || !userSwitcherEnabled) {
110             return false;
111         }
112 
113         final boolean guestEnabled = !mContext.getSystemService(DevicePolicyManager.class)
114                 .getGuestUserDisabled(null);
115         return mUserSwitcherController.getSwitchableUserCount() > 1
116                 // If we cannot add guests even if they are enabled, do not show
117                 || (guestEnabled && !mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER))
118                 || mContext.getResources().getBoolean(R.bool.qs_show_user_switcher_for_single_user);
119     }
120 
registerListener()121     private void registerListener() {
122         if (mUserManager.isUserSwitcherEnabled() && mUserListener == null) {
123 
124             final UserSwitcherController controller = mUserSwitcherController;
125             if (controller != null) {
126                 mUserListener = new UserSwitcherController.BaseUserAdapter(controller) {
127                     @Override
128                     public void notifyDataSetChanged() {
129                         refreshContentDescription();
130                     }
131 
132                     @Override
133                     public View getView(int position, View convertView, ViewGroup parent) {
134                         return null;
135                     }
136                 };
137                 refreshContentDescription();
138             }
139         }
140     }
141 
142     @Override
onClick(View v)143     public void onClick(View v) {
144         if (mKeyguardMode) {
145             if (mKeyguardUserSwitcher != null) {
146                 mKeyguardUserSwitcher.show(true /* animate */);
147             }
148         } else if (mQsPanel != null && mUserSwitcherController != null) {
149             View center = getChildCount() > 0 ? getChildAt(0) : this;
150 
151             center.getLocationInWindow(mTmpInt2);
152             mTmpInt2[0] += center.getWidth() / 2;
153             mTmpInt2[1] += center.getHeight() / 2;
154 
155             mQsPanel.showDetailAdapter(true,
156                     getUserDetailAdapter(),
157                     mTmpInt2);
158         }
159     }
160 
161     @Override
setClickable(boolean clickable)162     public void setClickable(boolean clickable) {
163         super.setClickable(clickable);
164         refreshContentDescription();
165     }
166 
refreshContentDescription()167     private void refreshContentDescription() {
168         String currentUser = null;
169         if (mUserManager.isUserSwitcherEnabled()
170                 && mUserSwitcherController != null) {
171             currentUser = mUserSwitcherController.getCurrentUserName(mContext);
172         }
173 
174         String text = null;
175 
176         if (!TextUtils.isEmpty(currentUser)) {
177             text = mContext.getString(
178                     R.string.accessibility_quick_settings_user,
179                     currentUser);
180         }
181 
182         if (!TextUtils.equals(getContentDescription(), text)) {
183             setContentDescription(text);
184         }
185     }
186 
187     @Override
onInitializeAccessibilityEvent(AccessibilityEvent event)188     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
189         super.onInitializeAccessibilityEvent(event);
190         event.setClassName(Button.class.getName());
191     }
192 
193     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)194     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
195         super.onInitializeAccessibilityNodeInfo(info);
196         info.setClassName(Button.class.getName());
197     }
198 
199     @Override
hasOverlappingRendering()200     public boolean hasOverlappingRendering() {
201         return false;
202     }
203 
getUserDetailAdapter()204     protected DetailAdapter getUserDetailAdapter() {
205         return mUserSwitcherController.userDetailAdapter;
206     }
207 }
208