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.content.Context;
20 import android.content.Intent;
21 import android.os.UserManager;
22 import android.provider.ContactsContract;
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.ActivityStarter;
37 import com.android.systemui.plugins.qs.DetailAdapter;
38 import com.android.systemui.qs.QSPanel;
39 import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
40 import com.android.systemui.statusbar.policy.UserSwitcherController;
41 
42 /**
43  * Container for image of the multi user switcher (tappable).
44  */
45 public class MultiUserSwitch extends FrameLayout implements View.OnClickListener {
46 
47     protected QSPanel mQsPanel;
48     private KeyguardUserSwitcher mKeyguardUserSwitcher;
49     private boolean mKeyguardMode;
50     private UserSwitcherController.BaseUserAdapter mUserListener;
51 
52     final UserManager mUserManager;
53 
54     private final int[] mTmpInt2 = new int[2];
55 
56     protected UserSwitcherController mUserSwitcherController;
57 
MultiUserSwitch(Context context, AttributeSet attrs)58     public MultiUserSwitch(Context context, AttributeSet attrs) {
59         super(context, attrs);
60         mUserManager = UserManager.get(getContext());
61     }
62 
63     @Override
onFinishInflate()64     protected void onFinishInflate() {
65         super.onFinishInflate();
66         setOnClickListener(this);
67         refreshContentDescription();
68     }
69 
setQsPanel(QSPanel qsPanel)70     public void setQsPanel(QSPanel qsPanel) {
71         mQsPanel = qsPanel;
72         setUserSwitcherController(Dependency.get(UserSwitcherController.class));
73     }
74 
hasMultipleUsers()75     public boolean hasMultipleUsers() {
76         if (mUserListener == null) {
77             return false;
78         }
79         return mUserListener.getUserCount() != 0
80                 && Prefs.getBoolean(getContext(), Key.SEEN_MULTI_USER, false);
81     }
82 
setUserSwitcherController(UserSwitcherController userSwitcherController)83     public void setUserSwitcherController(UserSwitcherController userSwitcherController) {
84         mUserSwitcherController = userSwitcherController;
85         registerListener();
86         refreshContentDescription();
87     }
88 
setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher)89     public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
90         mKeyguardUserSwitcher = keyguardUserSwitcher;
91     }
92 
setKeyguardMode(boolean keyguardShowing)93     public void setKeyguardMode(boolean keyguardShowing) {
94         mKeyguardMode = keyguardShowing;
95         registerListener();
96     }
97 
registerListener()98     private void registerListener() {
99         if (mUserManager.isUserSwitcherEnabled() && mUserListener == null) {
100 
101             final UserSwitcherController controller = mUserSwitcherController;
102             if (controller != null) {
103                 mUserListener = new UserSwitcherController.BaseUserAdapter(controller) {
104                     @Override
105                     public void notifyDataSetChanged() {
106                         refreshContentDescription();
107                     }
108 
109                     @Override
110                     public View getView(int position, View convertView, ViewGroup parent) {
111                         return null;
112                     }
113                 };
114                 refreshContentDescription();
115             }
116         }
117     }
118 
119     @Override
onClick(View v)120     public void onClick(View v) {
121         if (mUserManager.isUserSwitcherEnabled()) {
122             if (mKeyguardMode) {
123                 if (mKeyguardUserSwitcher != null) {
124                     mKeyguardUserSwitcher.show(true /* animate */);
125                 }
126             } else if (mQsPanel != null && mUserSwitcherController != null) {
127                 View center = getChildCount() > 0 ? getChildAt(0) : this;
128 
129                 center.getLocationInWindow(mTmpInt2);
130                 mTmpInt2[0] += center.getWidth() / 2;
131                 mTmpInt2[1] += center.getHeight() / 2;
132 
133                 mQsPanel.showDetailAdapter(true,
134                         getUserDetailAdapter(),
135                         mTmpInt2);
136             }
137         } else {
138             if (mQsPanel != null) {
139                 Intent intent = ContactsContract.QuickContact.composeQuickContactsIntent(
140                         getContext(), v, ContactsContract.Profile.CONTENT_URI,
141                         ContactsContract.QuickContact.MODE_LARGE, null);
142                 Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard(intent, 0);
143             }
144         }
145     }
146 
147     @Override
setClickable(boolean clickable)148     public void setClickable(boolean clickable) {
149         super.setClickable(clickable);
150         refreshContentDescription();
151     }
152 
refreshContentDescription()153     private void refreshContentDescription() {
154         String currentUser = null;
155         if (mUserManager.isUserSwitcherEnabled()
156                 && mUserSwitcherController != null) {
157             currentUser = mUserSwitcherController.getCurrentUserName(mContext);
158         }
159 
160         String text = null;
161 
162         if (!TextUtils.isEmpty(currentUser)) {
163             text = mContext.getString(
164                     R.string.accessibility_quick_settings_user,
165                     currentUser);
166         }
167 
168         if (!TextUtils.equals(getContentDescription(), text)) {
169             setContentDescription(text);
170         }
171     }
172 
173     @Override
onInitializeAccessibilityEvent(AccessibilityEvent event)174     public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
175         super.onInitializeAccessibilityEvent(event);
176         event.setClassName(Button.class.getName());
177     }
178 
179     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)180     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
181         super.onInitializeAccessibilityNodeInfo(info);
182         info.setClassName(Button.class.getName());
183     }
184 
185     @Override
hasOverlappingRendering()186     public boolean hasOverlappingRendering() {
187         return false;
188     }
189 
getUserDetailAdapter()190     protected DetailAdapter getUserDetailAdapter() {
191         return mUserSwitcherController.userDetailAdapter;
192     }
193 }
194