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.app.Flags; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.database.ContentObserver; 23 import android.net.Uri; 24 import android.os.Handler; 25 import android.os.UserHandle; 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.BasePreferenceController; 33 import com.android.settings.notification.modes.ZenModesListFragment; 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 public class ZenModePreferenceController extends BasePreferenceController 39 implements LifecycleObserver, OnResume, OnPause { 40 41 private SettingObserver mSettingObserver; 42 private ZenModeSettings.SummaryBuilder mSummaryBuilder; 43 ZenModePreferenceController(Context context, String key)44 public ZenModePreferenceController(Context context, String key) { 45 super(context, key); 46 mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context); 47 } 48 49 @Override displayPreference(PreferenceScreen screen)50 public void displayPreference(PreferenceScreen screen) { 51 super.displayPreference(screen); 52 Preference preference = screen.findPreference(getPreferenceKey()); 53 mSettingObserver = new SettingObserver(preference); 54 maybeSetTitleAndDestination(preference); 55 } 56 57 @Override onResume()58 public void onResume() { 59 if (mSettingObserver != null) { 60 mSettingObserver.register(mContext.getContentResolver()); 61 } 62 } 63 64 @Override onPause()65 public void onPause() { 66 if (mSettingObserver != null) { 67 mSettingObserver.unregister(mContext.getContentResolver()); 68 } 69 } 70 71 @Override getAvailabilityStatus()72 public int getAvailabilityStatus() { 73 return AVAILABLE_UNSEARCHABLE; 74 } 75 76 @Override updateState(Preference preference)77 public void updateState(Preference preference) { 78 super.updateState(preference); 79 maybeSetTitleAndDestination(preference); 80 if (preference.isEnabled()) { 81 preference.setSummary(mSummaryBuilder.getSoundSummary()); 82 } 83 } 84 85 // Only when modes_ui is active: change title & target fragment. maybeSetTitleAndDestination(Preference preference)86 private void maybeSetTitleAndDestination(Preference preference) { 87 if (!Flags.modesUi()) { 88 return; 89 } 90 91 preference.setTitle(R.string.zen_modes_list_title); 92 preference.setFragment(ZenModesListFragment.class.getCanonicalName()); 93 } 94 95 class SettingObserver extends ContentObserver { 96 private final Uri ZEN_MODE_URI = Settings.Global.getUriFor(Settings.Global.ZEN_MODE); 97 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Settings.Global.getUriFor( 98 Settings.Global.ZEN_MODE_CONFIG_ETAG); 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)107 public void register(ContentResolver cr) { 108 cr.registerContentObserver(ZEN_MODE_URI, false, this, UserHandle.USER_ALL); 109 cr.registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this, UserHandle.USER_ALL); 110 } 111 unregister(ContentResolver cr)112 public void unregister(ContentResolver cr) { 113 cr.unregisterContentObserver(this); 114 } 115 116 @Override onChange(boolean selfChange, Uri uri)117 public void onChange(boolean selfChange, Uri uri) { 118 super.onChange(selfChange, uri); 119 if (ZEN_MODE_URI.equals(uri)) { 120 updateState(mPreference); 121 } 122 123 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { 124 updateState(mPreference); 125 } 126 } 127 } 128 } 129