blob: af9454ceab1352582f3ad153521cf976f5483da3 [file] [log] [blame]
Jorim Jaggi3d878be2014-05-10 03:22:32 +02001/*
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
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
20import android.content.Intent;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020021import android.os.UserManager;
22import android.provider.ContactsContract;
Adrian Roos1de02ee2014-08-12 15:10:19 +020023import android.text.TextUtils;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020024import android.util.AttributeSet;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020025import android.view.View;
Adrian Roosffc90972015-06-09 18:09:49 -070026import android.view.ViewGroup;
Julia Reynolds20aef8a2016-05-04 16:44:08 -040027import android.view.accessibility.AccessibilityEvent;
28import android.view.accessibility.AccessibilityNodeInfo;
29import android.widget.Button;
Jorim Jaggi3f48f462014-07-08 16:53:29 +020030import android.widget.FrameLayout;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020031
Adrian Roos1de02ee2014-08-12 15:10:19 +020032import com.android.systemui.R;
Jason Monk46767b72016-08-18 10:58:04 -040033import com.android.systemui.plugins.qs.QSContainer.DetailAdapter;
Adrian Roos1ef80fe2014-07-14 22:53:54 +020034import com.android.systemui.qs.QSPanel;
Adrian Roos723632e2014-07-23 21:13:21 +020035import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
Adrian Roos57cf5702014-09-03 15:56:30 +020036import com.android.systemui.statusbar.policy.UserSwitcherController;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020037
38/**
Jorim Jaggi3f48f462014-07-08 16:53:29 +020039 * Container for image of the multi user switcher (tappable).
Jorim Jaggi3d878be2014-05-10 03:22:32 +020040 */
Jorim Jaggi3f48f462014-07-08 16:53:29 +020041public class MultiUserSwitch extends FrameLayout implements View.OnClickListener {
Jorim Jaggi3d878be2014-05-10 03:22:32 +020042
Muyuan Li92c91412016-05-04 17:06:29 -070043 protected QSPanel mQsPanel;
Adrian Roos723632e2014-07-23 21:13:21 +020044 private KeyguardUserSwitcher mKeyguardUserSwitcher;
45 private boolean mKeyguardMode;
Adrian Roosffc90972015-06-09 18:09:49 -070046 private UserSwitcherController.BaseUserAdapter mUserListener;
47
Adrian Roos1de02ee2014-08-12 15:10:19 +020048 final UserManager mUserManager;
Jorim Jaggi3d878be2014-05-10 03:22:32 +020049
Adrian Roos970be532014-11-21 15:50:16 +010050 private final int[] mTmpInt2 = new int[2];
51
Muyuan Li92c91412016-05-04 17:06:29 -070052 protected UserSwitcherController mUserSwitcherController;
Adrian Roosffc90972015-06-09 18:09:49 -070053
Jorim Jaggi3d878be2014-05-10 03:22:32 +020054 public MultiUserSwitch(Context context, AttributeSet attrs) {
55 super(context, attrs);
Adrian Roos1de02ee2014-08-12 15:10:19 +020056 mUserManager = UserManager.get(getContext());
Jorim Jaggi3d878be2014-05-10 03:22:32 +020057 }
58
59 @Override
60 protected void onFinishInflate() {
61 super.onFinishInflate();
62 setOnClickListener(this);
Adrian Roosffc90972015-06-09 18:09:49 -070063 refreshContentDescription();
Jorim Jaggi3d878be2014-05-10 03:22:32 +020064 }
65
Adrian Roos1ef80fe2014-07-14 22:53:54 +020066 public void setQsPanel(QSPanel qsPanel) {
67 mQsPanel = qsPanel;
Adrian Roosffc90972015-06-09 18:09:49 -070068 setUserSwitcherController(qsPanel.getHost().getUserSwitcherController());
69 }
70
Jason Monk69e76cc2016-01-26 10:56:37 -050071 public boolean hasMultipleUsers() {
Steve Pfetschef39ce82016-01-28 18:41:19 -080072 if (mUserListener == null) {
73 return false;
74 }
Jason Monk69e76cc2016-01-26 10:56:37 -050075 return mUserListener.getCount() != 0;
76 }
77
Adrian Roosffc90972015-06-09 18:09:49 -070078 public void setUserSwitcherController(UserSwitcherController userSwitcherController) {
79 mUserSwitcherController = userSwitcherController;
80 registerListener();
81 refreshContentDescription();
Jorim Jaggi3d878be2014-05-10 03:22:32 +020082 }
83
Adrian Roos723632e2014-07-23 21:13:21 +020084 public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
85 mKeyguardUserSwitcher = keyguardUserSwitcher;
86 }
87
88 public void setKeyguardMode(boolean keyguardShowing) {
89 mKeyguardMode = keyguardShowing;
Adrian Roosffc90972015-06-09 18:09:49 -070090 registerListener();
91 }
92
93 private void registerListener() {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -070094 if (mUserManager.isUserSwitcherEnabled() && mUserListener == null) {
Adrian Roosffc90972015-06-09 18:09:49 -070095
96 final UserSwitcherController controller = mUserSwitcherController;
97 if (controller != null) {
98 mUserListener = new UserSwitcherController.BaseUserAdapter(controller) {
99 @Override
100 public void notifyDataSetChanged() {
101 refreshContentDescription();
102 }
103
104 @Override
105 public View getView(int position, View convertView, ViewGroup parent) {
106 return null;
107 }
108 };
109 refreshContentDescription();
110 }
111 }
Adrian Roos723632e2014-07-23 21:13:21 +0200112 }
113
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200114 @Override
115 public void onClick(View v) {
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -0700116 if (mUserManager.isUserSwitcherEnabled()) {
Adrian Roos723632e2014-07-23 21:13:21 +0200117 if (mKeyguardMode) {
118 if (mKeyguardUserSwitcher != null) {
Jorim Jaggi98f85302014-08-07 17:45:04 +0200119 mKeyguardUserSwitcher.show(true /* animate */);
Adrian Roos723632e2014-07-23 21:13:21 +0200120 }
Adrian Roosffc90972015-06-09 18:09:49 -0700121 } else if (mQsPanel != null && mUserSwitcherController != null) {
122 View center = getChildCount() > 0 ? getChildAt(0) : this;
Adrian Roos970be532014-11-21 15:50:16 +0100123
Adrian Roosffc90972015-06-09 18:09:49 -0700124 center.getLocationInWindow(mTmpInt2);
125 mTmpInt2[0] += center.getWidth() / 2;
126 mTmpInt2[1] += center.getHeight() / 2;
Adrian Roos970be532014-11-21 15:50:16 +0100127
Adrian Roosffc90972015-06-09 18:09:49 -0700128 mQsPanel.showDetailAdapter(true,
Muyuan Li92c91412016-05-04 17:06:29 -0700129 getUserDetailAdapter(),
Adrian Roosffc90972015-06-09 18:09:49 -0700130 mTmpInt2);
Adrian Roos723632e2014-07-23 21:13:21 +0200131 }
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200132 } else {
Akira Oshimif37adce2015-02-25 13:57:28 +0900133 if (mQsPanel != null) {
Kaori Katouccf08d72015-05-26 16:25:04 +0900134 Intent intent = ContactsContract.QuickContact.composeQuickContactsIntent(
135 getContext(), v, ContactsContract.Profile.CONTENT_URI,
136 ContactsContract.QuickContact.MODE_LARGE, null);
Adrian Roosbc3e2e12016-03-23 11:23:51 -0700137 mQsPanel.getHost().startActivityDismissingKeyguard(intent);
Akira Oshimif37adce2015-02-25 13:57:28 +0900138 }
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200139 }
140 }
Jorim Jaggi98f85302014-08-07 17:45:04 +0200141
142 @Override
Adrian Roosffc90972015-06-09 18:09:49 -0700143 public void setClickable(boolean clickable) {
144 super.setClickable(clickable);
145 refreshContentDescription();
146 }
Adrian Roos1de02ee2014-08-12 15:10:19 +0200147
Adrian Roosffc90972015-06-09 18:09:49 -0700148 private void refreshContentDescription() {
149 String currentUser = null;
Fyodor Kupolovcd86ebf2015-09-29 17:06:50 -0700150 if (mUserManager.isUserSwitcherEnabled()
Adrian Roosffc90972015-06-09 18:09:49 -0700151 && mUserSwitcherController != null) {
152 currentUser = mUserSwitcherController.getCurrentUserName(mContext);
153 }
154
155 String text = null;
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400156
157 if (!TextUtils.isEmpty(currentUser)) {
158 text = mContext.getString(
159 R.string.accessibility_quick_settings_user,
160 currentUser);
Adrian Roos1de02ee2014-08-12 15:10:19 +0200161 }
162
Adrian Roosffc90972015-06-09 18:09:49 -0700163 if (!TextUtils.equals(getContentDescription(), text)) {
164 setContentDescription(text);
165 }
Adrian Roos1de02ee2014-08-12 15:10:19 +0200166 }
167
168 @Override
Julia Reynolds20aef8a2016-05-04 16:44:08 -0400169 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
170 super.onInitializeAccessibilityEvent(event);
171 event.setClassName(Button.class.getName());
172 }
173
174 @Override
175 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
176 super.onInitializeAccessibilityNodeInfo(info);
177 info.setClassName(Button.class.getName());
178 }
179
180 @Override
Jorim Jaggi98f85302014-08-07 17:45:04 +0200181 public boolean hasOverlappingRendering() {
182 return false;
183 }
Adrian Roos81e3e942014-09-18 21:11:20 +0200184
Jason Monk46767b72016-08-18 10:58:04 -0400185 protected DetailAdapter getUserDetailAdapter() {
Muyuan Li92c91412016-05-04 17:06:29 -0700186 return mUserSwitcherController.userDetailAdapter;
187 }
Jorim Jaggi3d878be2014-05-10 03:22:32 +0200188}