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         mSampledRegion.set(stashedHandleBounds);
73         mSampledRegion.offset(mTmpArr[0], mTmpArr[1]);
74     }
75 
getSampledRegion()76     public Rect getSampledRegion() {
77         return mSampledRegion;
78     }
79 
80     /**
81      * Updates the handle color.
82      * @param isRegionDark Whether the background behind the handle is dark, and thus the handle
83      *                     should be light (and vice versa).
84      * @param animate Whether to animate the change, or apply it immediately.
85      */
updateHandleColor(boolean isRegionDark, boolean animate)86     public void updateHandleColor(boolean isRegionDark, boolean animate) {
87         int newColor = isRegionDark ? mStashedHandleLightColor : mStashedHandleDarkColor;
88         if (mColorChangeAnim != null) {
89             mColorChangeAnim.cancel();
90         }
91         if (animate) {
92             mColorChangeAnim = ObjectAnimator.ofArgb(this,
93                     LauncherAnimUtils.VIEW_BACKGROUND_COLOR, newColor);
94             mColorChangeAnim.addListener(new AnimatorListenerAdapter() {
95                 @Override
96                 public void onAnimationEnd(Animator animation) {
97                     mColorChangeAnim = null;
98                 }
99             });
100             mColorChangeAnim.setDuration(COLOR_CHANGE_DURATION);
101             mColorChangeAnim.start();
102         } else {
103             setBackgroundColor(newColor);
104         }
105     }
106 }
107