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