jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 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; |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 22 | import android.view.View; |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 23 | import android.widget.Button; |
| 24 | |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 25 | import androidx.annotation.VisibleForTesting; |
| 26 | import androidx.core.content.res.TypedArrayUtils; |
| 27 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 28 | import com.android.settings.R; |
tmfang | dce94bb | 2018-11-26 18:41:01 +0800 | [diff] [blame] | 29 | import com.android.settingslib.widget.LayoutPreference; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 30 | |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Preference that presents a button with two states(On vs Off) |
| 33 | */ |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 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 | |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 41 | public TwoStateButtonPreference(Context context, AttributeSet attrs) { |
| 42 | super(context, attrs, TypedArrayUtils.getAttr( |
| 43 | context, R.attr.twoStateButtonPreferenceStyle, android.R.attr.preferenceStyle)); |
| 44 | |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 45 | if (attrs == null) { |
| 46 | mButtonOn = null; |
| 47 | mButtonOff = null; |
| 48 | } else { |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 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 | |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 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()); |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public void onClick(View v) { |
| 71 | final boolean stateOn = v.getId() == R.id.state_on_button; |
| 72 | setChecked(stateOn); |
| 73 | callChangeListener(stateOn); |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 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 | } |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 88 | |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 89 | 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 |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 104 | public Button getStateOffButton() { |
Fan Zhang | ddc6c64 | 2018-05-22 14:12:32 -0700 | [diff] [blame] | 105 | return mButtonOff; |
jackqdyulei | 92757b2 | 2018-01-24 19:42:42 -0800 | [diff] [blame] | 106 | } |
| 107 | } |