1 /*
2  * Copyright (C) 2020 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 static android.provider.Settings.Global.NOTIFICATION_BUBBLES;
20 
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.database.ContentObserver;
24 import android.net.Uri;
25 import android.os.Handler;
26 import android.provider.Settings;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.core.PreferenceControllerMixin;
33 import com.android.settings.core.TogglePreferenceController;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
35 import com.android.settingslib.core.lifecycle.events.OnPause;
36 import com.android.settingslib.core.lifecycle.events.OnResume;
37 
38 /**
39  * Feature level screen for bubbles, available through notification menu.
40  * Allows user to turn bubbles on or off for the device.
41  */
42 public class BubbleNotificationPreferenceController extends TogglePreferenceController
43         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
44         LifecycleObserver, OnResume, OnPause {
45 
46     private static final String TAG = "BubbleNotifPrefContr";
47 
48     @VisibleForTesting
49     static final int ON = 1;
50     @VisibleForTesting
51     static final int OFF = 0;
52 
53     private SettingObserver mSettingObserver;
54 
BubbleNotificationPreferenceController(Context context, String preferenceKey)55     public BubbleNotificationPreferenceController(Context context, String preferenceKey) {
56         super(context, preferenceKey);
57     }
58 
59     @Override
displayPreference(PreferenceScreen screen)60     public void displayPreference(PreferenceScreen screen) {
61         super.displayPreference(screen);
62         Preference preference = screen.findPreference(getPreferenceKey());
63         if (preference != null) {
64             mSettingObserver = new SettingObserver(preference);
65         }
66     }
67 
68     @Override
onResume()69     public void onResume() {
70         if (mSettingObserver != null) {
71             mSettingObserver.register(mContext.getContentResolver(), true /* register */);
72         }
73     }
74 
75     @Override
onPause()76     public void onPause() {
77         if (mSettingObserver != null) {
78             mSettingObserver.register(mContext.getContentResolver(), false /* register */);
79         }
80     }
81 
82     @Override
getAvailabilityStatus()83     public int getAvailabilityStatus() {
84         return AVAILABLE;
85     }
86 
87     @Override
isChecked()88     public boolean isChecked() {
89         return Settings.Global.getInt(mContext.getContentResolver(),
90                 NOTIFICATION_BUBBLES, ON) == ON;
91     }
92 
93     @Override
setChecked(boolean isChecked)94     public boolean setChecked(boolean isChecked) {
95         return Settings.Global.putInt(mContext.getContentResolver(),
96                 NOTIFICATION_BUBBLES, isChecked ? ON : OFF);
97     }
98 
99     @Override
isSliceable()100     public boolean isSliceable() {
101         return false;
102     }
103 
104     class SettingObserver extends ContentObserver {
105 
106         private final Uri NOTIFICATION_BUBBLES_URI =
107                 Settings.Global.getUriFor(NOTIFICATION_BUBBLES);
108 
109         private final Preference mPreference;
110 
SettingObserver(Preference preference)111         SettingObserver(Preference preference) {
112             super(new Handler());
113             mPreference = preference;
114         }
115 
register(ContentResolver cr, boolean register)116         public void register(ContentResolver cr, boolean register) {
117             if (register) {
118                 cr.registerContentObserver(NOTIFICATION_BUBBLES_URI, false, this);
119             } else {
120                 cr.unregisterContentObserver(this);
121             }
122         }
123 
124         @Override
onChange(boolean selfChange, Uri uri)125         public void onChange(boolean selfChange, Uri uri) {
126             super.onChange(selfChange, uri);
127             if (NOTIFICATION_BUBBLES_URI.equals(uri)) {
128                 updateState(mPreference);
129             }
130         }
131     }
132 }
133