Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 20 | import android.util.AttributeSet; |
Arc Wang | 0466c6e | 2020-02-04 18:12:45 +0800 | [diff] [blame] | 21 | import android.view.MotionEvent; |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.View.OnClickListener; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 24 | import android.widget.Switch; |
| 25 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 26 | import androidx.preference.PreferenceViewHolder; |
| 27 | |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 28 | import com.android.settings.R; |
Doris Ling | c4c9f4d | 2017-01-23 16:16:06 -0800 | [diff] [blame] | 29 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 30 | import com.android.settingslib.RestrictedPreference; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * A custom preference that provides inline switch toggle. It has a mandatory field for title, and |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 34 | * optional fields for icon and sub-text. And it can be restricted by admin state. |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 35 | */ |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 36 | public class MasterSwitchPreference extends RestrictedPreference { |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 37 | |
| 38 | private Switch mSwitch; |
| 39 | private boolean mChecked; |
Jason Chiu | 10e675d | 2019-10-24 17:30:34 +0800 | [diff] [blame] | 40 | private boolean mCheckedSet; |
Julia Reynolds | c68ae0b | 2017-04-14 09:30:30 -0400 | [diff] [blame] | 41 | private boolean mEnableSwitch = true; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 42 | |
| 43 | public MasterSwitchPreference(Context context, AttributeSet attrs, |
| 44 | int defStyleAttr, int defStyleRes) { |
| 45 | super(context, attrs, defStyleAttr, defStyleRes); |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | public MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 49 | super(context, attrs, defStyleAttr); |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public MasterSwitchPreference(Context context, AttributeSet attrs) { |
| 53 | super(context, attrs); |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public MasterSwitchPreference(Context context) { |
| 57 | super(context); |
Fan Zhang | fe29251 | 2017-03-22 11:43:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected int getSecondTargetResId() { |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 62 | return R.layout.restricted_preference_widget_master_switch; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void onBindViewHolder(PreferenceViewHolder holder) { |
| 67 | super.onBindViewHolder(holder); |
Arc Wang | d1ea136 | 2019-11-06 10:02:18 +0800 | [diff] [blame] | 68 | final View switchWidget = holder.findViewById(R.id.switchWidget); |
| 69 | if (switchWidget != null) { |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 70 | switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE); |
Arc Wang | d1ea136 | 2019-11-06 10:02:18 +0800 | [diff] [blame] | 71 | switchWidget.setOnClickListener(new OnClickListener() { |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 72 | @Override |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 73 | public void onClick(View v) { |
Julia Reynolds | c68ae0b | 2017-04-14 09:30:30 -0400 | [diff] [blame] | 74 | if (mSwitch != null && !mSwitch.isEnabled()) { |
| 75 | return; |
| 76 | } |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 77 | setChecked(!mChecked); |
| 78 | if (!callChangeListener(mChecked)) { |
| 79 | setChecked(!mChecked); |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 80 | } else { |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 81 | persistBoolean(mChecked); |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | }); |
Arc Wang | 0466c6e | 2020-02-04 18:12:45 +0800 | [diff] [blame] | 85 | |
| 86 | // Consumes move events to ignore drag actions. |
| 87 | switchWidget.setOnTouchListener((v, event) -> { |
| 88 | return event.getActionMasked() == MotionEvent.ACTION_MOVE; |
| 89 | }); |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 90 | } |
Julia Reynolds | c68ae0b | 2017-04-14 09:30:30 -0400 | [diff] [blame] | 91 | |
Fan Zhang | fe29251 | 2017-03-22 11:43:22 -0700 | [diff] [blame] | 92 | mSwitch = (Switch) holder.findViewById(R.id.switchWidget); |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 93 | if (mSwitch != null) { |
Doris Ling | e2ad415 | 2017-04-27 15:09:21 -0700 | [diff] [blame] | 94 | mSwitch.setContentDescription(getTitle()); |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 95 | mSwitch.setChecked(mChecked); |
Julia Reynolds | c68ae0b | 2017-04-14 09:30:30 -0400 | [diff] [blame] | 96 | mSwitch.setEnabled(mEnableSwitch); |
Doris Ling | 6467d20 | 2017-03-13 10:29:34 -0700 | [diff] [blame] | 97 | } |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public boolean isChecked() { |
jackqdyulei | 9891b74 | 2017-11-30 13:55:09 -0800 | [diff] [blame] | 101 | return mSwitch != null && mChecked; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public void setChecked(boolean checked) { |
Jason Chiu | 10e675d | 2019-10-24 17:30:34 +0800 | [diff] [blame] | 105 | // Always set checked the first time; don't assume the field's default of false. |
| 106 | final boolean changed = mChecked != checked; |
| 107 | if (changed || !mCheckedSet) { |
| 108 | mChecked = checked; |
| 109 | mCheckedSet = true; |
| 110 | if (mSwitch != null) { |
| 111 | mSwitch.setChecked(checked); |
| 112 | } |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 116 | public void setSwitchEnabled(boolean enabled) { |
Julia Reynolds | c68ae0b | 2017-04-14 09:30:30 -0400 | [diff] [blame] | 117 | mEnableSwitch = enabled; |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 118 | if (mSwitch != null) { |
| 119 | mSwitch.setEnabled(enabled); |
| 120 | } |
| 121 | } |
| 122 | |
Doris Ling | c4c9f4d | 2017-01-23 16:16:06 -0800 | [diff] [blame] | 123 | /** |
| 124 | * If admin is not null, disables the switch. |
| 125 | * Otherwise, keep it enabled. |
| 126 | */ |
| 127 | public void setDisabledByAdmin(EnforcedAdmin admin) { |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 128 | super.setDisabledByAdmin(admin); |
Doris Ling | c4c9f4d | 2017-01-23 16:16:06 -0800 | [diff] [blame] | 129 | setSwitchEnabled(admin == null); |
| 130 | } |
| 131 | |
| 132 | public Switch getSwitch() { |
| 133 | return mSwitch; |
| 134 | } |
Zhen Zhang | 43b882c | 2019-12-09 18:13:51 -0800 | [diff] [blame] | 135 | |
| 136 | @Override |
| 137 | protected boolean shouldHideSecondTarget() { |
| 138 | return getSecondTargetResId() == 0; |
| 139 | } |
Doris Ling | 1432cb8 | 2017-01-18 13:59:36 -0800 | [diff] [blame] | 140 | } |