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 static final String KEY_ZEN_MODE = "zen_mode";
39 private SettingObserver mSettingObserver;
40 private ZenModeSettings.SummaryBuilder mSummaryBuilder;
42 public ZenModePreferenceController(Context context, Lifecycle lifecycle) {
44 mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context);
46 if (lifecycle != null) {
47 lifecycle.addObserver(this);
52 public void displayPreference(PreferenceScreen screen) {
53 super.displayPreference(screen);
54 mSettingObserver = new SettingObserver(screen.findPreference(KEY_ZEN_MODE));
58 public void onResume() {
59 if (mSettingObserver != null) {
60 mSettingObserver.register(mContext.getContentResolver());
65 public void onPause() {
66 if (mSettingObserver != null) {
67 mSettingObserver.unregister(mContext.getContentResolver());
72 public String getPreferenceKey() {
77 public boolean isAvailable() {
82 public void updateState(Preference preference) {
83 super.updateState(preference);
84 if (preference.isEnabled()) {
85 preference.setSummary(mSummaryBuilder.getSoundSummary());
89 class SettingObserver extends ContentObserver {
90 private final Uri ZEN_MODE_URI = Settings.Global.getUriFor(Settings.Global.ZEN_MODE);
91 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Settings.Global.getUriFor(
92 Settings.Global.ZEN_MODE_CONFIG_ETAG);
94 private final Preference mPreference;
96 public SettingObserver(Preference preference) {
98 mPreference = preference;
101 public void register(ContentResolver cr) {
102 cr.registerContentObserver(ZEN_MODE_URI, false, this, UserHandle.USER_ALL);
103 cr.registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this, UserHandle.USER_ALL);
106 public void unregister(ContentResolver cr) {
107 cr.unregisterContentObserver(this);
111 public void onChange(boolean selfChange, Uri uri) {
112 super.onChange(selfChange, uri);
113 if (ZEN_MODE_URI.equals(uri)) {
114 updateState(mPreference);
117 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
118 updateState(mPreference);