summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/keyboard/FocusIndicatorHelper.java
blob: c07ab084c96f08d5dafaa6c15d9941af1a474284 (plain)
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
/*
 * Copyright (C) 2016 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.launcher3.keyboard;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.RectEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Property;
import android.view.View;
import android.view.View.OnFocusChangeListener;

import com.android.launcher3.R;

/**
 * A helper class to draw background of a focused view.
 */
public abstract class FocusIndicatorHelper implements
        OnFocusChangeListener, AnimatorUpdateListener {

    private static final float MIN_VISIBLE_ALPHA = 0.2f;
    private static final long ANIM_DURATION = 150;

    public static final Property<FocusIndicatorHelper, Float> ALPHA =
            new Property<FocusIndicatorHelper, Float>(Float.TYPE, "alpha") {
                @Override
                public void set(FocusIndicatorHelper object, Float value) {
                    object.setAlpha(value);
                }

                @Override
                public Float get(FocusIndicatorHelper object) {
                    return object.mAlpha;
                }
            };

    public static final Property<FocusIndicatorHelper, Float> SHIFT =
            new Property<FocusIndicatorHelper, Float>(
                    Float.TYPE, "shift") {

                @Override
                public void set(FocusIndicatorHelper object, Float value) {
                    object.mShift = value;
                }

                @Override
                public Float get(FocusIndicatorHelper object) {
                    return object.mShift;
                }
            };

    private static final RectEvaluator RECT_EVALUATOR = new RectEvaluator(new Rect());
    private static final Rect sTempRect1 = new Rect();
    private static final Rect sTempRect2 = new Rect();

    private final View mContainer;
    private final Paint mPaint;
    private final int mMaxAlpha;

    private final Rect mDirtyRect = new Rect();
    private boolean mIsDirty = false;

    private View mLastFocusedView;

    private View mCurrentView;
    private View mTargetView;
    /**
     * The fraction indicating the position of the focusRect between {@link #mCurrentView}
     * & {@link #mTargetView}
     */
    private float mShift;

    private ObjectAnimator mCurrentAnimation;
    private float mAlpha;

    public FocusIndicatorHelper(View container) {
        mContainer = container;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        int color = container.getResources().getColor(R.color.focused_background);
        mMaxAlpha = Color.alpha(color);
        mPaint.setColor(0xFF000000 | color);

        setAlpha(0);
        mShift = 0;
    }

    protected void setAlpha(float alpha) {
        mAlpha = alpha;
        mPaint.setAlpha((int) (mAlpha * mMaxAlpha));
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        invalidateDirty();
    }

    protected void invalidateDirty() {
        if (mIsDirty) {
            mContainer.invalidate(mDirtyRect);
            mIsDirty = false;
        }

        Rect newRect = getDrawRect();
        if (newRect != null) {
            mContainer.invalidate(newRect);
        }
    }

    public void draw(Canvas c) {
        if (mAlpha > 0) {
            Rect newRect = getDrawRect();
            if (newRect != null) {
                mDirtyRect.set(newRect);
                c.drawRect(mDirtyRect, mPaint);
                mIsDirty = true;
            }
        }
    }

    private Rect getDrawRect() {
        if (mCurrentView != null && mCurrentView.isAttachedToWindow()) {
            viewToRect(mCurrentView, sTempRect1);

            if (mShift > 0 && mTargetView != null) {
                viewToRect(mTargetView, sTempRect2);
                return RECT_EVALUATOR.evaluate(mShift, sTempRect1, sTempRect2);
            } else {
                return sTempRect1;
            }
        }
        return null;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            endCurrentAnimation();

            if (mAlpha > MIN_VISIBLE_ALPHA) {
                mTargetView = v;

                mCurrentAnimation = ObjectAnimator.ofPropertyValuesHolder(this,
                        PropertyValuesHolder.ofFloat(ALPHA, 1),
                        PropertyValuesHolder.ofFloat(SHIFT, 1));
                mCurrentAnimation.addListener(new ViewSetListener(v, true));
            } else {
                setCurrentView(v);

                mCurrentAnimation = ObjectAnimator.ofPropertyValuesHolder(this,
                        PropertyValuesHolder.ofFloat(ALPHA, 1));
            }

            mLastFocusedView = v;
        } else {
            if (mLastFocusedView == v) {
                mLastFocusedView = null;
                endCurrentAnimation();
                mCurrentAnimation = ObjectAnimator.ofPropertyValuesHolder(this,
                        PropertyValuesHolder.ofFloat(ALPHA, 0));
                mCurrentAnimation.addListener(new ViewSetListener(null, false));
            }
        }

        // invalidate once
        invalidateDirty();

        mLastFocusedView = hasFocus ? v : null;
        if (mCurrentAnimation != null) {
            mCurrentAnimation.addUpdateListener(this);
            mCurrentAnimation.setDuration(ANIM_DURATION).start();
        }
    }

    protected void endCurrentAnimation() {
        if (mCurrentAnimation != null) {
            mCurrentAnimation.cancel();
            mCurrentAnimation = null;
        }
    }

    protected void setCurrentView(View v) {
        mCurrentView = v;
        mShift = 0;
        mTargetView = null;
    }

    /**
     * Gets the position of {@param v} relative to {@link #mContainer}.
     */
    public abstract void viewToRect(View v, Rect outRect);

    private class ViewSetListener extends AnimatorListenerAdapter {
        private final View mViewToSet;
        private final boolean mCallOnCancel;
        private boolean mCalled = false;

        public ViewSetListener(View v, boolean callOnCancel) {
            mViewToSet = v;
            mCallOnCancel = callOnCancel;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            if (!mCallOnCancel) {
                mCalled = true;
            }
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (!mCalled) {
                setCurrentView(mViewToSet);
                mCalled = true;
            }
        }
    }
}