blob: aa2ae0eb65d573d2a53511aff9fd5d8c0b56e3f3 [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
Adam Cohen24b351a2012-09-14 15:02:27 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohen24b351a2012-09-14 15:02:27 -070021import android.animation.ValueAnimator.AnimatorUpdateListener;
Jim Miller022554e2012-10-22 19:03:06 -070022import android.animation.ValueAnimator;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070023import android.content.Context;
24import android.content.pm.UserInfo;
Adam Cohen61cd69c2012-10-02 21:42:54 -070025import android.content.res.Resources;
Jim Miller022554e2012-10-22 19:03:06 -070026import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
Adam Cohen24b351a2012-09-14 15:02:27 -070028import android.graphics.Color;
Jim Miller5ecd8112013-01-09 18:50:26 -080029import android.os.UserManager;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070030import android.util.AttributeSet;
Jim Miller022554e2012-10-22 19:03:06 -070031import android.util.Log;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070032import android.view.LayoutInflater;
Chris Wren692bb132012-10-23 13:37:36 -040033import android.view.View;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070034import android.widget.FrameLayout;
35import android.widget.ImageView;
36import android.widget.TextView;
37
Adam Cohen66b9fb1662012-09-05 16:23:58 -070038class KeyguardMultiUserAvatar extends FrameLayout {
Chris Wren28c5ee92012-10-24 16:05:14 -040039 private static final String TAG = KeyguardMultiUserAvatar.class.getSimpleName();
40 private static final boolean DEBUG = KeyguardHostView.DEBUG;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070041
42 private ImageView mUserImage;
43 private TextView mUserName;
44 private UserInfo mUserInfo;
Adam Cohen61cd69c2012-10-02 21:42:54 -070045 private static final float ACTIVE_ALPHA = 1.0f;
Jim Miller022554e2012-10-22 19:03:06 -070046 private static final float INACTIVE_ALPHA = 1.0f;
47 private static final float ACTIVE_SCALE = 1.5f;
Chris Wren692bb132012-10-23 13:37:36 -040048 private static final float ACTIVE_TEXT_ALPHA = 0f;
49 private static final float INACTIVE_TEXT_ALPHA = 0.5f;
Jim Miller022554e2012-10-22 19:03:06 -070050 private static final int SWITCH_ANIMATION_DURATION = 150;
51
52 private final float mActiveAlpha;
53 private final float mActiveScale;
Chris Wren692bb132012-10-23 13:37:36 -040054 private final float mActiveTextAlpha;
Jim Miller022554e2012-10-22 19:03:06 -070055 private final float mInactiveAlpha;
Chris Wren692bb132012-10-23 13:37:36 -040056 private final float mInactiveTextAlpha;
Jim Miller022554e2012-10-22 19:03:06 -070057 private final float mShadowRadius;
58 private final float mStroke;
59 private final float mIconSize;
Jim Miller022554e2012-10-22 19:03:06 -070060 private final int mFrameColor;
61 private final int mFrameShadowColor;
Chris Wren692bb132012-10-23 13:37:36 -040062 private final int mTextColor;
63 private final int mHighlightColor;
Jim Miller022554e2012-10-22 19:03:06 -070064
65 private boolean mTouched;
66
Adam Cohen24b351a2012-09-14 15:02:27 -070067 private boolean mActive;
68 private boolean mInit = true;
Adam Cohen66b9fb1662012-09-05 16:23:58 -070069 private KeyguardMultiUserSelectorView mUserSelector;
Chris Wren692bb132012-10-23 13:37:36 -040070 private KeyguardCircleFramedDrawable mFramed;
Chris Wrenf41c61b2012-11-29 15:19:54 -050071 private boolean mPressLock;
Jim Miller5ecd8112013-01-09 18:50:26 -080072 private UserManager mUserManager;
Adam Cohen96543292012-09-28 18:13:14 -070073
Adam Cohen66b9fb1662012-09-05 16:23:58 -070074 public static KeyguardMultiUserAvatar fromXml(int resId, Context context,
75 KeyguardMultiUserSelectorView userSelector, UserInfo info) {
76 KeyguardMultiUserAvatar icon = (KeyguardMultiUserAvatar)
77 LayoutInflater.from(context).inflate(resId, userSelector, false);
78
Adam Cohen0a8d3262012-10-03 13:50:55 -070079 icon.init(info, userSelector);
Adam Cohen66b9fb1662012-09-05 16:23:58 -070080 return icon;
81 }
82
83 public KeyguardMultiUserAvatar(Context context) {
Adam Cohen0a8d3262012-10-03 13:50:55 -070084 this(context, null, 0);
Adam Cohen66b9fb1662012-09-05 16:23:58 -070085 }
86
87 public KeyguardMultiUserAvatar(Context context, AttributeSet attrs) {
Adam Cohen0a8d3262012-10-03 13:50:55 -070088 this(context, attrs, 0);
Adam Cohen66b9fb1662012-09-05 16:23:58 -070089 }
90
91 public KeyguardMultiUserAvatar(Context context, AttributeSet attrs, int defStyle) {
92 super(context, attrs, defStyle);
Adam Cohen66b9fb1662012-09-05 16:23:58 -070093
Jim Miller6bcd7322012-10-02 16:32:04 -070094 Resources res = mContext.getResources();
Chris Wren692bb132012-10-23 13:37:36 -040095 mTextColor = res.getColor(R.color.keyguard_avatar_nick_color);
96 mIconSize = res.getDimension(R.dimen.keyguard_avatar_size);
Jim Miller022554e2012-10-22 19:03:06 -070097 mStroke = res.getDimension(R.dimen.keyguard_avatar_frame_stroke_width);
98 mShadowRadius = res.getDimension(R.dimen.keyguard_avatar_frame_shadow_radius);
Jim Miller022554e2012-10-22 19:03:06 -070099 mFrameColor = res.getColor(R.color.keyguard_avatar_frame_color);
100 mFrameShadowColor = res.getColor(R.color.keyguard_avatar_frame_shadow_color);
Chris Wren692bb132012-10-23 13:37:36 -0400101 mHighlightColor = res.getColor(R.color.keyguard_avatar_frame_pressed_color);
102 mActiveTextAlpha = ACTIVE_TEXT_ALPHA;
103 mInactiveTextAlpha = INACTIVE_TEXT_ALPHA;
Jim Miller022554e2012-10-22 19:03:06 -0700104 mActiveScale = ACTIVE_SCALE;
105 mActiveAlpha = ACTIVE_ALPHA;
106 mInactiveAlpha = INACTIVE_ALPHA;
Jim Miller5ecd8112013-01-09 18:50:26 -0800107 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Jim Miller022554e2012-10-22 19:03:06 -0700108
109 mTouched = false;
Chris Wren692bb132012-10-23 13:37:36 -0400110
111 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Jim Miller022554e2012-10-22 19:03:06 -0700112 }
113
114 protected String rewriteIconPath(String path) {
Jim Miller022554e2012-10-22 19:03:06 -0700115 return path;
116 }
117
Adam Cohen0a8d3262012-10-03 13:50:55 -0700118 public void init(UserInfo user, KeyguardMultiUserSelectorView userSelector) {
119 mUserInfo = user;
120 mUserSelector = userSelector;
Jim Miller6bcd7322012-10-02 16:32:04 -0700121
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700122 mUserImage = (ImageView) findViewById(R.id.keyguard_user_avatar);
Jim Miller71b3cd52012-10-10 19:02:40 -0700123 mUserName = (TextView) findViewById(R.id.keyguard_user_name);
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700124
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700125 mFramed = (KeyguardCircleFramedDrawable)
126 KeyguardViewMediator.getAvatarCache().get(user.id);
127
128 // If we can't find it or the params don't match, create the drawable again
129 if (mFramed == null
130 || !mFramed.verifyParams(mIconSize, mFrameColor, mStroke, mFrameShadowColor,
131 mShadowRadius, mHighlightColor)) {
132 Bitmap icon = null;
133 try {
Amith Yamasani99f7fae2013-05-13 16:08:31 -0700134 icon = mUserManager.getUserIcon(user.id);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700135 } catch (Exception e) {
Amith Yamasani99f7fae2013-05-13 16:08:31 -0700136 if (DEBUG) Log.d(TAG, "failed to get profile icon " + user, e);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700137 }
138
139 if (icon == null) {
140 icon = BitmapFactory.decodeResource(mContext.getResources(),
141 com.android.internal.R.drawable.ic_contact_picture);
142 }
143
144 mFramed = new KeyguardCircleFramedDrawable(icon, (int) mIconSize, mFrameColor, mStroke,
145 mFrameShadowColor, mShadowRadius, mHighlightColor);
146 KeyguardViewMediator.getAvatarCache().put(user.id, mFramed);
Chris Wren28c5ee92012-10-24 16:05:14 -0400147 }
148
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700149 mFramed.reset();
Chris Wren28c5ee92012-10-24 16:05:14 -0400150
Chris Wren692bb132012-10-23 13:37:36 -0400151 mUserImage.setImageDrawable(mFramed);
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700152 mUserName.setText(mUserInfo.name);
Adam Cohen24b351a2012-09-14 15:02:27 -0700153 setOnClickListener(mUserSelector);
Adam Cohen24b351a2012-09-14 15:02:27 -0700154 mInit = false;
155 }
156
Jim Miller022554e2012-10-22 19:03:06 -0700157 public void setActive(boolean active, boolean animate, final Runnable onComplete) {
Adam Cohen24b351a2012-09-14 15:02:27 -0700158 if (mActive != active || mInit) {
159 mActive = active;
Adam Cohen24b351a2012-09-14 15:02:27 -0700160
161 if (active) {
Jim Miller022554e2012-10-22 19:03:06 -0700162 KeyguardLinearLayout parent = (KeyguardLinearLayout) getParent();
163 parent.setTopChild(this);
Svetoslav Ganovfc9c4cd2012-11-02 11:49:22 -0700164 // TODO: Create an appropriate asset when string changes are possible.
165 setContentDescription(mUserName.getText()
166 + ". " + mContext.getString(R.string.user_switched, ""));
167 } else {
168 setContentDescription(mUserName.getText());
Adam Cohen24b351a2012-09-14 15:02:27 -0700169 }
Adam Cohen61cd69c2012-10-02 21:42:54 -0700170 }
Jim Miller022554e2012-10-22 19:03:06 -0700171 updateVisualsForActive(mActive, animate, SWITCH_ANIMATION_DURATION, onComplete);
Adam Cohen61cd69c2012-10-02 21:42:54 -0700172 }
Adam Cohen24b351a2012-09-14 15:02:27 -0700173
Jim Miller022554e2012-10-22 19:03:06 -0700174 void updateVisualsForActive(boolean active, boolean animate, int duration,
Adam Cohen61cd69c2012-10-02 21:42:54 -0700175 final Runnable onComplete) {
Jim Miller022554e2012-10-22 19:03:06 -0700176 final float finalAlpha = active ? mActiveAlpha : mInactiveAlpha;
177 final float initAlpha = active ? mInactiveAlpha : mActiveAlpha;
Chris Wren692bb132012-10-23 13:37:36 -0400178 final float finalScale = active ? 1f : 1f / mActiveScale;
179 final float initScale = mFramed.getScale();
180 final int finalTextAlpha = active ? (int) (mActiveTextAlpha * 255) :
181 (int) (mInactiveTextAlpha * 255);
182 final int initTextAlpha = active ? (int) (mInactiveTextAlpha * 255) :
183 (int) (mActiveTextAlpha * 255);
184 int textColor = mTextColor;
Adam Cohen61cd69c2012-10-02 21:42:54 -0700185 mUserName.setTextColor(textColor);
Adam Cohen24b351a2012-09-14 15:02:27 -0700186
Jim Miller022554e2012-10-22 19:03:06 -0700187 if (animate && mTouched) {
Adam Cohen61cd69c2012-10-02 21:42:54 -0700188 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
189 va.addUpdateListener(new AnimatorUpdateListener() {
190 @Override
191 public void onAnimationUpdate(ValueAnimator animation) {
192 float r = animation.getAnimatedFraction();
193 float scale = (1 - r) * initScale + r * finalScale;
194 float alpha = (1 - r) * initAlpha + r * finalAlpha;
Chris Wren692bb132012-10-23 13:37:36 -0400195 int textAlpha = (int) ((1 - r) * initTextAlpha + r * finalTextAlpha);
196 mFramed.setScale(scale);
Adam Cohen61cd69c2012-10-02 21:42:54 -0700197 mUserImage.setAlpha(alpha);
Chris Wren692bb132012-10-23 13:37:36 -0400198 mUserName.setTextColor(Color.argb(textAlpha, 255, 255, 255));
199 mUserImage.invalidate();
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700200 }
Adam Cohen61cd69c2012-10-02 21:42:54 -0700201 });
202 va.addListener(new AnimatorListenerAdapter() {
203 @Override
204 public void onAnimationEnd(Animator animation) {
205 if (onComplete != null) {
206 onComplete.run();
207 }
208 }
209 });
210 va.setDuration(duration);
211 va.start();
212 } else {
Chris Wren692bb132012-10-23 13:37:36 -0400213 mFramed.setScale(finalScale);
Adam Cohen61cd69c2012-10-02 21:42:54 -0700214 mUserImage.setAlpha(finalAlpha);
Chris Wren692bb132012-10-23 13:37:36 -0400215 mUserName.setTextColor(Color.argb(finalTextAlpha, 255, 255, 255));
Adam Cohen61cd69c2012-10-02 21:42:54 -0700216 if (onComplete != null) {
217 post(onComplete);
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700218 }
Adam Cohen24b351a2012-09-14 15:02:27 -0700219 }
Jim Miller022554e2012-10-22 19:03:06 -0700220
221 mTouched = true;
Adam Cohen24b351a2012-09-14 15:02:27 -0700222 }
223
Adam Cohen96543292012-09-28 18:13:14 -0700224 @Override
225 public void setPressed(boolean pressed) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500226 if (mPressLock && !pressed) {
227 return;
228 }
229
230 if (mPressLock || !pressed || isClickable()) {
Chris Wrenf0fd1722012-10-26 09:45:43 -0400231 super.setPressed(pressed);
232 mFramed.setPressed(pressed);
233 mUserImage.invalidate();
234 }
Adam Cohen24b351a2012-09-14 15:02:27 -0700235 }
236
Chris Wrenf41c61b2012-11-29 15:19:54 -0500237 public void lockPressed(boolean pressed) {
238 mPressLock = pressed;
239 setPressed(pressed);
240 }
241
Adam Cohen24b351a2012-09-14 15:02:27 -0700242 public UserInfo getUserInfo() {
243 return mUserInfo;
244 }
Adam Cohen66b9fb1662012-09-05 16:23:58 -0700245}