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.zen; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.net.Uri; 23 import android.os.Handler; 24 import android.os.UserHandle; 25 import android.provider.Settings; 26 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settingslib.core.lifecycle.LifecycleObserver; 32 import com.android.settingslib.core.lifecycle.events.OnPause; 33 import com.android.settingslib.core.lifecycle.events.OnResume; 34 35 public class ZenModePreferenceController extends BasePreferenceController 36 implements LifecycleObserver, OnResume, OnPause { 37 38 private SettingObserver mSettingObserver; 39 private ZenModeSettings.SummaryBuilder mSummaryBuilder; 40 ZenModePreferenceController(Context context, String key)41 public ZenModePreferenceController(Context context, String key) { 42 super(context, key); 43 mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context); 44 } 45 46 @Override displayPreference(PreferenceScreen screen)47 public void displayPreference(PreferenceScreen screen) { 48 super.displayPreference(screen); 49 mSettingObserver = new SettingObserver(screen.findPreference(getPreferenceKey())); 50 } 51 52 @Override onResume()53 public void onResume() { 54 if (mSettingObserver != null) { 55 mSettingObserver.register(mContext.getContentResolver()); 56 } 57 } 58 59 @Override onPause()60 public void onPause() { 61 if (mSettingObserver != null) { 62 mSettingObserver.unregister(mContext.getContentResolver()); 63 } 64 } 65 66 @Override getAvailabilityStatus()67 public int getAvailabilityStatus() { 68 return AVAILABLE_UNSEARCHABLE; 69 } 70 71 @Override updateState(Preference preference)72 public void updateState(Preference preference) { 73 super.updateState(preference); 74 if (preference.isEnabled()) { 75 preference.setSummary(mSummaryBuilder.getSoundSummary()); 76 } 77 } 78 79 class SettingObserver extends ContentObserver { 80 private final Uri ZEN_MODE_URI = Settings.Global.getUriFor(Settings.Global.ZEN_MODE); 81 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Settings.Global.getUriFor( 82 Settings.Global.ZEN_MODE_CONFIG_ETAG); 83 84 private final Preference mPreference; 85 SettingObserver(Preference preference)86 public SettingObserver(Preference preference) { 87 super(new Handler()); 88 mPreference = preference; 89 } 90 register(ContentResolver cr)91 public void register(ContentResolver cr) { 92 cr.registerContentObserver(ZEN_MODE_URI, false, this, UserHandle.USER_ALL); 93 cr.registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this, UserHandle.USER_ALL); 94 } 95 unregister(ContentResolver cr)96 public void unregister(ContentResolver cr) { 97 cr.unregisterContentObserver(this); 98 } 99 100 @Override onChange(boolean selfChange, Uri uri)101 public void onChange(boolean selfChange, Uri uri) { 102 super.onChange(selfChange, uri); 103 if (ZEN_MODE_URI.equals(uri)) { 104 updateState(mPreference); 105 } 106 107 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { 108 updateState(mPreference); 109 } 110 } 111 } 112 } 113