|
16 | 16 |
|
17 | 17 | package com.android.systemui.statusbar.policy; |
18 | 18 |
|
| 19 | +import android.animation.ObjectAnimator; |
19 | 20 | import android.content.Context; |
| 21 | +import android.content.res.TypedArray; |
| 22 | +import android.graphics.Canvas; |
| 23 | +import android.os.SystemClock; |
20 | 24 | import android.util.AttributeSet; |
| 25 | +import android.util.Slog; |
21 | 26 | import android.view.MotionEvent; |
22 | 27 | import android.view.View; |
23 | 28 |
|
24 | 29 | import com.android.systemui.R; |
25 | 30 |
|
26 | 31 | public class DeadZone extends View { |
| 32 | + public static final String TAG = "DeadZone"; |
| 33 | + |
| 34 | + public static final boolean DEBUG = false; |
| 35 | + public static final int HORIZONTAL = 0; |
| 36 | + public static final int VERTICAL = 1; |
| 37 | + |
| 38 | + private boolean mShouldFlash; |
| 39 | + private float mFlashFrac = 0f; |
| 40 | + |
| 41 | + private int mSizeMax; |
| 42 | + private int mSizeMin; |
| 43 | + // Upon activity elsewhere in the UI, the dead zone will hold steady for |
| 44 | + // mHold ms, then move back over the course of mDecay ms |
| 45 | + private int mHold, mDecay; |
| 46 | + private boolean mVertical; |
| 47 | + private long mLastPokeTime; |
| 48 | + |
| 49 | + private final Runnable mDebugFlash = new Runnable() { |
| 50 | + @Override |
| 51 | + public void run() { |
| 52 | + ObjectAnimator.ofFloat(DeadZone.this, "flash", 1f, 0f).setDuration(150).start(); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
27 | 56 | public DeadZone(Context context, AttributeSet attrs) { |
28 | 57 | this(context, attrs, 0); |
29 | 58 | } |
30 | 59 |
|
31 | 60 | public DeadZone(Context context, AttributeSet attrs, int defStyle) { |
32 | 61 | super(context, attrs); |
| 62 | + |
| 63 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeadZone, |
| 64 | + defStyle, 0); |
| 65 | + |
| 66 | + mHold = a.getInteger(R.styleable.DeadZone_holdTime, 0); |
| 67 | + mDecay = a.getInteger(R.styleable.DeadZone_decayTime, 0); |
| 68 | + |
| 69 | + mSizeMin = a.getDimensionPixelSize(R.styleable.DeadZone_minSize, 0); |
| 70 | + mSizeMax = a.getDimensionPixelSize(R.styleable.DeadZone_maxSize, 0); |
| 71 | + |
| 72 | + int index = a.getInt(R.styleable.DeadZone_orientation, -1); |
| 73 | + mVertical = (index == VERTICAL); |
| 74 | + |
| 75 | + if (DEBUG) |
| 76 | + Slog.v(TAG, this + " size=[" + mSizeMin + "-" + mSizeMax + "] hold=" + mHold |
| 77 | + + (mVertical ? " vertical" : " horizontal")); |
| 78 | + |
| 79 | + setFlashOnTouchCapture(true); |
| 80 | + } |
| 81 | + |
| 82 | + static float lerp(float a, float b, float f) { |
| 83 | + return (b - a) * f + a; |
| 84 | + } |
| 85 | + |
| 86 | + private float getSize(long now) { |
| 87 | + if (mSizeMax == 0) |
| 88 | + return 0; |
| 89 | + long dt = (now - mLastPokeTime); |
| 90 | + if (dt > mHold + mDecay) |
| 91 | + return mSizeMin; |
| 92 | + if (dt < mHold) |
| 93 | + return mSizeMax; |
| 94 | + return (int) lerp(mSizeMax, mSizeMin, (float) (dt - mHold) / mDecay); |
| 95 | + } |
| 96 | + |
| 97 | + public void setFlashOnTouchCapture(boolean dbg) { |
| 98 | + mShouldFlash = dbg; |
| 99 | + mFlashFrac = 0f; |
| 100 | + postInvalidate(); |
33 | 101 | } |
34 | 102 |
|
35 | 103 | // I made you a touch event |
36 | 104 | @Override |
37 | | - public boolean onTouchEvent (MotionEvent event) { |
38 | | - return true; // but I eated it |
| 105 | + public boolean onTouchEvent(MotionEvent event) { |
| 106 | + if (DEBUG) |
| 107 | + Slog.v(TAG, this + " onTouch: " + MotionEvent.actionToString(event.getAction())); |
| 108 | + |
| 109 | + final int action = event.getAction(); |
| 110 | + if (action == MotionEvent.ACTION_OUTSIDE) { |
| 111 | + poke(event); |
| 112 | + } else if (action == MotionEvent.ACTION_DOWN) { |
| 113 | + if (DEBUG) |
| 114 | + Slog.v(TAG, this + " ACTION_DOWN: " + event.getX() + "," + event.getY()); |
| 115 | + int size = (int) getSize(event.getEventTime()); |
| 116 | + if ((mVertical && event.getX() < size) || event.getY() < size) { |
| 117 | + if (DEBUG) |
| 118 | + Slog.v(TAG, "eating click!"); |
| 119 | + if (mShouldFlash) { |
| 120 | + post(mDebugFlash); |
| 121 | + postInvalidate(); |
| 122 | + } |
| 123 | + return true; // but I eated it |
| 124 | + } |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + public void poke(MotionEvent event) { |
| 130 | + mLastPokeTime = event.getEventTime(); |
| 131 | + if (DEBUG) |
| 132 | + Slog.v(TAG, "poked! size=" + getSize(mLastPokeTime)); |
| 133 | + postInvalidate(); |
| 134 | + } |
| 135 | + |
| 136 | + public void setFlash(float f) { |
| 137 | + mFlashFrac = f; |
| 138 | + postInvalidate(); |
39 | 139 | } |
40 | | -} |
41 | 140 |
|
| 141 | + public float getFlash() { |
| 142 | + return mFlashFrac; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void onDraw(Canvas can) { |
| 147 | + if (!mShouldFlash || mFlashFrac <= 0f) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + final int size = (int) getSize(SystemClock.uptimeMillis()); |
| 152 | + can.clipRect(0, 0, mVertical ? size : can.getWidth(), mVertical ? can.getHeight() : size); |
| 153 | + final float frac = DEBUG ? (mFlashFrac - 0.5f) + 0.5f : mFlashFrac; |
| 154 | + can.drawARGB((int) (frac * 0xFF), 0xDD, 0xEE, 0xAA); |
| 155 | + |
| 156 | + if (DEBUG && size > mSizeMin) |
| 157 | + // crazy aggressive redrawing here, for debugging only |
| 158 | + postInvalidateDelayed(100); |
| 159 | + } |
| 160 | +} |
0 commit comments