blob: aafda11b82fdf8c1fd8e5ae50475c08e81939934 [file] [log] [blame]
Doris Ling1b3ec042016-11-30 17:33:50 -08001/*
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
17package com.android.settings.notification;
18
Beverly036d7e02018-02-01 18:14:33 -050019import android.content.ContentResolver;
Doris Ling1b3ec042016-11-30 17:33:50 -080020import android.content.Context;
Beverly036d7e02018-02-01 18:14:33 -050021import android.database.ContentObserver;
22import android.net.Uri;
23import android.os.Handler;
24import android.os.UserHandle;
25import android.provider.Settings;
Doris Ling4d8a9bd2017-02-15 12:04:57 -080026import android.support.v7.preference.Preference;
Beverly036d7e02018-02-01 18:14:33 -050027import android.support.v7.preference.PreferenceScreen;
28import android.util.Slog;
Doris Ling1b3ec042016-11-30 17:33:50 -080029
Beverly036d7e02018-02-01 18:14:33 -050030import com.android.settingslib.core.lifecycle.Lifecycle;
31import com.android.settingslib.core.lifecycle.LifecycleObserver;
32import com.android.settingslib.core.lifecycle.events.OnPause;
33import com.android.settingslib.core.lifecycle.events.OnResume;
34
35public class ZenModePreferenceController extends AdjustVolumeRestrictedPreferenceController
36 implements LifecycleObserver, OnResume, OnPause {
Doris Ling1b3ec042016-11-30 17:33:50 -080037
38 private static final String KEY_ZEN_MODE = "zen_mode";
Beverly036d7e02018-02-01 18:14:33 -050039 private SettingObserver mSettingObserver;
Doris Ling4d8a9bd2017-02-15 12:04:57 -080040 private ZenModeSettings.SummaryBuilder mSummaryBuilder;
Doris Ling1b3ec042016-11-30 17:33:50 -080041
Beverly036d7e02018-02-01 18:14:33 -050042 public ZenModePreferenceController(Context context, Lifecycle lifecycle) {
Doris Ling1b3ec042016-11-30 17:33:50 -080043 super(context);
Doris Ling4d8a9bd2017-02-15 12:04:57 -080044 mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context);
Beverly036d7e02018-02-01 18:14:33 -050045
46 if (lifecycle != null) {
47 lifecycle.addObserver(this);
48 }
49 }
50
51 @Override
52 public void displayPreference(PreferenceScreen screen) {
53 super.displayPreference(screen);
54 mSettingObserver = new SettingObserver(screen.findPreference(KEY_ZEN_MODE));
55 }
56
57 @Override
58 public void onResume() {
59 if (mSettingObserver != null) {
60 mSettingObserver.register(mContext.getContentResolver());
61 }
62 }
63
64 @Override
65 public void onPause() {
66 if (mSettingObserver != null) {
67 mSettingObserver.unregister(mContext.getContentResolver());
68 }
Doris Ling1b3ec042016-11-30 17:33:50 -080069 }
70
71 @Override
Doris Ling1b3ec042016-11-30 17:33:50 -080072 public String getPreferenceKey() {
73 return KEY_ZEN_MODE;
74 }
75
76 @Override
77 public boolean isAvailable() {
78 return true;
79 }
80
Doris Ling4d8a9bd2017-02-15 12:04:57 -080081 @Override
82 public void updateState(Preference preference) {
83 super.updateState(preference);
84 if (preference.isEnabled()) {
Beverly036d7e02018-02-01 18:14:33 -050085 preference.setSummary(mSummaryBuilder.getSoundSummary());
86 }
87 }
88
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);
93
94 private final Preference mPreference;
95
96 public SettingObserver(Preference preference) {
97 super(new Handler());
98 mPreference = preference;
99 }
100
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);
104 }
105
106 public void unregister(ContentResolver cr) {
107 cr.unregisterContentObserver(this);
108 }
109
110 @Override
111 public void onChange(boolean selfChange, Uri uri) {
112 super.onChange(selfChange, uri);
113 if (ZEN_MODE_URI.equals(uri)) {
114 updateState(mPreference);
115 }
116
117 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
118 updateState(mPreference);
119 }
Doris Ling4d8a9bd2017-02-15 12:04:57 -0800120 }
121 }
Doris Ling1b3ec042016-11-30 17:33:50 -0800122}