Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 18 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.ObjectAnimator; |
| 21 | import android.animation.PropertyValuesHolder; |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 22 | import android.appwidget.AppWidgetHostView; |
Michael Jurka | 67a871d | 2012-11-01 18:26:01 -0700 | [diff] [blame] | 23 | import android.appwidget.AppWidgetManager; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.res.Resources; |
| 26 | import android.graphics.Canvas; |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 27 | import android.graphics.LinearGradient; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 28 | import android.graphics.Paint; |
| 29 | import android.graphics.PorterDuff; |
| 30 | import android.graphics.PorterDuffXfermode; |
| 31 | import android.graphics.Rect; |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 32 | import android.graphics.Shader; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 34 | import android.os.Handler; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 36 | import android.view.MotionEvent; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 37 | import android.view.View; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 38 | import android.widget.FrameLayout; |
| 39 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 40 | public class KeyguardWidgetFrame extends FrameLayout { |
| 41 | private final static PorterDuffXfermode sAddBlendMode = |
| 42 | new PorterDuffXfermode(PorterDuff.Mode.ADD); |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 43 | |
Adam Powell | cfc3086 | 2012-10-29 18:21:31 -0700 | [diff] [blame] | 44 | static final float OUTLINE_ALPHA_MULTIPLIER = 0.6f; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 45 | static final int HOVER_OVER_DELETE_DROP_TARGET_OVERLAY_COLOR = 0x99FF0000; |
| 46 | |
| 47 | // Temporarily disable this for the time being until we know why the gfx is messing up |
| 48 | static final boolean ENABLE_HOVER_OVER_DELETE_DROP_TARGET_OVERLAY = true; |
Adam Powell | cfc3086 | 2012-10-29 18:21:31 -0700 | [diff] [blame] | 49 | |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 50 | private int mGradientColor; |
| 51 | private LinearGradient mForegroundGradient; |
| 52 | private LinearGradient mLeftToRightGradient; |
| 53 | private LinearGradient mRightToLeftGradient; |
| 54 | private Paint mGradientPaint = new Paint(); |
| 55 | boolean mLeftToRight = true; |
| 56 | |
Adam Cohen | 66b9fb166 | 2012-09-05 16:23:58 -0700 | [diff] [blame] | 57 | private float mOverScrollAmount = 0f; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 58 | private final Rect mForegroundRect = new Rect(); |
| 59 | private int mForegroundAlpha = 0; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 60 | private CheckLongPressHelper mLongPressHelper; |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 61 | private Animator mFrameFade; |
| 62 | private boolean mIsSmall = false; |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 63 | private Handler mWorkerHandler; |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 64 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 65 | private float mBackgroundAlpha; |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 66 | private float mContentAlpha; |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 67 | private float mBackgroundAlphaMultiplier = 1.0f; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 68 | private Drawable mBackgroundDrawable; |
| 69 | private Rect mBackgroundRect = new Rect(); |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 70 | |
| 71 | // These variables are all needed in order to size things properly before we're actually |
| 72 | // measured. |
Adam Cohen | 5be14de | 2012-10-30 11:19:48 -0700 | [diff] [blame] | 73 | private int mSmallWidgetHeight; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 74 | private int mSmallFrameHeight; |
| 75 | private boolean mWidgetLockedSmall = false; |
| 76 | private int mMaxChallengeTop = -1; |
Adam Cohen | 5d47a8d | 2012-11-03 19:10:56 -0700 | [diff] [blame] | 77 | private int mFrameStrokeAdjustment; |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 78 | private boolean mPerformAppWidgetSizeUpdateOnBootComplete; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 79 | |
| 80 | // This will hold the width value before we've actually been measured |
| 81 | private int mFrameHeight; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 82 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 83 | private boolean mIsHoveringOverDeleteDropTarget; |
| 84 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 85 | // Multiple callers may try and adjust the alpha of the frame. When a caller shows |
| 86 | // the outlines, we give that caller control, and nobody else can fade them out. |
| 87 | // This prevents animation conflicts. |
| 88 | private Object mBgAlphaController; |
| 89 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 90 | public KeyguardWidgetFrame(Context context) { |
| 91 | this(context, null, 0); |
| 92 | } |
| 93 | |
| 94 | public KeyguardWidgetFrame(Context context, AttributeSet attrs) { |
| 95 | this(context, attrs, 0); |
| 96 | } |
| 97 | |
| 98 | public KeyguardWidgetFrame(Context context, AttributeSet attrs, int defStyle) { |
| 99 | super(context, attrs, defStyle); |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 100 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 101 | mLongPressHelper = new CheckLongPressHelper(this); |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 102 | |
Adam Cohen | 7ffa8c0 | 2012-10-01 19:03:46 -0700 | [diff] [blame] | 103 | Resources res = context.getResources(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 104 | // TODO: this padding should really correspond to the padding embedded in the background |
Jim Miller | 022554e | 2012-10-22 19:03:06 -0700 | [diff] [blame] | 105 | // drawable (ie. outlines). |
Adam Cohen | 5d47a8d | 2012-11-03 19:10:56 -0700 | [diff] [blame] | 106 | float density = res.getDisplayMetrics().density; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 107 | int padding = (int) (res.getDisplayMetrics().density * 8); |
| 108 | setPadding(padding, padding, padding, padding); |
| 109 | |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 110 | mFrameStrokeAdjustment = 2 + (int) (2 * density); |
Adam Cohen | 5d47a8d | 2012-11-03 19:10:56 -0700 | [diff] [blame] | 111 | |
Adam Cohen | 6f72b1c | 2012-11-05 15:20:30 -0800 | [diff] [blame] | 112 | // This will be overriden on phones based on the current security mode, however on tablets |
| 113 | // we need to specify a height. |
| 114 | mSmallWidgetHeight = |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 115 | res.getDimensionPixelSize(R.dimen.kg_small_widget_height); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 116 | mBackgroundDrawable = res.getDrawable(R.drawable.kg_widget_bg_padded); |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 117 | mGradientColor = res.getColor(R.color.kg_widget_pager_gradient); |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 118 | mGradientPaint.setXfermode(sAddBlendMode); |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 121 | @Override |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 122 | protected void onDetachedFromWindow() { |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 123 | super.onDetachedFromWindow(); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 124 | cancelLongPress(); |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 125 | KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateMonitorCallbacks); |
| 126 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 129 | @Override |
| 130 | protected void onAttachedToWindow() { |
| 131 | super.onAttachedToWindow(); |
| 132 | KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateMonitorCallbacks); |
| 133 | } |
| 134 | |
| 135 | private KeyguardUpdateMonitorCallback mUpdateMonitorCallbacks = |
| 136 | new KeyguardUpdateMonitorCallback() { |
| 137 | @Override |
| 138 | public void onBootCompleted() { |
| 139 | if (mPerformAppWidgetSizeUpdateOnBootComplete) { |
| 140 | performAppWidgetSizeCallbacksIfNecessary(); |
| 141 | mPerformAppWidgetSizeUpdateOnBootComplete = false; |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 146 | void setIsHoveringOverDeleteDropTarget(boolean isHovering) { |
| 147 | if (ENABLE_HOVER_OVER_DELETE_DROP_TARGET_OVERLAY) { |
| 148 | if (mIsHoveringOverDeleteDropTarget != isHovering) { |
| 149 | mIsHoveringOverDeleteDropTarget = isHovering; |
| 150 | invalidate(); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 155 | @Override |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 156 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 157 | // Watch for longpress events at this level to make sure |
| 158 | // users can always pick up this widget |
| 159 | switch (ev.getAction()) { |
| 160 | case MotionEvent.ACTION_DOWN: |
| 161 | mLongPressHelper.postCheckForLongPress(ev); |
| 162 | break; |
| 163 | case MotionEvent.ACTION_MOVE: |
| 164 | mLongPressHelper.onMove(ev); |
| 165 | break; |
| 166 | case MotionEvent.ACTION_POINTER_DOWN: |
| 167 | case MotionEvent.ACTION_UP: |
| 168 | case MotionEvent.ACTION_CANCEL: |
| 169 | mLongPressHelper.cancelLongPress(); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | // Otherwise continue letting touch events fall through to children |
| 174 | return false; |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 178 | public boolean onTouchEvent(MotionEvent ev) { |
| 179 | // Watch for longpress events at this level to make sure |
| 180 | // users can always pick up this widget |
| 181 | switch (ev.getAction()) { |
| 182 | case MotionEvent.ACTION_MOVE: |
| 183 | mLongPressHelper.onMove(ev); |
| 184 | break; |
| 185 | case MotionEvent.ACTION_POINTER_DOWN: |
| 186 | case MotionEvent.ACTION_UP: |
| 187 | case MotionEvent.ACTION_CANCEL: |
| 188 | mLongPressHelper.cancelLongPress(); |
| 189 | break; |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 190 | } |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 191 | |
| 192 | // We return true here to ensure that we will get cancel / up signal |
| 193 | // even if none of our children have requested touch. |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 194 | return true; |
| 195 | } |
| 196 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 197 | @Override |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 198 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 199 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 200 | cancelLongPress(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void cancelLongPress() { |
| 205 | super.cancelLongPress(); |
| 206 | mLongPressHelper.cancelLongPress(); |
| 207 | } |
| 208 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 209 | |
| 210 | private void drawGradientOverlay(Canvas c) { |
| 211 | mGradientPaint.setShader(mForegroundGradient); |
| 212 | mGradientPaint.setAlpha(mForegroundAlpha); |
| 213 | c.drawRect(mForegroundRect, mGradientPaint); |
| 214 | } |
| 215 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 216 | private void drawHoveringOverDeleteOverlay(Canvas c) { |
| 217 | if (mIsHoveringOverDeleteDropTarget) { |
| 218 | c.drawColor(HOVER_OVER_DELETE_DROP_TARGET_OVERLAY_COLOR); |
| 219 | } |
| 220 | } |
| 221 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 222 | protected void drawBg(Canvas canvas) { |
| 223 | if (mBackgroundAlpha > 0.0f) { |
| 224 | Drawable bg = mBackgroundDrawable; |
| 225 | |
| 226 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
| 227 | bg.setBounds(mBackgroundRect); |
| 228 | bg.draw(canvas); |
| 229 | } |
| 230 | } |
| 231 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 232 | @Override |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 233 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 234 | if (ENABLE_HOVER_OVER_DELETE_DROP_TARGET_OVERLAY) { |
| 235 | canvas.save(); |
| 236 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 237 | drawBg(canvas); |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 238 | super.dispatchDraw(canvas); |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 239 | drawGradientOverlay(canvas); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 240 | if (ENABLE_HOVER_OVER_DELETE_DROP_TARGET_OVERLAY) { |
| 241 | drawHoveringOverDeleteOverlay(canvas); |
| 242 | canvas.restore(); |
| 243 | } |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Because this view has fading outlines, it is essential that we enable hardware |
| 248 | * layers on the content (child) so that updating the alpha of the outlines doesn't |
| 249 | * result in the content layer being recreated. |
| 250 | */ |
| 251 | public void enableHardwareLayersForContent() { |
| 252 | View widget = getContent(); |
| 253 | if (widget != null) { |
| 254 | widget.setLayerType(LAYER_TYPE_HARDWARE, null); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Because this view has fading outlines, it is essential that we enable hardware |
| 260 | * layers on the content (child) so that updating the alpha of the outlines doesn't |
| 261 | * result in the content layer being recreated. |
| 262 | */ |
| 263 | public void disableHardwareLayersForContent() { |
| 264 | View widget = getContent(); |
| 265 | if (widget != null) { |
| 266 | widget.setLayerType(LAYER_TYPE_NONE, null); |
| 267 | } |
| 268 | } |
| 269 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 270 | public void enableHardwareLayers() { |
| 271 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 272 | } |
| 273 | |
| 274 | public void disableHardwareLayers() { |
| 275 | setLayerType(LAYER_TYPE_NONE, null); |
| 276 | } |
| 277 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 278 | public View getContent() { |
| 279 | return getChildAt(0); |
| 280 | } |
| 281 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 282 | public int getContentAppWidgetId() { |
| 283 | View content = getContent(); |
| 284 | if (content instanceof AppWidgetHostView) { |
| 285 | return ((AppWidgetHostView) content).getAppWidgetId(); |
Michael Jurka | 67a871d | 2012-11-01 18:26:01 -0700 | [diff] [blame] | 286 | } else if (content instanceof KeyguardStatusView) { |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 287 | return ((KeyguardStatusView) content).getAppWidgetId(); |
Michael Jurka | 67a871d | 2012-11-01 18:26:01 -0700 | [diff] [blame] | 288 | } else { |
| 289 | return AppWidgetManager.INVALID_APPWIDGET_ID; |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 293 | public float getBackgroundAlpha() { |
| 294 | return mBackgroundAlpha; |
| 295 | } |
| 296 | |
| 297 | public void setBackgroundAlphaMultiplier(float multiplier) { |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 298 | if (Float.compare(mBackgroundAlphaMultiplier, multiplier) != 0) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 299 | mBackgroundAlphaMultiplier = multiplier; |
| 300 | invalidate(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | public float getBackgroundAlphaMultiplier() { |
| 305 | return mBackgroundAlphaMultiplier; |
| 306 | } |
| 307 | |
| 308 | public void setBackgroundAlpha(float alpha) { |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 309 | if (Float.compare(mBackgroundAlpha, alpha) != 0) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 310 | mBackgroundAlpha = alpha; |
| 311 | invalidate(); |
| 312 | } |
| 313 | } |
| 314 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 315 | public float getContentAlpha() { |
| 316 | return mContentAlpha; |
| 317 | } |
| 318 | |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 319 | public void setContentAlpha(float alpha) { |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 320 | mContentAlpha = alpha; |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 321 | View content = getContent(); |
| 322 | if (content != null) { |
| 323 | content.setAlpha(alpha); |
| 324 | } |
| 325 | } |
| 326 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 327 | /** |
| 328 | * Depending on whether the security is up, the widget size needs to change |
| 329 | * |
| 330 | * @param height The height of the widget, -1 for full height |
| 331 | */ |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 332 | private void setWidgetHeight(int height) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 333 | boolean needLayout = false; |
| 334 | View widget = getContent(); |
| 335 | if (widget != null) { |
| 336 | LayoutParams lp = (LayoutParams) widget.getLayoutParams(); |
| 337 | if (lp.height != height) { |
| 338 | needLayout = true; |
| 339 | lp.height = height; |
| 340 | } |
| 341 | } |
| 342 | if (needLayout) { |
| 343 | requestLayout(); |
| 344 | } |
| 345 | } |
| 346 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 347 | public void setMaxChallengeTop(int top) { |
| 348 | boolean dirty = mMaxChallengeTop != top; |
Adam Cohen | 44dc141 | 2012-11-07 20:50:39 -0800 | [diff] [blame] | 349 | mMaxChallengeTop = top; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 350 | mSmallWidgetHeight = top - getPaddingTop(); |
| 351 | mSmallFrameHeight = top + getPaddingBottom(); |
| 352 | if (dirty && mIsSmall) { |
| 353 | setWidgetHeight(mSmallWidgetHeight); |
| 354 | setFrameHeight(mSmallFrameHeight); |
| 355 | } else if (dirty && mWidgetLockedSmall) { |
| 356 | setWidgetHeight(mSmallWidgetHeight); |
| 357 | } |
| 358 | } |
| 359 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 360 | public boolean isSmall() { |
| 361 | return mIsSmall; |
| 362 | } |
| 363 | |
| 364 | public void adjustFrame(int challengeTop) { |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 365 | int frameHeight = challengeTop + getPaddingBottom(); |
| 366 | setFrameHeight(frameHeight); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Adam Cohen | 44dc141 | 2012-11-07 20:50:39 -0800 | [diff] [blame] | 369 | public void shrinkWidget(boolean alsoShrinkFrame) { |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 370 | mIsSmall = true; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 371 | setWidgetHeight(mSmallWidgetHeight); |
Adam Cohen | 44dc141 | 2012-11-07 20:50:39 -0800 | [diff] [blame] | 372 | |
| 373 | if (alsoShrinkFrame) { |
| 374 | setFrameHeight(mSmallFrameHeight); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | public int getSmallFrameHeight() { |
| 379 | return mSmallFrameHeight; |
| 380 | } |
| 381 | |
| 382 | public void shrinkWidget() { |
| 383 | shrinkWidget(true); |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | public void setWidgetLockedSmall(boolean locked) { |
Adam Cohen | 86006bb | 2012-11-02 00:13:24 -0700 | [diff] [blame] | 387 | if (locked) { |
| 388 | setWidgetHeight(mSmallWidgetHeight); |
| 389 | } |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 390 | mWidgetLockedSmall = locked; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | public void resetSize() { |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 394 | mIsSmall = false; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 395 | if (!mWidgetLockedSmall) { |
| 396 | setWidgetHeight(LayoutParams.MATCH_PARENT); |
| 397 | } |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 398 | setFrameHeight(getMeasuredHeight()); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 401 | public void setFrameHeight(int height) { |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 402 | mFrameHeight = height; |
| 403 | mBackgroundRect.set(0, 0, getMeasuredWidth(), Math.min(mFrameHeight, getMeasuredHeight())); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 404 | mForegroundRect.set(mFrameStrokeAdjustment, mFrameStrokeAdjustment,getMeasuredWidth() - |
| 405 | mFrameStrokeAdjustment, Math.min(getMeasuredHeight(), mFrameHeight) - |
| 406 | mFrameStrokeAdjustment); |
| 407 | updateGradient(); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 408 | invalidate(); |
| 409 | } |
| 410 | |
| 411 | public void hideFrame(Object caller) { |
Adam Cohen | db1c5d5 | 2012-11-03 17:10:07 -0700 | [diff] [blame] | 412 | fadeFrame(caller, false, 0f, KeyguardWidgetPager.CHILDREN_OUTLINE_FADE_OUT_DURATION); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | public void showFrame(Object caller) { |
Adam Cohen | db1c5d5 | 2012-11-03 17:10:07 -0700 | [diff] [blame] | 416 | fadeFrame(caller, true, OUTLINE_ALPHA_MULTIPLIER, |
| 417 | KeyguardWidgetPager.CHILDREN_OUTLINE_FADE_IN_DURATION); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | public void fadeFrame(Object caller, boolean takeControl, float alpha, int duration) { |
| 421 | if (takeControl) { |
| 422 | mBgAlphaController = caller; |
| 423 | } |
| 424 | |
Adam Cohen | 2b0501b | 2012-11-21 16:49:13 -0800 | [diff] [blame] | 425 | if (mBgAlphaController != caller && mBgAlphaController != null) { |
| 426 | return; |
| 427 | } |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 428 | |
| 429 | if (mFrameFade != null) { |
| 430 | mFrameFade.cancel(); |
| 431 | mFrameFade = null; |
| 432 | } |
| 433 | PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", alpha); |
| 434 | mFrameFade = ObjectAnimator.ofPropertyValuesHolder(this, bgAlpha); |
| 435 | mFrameFade.setDuration(duration); |
| 436 | mFrameFade.start(); |
| 437 | } |
| 438 | |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 439 | private void updateGradient() { |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 440 | float x0 = mLeftToRight ? 0 : mForegroundRect.width(); |
| 441 | float x1 = mLeftToRight ? mForegroundRect.width(): 0; |
| 442 | mLeftToRightGradient = new LinearGradient(x0, 0f, x1, 0f, |
| 443 | mGradientColor, 0, Shader.TileMode.CLAMP); |
| 444 | mRightToLeftGradient = new LinearGradient(x1, 0f, x0, 0f, |
| 445 | mGradientColor, 0, Shader.TileMode.CLAMP); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | @Override |
| 449 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 450 | super.onSizeChanged(w, h, oldw, oldh); |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 451 | |
| 452 | if (!mIsSmall) { |
| 453 | mFrameHeight = h; |
| 454 | } |
| 455 | |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 456 | // mFrameStrokeAdjustment is a cludge to prevent the overlay from drawing outside the |
| 457 | // rounded rect background. |
| 458 | mForegroundRect.set(mFrameStrokeAdjustment, mFrameStrokeAdjustment, |
| 459 | w - mFrameStrokeAdjustment, Math.min(h, mFrameHeight) - mFrameStrokeAdjustment); |
| 460 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 461 | mBackgroundRect.set(0, 0, getMeasuredWidth(), Math.min(h, mFrameHeight)); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 462 | updateGradient(); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 463 | invalidate(); |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 466 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 467 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 468 | performAppWidgetSizeCallbacksIfNecessary(); |
| 469 | } |
| 470 | |
| 471 | private void performAppWidgetSizeCallbacksIfNecessary() { |
| 472 | View content = getContent(); |
| 473 | if (!(content instanceof AppWidgetHostView)) return; |
| 474 | |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 475 | if (!KeyguardUpdateMonitor.getInstance(mContext).hasBootCompleted()) { |
| 476 | mPerformAppWidgetSizeUpdateOnBootComplete = true; |
| 477 | return; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 480 | // TODO: there's no reason to force the AppWidgetHostView to catch duplicate size calls. |
| 481 | // We can do that even more cheaply here. It's not an issue right now since we're in the |
| 482 | // system process and hence no binder calls. |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 483 | AppWidgetHostView awhv = (AppWidgetHostView) content; |
| 484 | float density = getResources().getDisplayMetrics().density; |
| 485 | |
| 486 | int width = (int) (content.getMeasuredWidth() / density); |
| 487 | int height = (int) (content.getMeasuredHeight() / density); |
| 488 | awhv.updateAppWidgetSize(null, width, height, width, height, true); |
| 489 | } |
| 490 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 491 | void setOverScrollAmount(float r, boolean left) { |
Adam Cohen | 66b9fb166 | 2012-09-05 16:23:58 -0700 | [diff] [blame] | 492 | if (Float.compare(mOverScrollAmount, r) != 0) { |
| 493 | mOverScrollAmount = r; |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 494 | mForegroundGradient = left ? mLeftToRightGradient : mRightToLeftGradient; |
Adam Cohen | 5d47a8d | 2012-11-03 19:10:56 -0700 | [diff] [blame] | 495 | mForegroundAlpha = (int) Math.round((0.5f * r * 255)); |
| 496 | |
| 497 | // We bump up the alpha of the outline to hide the fact that the overlay is drawing |
| 498 | // over the rounded part of the frame. |
Adam Cohen | 934d083 | 2012-11-03 20:02:33 -0700 | [diff] [blame] | 499 | float bgAlpha = Math.min(OUTLINE_ALPHA_MULTIPLIER + r * (1 - OUTLINE_ALPHA_MULTIPLIER), |
| 500 | 1f); |
| 501 | setBackgroundAlpha(bgAlpha); |
Adam Cohen | 66b9fb166 | 2012-09-05 16:23:58 -0700 | [diff] [blame] | 502 | invalidate(); |
| 503 | } |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 504 | } |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 505 | |
| 506 | public void onActive(boolean isActive) { |
| 507 | // hook for subclasses |
| 508 | } |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 509 | |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 510 | public boolean onUserInteraction(MotionEvent event) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 511 | // hook for subclasses |
| 512 | return false; |
| 513 | } |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 514 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 515 | public void onBouncerShowing(boolean showing) { |
| 516 | // hook for subclasses |
| 517 | } |
| 518 | |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 519 | public void setWorkerHandler(Handler workerHandler) { |
| 520 | mWorkerHandler = workerHandler; |
| 521 | } |
| 522 | |
| 523 | public Handler getWorkerHandler() { |
| 524 | return mWorkerHandler; |
| 525 | } |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 526 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 527 | } |