1 /* 2 * Copyright (C) 2016 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.System.NOTIFICATION_LIGHT_PULSE; 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.R; 32 import com.android.settings.core.TogglePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnPause; 35 import com.android.settingslib.core.lifecycle.events.OnResume; 36 37 public class PulseNotificationPreferenceController extends TogglePreferenceController 38 implements LifecycleObserver, OnResume, OnPause { 39 40 private static final int ON = 1; 41 private static final int OFF = 0; 42 private SettingObserver mSettingObserver; 43 PulseNotificationPreferenceController(Context context, String key)44 public PulseNotificationPreferenceController(Context context, String key) { 45 super(context, key); 46 } 47 48 @Override displayPreference(PreferenceScreen screen)49 public void displayPreference(PreferenceScreen screen) { 50 super.displayPreference(screen); 51 Preference preference = screen.findPreference(getPreferenceKey()); 52 if (preference != null) { 53 mSettingObserver = new SettingObserver(preference); 54 } 55 } 56 57 @Override onResume()58 public void onResume() { 59 if (mSettingObserver != null) { 60 mSettingObserver.register(mContext.getContentResolver(), true /* register */); 61 } 62 } 63 64 @Override onPause()65 public void onPause() { 66 if (mSettingObserver != null) { 67 mSettingObserver.register(mContext.getContentResolver(), false /* register */); 68 } 69 } 70 71 @Override getAvailabilityStatus()72 public int getAvailabilityStatus() { 73 return mContext.getResources().getBoolean( 74 com.android.internal.R.bool.config_intrusiveNotificationLed) ? AVAILABLE 75 : UNSUPPORTED_ON_DEVICE; 76 } 77 78 @Override isChecked()79 public boolean isChecked() { 80 return Settings.System.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, OFF) 81 == ON; 82 } 83 84 @Override setChecked(boolean isChecked)85 public boolean setChecked(boolean isChecked) { 86 return Settings.System.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 87 isChecked ? ON : OFF); 88 } 89 90 @Override getSliceHighlightMenuRes()91 public int getSliceHighlightMenuRes() { 92 return R.string.menu_key_notifications; 93 } 94 95 class SettingObserver extends ContentObserver { 96 97 private final Uri NOTIFICATION_LIGHT_PULSE_URI = 98 Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); 99 100 private final Preference mPreference; 101 SettingObserver(Preference preference)102 public SettingObserver(Preference preference) { 103 super(new Handler()); 104 mPreference = preference; 105 } 106 register(ContentResolver cr, boolean register)107 public void register(ContentResolver cr, boolean register) { 108 if (register) { 109 cr.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, false, this); 110 } else { 111 cr.unregisterContentObserver(this); 112 } 113 } 114 115 @Override onChange(boolean selfChange, Uri uri)116 public void onChange(boolean selfChange, Uri uri) { 117 super.onChange(selfChange, uri); 118 if (NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { 119 updateState(mPreference); 120 } 121 } 122 } 123 } 124