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