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.car.ui.uxr;
18 
19 import android.annotation.SuppressLint;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.widget.ToggleButton;
23 
24 import androidx.annotation.Nullable;
25 
26 /**
27  * A {@link ToggleButton} that implements {@link DrawableStateView}, for allowing additional states
28  * such as ux restriction.
29  */
30 @SuppressLint("AppCompatCustomView")
31 public class DrawableStateToggleButton extends ToggleButton implements DrawableStateView {
32     private DrawableStateUtil mUtil;
33 
DrawableStateToggleButton(Context context)34     public DrawableStateToggleButton(Context context) {
35         super(context);
36     }
37 
DrawableStateToggleButton(Context context, @Nullable AttributeSet attrs)38     public DrawableStateToggleButton(Context context, @Nullable AttributeSet attrs) {
39         super(context, attrs);
40     }
41 
DrawableStateToggleButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr)42     public DrawableStateToggleButton(Context context, @Nullable AttributeSet attrs,
43             int defStyleAttr) {
44         super(context, attrs, defStyleAttr);
45     }
46 
47     @Override
setExtraDrawableState(@ullable int[] stateToAdd, @Nullable int[] stateToRemove)48     public void setExtraDrawableState(@Nullable int[] stateToAdd, @Nullable int[] stateToRemove) {
49         if (mUtil == null) {
50             mUtil = new DrawableStateUtil(this);
51         }
52         mUtil.setExtraDrawableState(stateToAdd, stateToRemove);
53     }
54 
55     @Override
onCreateDrawableState(int extraSpace)56     public int[] onCreateDrawableState(int extraSpace) {
57         if (mUtil == null) {
58             mUtil = new DrawableStateUtil(this);
59         }
60         return mUtil.onCreateDrawableState(extraSpace, space -> super.onCreateDrawableState(space));
61     }
62 }
63