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