1 /*
2  * Copyright (C) 2020 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 
17 package com.android.systemui.navigationbar.gestural;
18 
19 import android.animation.ArgbEvaluator;
20 import android.annotation.ColorInt;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.graphics.Canvas;
24 import android.graphics.Paint;
25 import android.graphics.drawable.Drawable;
26 import android.util.AttributeSet;
27 import android.view.ContextThemeWrapper;
28 import android.view.View;
29 
30 import com.android.settingslib.Utils;
31 import com.android.systemui.R;
32 import com.android.systemui.navigationbar.buttons.ButtonInterface;
33 
34 public class NavigationHandle extends View implements ButtonInterface {
35 
36     protected final Paint mPaint = new Paint();
37     private @ColorInt final int mLightColor;
38     private @ColorInt final int mDarkColor;
39     protected final float mRadius;
40     protected final float mBottom;
41     private boolean mRequiresInvalidate;
42 
NavigationHandle(Context context)43     public NavigationHandle(Context context) {
44         this(context, null);
45     }
46 
NavigationHandle(Context context, AttributeSet attr)47     public NavigationHandle(Context context, AttributeSet attr) {
48         super(context, attr);
49         final Resources res = context.getResources();
50         mRadius = res.getDimension(R.dimen.navigation_handle_radius);
51         mBottom = res.getDimension(R.dimen.navigation_handle_bottom);
52 
53         final int dualToneDarkTheme = Utils.getThemeAttr(context, R.attr.darkIconTheme);
54         final int dualToneLightTheme = Utils.getThemeAttr(context, R.attr.lightIconTheme);
55         Context lightContext = new ContextThemeWrapper(context, dualToneLightTheme);
56         Context darkContext = new ContextThemeWrapper(context, dualToneDarkTheme);
57         mLightColor = Utils.getColorAttrDefaultColor(lightContext, R.attr.homeHandleColor);
58         mDarkColor = Utils.getColorAttrDefaultColor(darkContext, R.attr.homeHandleColor);
59         mPaint.setAntiAlias(true);
60         setFocusable(false);
61     }
62 
63     @Override
setAlpha(float alpha)64     public void setAlpha(float alpha) {
65         super.setAlpha(alpha);
66         if (alpha > 0f && mRequiresInvalidate) {
67             mRequiresInvalidate = false;
68             invalidate();
69         }
70     }
71 
72     @Override
onDraw(Canvas canvas)73     protected void onDraw(Canvas canvas) {
74         super.onDraw(canvas);
75 
76         // Draw that bar
77         int navHeight = getHeight();
78         float height = mRadius * 2;
79         int width = getWidth();
80         float y = (navHeight - mBottom - height);
81         canvas.drawRoundRect(0, y, width, y + height, mRadius, mRadius, mPaint);
82     }
83 
84     @Override
setImageDrawable(Drawable drawable)85     public void setImageDrawable(Drawable drawable) {
86     }
87 
88     @Override
abortCurrentGesture()89     public void abortCurrentGesture() {
90     }
91 
92     @Override
setVertical(boolean vertical)93     public void setVertical(boolean vertical) {
94     }
95 
96     @Override
setDarkIntensity(float intensity)97     public void setDarkIntensity(float intensity) {
98         int color = (int) ArgbEvaluator.getInstance().evaluate(intensity, mLightColor, mDarkColor);
99         if (mPaint.getColor() != color) {
100             mPaint.setColor(color);
101             if (getVisibility() == VISIBLE && getAlpha() > 0) {
102                 invalidate();
103             } else {
104                 // If we are currently invisible, then invalidate when we are next made visible
105                 mRequiresInvalidate = true;
106             }
107         }
108     }
109 
110     @Override
setDelayTouchFeedback(boolean shouldDelay)111     public void setDelayTouchFeedback(boolean shouldDelay) {
112     }
113 }
114