• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.taskbar;
17 
18 import android.animation.Animator;
19 import android.animation.AnimatorListenerAdapter;
20 import android.animation.ObjectAnimator;
21 import android.annotation.Nullable;
22 import android.content.Context;
23 import android.graphics.Rect;
24 import android.util.AttributeSet;
25 import android.view.View;
26 
27 import androidx.annotation.ColorInt;
28 import androidx.core.content.ContextCompat;
29 
30 import com.android.launcher3.LauncherAnimUtils;
31 import com.android.launcher3.R;
32 
33 /**
34  * View to render a handle that changes color based on the background to ensure contrast. Used for
35  * the taskbar when stashed as well as the bubble bar when stashed.
36  */
37 public class StashedHandleView extends View {
38 
39     private static final long COLOR_CHANGE_DURATION = 120;
40 
41     private final @ColorInt int mStashedHandleLightColor;
42     private final @ColorInt int mStashedHandleDarkColor;
43     private final Rect mSampledRegion = new Rect();
44     private final int[] mTmpArr = new int[2];
45 
46     private @Nullable ObjectAnimator mColorChangeAnim;
47 
StashedHandleView(Context context)48     public StashedHandleView(Context context) {
49         this(context, null);
50     }
51 
StashedHandleView(Context context, AttributeSet attrs)52     public StashedHandleView(Context context, AttributeSet attrs) {
53         this(context, attrs, 0);
54     }
55 
StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr)56     public StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr) {
57         this(context, attrs, defStyleAttr, 0);
58     }
59 
StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)60     public StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr,
61             int defStyleRes) {
62         super(context, attrs, defStyleAttr, defStyleRes);
63 
64         mStashedHandleLightColor = ContextCompat.getColor(context,
65                 R.color.taskbar_stashed_handle_light_color);
66         mStashedHandleDarkColor = ContextCompat.getColor(context,
67                 R.color.taskbar_stashed_handle_dark_color);
68     }
69 
70     /**
71      * Updates mSampledRegion to be the location of the stashedHandleBounds relative to the screen.
72      * @see #getSampledRegion()
73      */
updateSampledRegion(Rect stashedHandleBounds)74     public void updateSampledRegion(Rect stashedHandleBounds) {
75         getLocationOnScreen(mTmpArr);
76         // Translations are temporary due to animations, remove them for the purpose of determining
77         // the final region we want sampled.
78         mTmpArr[0] -= Math.round(getTranslationX());
79         mTmpArr[1] -= Math.round(getTranslationY());
80         mSampledRegion.set(stashedHandleBounds);
81         mSampledRegion.offset(mTmpArr[0], mTmpArr[1]);
82     }
83 
getSampledRegion()84     public Rect getSampledRegion() {
85         return mSampledRegion;
86     }
87 
88     /**
89      * Updates the handle color.
90      * @param isRegionDark Whether the background behind the handle is dark, and thus the handle
91      *                     should be light (and vice versa).
92      * @param animate Whether to animate the change, or apply it immediately.
93      */
updateHandleColor(boolean isRegionDark, boolean animate)94     public void updateHandleColor(boolean isRegionDark, boolean animate) {
95         int newColor = isRegionDark ? mStashedHandleLightColor : mStashedHandleDarkColor;
96         if (mColorChangeAnim != null) {
97             mColorChangeAnim.cancel();
98         }
99         if (animate) {
100             mColorChangeAnim = ObjectAnimator.ofArgb(this,
101                     LauncherAnimUtils.VIEW_BACKGROUND_COLOR, newColor);
102             mColorChangeAnim.addListener(new AnimatorListenerAdapter() {
103                 @Override
104                 public void onAnimationEnd(Animator animation) {
105                     mColorChangeAnim = null;
106                 }
107             });
108             mColorChangeAnim.setDuration(COLOR_CHANGE_DURATION);
109             mColorChangeAnim.start();
110         } else {
111             setBackgroundColor(newColor);
112         }
113     }
114 }
115