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.development;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.provider.Settings;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 import androidx.preference.SwitchPreference;
26 
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
29 
30 public class NotificationChannelWarningsPreferenceController extends
31         DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
32         PreferenceControllerMixin {
33 
34     private static final String SHOW_NOTIFICATION_CHANNEL_WARNINGS_KEY =
35             "show_notification_channel_warnings";
36 
37     @VisibleForTesting
38     final static int SETTING_VALUE_ON = 1;
39     @VisibleForTesting
40     final static int SETTING_VALUE_OFF = 0;
41     @VisibleForTesting
42     final static int DEBUGGING_ENABLED = 1;
43     @VisibleForTesting
44     final static int DEBUGGING_DISABLED = 0;
45 
NotificationChannelWarningsPreferenceController(Context context)46     public NotificationChannelWarningsPreferenceController(Context context) {
47         super(context);
48     }
49 
50 
51     @Override
getPreferenceKey()52     public String getPreferenceKey() {
53         return SHOW_NOTIFICATION_CHANNEL_WARNINGS_KEY;
54     }
55 
56     @Override
onPreferenceChange(Preference preference, Object newValue)57     public boolean onPreferenceChange(Preference preference, Object newValue) {
58         final boolean isEnabled = (Boolean) newValue;
59         Settings.Global.putInt(mContext.getContentResolver(),
60                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS,
61                 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
62         return true;
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         final int defaultWarningEnabled = isDebuggable() ? DEBUGGING_ENABLED : DEBUGGING_DISABLED;
68         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
69                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled);
70         ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
71     }
72 
73     @Override
onDeveloperOptionsSwitchDisabled()74     protected void onDeveloperOptionsSwitchDisabled() {
75         super.onDeveloperOptionsSwitchDisabled();
76         Settings.Global.putInt(mContext.getContentResolver(),
77                 Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, SETTING_VALUE_OFF);
78         ((SwitchPreference) mPreference).setChecked(false);
79     }
80 
81     @VisibleForTesting
isDebuggable()82     boolean isDebuggable() {
83         return Build.IS_DEBUGGABLE;
84     }
85 }
86