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.car.qc;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.res.TypedArray;
22 import android.util.AttributeSet;
23 import android.widget.Button;
24 
25 import androidx.annotation.Nullable;
26 
27 import com.android.systemui.R;
28 import com.android.systemui.car.systembar.element.CarSystemBarElement;
29 import com.android.systemui.car.systembar.element.CarSystemBarElementFlags;
30 import com.android.systemui.car.systembar.element.CarSystemBarElementResolver;
31 
32 import java.net.URISyntaxException;
33 
34 /**
35  * Footer button for quick control panels.
36  *
37  * Allows for an intent action to be specified via the {@link R.styleable.QCFooterButton_intent}
38  * attribute and for enabled state to be set according to driving mode via the
39  * {@link R.styleable.QCFooterButton_disableWhileDriving} attribute.
40  */
41 public class QCFooterButton extends Button implements CarSystemBarElement {
42     private final Class<?> mElementControllerClassAttr;
43     private final int mSystemBarDisableFlags;
44     private final int mSystemBarDisable2Flags;
45     private final boolean mDisableForLockTaskModeLocked;
46     private final Intent mIntent;
47     private final boolean mDisableWhileDriving;
48 
QCFooterButton(Context context)49     public QCFooterButton(Context context) {
50         this(context, /* attrs= */ null);
51     }
52 
QCFooterButton(Context context, AttributeSet attrs)53     public QCFooterButton(Context context, AttributeSet attrs) {
54         this(context, attrs, /* defStyleAttr= */ 0);
55     }
56 
QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr)57     public QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr) {
58         this(context, attrs, defStyleAttr, /* defStyleRes= */ 0);
59     }
60 
QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)61     public QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
62         super(context, attrs, defStyleAttr, defStyleRes);
63         mElementControllerClassAttr =
64                 CarSystemBarElementResolver.getElementControllerClassFromAttributes(context, attrs);
65         mSystemBarDisableFlags =
66                 CarSystemBarElementFlags.getStatusBarManagerDisableFlagsFromAttributes(context,
67                         attrs);
68         mSystemBarDisable2Flags =
69                 CarSystemBarElementFlags.getStatusBarManagerDisable2FlagsFromAttributes(context,
70                         attrs);
71         mDisableForLockTaskModeLocked =
72                 CarSystemBarElementFlags.getDisableForLockTaskModeLockedFromAttributes(context,
73                         attrs);
74         if (attrs == null) {
75             mIntent = null;
76             mDisableWhileDriving = false;
77             return;
78         }
79 
80         TypedArray typedArray = context.obtainStyledAttributes(attrs,
81                 R.styleable.QCFooterButton);
82         String intentString = typedArray.getString(R.styleable.QCFooterButton_intent);
83         if (intentString != null) {
84             try {
85                 mIntent = Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME);
86             } catch (URISyntaxException e) {
87                 throw new RuntimeException("Failed to attach intent", e);
88             }
89         } else {
90             mIntent = null;
91         }
92 
93         mDisableWhileDriving = typedArray.getBoolean(
94                 R.styleable.QCFooterButton_disableWhileDriving, /* defValue= */ false);
95     }
96 
97     @Nullable
getOnClickIntent()98     public Intent getOnClickIntent() {
99         return mIntent;
100     }
101 
isDisableWhileDriving()102     public boolean isDisableWhileDriving() {
103         return mDisableWhileDriving;
104     }
105 
106     @Override
getElementControllerClass()107     public Class<?> getElementControllerClass() {
108         if (mElementControllerClassAttr != null) {
109             return mElementControllerClassAttr;
110         }
111         return QCFooterButtonController.class;
112     }
113 
114     @Override
getSystemBarDisableFlags()115     public int getSystemBarDisableFlags() {
116         return mSystemBarDisableFlags;
117     }
118 
119     @Override
getSystemBarDisable2Flags()120     public int getSystemBarDisable2Flags() {
121         return mSystemBarDisable2Flags;
122     }
123 
124     @Override
disableForLockTaskModeLocked()125     public boolean disableForLockTaskModeLocked() {
126         return mDisableForLockTaskModeLocked;
127     }
128 }
129