KeyguardMultiUserAvatar.java revision 9654329008bfde134de63df06dc15ac0271a0d4c
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.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
22import android.animation.ValueAnimator.AnimatorUpdateListener;
23import android.content.Context;
24import android.content.pm.UserInfo;
25import android.graphics.Color;
26import android.graphics.drawable.Drawable;
27import android.util.AttributeSet;
28import android.view.LayoutInflater;
29import android.view.ViewGroup;
30import android.widget.FrameLayout;
31import android.widget.ImageView;
32import android.widget.TextView;
33
34import com.android.internal.R;
35
36class KeyguardMultiUserAvatar extends FrameLayout {
37
38    private ImageView mUserImage;
39    private TextView mUserName;
40    private UserInfo mUserInfo;
41    private static final int INACTIVE_COLOR = 85;
42    private static final int INACTIVE_ALPHA = 195;
43    private static final float ACTIVE_SCALE = 1.1f;
44    private boolean mActive;
45    private boolean mInit = true;
46    private KeyguardMultiUserSelectorView mUserSelector;
47
48    boolean mPressedStateLocked = false;
49    boolean mTempPressedStateHolder = false;
50
51    public static KeyguardMultiUserAvatar fromXml(int resId, Context context,
52            KeyguardMultiUserSelectorView userSelector, UserInfo info) {
53        KeyguardMultiUserAvatar icon = (KeyguardMultiUserAvatar)
54                LayoutInflater.from(context).inflate(resId, userSelector, false);
55
56        icon.setup(info, userSelector);
57        return icon;
58    }
59
60    public KeyguardMultiUserAvatar(Context context) {
61        super(context, null, 0);
62    }
63
64    public KeyguardMultiUserAvatar(Context context, AttributeSet attrs) {
65        super(context, attrs, 0);
66    }
67
68    public KeyguardMultiUserAvatar(Context context, AttributeSet attrs, int defStyle) {
69        super(context, attrs, defStyle);
70    }
71
72    public void setup(UserInfo user, KeyguardMultiUserSelectorView userSelector) {
73        mUserInfo = user;
74        mUserSelector = userSelector;
75        init();
76    }
77
78    private void init() {
79        mUserImage = (ImageView) findViewById(R.id.keyguard_user_avatar);
80        mUserName = (TextView) findViewById(R.id.keyguard_user_name);
81
82        mUserImage.setImageDrawable(Drawable.createFromPath(mUserInfo.iconPath));
83        mUserName.setText(mUserInfo.name);
84        setOnClickListener(mUserSelector);
85        setActive(false, false, 0, null);
86        mInit = false;
87    }
88
89    public void setActive(boolean active, boolean animate, int duration, final Runnable onComplete) {
90        if (mActive != active || mInit) {
91            mActive = active;
92            final int finalFilterAlpha = mActive ? 0 : INACTIVE_ALPHA;
93            final int initFilterAlpha = mActive ? INACTIVE_ALPHA : 0;
94
95            final float finalScale = mActive ? ACTIVE_SCALE : 1.0f;
96            final float initScale = mActive ? 1.0f : ACTIVE_SCALE;
97
98            if (active) {
99                KeyguardSubdivisionLayout parent = (KeyguardSubdivisionLayout) getParent();
100                parent.setTopChild(parent.indexOfChild(this));
101            }
102
103            if (animate) {
104                ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
105                va.addUpdateListener(new AnimatorUpdateListener() {
106                    @Override
107                    public void onAnimationUpdate(ValueAnimator animation) {
108                        float r = animation.getAnimatedFraction();
109                        float scale = (1 - r) * initScale + r * finalScale;
110                        int filterAlpha = (int) ((1 - r) * initFilterAlpha + r * finalFilterAlpha);
111                        setScaleX(scale);
112                        setScaleY(scale);
113                        mUserImage.setColorFilter(Color.argb(filterAlpha, INACTIVE_COLOR,
114                                INACTIVE_COLOR, INACTIVE_COLOR));
115                        mUserSelector.invalidate();
116
117                    }
118                });
119                va.addListener(new AnimatorListenerAdapter() {
120                    @Override
121                    public void onAnimationEnd(Animator animation) {
122                        if (onComplete != null) {
123                            onComplete.run();
124                        }
125                    }
126                });
127                va.setDuration(duration);
128                va.start();
129            } else {
130                setScaleX(finalScale);
131                setScaleY(finalScale);
132                mUserImage.setColorFilter(Color.argb(finalFilterAlpha, INACTIVE_COLOR,
133                        INACTIVE_COLOR, INACTIVE_COLOR));
134                if (onComplete != null) {
135                    post(onComplete);
136                }
137            }
138        }
139    }
140
141    public void lockPressedState() {
142        mPressedStateLocked = true;
143    }
144
145    public void resetPressedState() {
146        mPressedStateLocked = false;
147        post(new Runnable() {
148            @Override
149            public void run() {
150                KeyguardMultiUserAvatar.this.setPressed(mTempPressedStateHolder);
151            }
152        });
153    }
154
155    @Override
156    public void setPressed(boolean pressed) {
157        if (!mPressedStateLocked) {
158            super.setPressed(pressed);
159            if (pressed) {
160                mUserImage.setColorFilter(Color.argb(0, INACTIVE_COLOR,
161                        INACTIVE_COLOR, INACTIVE_COLOR));
162            } else if (!mActive) {
163                mUserImage.setColorFilter(Color.argb(INACTIVE_ALPHA, INACTIVE_COLOR,
164                        INACTIVE_COLOR, INACTIVE_COLOR));
165            }
166        } else {
167            mTempPressedStateHolder = pressed;
168        }
169    }
170
171    public UserInfo getUserInfo() {
172        return mUserInfo;
173    }
174}
175