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; |
Jim Miller | 1962e26 | 2013-09-25 17:08:48 -0700 | [diff] [blame] | 150 | int resId = isHovering ? R.string.keyguard_accessibility_delete_widget_start |
| 151 | : R.string.keyguard_accessibility_delete_widget_end; |
| 152 | String text = getContext().getResources().getString(resId, getContentDescription()); |
| 153 | announceForAccessibility(text); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 154 | invalidate(); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 159 | @Override |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 160 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 161 | // Watch for longpress events at this level to make sure |
| 162 | // users can always pick up this widget |
| 163 | switch (ev.getAction()) { |
| 164 | case MotionEvent.ACTION_DOWN: |
| 165 | mLongPressHelper.postCheckForLongPress(ev); |
| 166 | break; |
| 167 | case MotionEvent.ACTION_MOVE: |
| 168 | mLongPressHelper.onMove(ev); |
| 169 | break; |
| 170 | case MotionEvent.ACTION_POINTER_DOWN: |
| 171 | case MotionEvent.ACTION_UP: |
| 172 | case MotionEvent.ACTION_CANCEL: |
| 173 | mLongPressHelper.cancelLongPress(); |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | // Otherwise continue letting touch events fall through to children |
| 178 | return false; |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 182 | public boolean onTouchEvent(MotionEvent ev) { |
| 183 | // Watch for longpress events at this level to make sure |
| 184 | // users can always pick up this widget |
| 185 | switch (ev.getAction()) { |
| 186 | case MotionEvent.ACTION_MOVE: |
| 187 | mLongPressHelper.onMove(ev); |
| 188 | break; |
| 189 | case MotionEvent.ACTION_POINTER_DOWN: |
| 190 | case MotionEvent.ACTION_UP: |
| 191 | case MotionEvent.ACTION_CANCEL: |
| 192 | mLongPressHelper.cancelLongPress(); |
| 193 | break; |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 194 | } |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 195 | |
| 196 | // We return true here to ensure that we will get cancel / up signal |
| 197 | // even if none of our children have requested touch. |
Jim Miller | 5d2da713 | 2012-10-02 18:38:09 -0700 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 201 | @Override |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 202 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 203 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 204 | cancelLongPress(); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void cancelLongPress() { |
| 209 | super.cancelLongPress(); |
| 210 | mLongPressHelper.cancelLongPress(); |
| 211 | } |
| 212 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 213 | |
| 214 | private void drawGradientOverlay(Canvas c) { |
| 215 | mGradientPaint.setShader(mForegroundGradient); |
| 216 | mGradientPaint.setAlpha(mForegroundAlpha); |
| 217 | c.drawRect(mForegroundRect, mGradientPaint); |
| 218 | } |
| 219 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 220 | private void drawHoveringOverDeleteOverlay(Canvas c) { |
| 221 | if (mIsHoveringOverDeleteDropTarget) { |
| 222 | c.drawColor(HOVER_OVER_DELETE_DROP_TARGET_OVERLAY_COLOR); |
| 223 | } |
| 224 | } |
| 225 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 226 | protected void drawBg(Canvas canvas) { |
| 227 | if (mBackgroundAlpha > 0.0f) { |
| 228 | Drawable bg = mBackgroundDrawable; |
| 229 | |
| 230 | bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255)); |
| 231 | bg.setBounds(mBackgroundRect); |
| 232 | bg.draw(canvas); |
| 233 | } |
| 234 | } |
| 235 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 236 | @Override |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 237 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 238 | if (ENABLE_HOVER_OVER_DELETE_DROP_TARGET_OVERLAY) { |
| 239 | canvas.save(); |
| 240 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 241 | drawBg(canvas); |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 242 | super.dispatchDraw(canvas); |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 243 | drawGradientOverlay(canvas); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 244 | if (ENABLE_HOVER_OVER_DELETE_DROP_TARGET_OVERLAY) { |
| 245 | drawHoveringOverDeleteOverlay(canvas); |
| 246 | canvas.restore(); |
| 247 | } |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Because this view has fading outlines, it is essential that we enable hardware |
| 252 | * layers on the content (child) so that updating the alpha of the outlines doesn't |
| 253 | * result in the content layer being recreated. |
| 254 | */ |
| 255 | public void enableHardwareLayersForContent() { |
| 256 | View widget = getContent(); |
John Reck | 0a4079e | 2013-09-30 15:31:31 -0700 | [diff] [blame] | 257 | if (widget != null && widget.isHardwareAccelerated()) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 258 | widget.setLayerType(LAYER_TYPE_HARDWARE, null); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Because this view has fading outlines, it is essential that we enable hardware |
| 264 | * layers on the content (child) so that updating the alpha of the outlines doesn't |
| 265 | * result in the content layer being recreated. |
| 266 | */ |
| 267 | public void disableHardwareLayersForContent() { |
| 268 | View widget = getContent(); |
| 269 | if (widget != null) { |
| 270 | widget.setLayerType(LAYER_TYPE_NONE, null); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | public View getContent() { |
| 275 | return getChildAt(0); |
| 276 | } |
| 277 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 278 | public int getContentAppWidgetId() { |
| 279 | View content = getContent(); |
| 280 | if (content instanceof AppWidgetHostView) { |
| 281 | return ((AppWidgetHostView) content).getAppWidgetId(); |
Michael Jurka | 67a871d | 2012-11-01 18:26:01 -0700 | [diff] [blame] | 282 | } else if (content instanceof KeyguardStatusView) { |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 283 | return ((KeyguardStatusView) content).getAppWidgetId(); |
Michael Jurka | 67a871d | 2012-11-01 18:26:01 -0700 | [diff] [blame] | 284 | } else { |
| 285 | return AppWidgetManager.INVALID_APPWIDGET_ID; |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 289 | public float getBackgroundAlpha() { |
| 290 | return mBackgroundAlpha; |
| 291 | } |
| 292 | |
| 293 | public void setBackgroundAlphaMultiplier(float multiplier) { |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 294 | if (Float.compare(mBackgroundAlphaMultiplier, multiplier) != 0) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 295 | mBackgroundAlphaMultiplier = multiplier; |
| 296 | invalidate(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | public float getBackgroundAlphaMultiplier() { |
| 301 | return mBackgroundAlphaMultiplier; |
| 302 | } |
| 303 | |
| 304 | public void setBackgroundAlpha(float alpha) { |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 305 | if (Float.compare(mBackgroundAlpha, alpha) != 0) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 306 | mBackgroundAlpha = alpha; |
| 307 | invalidate(); |
| 308 | } |
| 309 | } |
| 310 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 311 | public float getContentAlpha() { |
| 312 | return mContentAlpha; |
| 313 | } |
| 314 | |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 315 | public void setContentAlpha(float alpha) { |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 316 | mContentAlpha = alpha; |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 317 | View content = getContent(); |
| 318 | if (content != null) { |
| 319 | content.setAlpha(alpha); |
| 320 | } |
| 321 | } |
| 322 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 323 | /** |
| 324 | * Depending on whether the security is up, the widget size needs to change |
Jim Miller | 1962e26 | 2013-09-25 17:08:48 -0700 | [diff] [blame] | 325 | * |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 326 | * @param height The height of the widget, -1 for full height |
| 327 | */ |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 328 | private void setWidgetHeight(int height) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 329 | boolean needLayout = false; |
| 330 | View widget = getContent(); |
| 331 | if (widget != null) { |
| 332 | LayoutParams lp = (LayoutParams) widget.getLayoutParams(); |
| 333 | if (lp.height != height) { |
| 334 | needLayout = true; |
| 335 | lp.height = height; |
| 336 | } |
| 337 | } |
| 338 | if (needLayout) { |
| 339 | requestLayout(); |
| 340 | } |
| 341 | } |
| 342 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 343 | public void setMaxChallengeTop(int top) { |
| 344 | boolean dirty = mMaxChallengeTop != top; |
Adam Cohen | 44dc141 | 2012-11-07 20:50:39 -0800 | [diff] [blame] | 345 | mMaxChallengeTop = top; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 346 | mSmallWidgetHeight = top - getPaddingTop(); |
| 347 | mSmallFrameHeight = top + getPaddingBottom(); |
| 348 | if (dirty && mIsSmall) { |
| 349 | setWidgetHeight(mSmallWidgetHeight); |
| 350 | setFrameHeight(mSmallFrameHeight); |
| 351 | } else if (dirty && mWidgetLockedSmall) { |
| 352 | setWidgetHeight(mSmallWidgetHeight); |
| 353 | } |
| 354 | } |
| 355 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 356 | public boolean isSmall() { |
| 357 | return mIsSmall; |
| 358 | } |
| 359 | |
| 360 | public void adjustFrame(int challengeTop) { |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 361 | int frameHeight = challengeTop + getPaddingBottom(); |
| 362 | setFrameHeight(frameHeight); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Adam Cohen | 44dc141 | 2012-11-07 20:50:39 -0800 | [diff] [blame] | 365 | public void shrinkWidget(boolean alsoShrinkFrame) { |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 366 | mIsSmall = true; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 367 | setWidgetHeight(mSmallWidgetHeight); |
Adam Cohen | 44dc141 | 2012-11-07 20:50:39 -0800 | [diff] [blame] | 368 | |
| 369 | if (alsoShrinkFrame) { |
| 370 | setFrameHeight(mSmallFrameHeight); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | public int getSmallFrameHeight() { |
| 375 | return mSmallFrameHeight; |
| 376 | } |
| 377 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 378 | public void setWidgetLockedSmall(boolean locked) { |
Adam Cohen | 86006bb | 2012-11-02 00:13:24 -0700 | [diff] [blame] | 379 | if (locked) { |
| 380 | setWidgetHeight(mSmallWidgetHeight); |
| 381 | } |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 382 | mWidgetLockedSmall = locked; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | public void resetSize() { |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 386 | mIsSmall = false; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 387 | if (!mWidgetLockedSmall) { |
| 388 | setWidgetHeight(LayoutParams.MATCH_PARENT); |
| 389 | } |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 390 | setFrameHeight(getMeasuredHeight()); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 393 | public void setFrameHeight(int height) { |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 394 | mFrameHeight = height; |
| 395 | mBackgroundRect.set(0, 0, getMeasuredWidth(), Math.min(mFrameHeight, getMeasuredHeight())); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 396 | mForegroundRect.set(mFrameStrokeAdjustment, mFrameStrokeAdjustment,getMeasuredWidth() - |
| 397 | mFrameStrokeAdjustment, Math.min(getMeasuredHeight(), mFrameHeight) - |
| 398 | mFrameStrokeAdjustment); |
| 399 | updateGradient(); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 400 | invalidate(); |
| 401 | } |
| 402 | |
| 403 | public void hideFrame(Object caller) { |
Adam Cohen | db1c5d5 | 2012-11-03 17:10:07 -0700 | [diff] [blame] | 404 | fadeFrame(caller, false, 0f, KeyguardWidgetPager.CHILDREN_OUTLINE_FADE_OUT_DURATION); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | public void showFrame(Object caller) { |
Adam Cohen | db1c5d5 | 2012-11-03 17:10:07 -0700 | [diff] [blame] | 408 | fadeFrame(caller, true, OUTLINE_ALPHA_MULTIPLIER, |
| 409 | KeyguardWidgetPager.CHILDREN_OUTLINE_FADE_IN_DURATION); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | public void fadeFrame(Object caller, boolean takeControl, float alpha, int duration) { |
| 413 | if (takeControl) { |
| 414 | mBgAlphaController = caller; |
| 415 | } |
| 416 | |
Adam Cohen | 2b0501b | 2012-11-21 16:49:13 -0800 | [diff] [blame] | 417 | if (mBgAlphaController != caller && mBgAlphaController != null) { |
| 418 | return; |
| 419 | } |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 420 | |
| 421 | if (mFrameFade != null) { |
| 422 | mFrameFade.cancel(); |
| 423 | mFrameFade = null; |
| 424 | } |
| 425 | PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", alpha); |
| 426 | mFrameFade = ObjectAnimator.ofPropertyValuesHolder(this, bgAlpha); |
| 427 | mFrameFade.setDuration(duration); |
| 428 | mFrameFade.start(); |
| 429 | } |
| 430 | |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 431 | private void updateGradient() { |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 432 | float x0 = mLeftToRight ? 0 : mForegroundRect.width(); |
| 433 | float x1 = mLeftToRight ? mForegroundRect.width(): 0; |
| 434 | mLeftToRightGradient = new LinearGradient(x0, 0f, x1, 0f, |
| 435 | mGradientColor, 0, Shader.TileMode.CLAMP); |
| 436 | mRightToLeftGradient = new LinearGradient(x1, 0f, x0, 0f, |
| 437 | mGradientColor, 0, Shader.TileMode.CLAMP); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | @Override |
| 441 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 442 | super.onSizeChanged(w, h, oldw, oldh); |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 443 | |
| 444 | if (!mIsSmall) { |
| 445 | mFrameHeight = h; |
| 446 | } |
| 447 | |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 448 | // mFrameStrokeAdjustment is a cludge to prevent the overlay from drawing outside the |
| 449 | // rounded rect background. |
| 450 | mForegroundRect.set(mFrameStrokeAdjustment, mFrameStrokeAdjustment, |
| 451 | w - mFrameStrokeAdjustment, Math.min(h, mFrameHeight) - mFrameStrokeAdjustment); |
| 452 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 453 | mBackgroundRect.set(0, 0, getMeasuredWidth(), Math.min(h, mFrameHeight)); |
Adam Cohen | d51700b3 | 2012-11-07 16:26:46 -0800 | [diff] [blame] | 454 | updateGradient(); |
Adam Cohen | e364313 | 2012-10-28 18:29:17 -0700 | [diff] [blame] | 455 | invalidate(); |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 458 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 459 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 460 | performAppWidgetSizeCallbacksIfNecessary(); |
| 461 | } |
| 462 | |
| 463 | private void performAppWidgetSizeCallbacksIfNecessary() { |
| 464 | View content = getContent(); |
| 465 | if (!(content instanceof AppWidgetHostView)) return; |
| 466 | |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 467 | if (!KeyguardUpdateMonitor.getInstance(mContext).hasBootCompleted()) { |
| 468 | mPerformAppWidgetSizeUpdateOnBootComplete = true; |
| 469 | return; |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Adam Cohen | c276e82 | 2012-11-08 13:01:08 -0800 | [diff] [blame] | 472 | // TODO: there's no reason to force the AppWidgetHostView to catch duplicate size calls. |
| 473 | // We can do that even more cheaply here. It's not an issue right now since we're in the |
| 474 | // system process and hence no binder calls. |
Adam Cohen | 4ddcd57 | 2012-11-01 17:36:32 -0700 | [diff] [blame] | 475 | AppWidgetHostView awhv = (AppWidgetHostView) content; |
| 476 | float density = getResources().getDisplayMetrics().density; |
| 477 | |
| 478 | int width = (int) (content.getMeasuredWidth() / density); |
| 479 | int height = (int) (content.getMeasuredHeight() / density); |
| 480 | awhv.updateAppWidgetSize(null, width, height, width, height, true); |
| 481 | } |
| 482 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 483 | void setOverScrollAmount(float r, boolean left) { |
Adam Cohen | 66b9fb166 | 2012-09-05 16:23:58 -0700 | [diff] [blame] | 484 | if (Float.compare(mOverScrollAmount, r) != 0) { |
| 485 | mOverScrollAmount = r; |
Adam Cohen | 61cd69c | 2012-10-02 21:42:54 -0700 | [diff] [blame] | 486 | mForegroundGradient = left ? mLeftToRightGradient : mRightToLeftGradient; |
Adam Cohen | 5d47a8d | 2012-11-03 19:10:56 -0700 | [diff] [blame] | 487 | mForegroundAlpha = (int) Math.round((0.5f * r * 255)); |
| 488 | |
| 489 | // We bump up the alpha of the outline to hide the fact that the overlay is drawing |
| 490 | // over the rounded part of the frame. |
Adam Cohen | 934d083 | 2012-11-03 20:02:33 -0700 | [diff] [blame] | 491 | float bgAlpha = Math.min(OUTLINE_ALPHA_MULTIPLIER + r * (1 - OUTLINE_ALPHA_MULTIPLIER), |
| 492 | 1f); |
| 493 | setBackgroundAlpha(bgAlpha); |
Adam Cohen | 66b9fb166 | 2012-09-05 16:23:58 -0700 | [diff] [blame] | 494 | invalidate(); |
| 495 | } |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 496 | } |
John Spurlock | 86b6357 | 2012-10-24 11:24:25 -0400 | [diff] [blame] | 497 | |
| 498 | public void onActive(boolean isActive) { |
| 499 | // hook for subclasses |
| 500 | } |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 501 | |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 502 | public boolean onUserInteraction(MotionEvent event) { |
John Spurlock | 4b976ea | 2012-10-28 12:34:11 -0400 | [diff] [blame] | 503 | // hook for subclasses |
| 504 | return false; |
| 505 | } |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 506 | |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 507 | public void onBouncerShowing(boolean showing) { |
| 508 | // hook for subclasses |
| 509 | } |
| 510 | |
John Spurlock | 37d84ae | 2012-11-04 11:11:47 -0500 | [diff] [blame] | 511 | public void setWorkerHandler(Handler workerHandler) { |
| 512 | mWorkerHandler = workerHandler; |
| 513 | } |
| 514 | |
| 515 | public Handler getWorkerHandler() { |
| 516 | return mWorkerHandler; |
| 517 | } |
John Spurlock | 0552c5d | 2012-11-15 08:04:45 -0500 | [diff] [blame] | 518 | |
Jim Miller | f2dfc35 | 2012-08-29 18:42:21 -0700 | [diff] [blame] | 519 | } |