KeyguardUserSwitcher.java revision ccdff62159b41ab130a8f90d30edb9b9542d8c72
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.policy;
18
19import android.content.Context;
20import android.database.DataSetObserver;
21import android.provider.Settings;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.ViewStub;
26import android.widget.TextView;
27
28import com.android.systemui.R;
29import com.android.systemui.qs.tiles.UserDetailItemView;
30import com.android.systemui.statusbar.phone.KeyguardStatusBarView;
31import com.android.systemui.statusbar.phone.UserAvatarView;
32
33/**
34 * Manages the user switcher on the Keyguard.
35 */
36public class KeyguardUserSwitcher {
37
38    private static final String TAG = "KeyguardUserSwitcher";
39    private static final boolean ALWAYS_ON = false;
40
41    private final ViewGroup mUserSwitcher;
42    private final KeyguardStatusBarView mStatusBarView;
43    private final Adapter mAdapter;
44    private final boolean mSimpleUserSwitcher;
45
46    public KeyguardUserSwitcher(Context context, ViewStub userSwitcher,
47            KeyguardStatusBarView statusBarView, UserSwitcherController userSwitcherController) {
48        if (context.getResources().getBoolean(R.bool.config_keyguardUserSwitcher) || ALWAYS_ON) {
49            mUserSwitcher = (ViewGroup) userSwitcher.inflate();
50            mStatusBarView = statusBarView;
51            mStatusBarView.setKeyguardUserSwitcher(this);
52            mAdapter = new Adapter(context, userSwitcherController);
53            mAdapter.registerDataSetObserver(mDataSetObserver);
54            mSimpleUserSwitcher = userSwitcherController.isSimpleUserSwitcher();
55        } else {
56            mUserSwitcher = null;
57            mStatusBarView = null;
58            mAdapter = null;
59            mSimpleUserSwitcher = false;
60        }
61    }
62
63    public void setKeyguard(boolean keyguard) {
64        if (mUserSwitcher != null) {
65            if (keyguard && shouldExpandByDefault()) {
66                show();
67            } else {
68                hide();
69            }
70        }
71    }
72
73    /**
74     * @return true if the user switcher should be expanded by default on the lock screen.
75     * @see android.os.UserManager#isUserSwitcherEnabled()
76     */
77    private boolean shouldExpandByDefault() {
78        return mSimpleUserSwitcher || mAdapter.getSwitchableUsers() > 1;
79    }
80
81    public void show() {
82        if (mUserSwitcher != null) {
83            // TODO: animate
84            mUserSwitcher.setVisibility(View.VISIBLE);
85            mStatusBarView.setKeyguardUserSwitcherShowing(true);
86        }
87    }
88
89    public void hide() {
90        if (mUserSwitcher != null) {
91            // TODO: animate
92            mUserSwitcher.setVisibility(View.GONE);
93            mStatusBarView.setKeyguardUserSwitcherShowing(false);
94        }
95    }
96
97    private void refresh() {
98        final int childCount = mUserSwitcher.getChildCount();
99        final int adapterCount = mAdapter.getCount();
100        final int N = Math.max(childCount, adapterCount);
101        for (int i = 0; i < N; i++) {
102            if (i < adapterCount) {
103                View oldView = null;
104                if (i < childCount) {
105                    oldView = mUserSwitcher.getChildAt(i);
106                }
107                View newView = mAdapter.getView(i, oldView, mUserSwitcher);
108                if (oldView == null) {
109                    // We ran out of existing views. Add it at the end.
110                    mUserSwitcher.addView(newView);
111                } else if (oldView != newView) {
112                    // We couldn't rebind the view. Replace it.
113                    mUserSwitcher.removeViewAt(i);
114                    mUserSwitcher.addView(newView, i);
115                }
116            } else {
117                int lastIndex = mUserSwitcher.getChildCount() - 1;
118                mUserSwitcher.removeViewAt(lastIndex);
119            }
120        }
121    }
122
123    public final DataSetObserver mDataSetObserver = new DataSetObserver() {
124        @Override
125        public void onChanged() {
126            refresh();
127        }
128    };
129
130    public static class Adapter extends UserSwitcherController.BaseUserAdapter implements
131            View.OnClickListener {
132
133        private Context mContext;
134
135        public Adapter(Context context, UserSwitcherController controller) {
136            super(controller);
137            mContext = context;
138        }
139
140        @Override
141        public View getView(int position, View convertView, ViewGroup parent) {
142            UserSwitcherController.UserRecord item = getItem(position);
143
144            if (!(convertView instanceof UserDetailItemView)
145                    || !(convertView.getTag() instanceof UserSwitcherController.UserRecord)) {
146                convertView = LayoutInflater.from(mContext).inflate(
147                        R.layout.keyguard_user_switcher_item, parent, false);
148                convertView.setOnClickListener(this);
149            }
150            UserDetailItemView v = (UserDetailItemView) convertView;
151
152            String name = getName(mContext, item);
153            if (item.picture == null) {
154                v.bind(name, getDrawable(mContext, item));
155            } else {
156                v.bind(name, item.picture);
157            }
158            convertView.setActivated(item.isCurrent);
159            convertView.setTag(item);
160            return convertView;
161        }
162
163        @Override
164        public void onClick(View v) {
165            switchTo(((UserSwitcherController.UserRecord)v.getTag()));
166        }
167    }
168}
169