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