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.app.ActivityManager; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.database.ContentObserver; 25 import android.net.Uri; 26 import android.os.Handler; 27 import android.provider.Settings; 28 29 import androidx.annotation.VisibleForTesting; 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.widget.SettingsMainSwitchPreferenceController; 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 SettingsMainSwitchPreferenceController implements LifecycleObserver, OnResume, OnPause { 44 45 private static final String TAG = "BubbleNotifPrefContr"; 46 47 @VisibleForTesting 48 static final int ON = 1; 49 @VisibleForTesting 50 static final int OFF = 0; 51 52 private SettingObserver mSettingObserver; 53 BubbleNotificationPreferenceController(Context context, String preferenceKey)54 public BubbleNotificationPreferenceController(Context context, String preferenceKey) { 55 super(context, preferenceKey); 56 } 57 58 @Override displayPreference(PreferenceScreen screen)59 public void displayPreference(PreferenceScreen screen) { 60 super.displayPreference(screen); 61 if (mSwitchPreference != null) { 62 mSettingObserver = new SettingObserver(mSwitchPreference); 63 } 64 } 65 66 @Override onResume()67 public void onResume() { 68 if (mSettingObserver != null) { 69 mSettingObserver.register(mContext.getContentResolver(), true /* register */); 70 } 71 } 72 73 @Override onPause()74 public void onPause() { 75 if (mSettingObserver != null) { 76 mSettingObserver.register(mContext.getContentResolver(), false /* register */); 77 } 78 } 79 80 @Override getAvailabilityStatus()81 public int getAvailabilityStatus() { 82 ActivityManager am = mContext.getSystemService(ActivityManager.class); 83 return am.isLowRamDevice() ? UNSUPPORTED_ON_DEVICE : AVAILABLE; 84 } 85 86 @Override isSliceable()87 public boolean isSliceable() { 88 return false; 89 } 90 91 @Override isChecked()92 public boolean isChecked() { 93 return Settings.Global.getInt(mContext.getContentResolver(), 94 NOTIFICATION_BUBBLES, ON) == ON; 95 } 96 97 @Override setChecked(boolean isChecked)98 public boolean setChecked(boolean isChecked) { 99 Settings.Global.putInt(mContext.getContentResolver(), 100 NOTIFICATION_BUBBLES, isChecked ? ON : OFF); 101 return true; 102 } 103 104 class SettingObserver extends ContentObserver { 105 106 private final Uri NOTIFICATION_BUBBLES_URI = 107 Settings.Secure.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