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.UserHandle;
22 import android.os.UserManager;
23 import android.provider.ContactsContract;
24 import android.text.TextUtils;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.FrameLayout;
29 
30 import com.android.systemui.R;
31 import com.android.systemui.qs.QSPanel;
32 import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
33 import com.android.systemui.statusbar.policy.UserSwitcherController;
34 
35 /**
36  * Container for image of the multi user switcher (tappable).
37  */
38 public class MultiUserSwitch extends FrameLayout implements View.OnClickListener {
39 
40     private QSPanel mQsPanel;
41     private KeyguardUserSwitcher mKeyguardUserSwitcher;
42     private boolean mKeyguardMode;
43     private UserSwitcherController.BaseUserAdapter mUserListener;
44 
45     final UserManager mUserManager;
46 
47     private final int[] mTmpInt2 = new int[2];
48 
49     private UserSwitcherController mUserSwitcherController;
50 
MultiUserSwitch(Context context, AttributeSet attrs)51     public MultiUserSwitch(Context context, AttributeSet attrs) {
52         super(context, attrs);
53         mUserManager = UserManager.get(getContext());
54     }
55 
56     @Override
onFinishInflate()57     protected void onFinishInflate() {
58         super.onFinishInflate();
59         setOnClickListener(this);
60         refreshContentDescription();
61     }
62 
setQsPanel(QSPanel qsPanel)63     public void setQsPanel(QSPanel qsPanel) {
64         mQsPanel = qsPanel;
65         setUserSwitcherController(qsPanel.getHost().getUserSwitcherController());
66     }
67 
setUserSwitcherController(UserSwitcherController userSwitcherController)68     public void setUserSwitcherController(UserSwitcherController userSwitcherController) {
69         mUserSwitcherController = userSwitcherController;
70         registerListener();
71         refreshContentDescription();
72     }
73 
setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher)74     public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
75         mKeyguardUserSwitcher = keyguardUserSwitcher;
76     }
77 
setKeyguardMode(boolean keyguardShowing)78     public void setKeyguardMode(boolean keyguardShowing) {
79         mKeyguardMode = keyguardShowing;
80         registerListener();
81     }
82 
registerListener()83     private void registerListener() {
84         if (UserSwitcherController.isUserSwitcherAvailable(mUserManager) && mUserListener == null) {
85 
86             final UserSwitcherController controller = mUserSwitcherController;
87             if (controller != null) {
88                 mUserListener = new UserSwitcherController.BaseUserAdapter(controller) {
89                     @Override
90                     public void notifyDataSetChanged() {
91                         refreshContentDescription();
92                     }
93 
94                     @Override
95                     public View getView(int position, View convertView, ViewGroup parent) {
96                         return null;
97                     }
98                 };
99                 refreshContentDescription();
100             }
101         }
102     }
103 
104     @Override
onClick(View v)105     public void onClick(View v) {
106         if (UserSwitcherController.isUserSwitcherAvailable(mUserManager)) {
107             if (mKeyguardMode) {
108                 if (mKeyguardUserSwitcher != null) {
109                     mKeyguardUserSwitcher.show(true /* animate */);
110                 }
111             } else if (mQsPanel != null && mUserSwitcherController != null) {
112                 View center = getChildCount() > 0 ? getChildAt(0) : this;
113 
114                 center.getLocationInWindow(mTmpInt2);
115                 mTmpInt2[0] += center.getWidth() / 2;
116                 mTmpInt2[1] += center.getHeight() / 2;
117 
118                 mQsPanel.showDetailAdapter(true,
119                         mUserSwitcherController.userDetailAdapter,
120                         mTmpInt2);
121             }
122         } else {
123             Intent intent = ContactsContract.QuickContact.composeQuickContactsIntent(
124                     getContext(), v, ContactsContract.Profile.CONTENT_URI,
125                     ContactsContract.QuickContact.MODE_LARGE, null);
126             getContext().startActivityAsUser(intent, new UserHandle(UserHandle.USER_CURRENT));
127         }
128     }
129 
130     @Override
setClickable(boolean clickable)131     public void setClickable(boolean clickable) {
132         super.setClickable(clickable);
133         refreshContentDescription();
134     }
135 
refreshContentDescription()136     private void refreshContentDescription() {
137         String currentUser = null;
138         if (UserSwitcherController.isUserSwitcherAvailable(mUserManager)
139                 && mUserSwitcherController != null) {
140             currentUser = mUserSwitcherController.getCurrentUserName(mContext);
141         }
142 
143         String text = null;
144         if (isClickable()) {
145             if (UserSwitcherController.isUserSwitcherAvailable(mUserManager)) {
146                 if (TextUtils.isEmpty(currentUser)) {
147                     text = mContext.getString(R.string.accessibility_multi_user_switch_switcher);
148                 } else {
149                     text = mContext.getString(
150                             R.string.accessibility_multi_user_switch_switcher_with_current,
151                             currentUser);
152                 }
153             } else {
154                 text = mContext.getString(R.string.accessibility_multi_user_switch_quick_contact);
155             }
156         } else {
157             if (!TextUtils.isEmpty(currentUser)) {
158                 text = mContext.getString(
159                         R.string.accessibility_multi_user_switch_inactive,
160                         currentUser);
161             }
162         }
163 
164         if (!TextUtils.equals(getContentDescription(), text)) {
165             setContentDescription(text);
166         }
167     }
168 
169     @Override
hasOverlappingRendering()170     public boolean hasOverlappingRendering() {
171         return false;
172     }
173 
174 }
175