MultiUserSwitch.java revision 57cf5702e0634b8cb25daa8f2f73292428ec4e08
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.accessibility.AccessibilityEvent;
28import android.widget.FrameLayout;
29
30import com.android.systemui.R;
31import com.android.systemui.qs.QSPanel;
32import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
33import com.android.systemui.statusbar.policy.UserSwitcherController;
34
35/**
36 * Container for image of the multi user switcher (tappable).
37 */
38public class MultiUserSwitch extends FrameLayout implements View.OnClickListener {
39
40    private QSPanel mQsPanel;
41    private KeyguardUserSwitcher mKeyguardUserSwitcher;
42    private boolean mKeyguardMode;
43    final UserManager mUserManager;
44
45    public MultiUserSwitch(Context context, AttributeSet attrs) {
46        super(context, attrs);
47        mUserManager = UserManager.get(getContext());
48    }
49
50    @Override
51    protected void onFinishInflate() {
52        super.onFinishInflate();
53        setOnClickListener(this);
54    }
55
56    public void setQsPanel(QSPanel qsPanel) {
57        mQsPanel = qsPanel;
58    }
59
60    public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
61        mKeyguardUserSwitcher = keyguardUserSwitcher;
62    }
63
64    public void setKeyguardMode(boolean keyguardShowing) {
65        mKeyguardMode = keyguardShowing;
66    }
67
68    @Override
69    public void onClick(View v) {
70        final UserManager um = UserManager.get(getContext());
71        if (um.isUserSwitcherEnabled()) {
72            if (mKeyguardMode) {
73                if (mKeyguardUserSwitcher != null) {
74                    mKeyguardUserSwitcher.show(true /* animate */);
75                }
76            } else {
77                mQsPanel.showDetailAdapter(true,
78                        mQsPanel.getHost().getUserSwitcherController().userDetailAdapter);
79            }
80        } else {
81            Intent intent = ContactsContract.QuickContact.composeQuickContactsIntent(
82                    getContext(), v, ContactsContract.Profile.CONTENT_URI,
83                    ContactsContract.QuickContact.MODE_LARGE, null);
84            getContext().startActivityAsUser(intent, new UserHandle(UserHandle.USER_CURRENT));
85        }
86    }
87
88    @Override
89    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
90        super.onPopulateAccessibilityEvent(event);
91
92        if (isClickable()) {
93            final UserManager um = UserManager.get(getContext());
94            String text;
95            if (um.isUserSwitcherEnabled()) {
96                UserSwitcherController controller = mQsPanel.getHost()
97                        .getUserSwitcherController();
98                String currentUser = controller.getCurrentUserName(mContext);
99                if (TextUtils.isEmpty(currentUser)) {
100                    text = mContext.getString(R.string.accessibility_multi_user_switch_switcher);
101                } else {
102                    text = mContext.getString(
103                            R.string.accessibility_multi_user_switch_switcher_with_current,
104                            currentUser);
105                }
106            } else {
107                text = mContext.getString(R.string.accessibility_multi_user_switch_quick_contact);
108            }
109            if (!TextUtils.isEmpty(text)) {
110                event.getText().add(text);
111            }
112        }
113
114    }
115
116    @Override
117    public boolean hasOverlappingRendering() {
118        return false;
119    }
120}
121