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