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 
17 package com.android.systemui.shared.rotation;
18 
19 import android.content.Context;
20 import android.content.pm.ActivityInfo;
21 import android.content.res.Configuration;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.Paint;
25 import android.graphics.PorterDuff;
26 import android.graphics.PorterDuffColorFilter;
27 import android.util.AttributeSet;
28 import android.view.View;
29 import android.widget.ImageView;
30 
31 import androidx.annotation.DimenRes;
32 
33 import com.android.systemui.navigationbar.buttons.KeyButtonRipple;
34 
35 public class FloatingRotationButtonView extends ImageView {
36 
37     private static final float BACKGROUND_ALPHA = 0.92f;
38 
39     private KeyButtonRipple mRipple;
40     private final Paint mOvalBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
41 
42     private final Configuration mLastConfiguration;
43 
FloatingRotationButtonView(Context context, AttributeSet attrs)44     public FloatingRotationButtonView(Context context, AttributeSet attrs) {
45         this(context, attrs, 0);
46     }
47 
FloatingRotationButtonView(Context context, AttributeSet attrs, int defStyle)48     public FloatingRotationButtonView(Context context, AttributeSet attrs, int defStyle) {
49         super(context, attrs, defStyle);
50         mLastConfiguration = getResources().getConfiguration();
51 
52         setClickable(true);
53 
54         setWillNotDraw(false);
55         forceHasOverlappingRendering(false);
56     }
57 
setRipple(@imenRes int rippleMaxWidthResource)58     public void setRipple(@DimenRes int rippleMaxWidthResource) {
59         mRipple = new KeyButtonRipple(getContext(), this, rippleMaxWidthResource);
60         setBackground(mRipple);
61     }
62 
63     @Override
onWindowVisibilityChanged(int visibility)64     protected void onWindowVisibilityChanged(int visibility) {
65         super.onWindowVisibilityChanged(visibility);
66         if (visibility != View.VISIBLE) {
67             jumpDrawablesToCurrentState();
68         }
69     }
70 
71     @Override
onConfigurationChanged(Configuration newConfig)72     protected void onConfigurationChanged(Configuration newConfig) {
73         final int changes = mLastConfiguration.updateFrom(newConfig);
74         if ((changes & ActivityInfo.CONFIG_SCREEN_SIZE) != 0
75                 || ((changes & ActivityInfo.CONFIG_DENSITY) != 0)) {
76             if (mRipple != null) {
77                 mRipple.updateResources();
78             }
79         }
80     }
81 
setColors(int lightColor, int darkColor)82     public void setColors(int lightColor, int darkColor) {
83         getDrawable().setColorFilter(new PorterDuffColorFilter(lightColor, PorterDuff.Mode.SRC_IN));
84 
85         final int ovalBackgroundColor = Color.valueOf(Color.red(darkColor),
86                 Color.green(darkColor), Color.blue(darkColor), BACKGROUND_ALPHA).toArgb();
87 
88         mOvalBgPaint.setColor(ovalBackgroundColor);
89         mRipple.setType(KeyButtonRipple.Type.OVAL);
90     }
91 
setDarkIntensity(float darkIntensity)92     public void setDarkIntensity(float darkIntensity) {
93         mRipple.setDarkIntensity(darkIntensity);
94     }
95 
96     @Override
draw(Canvas canvas)97     public void draw(Canvas canvas) {
98         int d = Math.min(getWidth(), getHeight());
99         canvas.drawOval(0, 0, d, d, mOvalBgPaint);
100         super.draw(canvas);
101     }
102 }
103