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