1 /*
2  * Copyright (C) 2024 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.systembar;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.widget.LinearLayout;
23 
24 import androidx.annotation.DimenRes;
25 import androidx.annotation.LayoutRes;
26 import androidx.annotation.Nullable;
27 
28 import com.android.systemui.R;
29 import com.android.systemui.car.systembar.element.CarSystemBarElement;
30 import com.android.systemui.car.systembar.element.CarSystemBarElementFlags;
31 import com.android.systemui.car.systembar.element.CarSystemBarElementResolver;
32 
33 /** Custom view that provides the layout and attributes for creating system bar panels. */
34 public class CarSystemBarPanelButtonView extends LinearLayout implements CarSystemBarElement {
35     static final int INVALID_RESOURCE_ID = -1;
36 
37     private Class<?> mElementControllerClassAttr;
38     private int mSystemBarDisableFlags;
39     private int mSystemBarDisable2Flags;
40     private boolean mDisableForLockTaskModeLocked;
41 
42     @LayoutRes
43     private int mPanelLayoutRes;
44     @DimenRes
45     private int mPanelWidthRes;
46     @Nullable
47     private Integer mXOffset;
48     @Nullable
49     private Integer mYOffset;
50     @Nullable
51     private Integer mGravity;
52     @Nullable
53     private Boolean mDisabledWhileDriving;
54     @Nullable
55     private Boolean mDisabledWhileUnprovisioned;
56     @Nullable
57     private Boolean mShowAsDropDown;
58 
CarSystemBarPanelButtonView(Context context)59     public CarSystemBarPanelButtonView(Context context) {
60         super(context);
61         init(context, /* attrs= */ null);
62     }
63 
CarSystemBarPanelButtonView(Context context, @Nullable AttributeSet attrs)64     public CarSystemBarPanelButtonView(Context context, @Nullable AttributeSet attrs) {
65         super(context, attrs);
66         init(context, attrs);
67     }
68 
CarSystemBarPanelButtonView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)69     public CarSystemBarPanelButtonView(Context context, @Nullable AttributeSet attrs,
70             int defStyleAttr) {
71         super(context, attrs, defStyleAttr);
72         init(context, attrs);
73     }
74 
CarSystemBarPanelButtonView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)75     public CarSystemBarPanelButtonView(Context context, AttributeSet attrs, int defStyleAttr,
76             int defStyleRes) {
77         super(context, attrs, defStyleAttr, defStyleRes);
78         init(context, attrs);
79     }
80 
init(Context context, AttributeSet attrs)81     private void init(Context context, AttributeSet attrs) {
82         mElementControllerClassAttr =
83                 CarSystemBarElementResolver.getElementControllerClassFromAttributes(context, attrs);
84         mSystemBarDisableFlags =
85                 CarSystemBarElementFlags.getStatusBarManagerDisableFlagsFromAttributes(context,
86                         attrs);
87         mSystemBarDisable2Flags =
88                 CarSystemBarElementFlags.getStatusBarManagerDisableFlagsFromAttributes(context,
89                         attrs);
90         mDisableForLockTaskModeLocked =
91                 CarSystemBarElementFlags.getDisableForLockTaskModeLockedFromAttributes(context,
92                         attrs);
93 
94         TypedArray typedArray = context.obtainStyledAttributes(attrs,
95                 R.styleable.CarSystemBarPanelButtonView);
96         mPanelLayoutRes = typedArray.getResourceId(
97                 R.styleable.CarSystemBarPanelButtonView_panelLayoutRes,
98                 INVALID_RESOURCE_ID);
99         mPanelWidthRes = typedArray.getResourceId(
100                 R.styleable.CarSystemBarPanelButtonView_panelWidthRes,
101                 R.dimen.car_status_icon_panel_default_width);
102         mXOffset = typedArray.hasValue(R.styleable.CarSystemBarPanelButtonView_xOffset)
103                 ? typedArray.getInteger(R.styleable.CarSystemBarPanelButtonView_xOffset, 0) : null;
104         mYOffset = typedArray.hasValue(R.styleable.CarSystemBarPanelButtonView_yOffset)
105                 ? typedArray.getInteger(R.styleable.CarSystemBarPanelButtonView_yOffset, 0) : null;
106         mGravity = typedArray.hasValue(R.styleable.CarSystemBarPanelButtonView_gravity)
107                 ? typedArray.getInteger(R.styleable.CarSystemBarPanelButtonView_gravity, 0) : null;
108         mDisabledWhileDriving =
109                 typedArray.hasValue(R.styleable.CarSystemBarPanelButtonView_disabledWhileDriving)
110                         ? typedArray.getBoolean(
111                         R.styleable.CarSystemBarPanelButtonView_disabledWhileDriving, false) : null;
112         mDisabledWhileUnprovisioned = typedArray.hasValue(
113                 R.styleable.CarSystemBarPanelButtonView_disabledWhileUnprovisioned)
114                 ? typedArray.getBoolean(
115                 R.styleable.CarSystemBarPanelButtonView_disabledWhileUnprovisioned, false) : null;
116         mShowAsDropDown =
117                 typedArray.hasValue(R.styleable.CarSystemBarPanelButtonView_showAsDropDown)
118                         ? typedArray.getBoolean(
119                         R.styleable.CarSystemBarPanelButtonView_showAsDropDown, true) : null;
120         typedArray.recycle();
121     }
122 
123 
124     @LayoutRes
getPanelContentLayout()125     public int getPanelContentLayout() {
126         return mPanelLayoutRes;
127     }
128 
129     @DimenRes
getPanelWidth()130     public int getPanelWidth() {
131         return mPanelWidthRes;
132     }
133 
134     @Nullable
getXOffset()135     public Integer getXOffset() {
136         return mXOffset;
137     }
138 
139     @Nullable
getYOffset()140     public Integer getYOffset() {
141         return mYOffset;
142     }
143 
144     @Nullable
getPanelGravity()145     public Integer getPanelGravity() {
146         return mGravity;
147     }
148 
149     @Nullable
getDisabledWhileDriving()150     public Boolean getDisabledWhileDriving() {
151         return mDisabledWhileDriving;
152     }
153 
154     @Nullable
getDisabledWhileUnprovisioned()155     public Boolean getDisabledWhileUnprovisioned() {
156         return mDisabledWhileUnprovisioned;
157     }
158 
159     @Nullable
getShowAsDropDown()160     public Boolean getShowAsDropDown() {
161         return mShowAsDropDown;
162     }
163 
164     @Override
getElementControllerClass()165     public Class<?> getElementControllerClass() {
166         if (mElementControllerClassAttr != null) {
167             return mElementControllerClassAttr;
168         }
169         return CarSystemBarPanelButtonViewController.class;
170     }
171 
172     @Override
getSystemBarDisableFlags()173     public int getSystemBarDisableFlags() {
174         return mSystemBarDisableFlags;
175     }
176 
177     @Override
getSystemBarDisable2Flags()178     public int getSystemBarDisable2Flags() {
179         return mSystemBarDisable2Flags;
180     }
181 
182     @Override
disableForLockTaskModeLocked()183     public boolean disableForLockTaskModeLocked() {
184         return mDisableForLockTaskModeLocked;
185     }
186 }
187