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.widget.Switch; 20 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 21 22 /* 23 * A controller class for general switch widget handling. We have different containers that provide 24 * different forms of switch layout. Provide a centralized control for updating the switch widget. 25 */ 26 public abstract class SwitchWidgetController { 27 28 protected OnSwitchChangeListener mListener; 29 30 /** 31 * Interface definition for a callback to be invoked when the switch has been toggled. 32 */ 33 public interface OnSwitchChangeListener { 34 /** 35 * Called when the checked state of the Switch has changed. 36 * 37 * @param isChecked The new checked state of switchView. 38 * 39 * @return true to update the state of the switch with the new value. 40 */ onSwitchToggled(boolean isChecked)41 boolean onSwitchToggled(boolean isChecked); 42 } 43 44 /** 45 * Perform any view setup. 46 */ setupView()47 public void setupView() { 48 } 49 50 /** 51 * Perform any view teardown. 52 */ teardownView()53 public void teardownView() { 54 } 55 56 /** 57 * Set the callback to be invoked when the switch is toggled by the user (but before the 58 * internal state has been updated). 59 * 60 * @param listener the callback to be invoked 61 */ setListener(OnSwitchChangeListener listener)62 public void setListener(OnSwitchChangeListener listener) { 63 mListener = listener; 64 } 65 66 /** 67 * Update the preference title associated with the switch. 68 * 69 * @param isChecked whether the switch is currently checked 70 */ updateTitle(boolean isChecked)71 public abstract void updateTitle(boolean isChecked); 72 73 /** 74 * Start listening to switch toggling. 75 */ startListening()76 public abstract void startListening(); 77 78 /** 79 * Stop listening to switch toggling. 80 */ stopListening()81 public abstract void stopListening(); 82 83 /** 84 * Set the checked state for the switch. 85 * 86 * @param checked whether the switch should be checked or not. 87 */ setChecked(boolean checked)88 public abstract void setChecked(boolean checked); 89 90 /** 91 * Get the checked state for the switch. 92 * 93 * @return true if the switch is currently checked, false otherwise. 94 */ isChecked()95 public abstract boolean isChecked(); 96 97 /** 98 * Set the enabled state for the switch. 99 * 100 * @param enabled whether the switch should be enabled or not. 101 */ setEnabled(boolean enabled)102 public abstract void setEnabled(boolean enabled); 103 104 /** 105 * Disable the switch based on the enforce admin. 106 * 107 * @param admin Details of the admin who enforced the restriction. If it 108 * is {@code null}, then this preference will be enabled. Otherwise, it will be disabled. 109 */ setDisabledByAdmin(EnforcedAdmin admin)110 public abstract void setDisabledByAdmin(EnforcedAdmin admin); 111 112 /** 113 * Get the underlying switch widget. 114 * 115 * @return the switch widget. 116 */ getSwitch()117 public abstract Switch getSwitch(); 118 119 }