blob: 7975d8e2493e19c21aba3a8b74b398f9992de0ff [file] [log] [blame]
Adam Cohen66b9fb1662012-09-05 16:23:58 -07001/*
2 * Copyright (C) 2012 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
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070018
19import android.app.ActivityManagerNative;
20import android.content.Context;
21import android.content.pm.UserInfo;
22import android.os.RemoteException;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070023import android.util.AttributeSet;
Adam Cohen24b351a2012-09-14 15:02:27 -070024import android.util.Log;
Jim Miller19a52672012-10-23 19:52:04 -070025import android.view.MotionEvent;
Adam Cohen24b351a2012-09-14 15:02:27 -070026import android.view.View;
Jim Miller022554e2012-10-22 19:03:06 -070027import android.view.ViewGroup;
Adam Cohen24b351a2012-09-14 15:02:27 -070028import android.widget.FrameLayout;
Michael Jurka1254f2f2012-10-25 11:44:31 -070029
Adam Cohen66b9fb1662012-09-05 16:23:58 -070030import java.util.ArrayList;
Chris Wren28c5ee92012-10-24 16:05:14 -040031import java.util.Collection;
Adam Cohen24b351a2012-09-14 15:02:27 -070032import java.util.Collections;
33import java.util.Comparator;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070034
Adam Cohen24b351a2012-09-14 15:02:27 -070035public class KeyguardMultiUserSelectorView extends FrameLayout implements View.OnClickListener {
36 private static final String TAG = "KeyguardMultiUserSelectorView";
37
Jim Miller022554e2012-10-22 19:03:06 -070038 private ViewGroup mUsersGrid;
Adam Cohen24b351a2012-09-14 15:02:27 -070039 private KeyguardMultiUserAvatar mActiveUserAvatar;
Jim Miller109f1fd2012-09-19 20:44:16 -070040 private KeyguardHostView.UserSwitcherCallback mCallback;
Adam Cohen24b351a2012-09-14 15:02:27 -070041 private static final int FADE_OUT_ANIMATION_DURATION = 100;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070042
43 public KeyguardMultiUserSelectorView(Context context) {
44 this(context, null, 0);
45 }
46
47 public KeyguardMultiUserSelectorView(Context context, AttributeSet attrs) {
48 this(context, attrs, 0);
49 }
50
51 public KeyguardMultiUserSelectorView(Context context, AttributeSet attrs, int defStyle) {
52 super(context, attrs, defStyle);
53 }
54
55 protected void onFinishInflate () {
Chris Wren28c5ee92012-10-24 16:05:14 -040056 mUsersGrid = (ViewGroup) findViewById(R.id.keyguard_users_grid);
57 mUsersGrid.removeAllViews();
58 setClipChildren(false);
59 setClipToPadding(false);
60
Adam Cohen66b9fb1662012-09-05 16:23:58 -070061 }
62
Jim Miller109f1fd2012-09-19 20:44:16 -070063 public void setCallback(KeyguardHostView.UserSwitcherCallback callback) {
Adam Cohen24b351a2012-09-14 15:02:27 -070064 mCallback = callback;
65 }
66
Chris Wren28c5ee92012-10-24 16:05:14 -040067 public void addUsers(Collection<UserInfo> userList) {
Adam Cohen24b351a2012-09-14 15:02:27 -070068 UserInfo activeUser;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070069 try {
Adam Cohen24b351a2012-09-14 15:02:27 -070070 activeUser = ActivityManagerNative.getDefault().getCurrentUser();
Adam Cohen66b9fb1662012-09-05 16:23:58 -070071 } catch (RemoteException re) {
Adam Cohen24b351a2012-09-14 15:02:27 -070072 activeUser = null;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070073 }
74
Chris Wren28c5ee92012-10-24 16:05:14 -040075 ArrayList<UserInfo> users = new ArrayList<UserInfo>(userList);
Adam Cohen24b351a2012-09-14 15:02:27 -070076 Collections.sort(users, mOrderAddedComparator);
77
Adam Cohen66b9fb1662012-09-05 16:23:58 -070078 for (UserInfo user: users) {
Adam Cohen24b351a2012-09-14 15:02:27 -070079 KeyguardMultiUserAvatar uv = createAndAddUser(user);
80 if (user.id == activeUser.id) {
81 mActiveUserAvatar = uv;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070082 }
Chris Wrenf41c61b2012-11-29 15:19:54 -050083 uv.setActive(false, false, null);
Adam Cohen66b9fb1662012-09-05 16:23:58 -070084 }
Chris Wrenf41c61b2012-11-29 15:19:54 -050085 mActiveUserAvatar.lockPressed(true);
86 }
87
88 public void finalizeActiveUserView(boolean animate) {
89 if (animate) {
90 getHandler().postDelayed(new Runnable() {
91 @Override
92 public void run() {
93 finalizeActiveUserNow(true);
94 }
95 }, 500);
96 } else {
97 finalizeActiveUserNow(animate);
98 }
99 }
100
101 void finalizeActiveUserNow(boolean animate) {
102 mActiveUserAvatar.lockPressed(false);
103 mActiveUserAvatar.setActive(true, animate, null);
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700104 }
105
Adam Cohen24b351a2012-09-14 15:02:27 -0700106 Comparator<UserInfo> mOrderAddedComparator = new Comparator<UserInfo>() {
107 @Override
108 public int compare(UserInfo lhs, UserInfo rhs) {
109 return (lhs.serialNumber - rhs.serialNumber);
110 }
111 };
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700112
Adam Cohen24b351a2012-09-14 15:02:27 -0700113 private KeyguardMultiUserAvatar createAndAddUser(UserInfo user) {
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700114 KeyguardMultiUserAvatar uv = KeyguardMultiUserAvatar.fromXml(
115 R.layout.keyguard_multi_user_avatar, mContext, this, user);
Adam Cohen24b351a2012-09-14 15:02:27 -0700116 mUsersGrid.addView(uv);
117 return uv;
118 }
119
120 @Override
Jim Miller19a52672012-10-23 19:52:04 -0700121 public boolean onInterceptTouchEvent(MotionEvent event) {
Jim Miller19a52672012-10-23 19:52:04 -0700122 if(event.getActionMasked() != MotionEvent.ACTION_CANCEL && mCallback != null) {
Jim Miller19a52672012-10-23 19:52:04 -0700123 mCallback.userActivity();
124 }
125 return false;
126 }
127
Chris Wrenf0fd1722012-10-26 09:45:43 -0400128 private void setAllClickable(boolean clickable)
129 {
130 for(int i = 0; i < mUsersGrid.getChildCount(); i++) {
131 View v = mUsersGrid.getChildAt(i);
132 v.setClickable(clickable);
133 v.setPressed(false);
134 }
135 }
136
Jim Miller19a52672012-10-23 19:52:04 -0700137 @Override
Adam Cohen24b351a2012-09-14 15:02:27 -0700138 public void onClick(View v) {
139 if (!(v instanceof KeyguardMultiUserAvatar)) return;
140 final KeyguardMultiUserAvatar avatar = (KeyguardMultiUserAvatar) v;
Chris Wrenf0fd1722012-10-26 09:45:43 -0400141 if (avatar.isClickable()) { // catch race conditions
142 if (mActiveUserAvatar == avatar) {
143 // If they click the currently active user, show the unlock hint
144 mCallback.showUnlockHint();
145 return;
146 } else {
147 // Reset the previously active user to appear inactive
148 mCallback.hideSecurityView(FADE_OUT_ANIMATION_DURATION);
149 setAllClickable(false);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500150 avatar.lockPressed(true);
Chris Wrenf0fd1722012-10-26 09:45:43 -0400151 mActiveUserAvatar.setActive(false, true, new Runnable() {
152 @Override
153 public void run() {
154 mActiveUserAvatar = avatar;
Jim Miller5ecd8112013-01-09 18:50:26 -0800155 try {
156 ActivityManagerNative.getDefault()
157 .switchUser(avatar.getUserInfo().id);
158 } catch (RemoteException re) {
159 Log.e(TAG, "Couldn't switch user " + re);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500160 }
Chris Wrenf0fd1722012-10-26 09:45:43 -0400161 }
162 });
163 }
Adam Cohen24b351a2012-09-14 15:02:27 -0700164 }
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700165 }
166}