2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceScreen;
28 import android.util.Slog;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
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;
35 public class ZenModePreferenceController extends AdjustVolumeRestrictedPreferenceController
36 implements LifecycleObserver, OnResume, OnPause {
38 private final String mKey;
39 private SettingObserver mSettingObserver;
40 private ZenModeSettings.SummaryBuilder mSummaryBuilder;
42 public ZenModePreferenceController(Context context, Lifecycle lifecycle, String key) {
44 mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context);
46 if (lifecycle != null) {
47 lifecycle.addObserver(this);
53 public void displayPreference(PreferenceScreen screen) {
54 super.displayPreference(screen);
55 mSettingObserver = new SettingObserver(screen.findPreference(mKey));
59 public void onResume() {
60 if (mSettingObserver != null) {
61 mSettingObserver.register(mContext.getContentResolver());
66 public void onPause() {
67 if (mSettingObserver != null) {
68 mSettingObserver.unregister(mContext.getContentResolver());
73 public String getPreferenceKey() {
78 public boolean isAvailable() {
83 public void updateState(Preference preference) {
84 super.updateState(preference);
85 if (preference.isEnabled()) {
86 preference.setSummary(mSummaryBuilder.getSoundSummary());
90 class SettingObserver extends ContentObserver {
91 private final Uri ZEN_MODE_URI = Settings.Global.getUriFor(Settings.Global.ZEN_MODE);
92 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Settings.Global.getUriFor(
93 Settings.Global.ZEN_MODE_CONFIG_ETAG);
95 private final Preference mPreference;
97 public SettingObserver(Preference preference) {
99 mPreference = preference;
102 public void register(ContentResolver cr) {
103 cr.registerContentObserver(ZEN_MODE_URI, false, this, UserHandle.USER_ALL);
104 cr.registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this, UserHandle.USER_ALL);
107 public void unregister(ContentResolver cr) {
108 cr.unregisterContentObserver(this);
112 public void onChange(boolean selfChange, Uri uri) {
113 super.onChange(selfChange, uri);
114 if (ZEN_MODE_URI.equals(uri)) {
115 updateState(mPreference);
118 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
119 updateState(mPreference);