blob: eeee65def8a8041deb500b869cdb549469c89fae [file] [log] [blame]
jackqdyulei92757b22018-01-24 19:42:42 -08001/*
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
17package com.android.settings.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
jackqdyulei92757b22018-01-24 19:42:42 -080021import android.util.AttributeSet;
Fan Zhangddc6c642018-05-22 14:12:32 -070022import android.view.View;
jackqdyulei92757b22018-01-24 19:42:42 -080023import android.widget.Button;
24
Fan Zhangddc6c642018-05-22 14:12:32 -070025import androidx.annotation.VisibleForTesting;
26import androidx.core.content.res.TypedArrayUtils;
27
Fan Zhang23f8d592018-08-28 15:11:40 -070028import com.android.settings.R;
tmfangdce94bb2018-11-26 18:41:01 +080029import com.android.settingslib.widget.LayoutPreference;
Fan Zhang23f8d592018-08-28 15:11:40 -070030
jackqdyulei92757b22018-01-24 19:42:42 -080031/**
32 * Preference that presents a button with two states(On vs Off)
33 */
Fan Zhangddc6c642018-05-22 14:12:32 -070034public class TwoStateButtonPreference extends LayoutPreference implements
35 View.OnClickListener {
36
37 private boolean mIsChecked;
38 private final Button mButtonOn;
39 private final Button mButtonOff;
40
jackqdyulei92757b22018-01-24 19:42:42 -080041 public TwoStateButtonPreference(Context context, AttributeSet attrs) {
42 super(context, attrs, TypedArrayUtils.getAttr(
43 context, R.attr.twoStateButtonPreferenceStyle, android.R.attr.preferenceStyle));
44
Fan Zhangddc6c642018-05-22 14:12:32 -070045 if (attrs == null) {
46 mButtonOn = null;
47 mButtonOff = null;
48 } else {
jackqdyulei92757b22018-01-24 19:42:42 -080049 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
Fan Zhangddc6c642018-05-22 14:12:32 -070059 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());
jackqdyulei92757b22018-01-24 19:42:42 -080066 }
67 }
68
Fan Zhangddc6c642018-05-22 14:12:32 -070069 @Override
70 public void onClick(View v) {
71 final boolean stateOn = v.getId() == R.id.state_on_button;
72 setChecked(stateOn);
73 callChangeListener(stateOn);
jackqdyulei92757b22018-01-24 19:42:42 -080074 }
75
Fan Zhangddc6c642018-05-22 14:12:32 -070076 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 }
jackqdyulei92757b22018-01-24 19:42:42 -080088
Fan Zhangddc6c642018-05-22 14:12:32 -070089 public boolean isChecked() {
90 return mIsChecked;
91 }
92
93 public void setButtonEnabled(boolean enabled) {
94 mButtonOn.setEnabled(enabled);
95 mButtonOff.setEnabled(enabled);
96 }
97
98 @VisibleForTesting
99 public Button getStateOnButton() {
100 return mButtonOn;
101 }
102
103 @VisibleForTesting
jackqdyulei92757b22018-01-24 19:42:42 -0800104 public Button getStateOffButton() {
Fan Zhangddc6c642018-05-22 14:12:32 -0700105 return mButtonOff;
jackqdyulei92757b22018-01-24 19:42:42 -0800106 }
107}