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  * The switch controller that is used to update the switch widget in the SwitchBar layout.
24  */
25 public class SwitchBarController extends SwitchWidgetController implements
26     SwitchBar.OnSwitchChangeListener {
27 
28     private final SwitchBar mSwitchBar;
29 
SwitchBarController(SwitchBar switchBar)30     public SwitchBarController(SwitchBar switchBar) {
31         mSwitchBar = switchBar;
32     }
33 
34     @Override
setupView()35     public void setupView() {
36         mSwitchBar.show();
37     }
38 
39     @Override
teardownView()40     public void teardownView() {
41         mSwitchBar.hide();
42     }
43 
44     @Override
updateTitle(boolean isChecked)45     public void updateTitle(boolean isChecked) {
46         mSwitchBar.setTextViewLabel(isChecked);
47     }
48 
49     @Override
startListening()50     public void startListening() {
51         mSwitchBar.addOnSwitchChangeListener(this);
52     }
53 
54     @Override
stopListening()55     public void stopListening() {
56         mSwitchBar.removeOnSwitchChangeListener(this);
57     }
58 
59     @Override
setChecked(boolean checked)60     public void setChecked(boolean checked) {
61         mSwitchBar.setChecked(checked);
62     }
63 
64     @Override
isChecked()65     public boolean isChecked() {
66         return mSwitchBar.isChecked();
67     }
68 
69     @Override
setEnabled(boolean enabled)70     public void setEnabled(boolean enabled) {
71         mSwitchBar.setEnabled(enabled);
72     }
73 
74     @Override
onSwitchChanged(Switch switchView, boolean isChecked)75     public void onSwitchChanged(Switch switchView, boolean isChecked) {
76         if (mListener != null) {
77             mListener.onSwitchToggled(isChecked);
78         }
79     }
80 
81     @Override
setDisabledByAdmin(EnforcedAdmin admin)82     public void setDisabledByAdmin(EnforcedAdmin admin) {
83         mSwitchBar.setDisabledByAdmin(admin);
84     }
85 
86     @Override
getSwitch()87     public Switch getSwitch() {
88         return mSwitchBar.getSwitch();
89     }
90 
91 }
92