1 /*
2  * Copyright (C) 2019 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.developeroptions.widget;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.Button;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.core.content.res.TypedArrayUtils;
27 
28 import com.android.car.developeroptions.R;
29 import com.android.settingslib.widget.LayoutPreference;
30 
31 /**
32  * Preference that presents a button with two states(On vs Off)
33  */
34 public class TwoStateButtonPreference extends LayoutPreference implements
35         View.OnClickListener {
36 
37     private boolean mIsChecked;
38     private final Button mButtonOn;
39     private final Button mButtonOff;
40 
TwoStateButtonPreference(Context context, AttributeSet attrs)41     public TwoStateButtonPreference(Context context, AttributeSet attrs) {
42         super(context, attrs, TypedArrayUtils.getAttr(
43                 context, R.attr.twoStateButtonPreferenceStyle, android.R.attr.preferenceStyle));
44 
45         if (attrs == null) {
46             mButtonOn = null;
47             mButtonOff = null;
48         } else {
49             final TypedArray styledAttrs = context.obtainStyledAttributes(attrs,
50                     R.styleable.TwoStateButtonPreference);
51             final int textOnId = styledAttrs.getResourceId(
52                     R.styleable.TwoStateButtonPreference_textOn,
53                     R.string.summary_placeholder);
54             final int textOffId = styledAttrs.getResourceId(
55                     R.styleable.TwoStateButtonPreference_textOff,
56                     R.string.summary_placeholder);
57             styledAttrs.recycle();
58 
59             mButtonOn = findViewById(R.id.state_on_button);
60             mButtonOn.setText(textOnId);
61             mButtonOn.setOnClickListener(this);
62             mButtonOff = findViewById(R.id.state_off_button);
63             mButtonOff.setText(textOffId);
64             mButtonOff.setOnClickListener(this);
65             setChecked(isChecked());
66         }
67     }
68 
69     @Override
onClick(View v)70     public void onClick(View v) {
71         final boolean stateOn = v.getId() == R.id.state_on_button;
72         setChecked(stateOn);
73         callChangeListener(stateOn);
74     }
75 
setChecked(boolean checked)76     public void setChecked(boolean checked) {
77         // Update state
78         mIsChecked = checked;
79         // And update UI
80         if (checked) {
81             mButtonOn.setVisibility(View.GONE);
82             mButtonOff.setVisibility(View.VISIBLE);
83         } else {
84             mButtonOn.setVisibility(View.VISIBLE);
85             mButtonOff.setVisibility(View.GONE);
86         }
87     }
88 
isChecked()89     public boolean isChecked() {
90         return mIsChecked;
91     }
92 
setButtonEnabled(boolean enabled)93     public void setButtonEnabled(boolean enabled) {
94         mButtonOn.setEnabled(enabled);
95         mButtonOff.setEnabled(enabled);
96     }
97 
98     @VisibleForTesting
getStateOnButton()99     public Button getStateOnButton() {
100         return mButtonOn;
101     }
102 
103     @VisibleForTesting
getStateOffButton()104     public Button getStateOffButton() {
105         return mButtonOff;
106     }
107 }