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.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 java.util.ArrayList;
     31 import java.util.Collection;
     32 import java.util.Collections;
     33 import java.util.Comparator;
     34 
     35 public class KeyguardMultiUserSelectorView extends FrameLayout implements View.OnClickListener {
     36     private static final String TAG = "KeyguardMultiUserSelectorView";
     37 
     38     private ViewGroup mUsersGrid;
     39     private KeyguardMultiUserAvatar mActiveUserAvatar;
     40     private KeyguardHostView.UserSwitcherCallback mCallback;
     41     private static final int FADE_OUT_ANIMATION_DURATION = 100;
     42 
     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 () {
     56         mUsersGrid = (ViewGroup) findViewById(R.id.keyguard_users_grid);
     57         mUsersGrid.removeAllViews();
     58         setClipChildren(false);
     59         setClipToPadding(false);
     60 
     61     }
     62 
     63     public void setCallback(KeyguardHostView.UserSwitcherCallback callback) {
     64         mCallback = callback;
     65     }
     66 
     67     public void addUsers(Collection<UserInfo> userList) {
     68         UserInfo activeUser;
     69         try {
     70             activeUser = ActivityManagerNative.getDefault().getCurrentUser();
     71         } catch (RemoteException re) {
     72             activeUser = null;
     73         }
     74 
     75         ArrayList<UserInfo> users = new ArrayList<UserInfo>(userList);
     76         Collections.sort(users, mOrderAddedComparator);
     77 
     78         for (UserInfo user: users) {
     79             KeyguardMultiUserAvatar uv = createAndAddUser(user);
     80             if (user.id == activeUser.id) {
     81                 mActiveUserAvatar = uv;
     82             }
     83             uv.setActive(false, false, null);
     84         }
     85         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);
    104     }
    105 
    106     Comparator<UserInfo> mOrderAddedComparator = new Comparator<UserInfo>() {
    107         @Override
    108         public int compare(UserInfo lhs, UserInfo rhs) {
    109             return (lhs.serialNumber - rhs.serialNumber);
    110         }
    111     };
    112 
    113     private KeyguardMultiUserAvatar createAndAddUser(UserInfo user) {
    114         KeyguardMultiUserAvatar uv = KeyguardMultiUserAvatar.fromXml(
    115                 R.layout.keyguard_multi_user_avatar, mContext, this, user);
    116         mUsersGrid.addView(uv);
    117         return uv;
    118     }
    119 
    120     @Override
    121     public boolean onInterceptTouchEvent(MotionEvent event) {
    122         if(event.getActionMasked() != MotionEvent.ACTION_CANCEL && mCallback != null) {
    123             mCallback.userActivity();
    124         }
    125         return false;
    126     }
    127 
    128     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 
    137     @Override
    138     public void onClick(View v) {
    139         if (!(v instanceof KeyguardMultiUserAvatar)) return;
    140         final KeyguardMultiUserAvatar avatar = (KeyguardMultiUserAvatar) v;
    141         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);
    150                 avatar.lockPressed(true);
    151                 mActiveUserAvatar.setActive(false, true, new Runnable() {
    152                     @Override
    153                     public void run() {
    154                         mActiveUserAvatar = avatar;
    155                         try {
    156                             ActivityManagerNative.getDefault()
    157                                     .switchUser(avatar.getUserInfo().id);
    158                         } catch (RemoteException re) {
    159                             Log.e(TAG, "Couldn't switch user " + re);
    160                         }
    161                     }
    162                 });
    163             }
    164         }
    165     }
    166 }
    167