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