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