summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/settings/PreferenceHighlighter.java
blob: 4ed4cf1133a8e9ed79b16b115f1cc3b089023bd1 (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
/*
 * Copyright (C) 2018 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.settings;

import static androidx.core.graphics.ColorUtils.setAlphaComponent;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Property;
import android.view.View;

import com.android.launcher3.util.Themes;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ItemDecoration;
import androidx.recyclerview.widget.RecyclerView.State;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

/**
 * Utility class for highlighting a preference
 */
public class PreferenceHighlighter extends ItemDecoration implements Runnable {

    private static final Property<PreferenceHighlighter, Integer> HIGHLIGHT_COLOR =
            new Property<PreferenceHighlighter, Integer>(Integer.TYPE, "highlightColor") {

                @Override
                public Integer get(PreferenceHighlighter highlighter) {
                    return highlighter.mHighlightColor;
                }

                @Override
                public void set(PreferenceHighlighter highlighter, Integer value) {
                    highlighter.mHighlightColor = value;
                    highlighter.mRv.invalidateItemDecorations();
                }
            };

    private static final long HIGHLIGHT_DURATION = 15000L;
    private static final long HIGHLIGHT_FADE_OUT_DURATION = 500L;
    private static final long HIGHLIGHT_FADE_IN_DURATION = 200L;
    private static final int END_COLOR = setAlphaComponent(Color.WHITE, 0);

    private final Paint mPaint = new Paint();
    private final RecyclerView mRv;
    private final int mIndex;

    private boolean mHighLightStarted = false;
    private int mHighlightColor = END_COLOR;


    public PreferenceHighlighter(RecyclerView rv, int index) {
        mRv = rv;
        mIndex = index;
    }

    @Override
    public void run() {
        mRv.addItemDecoration(this);
        mRv.smoothScrollToPosition(mIndex);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, State state) {
        ViewHolder holder = parent.findViewHolderForAdapterPosition(mIndex);
        if (holder == null) {
            return;
        }
        if (!mHighLightStarted && state.getRemainingScrollVertical() != 0) {
            // Wait until scrolling stopped
            return;
        }

        if (!mHighLightStarted) {
            // Start highlight
            int colorTo = setAlphaComponent(Themes.getColorAccent(mRv.getContext()), 66);
            ObjectAnimator anim = ObjectAnimator.ofArgb(this, HIGHLIGHT_COLOR, END_COLOR, colorTo);
            anim.setDuration(HIGHLIGHT_FADE_IN_DURATION);
            anim.setRepeatMode(ValueAnimator.REVERSE);
            anim.setRepeatCount(4);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    removeHighlight();
                }
            });
            anim.start();
            mHighLightStarted = true;
        }

        View view = holder.itemView;
        mPaint.setColor(mHighlightColor);
        c.drawRect(0, view.getY(), parent.getWidth(), view.getY() + view.getHeight(), mPaint);
    }

    private void removeHighlight() {
        ObjectAnimator anim = ObjectAnimator.ofArgb(
                this, HIGHLIGHT_COLOR, mHighlightColor, END_COLOR);
        anim.setDuration(HIGHLIGHT_FADE_OUT_DURATION);
        anim.setStartDelay(HIGHLIGHT_DURATION);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mRv.removeItemDecoration(PreferenceHighlighter.this);
            }
        });
        anim.start();
    }
}