1/*
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
17package com.android.internal.policy.impl.keyguard;
18
19import android.app.ActivityManagerNative;
20import android.content.Context;
21import android.content.pm.UserInfo;
22import android.os.RemoteException;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.FrameLayout;
29
30import com.android.internal.R;
31
32import java.util.ArrayList;
33import java.util.Collection;
34import java.util.Collections;
35import java.util.Comparator;
36
37public class KeyguardMultiUserSelectorView extends FrameLayout implements View.OnClickListener {
38    private static final String TAG = "KeyguardMultiUserSelectorView";
39
40    private ViewGroup mUsersGrid;
41    private KeyguardMultiUserAvatar mActiveUserAvatar;
42    private KeyguardHostView.UserSwitcherCallback mCallback;
43    private static final int FADE_OUT_ANIMATION_DURATION = 100;
44
45    public KeyguardMultiUserSelectorView(Context context) {
46        this(context, null, 0);
47    }
48
49    public KeyguardMultiUserSelectorView(Context context, AttributeSet attrs) {
50        this(context, attrs, 0);
51    }
52
53    public KeyguardMultiUserSelectorView(Context context, AttributeSet attrs, int defStyle) {
54        super(context, attrs, defStyle);
55    }
56
57    protected void onFinishInflate () {
58        mUsersGrid = (ViewGroup) findViewById(R.id.keyguard_users_grid);
59        mUsersGrid.removeAllViews();
60        setClipChildren(false);
61        setClipToPadding(false);
62
63    }
64
65    public void setCallback(KeyguardHostView.UserSwitcherCallback callback) {
66        mCallback = callback;
67    }
68
69    public void addUsers(Collection<UserInfo> userList) {
70        UserInfo activeUser;
71        try {
72            activeUser = ActivityManagerNative.getDefault().getCurrentUser();
73        } catch (RemoteException re) {
74            activeUser = null;
75        }
76
77        ArrayList<UserInfo> users = new ArrayList<UserInfo>(userList);
78        Collections.sort(users, mOrderAddedComparator);
79
80        for (UserInfo user: users) {
81            KeyguardMultiUserAvatar uv = createAndAddUser(user);
82            if (user.id == activeUser.id) {
83                mActiveUserAvatar = uv;
84                mActiveUserAvatar.setActive(true, false, null);
85            } else {
86                uv.setActive(false, false, null);
87            }
88        }
89    }
90
91    Comparator<UserInfo> mOrderAddedComparator = new Comparator<UserInfo>() {
92        @Override
93        public int compare(UserInfo lhs, UserInfo rhs) {
94            return (lhs.serialNumber - rhs.serialNumber);
95        }
96    };
97
98    private KeyguardMultiUserAvatar createAndAddUser(UserInfo user) {
99        KeyguardMultiUserAvatar uv = KeyguardMultiUserAvatar.fromXml(
100                R.layout.keyguard_multi_user_avatar, mContext, this, user);
101        mUsersGrid.addView(uv);
102        return uv;
103    }
104
105    @Override
106    public boolean onInterceptTouchEvent(MotionEvent event) {
107        if(event.getActionMasked() != MotionEvent.ACTION_CANCEL && mCallback != null) {
108            mCallback.userActivity();
109        }
110        return false;
111    }
112
113    private void setAllClickable(boolean clickable)
114    {
115        for(int i = 0; i < mUsersGrid.getChildCount(); i++) {
116            View v = mUsersGrid.getChildAt(i);
117            v.setClickable(clickable);
118            v.setPressed(false);
119        }
120    }
121
122    @Override
123    public void onClick(View v) {
124        if (!(v instanceof KeyguardMultiUserAvatar)) return;
125        final KeyguardMultiUserAvatar avatar = (KeyguardMultiUserAvatar) v;
126        if (avatar.isClickable()) { // catch race conditions
127            if (mActiveUserAvatar == avatar) {
128                // If they click the currently active user, show the unlock hint
129                mCallback.showUnlockHint();
130                return;
131            } else {
132                // Reset the previously active user to appear inactive
133                mCallback.hideSecurityView(FADE_OUT_ANIMATION_DURATION);
134                setAllClickable(false);
135                mActiveUserAvatar.setActive(false, true, new Runnable() {
136                    @Override
137                    public void run() {
138                        mActiveUserAvatar = avatar;
139                        mActiveUserAvatar.setActive(true, true, new Runnable() {
140                            @Override
141                            public void run() {
142                                if (this.getClass().getName().contains("internal")) {
143                                    try {
144                                        ActivityManagerNative.getDefault()
145                                                .switchUser(avatar.getUserInfo().id);
146                                    } catch (RemoteException re) {
147                                        Log.e(TAG, "Couldn't switch user " + re);
148                                    }
149                                } else {
150                                    setAllClickable(true);
151                                }
152                            }
153                        });
154                    }
155                });
156            }
157        }
158    }
159}
160