• 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 public class StashedHandleView extends View {
34 
35     private static final long COLOR_CHANGE_DURATION = 120;
36 
37     private final @ColorInt int mStashedHandleLightColor;
38     private final @ColorInt int mStashedHandleDarkColor;
39     private final Rect mSampledRegion = new Rect();
40     private final int[] mTmpArr = new int[2];
41 
42     private @Nullable ObjectAnimator mColorChangeAnim;
43 
StashedHandleView(Context context)44     public StashedHandleView(Context context) {
45         this(context, null);
46     }
47 
StashedHandleView(Context context, AttributeSet attrs)48     public StashedHandleView(Context context, AttributeSet attrs) {
49         this(context, attrs, 0);
50     }
51 
StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr)52     public StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr) {
53         this(context, attrs, defStyleAttr, 0);
54     }
55 
StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)56     public StashedHandleView(Context context, AttributeSet attrs, int defStyleAttr,
57             int defStyleRes) {
58         super(context, attrs, defStyleAttr, defStyleRes);
59 
60         mStashedHandleLightColor = ContextCompat.getColor(context,
61                 R.color.taskbar_stashed_handle_light_color);
62         mStashedHandleDarkColor = ContextCompat.getColor(context,
63                 R.color.taskbar_stashed_handle_dark_color);
64     }
65 
66     /**
67      * Updates mSampledRegion to be the location of the stashedHandleBounds relative to the screen.
68      * @see #getSampledRegion()
69      */
updateSampledRegion(Rect stashedHandleBounds)70     public void updateSampledRegion(Rect stashedHandleBounds) {
71         getLocationOnScreen(mTmpArr);
72         // Translations are temporary due to animations, remove them for the purpose of determining
73         // the final region we want sampled.
74         mTmpArr[0] -= Math.round(getTranslationX());
75         mTmpArr[1] -= Math.round(getTranslationY());
76         mSampledRegion.set(stashedHandleBounds);
77         mSampledRegion.offset(mTmpArr[0], mTmpArr[1]);
78     }
79 
getSampledRegion()80     public Rect getSampledRegion() {
81         return mSampledRegion;
82     }
83 
84     /**
85      * Updates the handle color.
86      * @param isRegionDark Whether the background behind the handle is dark, and thus the handle
87      *                     should be light (and vice versa).
88      * @param animate Whether to animate the change, or apply it immediately.
89      */
updateHandleColor(boolean isRegionDark, boolean animate)90     public void updateHandleColor(boolean isRegionDark, boolean animate) {
91         int newColor = isRegionDark ? mStashedHandleLightColor : mStashedHandleDarkColor;
92         if (mColorChangeAnim != null) {
93             mColorChangeAnim.cancel();
94         }
95         if (animate) {
96             mColorChangeAnim = ObjectAnimator.ofArgb(this,
97                     LauncherAnimUtils.VIEW_BACKGROUND_COLOR, newColor);
98             mColorChangeAnim.addListener(new AnimatorListenerAdapter() {
99                 @Override
100                 public void onAnimationEnd(Animator animation) {
101                     mColorChangeAnim = null;
102                 }
103             });
104             mColorChangeAnim.setDuration(COLOR_CHANGE_DURATION);
105             mColorChangeAnim.start();
106         } else {
107             setBackgroundColor(newColor);
108         }
109     }
110 }
111