1 /* 2 * Copyright (C) 2018 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 package com.android.launcher3.settings; 17 18 import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound; 19 20 import android.animation.Animator; 21 import android.animation.AnimatorListenerAdapter; 22 import android.animation.ObjectAnimator; 23 import android.animation.ValueAnimator; 24 import android.graphics.Canvas; 25 import android.graphics.Color; 26 import android.graphics.Paint; 27 import android.util.Property; 28 import android.view.View; 29 30 import com.android.launcher3.util.Themes; 31 32 import androidx.recyclerview.widget.RecyclerView; 33 import androidx.recyclerview.widget.RecyclerView.ItemDecoration; 34 import androidx.recyclerview.widget.RecyclerView.State; 35 import androidx.recyclerview.widget.RecyclerView.ViewHolder; 36 37 /** 38 * Utility class for highlighting a preference 39 */ 40 public class PreferenceHighlighter extends ItemDecoration implements Runnable { 41 42 private static final Property<PreferenceHighlighter, Integer> HIGHLIGHT_COLOR = 43 new Property<PreferenceHighlighter, Integer>(Integer.TYPE, "highlightColor") { 44 45 @Override 46 public Integer get(PreferenceHighlighter highlighter) { 47 return highlighter.mHighlightColor; 48 } 49 50 @Override 51 public void set(PreferenceHighlighter highlighter, Integer value) { 52 highlighter.mHighlightColor = value; 53 highlighter.mRv.invalidateItemDecorations(); 54 } 55 }; 56 57 private static final long HIGHLIGHT_DURATION = 15000L; 58 private static final long HIGHLIGHT_FADE_OUT_DURATION = 500L; 59 private static final long HIGHLIGHT_FADE_IN_DURATION = 200L; 60 private static final int END_COLOR = setColorAlphaBound(Color.WHITE, 0); 61 62 private final Paint mPaint = new Paint(); 63 private final RecyclerView mRv; 64 private final int mIndex; 65 66 private boolean mHighLightStarted = false; 67 private int mHighlightColor = END_COLOR; 68 69 PreferenceHighlighter(RecyclerView rv, int index)70 public PreferenceHighlighter(RecyclerView rv, int index) { 71 mRv = rv; 72 mIndex = index; 73 } 74 75 @Override run()76 public void run() { 77 mRv.addItemDecoration(this); 78 mRv.smoothScrollToPosition(mIndex); 79 } 80 81 @Override onDraw(Canvas c, RecyclerView parent, State state)82 public void onDraw(Canvas c, RecyclerView parent, State state) { 83 ViewHolder holder = parent.findViewHolderForAdapterPosition(mIndex); 84 if (holder == null) { 85 return; 86 } 87 if (!mHighLightStarted && state.getRemainingScrollVertical() != 0) { 88 // Wait until scrolling stopped 89 return; 90 } 91 92 if (!mHighLightStarted) { 93 // Start highlight 94 int colorTo = setColorAlphaBound(Themes.getColorAccent(mRv.getContext()), 66); 95 ObjectAnimator anim = ObjectAnimator.ofArgb(this, HIGHLIGHT_COLOR, END_COLOR, colorTo); 96 anim.setDuration(HIGHLIGHT_FADE_IN_DURATION); 97 anim.setRepeatMode(ValueAnimator.REVERSE); 98 anim.setRepeatCount(4); 99 anim.addListener(new AnimatorListenerAdapter() { 100 @Override 101 public void onAnimationEnd(Animator animation) { 102 removeHighlight(); 103 } 104 }); 105 anim.start(); 106 mHighLightStarted = true; 107 } 108 109 View view = holder.itemView; 110 mPaint.setColor(mHighlightColor); 111 c.drawRect(0, view.getY(), parent.getWidth(), view.getY() + view.getHeight(), mPaint); 112 } 113 removeHighlight()114 private void removeHighlight() { 115 ObjectAnimator anim = ObjectAnimator.ofArgb( 116 this, HIGHLIGHT_COLOR, mHighlightColor, END_COLOR); 117 anim.setDuration(HIGHLIGHT_FADE_OUT_DURATION); 118 anim.setStartDelay(HIGHLIGHT_DURATION); 119 anim.addListener(new AnimatorListenerAdapter() { 120 @Override 121 public void onAnimationEnd(Animator animation) { 122 mRv.removeItemDecoration(PreferenceHighlighter.this); 123 } 124 }); 125 anim.start(); 126 } 127 } 128