1 /*
2  * Copyright 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.car.calendar;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.ImageButton;
22 
23 import androidx.annotation.Nullable;
24 
25 import com.android.car.ui.uxr.DrawableStateView;
26 
27 /**
28  * An {@link ImageButton} that implements {@link DrawableStateView}, for allowing additional states
29  * such as ux restriction.
30  *
31  * @see com.android.car.ui.uxr.DrawableStateButton
32  *
33  * TODO(jdp) Move this to car-ui-lib.
34  */
35 public class DrawableStateImageButton extends ImageButton implements DrawableStateView {
36 
37     private int[] mState;
38 
DrawableStateImageButton(Context context)39     public DrawableStateImageButton(Context context) {
40         super(context);
41     }
42 
DrawableStateImageButton(Context context, @Nullable AttributeSet attrs)43     public DrawableStateImageButton(Context context, @Nullable AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
DrawableStateImageButton( Context context, @Nullable AttributeSet attrs, int defStyleAttr)47     public DrawableStateImageButton(
48             Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
49         super(context, attrs, defStyleAttr);
50     }
51 
DrawableStateImageButton( Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)52     public DrawableStateImageButton(
53             Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
54         super(context, attrs, defStyleAttr, defStyleRes);
55     }
56 
57     @Override
setDrawableState(int[] state)58     public void setDrawableState(int[] state) {
59         mState = state;
60         refreshDrawableState();
61     }
62 
63     @Override
onCreateDrawableState(int extraSpace)64     public int[] onCreateDrawableState(int extraSpace) {
65         if (mState == null) {
66             return super.onCreateDrawableState(extraSpace);
67         } else {
68             return mergeDrawableStates(
69                     super.onCreateDrawableState(extraSpace + mState.length), mState);
70         }
71     }
72 }
73