1 /*
2  * Copyright (C) 2020 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.CompoundButton;
20 import android.widget.CompoundButton.OnCheckedChangeListener;
21 
22 import com.android.settingslib.RestrictedLockUtils;
23 
24 /**
25  * The switch controller that is used to update the switch widget in the SettingsMainSwitchBar.
26  */
27 public class MainSwitchBarController extends SwitchWidgetController implements
28         OnCheckedChangeListener {
29 
30     private final SettingsMainSwitchBar mMainSwitch;
31 
MainSwitchBarController(SettingsMainSwitchBar mainSwitch)32     public MainSwitchBarController(SettingsMainSwitchBar mainSwitch) {
33         mMainSwitch = mainSwitch;
34     }
35 
36     @Override
setupView()37     public void setupView() {
38         mMainSwitch.show();
39     }
40 
41     @Override
teardownView()42     public void teardownView() {
43         mMainSwitch.hide();
44     }
45 
46     @Override
setTitle(String title)47     public void setTitle(String title) {
48         mMainSwitch.setTitle(title);
49     }
50 
51     @Override
startListening()52     public void startListening() {
53         mMainSwitch.addOnSwitchChangeListener(this);
54     }
55 
56     @Override
stopListening()57     public void stopListening() {
58         mMainSwitch.removeOnSwitchChangeListener(this);
59     }
60 
61     @Override
setChecked(boolean checked)62     public void setChecked(boolean checked) {
63         mMainSwitch.setChecked(checked);
64     }
65 
66     @Override
isChecked()67     public boolean isChecked() {
68         return mMainSwitch.isChecked();
69     }
70 
71     @Override
setEnabled(boolean enabled)72     public void setEnabled(boolean enabled) {
73         mMainSwitch.setEnabled(enabled);
74     }
75 
76     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)77     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
78         if (mListener != null) {
79             mListener.onSwitchToggled(isChecked);
80         }
81     }
82 
83     @Override
setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin)84     public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
85         mMainSwitch.setDisabledByAdmin(admin);
86     }
87 }
88