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; 20 import android.util.AttributeSet; 21 import android.view.MotionEvent; 22 import android.view.View; 23 import android.view.View.OnClickListener; 24 import android.widget.Switch; 25 26 import androidx.annotation.Keep; 27 import androidx.annotation.Nullable; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.settings.R; 31 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 32 import com.android.settingslib.RestrictedPreference; 33 34 /** 35 * A custom preference that provides inline switch toggle. It has a mandatory field for title, and 36 * optional fields for icon and sub-text. And it can be restricted by admin state. 37 */ 38 public class PrimarySwitchPreference extends RestrictedPreference { 39 40 private Switch mSwitch; 41 private boolean mChecked; 42 private boolean mCheckedSet; 43 private boolean mEnableSwitch = true; 44 PrimarySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)45 public PrimarySwitchPreference(Context context, AttributeSet attrs, 46 int defStyleAttr, int defStyleRes) { 47 super(context, attrs, defStyleAttr, defStyleRes); 48 } 49 PrimarySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)50 public PrimarySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 51 super(context, attrs, defStyleAttr); 52 } 53 PrimarySwitchPreference(Context context, AttributeSet attrs)54 public PrimarySwitchPreference(Context context, AttributeSet attrs) { 55 super(context, attrs); 56 } 57 PrimarySwitchPreference(Context context)58 public PrimarySwitchPreference(Context context) { 59 super(context); 60 } 61 62 @Override getSecondTargetResId()63 protected int getSecondTargetResId() { 64 return R.layout.restricted_preference_widget_primary_switch; 65 } 66 67 @Override onBindViewHolder(PreferenceViewHolder holder)68 public void onBindViewHolder(PreferenceViewHolder holder) { 69 super.onBindViewHolder(holder); 70 final View switchWidget = holder.findViewById(R.id.switchWidget); 71 if (switchWidget != null) { 72 switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE); 73 switchWidget.setOnClickListener(new OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 if (mSwitch != null && !mSwitch.isEnabled()) { 77 return; 78 } 79 setChecked(!mChecked); 80 if (!callChangeListener(mChecked)) { 81 setChecked(!mChecked); 82 } else { 83 persistBoolean(mChecked); 84 } 85 } 86 }); 87 88 // Consumes move events to ignore drag actions. 89 switchWidget.setOnTouchListener((v, event) -> { 90 return event.getActionMasked() == MotionEvent.ACTION_MOVE; 91 }); 92 } 93 94 mSwitch = (Switch) holder.findViewById(R.id.switchWidget); 95 if (mSwitch != null) { 96 mSwitch.setContentDescription(getTitle()); 97 mSwitch.setChecked(mChecked); 98 mSwitch.setEnabled(mEnableSwitch); 99 } 100 } 101 isChecked()102 public boolean isChecked() { 103 return mSwitch != null && mChecked; 104 } 105 106 /** 107 * Used to validate the state of mChecked and mCheckedSet when testing, without requiring 108 * that a ViewHolder be bound to the object. 109 */ 110 @Keep 111 @Nullable getCheckedState()112 public Boolean getCheckedState() { 113 return mCheckedSet ? mChecked : null; 114 } 115 116 /** 117 * Set the checked status to be {@code checked}. 118 * 119 * @param checked The new checked status 120 */ setChecked(boolean checked)121 public void setChecked(boolean checked) { 122 // Always set checked the first time; don't assume the field's default of false. 123 final boolean changed = mChecked != checked; 124 if (changed || !mCheckedSet) { 125 mChecked = checked; 126 mCheckedSet = true; 127 if (mSwitch != null) { 128 mSwitch.setChecked(checked); 129 } 130 } 131 } 132 133 /** 134 * Set the Switch to be the status of {@code enabled}. 135 * 136 * @param enabled The new enabled status 137 */ setSwitchEnabled(boolean enabled)138 public void setSwitchEnabled(boolean enabled) { 139 mEnableSwitch = enabled; 140 if (mSwitch != null) { 141 mSwitch.setEnabled(enabled); 142 } 143 } 144 145 /** 146 * If admin is not null, disables the switch. 147 * Otherwise, keep it enabled. 148 */ setDisabledByAdmin(EnforcedAdmin admin)149 public void setDisabledByAdmin(EnforcedAdmin admin) { 150 super.setDisabledByAdmin(admin); 151 setSwitchEnabled(admin == null); 152 } 153 getSwitch()154 public Switch getSwitch() { 155 return mSwitch; 156 } 157 158 @Override shouldHideSecondTarget()159 protected boolean shouldHideSecondTarget() { 160 return getSecondTargetResId() == 0; 161 } 162 } 163