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