blob: 9fe077e449641ad6e04c3e2c45ae37a79bdbcc80 [file] [log] [blame]
Doris Ling1432cb82017-01-18 13:59:36 -08001/*
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
17package com.android.settings.widget;
18
19import android.content.Context;
Doris Ling1432cb82017-01-18 13:59:36 -080020import android.util.AttributeSet;
Arc Wang0466c6e2020-02-04 18:12:45 +080021import android.view.MotionEvent;
Doris Ling6467d202017-03-13 10:29:34 -070022import android.view.View;
23import android.view.View.OnClickListener;
Doris Ling1432cb82017-01-18 13:59:36 -080024import android.widget.Switch;
25
Fan Zhang23f8d592018-08-28 15:11:40 -070026import androidx.preference.PreferenceViewHolder;
27
Doris Ling1432cb82017-01-18 13:59:36 -080028import com.android.settings.R;
Doris Lingc4c9f4d2017-01-23 16:16:06 -080029import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
Zhen Zhang43b882c2019-12-09 18:13:51 -080030import com.android.settingslib.RestrictedPreference;
Doris Ling1432cb82017-01-18 13:59:36 -080031
32/**
33 * A custom preference that provides inline switch toggle. It has a mandatory field for title, and
Zhen Zhang43b882c2019-12-09 18:13:51 -080034 * optional fields for icon and sub-text. And it can be restricted by admin state.
Doris Ling1432cb82017-01-18 13:59:36 -080035 */
Zhen Zhang43b882c2019-12-09 18:13:51 -080036public class MasterSwitchPreference extends RestrictedPreference {
Doris Ling1432cb82017-01-18 13:59:36 -080037
38 private Switch mSwitch;
39 private boolean mChecked;
Jason Chiu10e675d2019-10-24 17:30:34 +080040 private boolean mCheckedSet;
Julia Reynoldsc68ae0b2017-04-14 09:30:30 -040041 private boolean mEnableSwitch = true;
Doris Ling1432cb82017-01-18 13:59:36 -080042
43 public MasterSwitchPreference(Context context, AttributeSet attrs,
44 int defStyleAttr, int defStyleRes) {
45 super(context, attrs, defStyleAttr, defStyleRes);
Doris Ling1432cb82017-01-18 13:59:36 -080046 }
47
48 public MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
49 super(context, attrs, defStyleAttr);
Doris Ling1432cb82017-01-18 13:59:36 -080050 }
51
52 public MasterSwitchPreference(Context context, AttributeSet attrs) {
53 super(context, attrs);
Doris Ling1432cb82017-01-18 13:59:36 -080054 }
55
56 public MasterSwitchPreference(Context context) {
57 super(context);
Fan Zhangfe292512017-03-22 11:43:22 -070058 }
59
60 @Override
61 protected int getSecondTargetResId() {
Zhen Zhang43b882c2019-12-09 18:13:51 -080062 return R.layout.restricted_preference_widget_master_switch;
Doris Ling1432cb82017-01-18 13:59:36 -080063 }
64
65 @Override
66 public void onBindViewHolder(PreferenceViewHolder holder) {
67 super.onBindViewHolder(holder);
Arc Wangd1ea1362019-11-06 10:02:18 +080068 final View switchWidget = holder.findViewById(R.id.switchWidget);
69 if (switchWidget != null) {
Zhen Zhang43b882c2019-12-09 18:13:51 -080070 switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE);
Arc Wangd1ea1362019-11-06 10:02:18 +080071 switchWidget.setOnClickListener(new OnClickListener() {
Doris Ling1432cb82017-01-18 13:59:36 -080072 @Override
Doris Ling6467d202017-03-13 10:29:34 -070073 public void onClick(View v) {
Julia Reynoldsc68ae0b2017-04-14 09:30:30 -040074 if (mSwitch != null && !mSwitch.isEnabled()) {
75 return;
76 }
Doris Ling6467d202017-03-13 10:29:34 -070077 setChecked(!mChecked);
78 if (!callChangeListener(mChecked)) {
79 setChecked(!mChecked);
Doris Ling1432cb82017-01-18 13:59:36 -080080 } else {
Doris Ling6467d202017-03-13 10:29:34 -070081 persistBoolean(mChecked);
Doris Ling1432cb82017-01-18 13:59:36 -080082 }
83 }
84 });
Arc Wang0466c6e2020-02-04 18:12:45 +080085
86 // Consumes move events to ignore drag actions.
87 switchWidget.setOnTouchListener((v, event) -> {
88 return event.getActionMasked() == MotionEvent.ACTION_MOVE;
89 });
Doris Ling1432cb82017-01-18 13:59:36 -080090 }
Julia Reynoldsc68ae0b2017-04-14 09:30:30 -040091
Fan Zhangfe292512017-03-22 11:43:22 -070092 mSwitch = (Switch) holder.findViewById(R.id.switchWidget);
Doris Ling6467d202017-03-13 10:29:34 -070093 if (mSwitch != null) {
Doris Linge2ad4152017-04-27 15:09:21 -070094 mSwitch.setContentDescription(getTitle());
Doris Ling6467d202017-03-13 10:29:34 -070095 mSwitch.setChecked(mChecked);
Julia Reynoldsc68ae0b2017-04-14 09:30:30 -040096 mSwitch.setEnabled(mEnableSwitch);
Doris Ling6467d202017-03-13 10:29:34 -070097 }
Doris Ling1432cb82017-01-18 13:59:36 -080098 }
99
100 public boolean isChecked() {
jackqdyulei9891b742017-11-30 13:55:09 -0800101 return mSwitch != null && mChecked;
Doris Ling1432cb82017-01-18 13:59:36 -0800102 }
103
104 public void setChecked(boolean checked) {
Jason Chiu10e675d2019-10-24 17:30:34 +0800105 // 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 Ling1432cb82017-01-18 13:59:36 -0800113 }
114 }
115
Doris Ling1432cb82017-01-18 13:59:36 -0800116 public void setSwitchEnabled(boolean enabled) {
Julia Reynoldsc68ae0b2017-04-14 09:30:30 -0400117 mEnableSwitch = enabled;
Doris Ling1432cb82017-01-18 13:59:36 -0800118 if (mSwitch != null) {
119 mSwitch.setEnabled(enabled);
120 }
121 }
122
Doris Lingc4c9f4d2017-01-23 16:16:06 -0800123 /**
124 * If admin is not null, disables the switch.
125 * Otherwise, keep it enabled.
126 */
127 public void setDisabledByAdmin(EnforcedAdmin admin) {
Zhen Zhang43b882c2019-12-09 18:13:51 -0800128 super.setDisabledByAdmin(admin);
Doris Lingc4c9f4d2017-01-23 16:16:06 -0800129 setSwitchEnabled(admin == null);
130 }
131
132 public Switch getSwitch() {
133 return mSwitch;
134 }
Zhen Zhang43b882c2019-12-09 18:13:51 -0800135
136 @Override
137 protected boolean shouldHideSecondTarget() {
138 return getSecondTargetResId() == 0;
139 }
Doris Ling1432cb82017-01-18 13:59:36 -0800140}