• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.notification;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 
23 import androidx.preference.PreferenceViewHolder;
24 
25 import com.android.settings.widget.ToggleSwitch;
26 import com.android.settingslib.RestrictedLockUtils;
27 import com.android.settingslib.widget.LayoutPreference;
28 
29 public class NotificationSwitchBarPreference extends LayoutPreference {
30     private ToggleSwitch mSwitch;
31     private boolean mChecked;
32     private boolean mEnableSwitch = true;
33 
NotificationSwitchBarPreference(Context context, AttributeSet attrs)34     public NotificationSwitchBarPreference(Context context, AttributeSet attrs) {
35         super(context, attrs);
36     }
37 
38     @Override
onBindViewHolder(PreferenceViewHolder holder)39     public void onBindViewHolder(PreferenceViewHolder holder) {
40         super.onBindViewHolder(holder);
41         mSwitch = (ToggleSwitch) holder.findViewById(android.R.id.switch_widget);
42         if (mSwitch != null) {
43             mSwitch.setOnClickListener(new View.OnClickListener() {
44                 @Override
45                 public void onClick(View v) {
46                     if (!mSwitch.isEnabled()) {
47                         return;
48                     }
49                     mChecked = !mChecked;
50                     setChecked(mChecked);
51                     if (!callChangeListener(mChecked)) {
52                         setChecked(!mChecked);
53                     }
54                 }
55             });
56             mSwitch.setChecked(mChecked);
57             mSwitch.setEnabled(mEnableSwitch);
58         }
59     }
60 
isChecked()61     public boolean isChecked() {
62         return mSwitch != null && mSwitch.isEnabled() && mChecked;
63     }
64 
setChecked(boolean checked)65     public void setChecked(boolean checked) {
66         mChecked = checked;
67         if (mSwitch != null) {
68             mSwitch.setChecked(checked);
69         }
70     }
71 
setSwitchEnabled(boolean enabled)72     public void setSwitchEnabled(boolean enabled) {
73         mEnableSwitch = enabled;
74         if (mSwitch != null) {
75             mSwitch.setEnabled(enabled);
76         }
77     }
78 
setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin)79     public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
80         setSwitchEnabled(admin == null);
81     }
82 }
83