Home | History | Annotate | Download | only in keyguard
      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 
     17 package com.android.internal.policy.impl.keyguard;
     18 
     19 import android.app.ActivityManagerNative;
     20 import android.content.Context;
     21 import android.content.pm.UserInfo;
     22 import android.os.RemoteException;
     23 import android.util.AttributeSet;
     24 import android.util.Log;
     25 import android.view.MotionEvent;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.FrameLayout;
     29 
     30 import com.android.internal.R;
     31 
     32 import java.util.ArrayList;
     33 import java.util.Collection;
     34 import java.util.Collections;
     35 import java.util.Comparator;
     36 
     37 public 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             }
     85             uv.setActive(false, false, null);
     86         }
     87         mActiveUserAvatar.lockPressed(true);
     88     }
     89 
     90     public void finalizeActiveUserView(boolean animate) {
     91         if (animate) {
     92             getHandler().postDelayed(new Runnable() {
     93                     @Override
     94                         public void run() {
     95                         finalizeActiveUserNow(true);
     96                     }
     97                 }, 500);
     98         } else {
     99             finalizeActiveUserNow(animate);
    100         }
    101     }
    102 
    103     void finalizeActiveUserNow(boolean animate) {
    104         mActiveUserAvatar.lockPressed(false);
    105         mActiveUserAvatar.setActive(true, animate, null);
    106     }
    107 
    108     Comparator<UserInfo> mOrderAddedComparator = new Comparator<UserInfo>() {
    109         @Override
    110         public int compare(UserInfo lhs, UserInfo rhs) {
    111             return (lhs.serialNumber - rhs.serialNumber);
    112         }
    113     };
    114 
    115     private KeyguardMultiUserAvatar createAndAddUser(UserInfo user) {
    116         KeyguardMultiUserAvatar uv = KeyguardMultiUserAvatar.fromXml(
    117                 R.layout.keyguard_multi_user_avatar, mContext, this, user);
    118         mUsersGrid.addView(uv);
    119         return uv;
    120     }
    121 
    122     @Override
    123     public boolean onInterceptTouchEvent(MotionEvent event) {
    124         if(event.getActionMasked() != MotionEvent.ACTION_CANCEL && mCallback != null) {
    125             mCallback.userActivity();
    126         }
    127         return false;
    128     }
    129 
    130     private void setAllClickable(boolean clickable)
    131     {
    132         for(int i = 0; i < mUsersGrid.getChildCount(); i++) {
    133             View v = mUsersGrid.getChildAt(i);
    134             v.setClickable(clickable);
    135             v.setPressed(false);
    136         }
    137     }
    138 
    139     @Override
    140     public void onClick(View v) {
    141         if (!(v instanceof KeyguardMultiUserAvatar)) return;
    142         final KeyguardMultiUserAvatar avatar = (KeyguardMultiUserAvatar) v;
    143         if (avatar.isClickable()) { // catch race conditions
    144             if (mActiveUserAvatar == avatar) {
    145                 // If they click the currently active user, show the unlock hint
    146                 mCallback.showUnlockHint();
    147                 return;
    148             } else {
    149                 // Reset the previously active user to appear inactive
    150                 mCallback.hideSecurityView(FADE_OUT_ANIMATION_DURATION);
    151                 setAllClickable(false);
    152                 avatar.lockPressed(true);
    153                 mActiveUserAvatar.setActive(false, true, new Runnable() {
    154                     @Override
    155                     public void run() {
    156                         mActiveUserAvatar = avatar;
    157                         if (this.getClass().getName().contains("internal")) {
    158                             try {
    159                                 ActivityManagerNative.getDefault()
    160                                         .switchUser(avatar.getUserInfo().id);
    161                             } catch (RemoteException re) {
    162                                 Log.e(TAG, "Couldn't switch user " + re);
    163                             }
    164                         } else {
    165                             setAllClickable(true);
    166                         }
    167                     }
    168                 });
    169             }
    170         }
    171     }
    172 }
    173