• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package com.android.settings.notification;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.View;
21 import android.widget.Switch;
22 
23 import androidx.preference.PreferenceViewHolder;
24 
25 import com.android.settings.R;
26 import com.android.settings.widget.MasterSwitchPreference;
27 import com.android.settingslib.RestrictedLockUtils;
28 
29 /**
30  * Shows an app icon, title and summary. Has a second switch touch target.
31  */
32 public class NotificationAppPreference extends MasterSwitchPreference {
33 
34     private Switch mSwitch;
35     private boolean mChecked;
36     private boolean mEnableSwitch = true;
37 
NotificationAppPreference(Context context)38     public NotificationAppPreference(Context context) {
39         super(context);
40     }
41 
NotificationAppPreference(Context context, AttributeSet attrs)42     public NotificationAppPreference(Context context, AttributeSet attrs) {
43         super(context, attrs);
44     }
45 
NotificationAppPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)46     public NotificationAppPreference(Context context, AttributeSet attrs,
47             int defStyleAttr, int defStyleRes) {
48         super(context, attrs, defStyleAttr, defStyleRes);
49     }
50 
NotificationAppPreference(Context context, AttributeSet attrs, int defStyleAttr)51     public NotificationAppPreference(Context context, AttributeSet attrs, int defStyleAttr) {
52         super(context, attrs, defStyleAttr);
53     }
54 
55     @Override
getSecondTargetResId()56     protected int getSecondTargetResId() {
57         return R.layout.preference_widget_master_switch;
58     }
59 
60     @Override
onBindViewHolder(PreferenceViewHolder view)61     public void onBindViewHolder(PreferenceViewHolder view) {
62         super.onBindViewHolder(view);
63 
64         final View widgetView = view.findViewById(android.R.id.widget_frame);
65         if (widgetView != null) {
66             widgetView.setOnClickListener(new View.OnClickListener() {
67                 @Override
68                 public void onClick(View v) {
69                     if (mSwitch != null && !mSwitch.isEnabled()) {
70                         return;
71                     }
72                     setChecked(!mChecked);
73                     if (!callChangeListener(mChecked)) {
74                         setChecked(!mChecked);
75                     } else {
76                         persistBoolean(mChecked);
77                     }
78                 }
79             });
80         }
81 
82         mSwitch = (Switch) view.findViewById(R.id.switchWidget);
83         if (mSwitch != null) {
84             mSwitch.setContentDescription(getTitle());
85             mSwitch.setChecked(mChecked);
86             mSwitch.setEnabled(mEnableSwitch);
87         }
88     }
89 
isChecked()90     public boolean isChecked() {
91         return mSwitch != null && mChecked;
92     }
93 
setChecked(boolean checked)94     public void setChecked(boolean checked) {
95         mChecked = checked;
96         if (mSwitch != null) {
97             mSwitch.setChecked(checked);
98         }
99     }
100 
setSwitchEnabled(boolean enabled)101     public void setSwitchEnabled(boolean enabled) {
102         mEnableSwitch = enabled;
103         if (mSwitch != null) {
104             mSwitch.setEnabled(enabled);
105         }
106     }
107 
108     /**
109      * If admin is not null, disables the switch.
110      * Otherwise, keep it enabled.
111      */
setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin)112     public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
113         setSwitchEnabled(admin == null);
114     }
115 
getSwitch()116     public Switch getSwitch() {
117         return mSwitch;
118     }
119 }
120