OSDN Git Service

Link to dnd settings from notification settings
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenModePreferenceController.java
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;
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 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceScreen;
28 import android.util.Slog;
29
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;
34
35 public class ZenModePreferenceController extends AdjustVolumeRestrictedPreferenceController
36         implements LifecycleObserver, OnResume, OnPause {
37
38     private final String mKey;
39     private SettingObserver mSettingObserver;
40     private ZenModeSettings.SummaryBuilder mSummaryBuilder;
41
42     public ZenModePreferenceController(Context context, Lifecycle lifecycle, String key) {
43         super(context);
44         mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context);
45
46         if (lifecycle != null) {
47             lifecycle.addObserver(this);
48         }
49         mKey = key;
50     }
51
52     @Override
53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         mSettingObserver = new SettingObserver(screen.findPreference(mKey));
56     }
57
58     @Override
59     public void onResume() {
60         if (mSettingObserver != null) {
61             mSettingObserver.register(mContext.getContentResolver());
62         }
63     }
64
65     @Override
66     public void onPause() {
67         if (mSettingObserver != null) {
68             mSettingObserver.unregister(mContext.getContentResolver());
69         }
70     }
71
72     @Override
73     public String getPreferenceKey() {
74         return mKey;
75     }
76
77     @Override
78     public boolean isAvailable() {
79         return true;
80     }
81
82     @Override
83     public void updateState(Preference preference) {
84         super.updateState(preference);
85         if (preference.isEnabled()) {
86             preference.setSummary(mSummaryBuilder.getSoundSummary());
87         }
88     }
89
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);
94
95         private final Preference mPreference;
96
97         public SettingObserver(Preference preference) {
98             super(new Handler());
99             mPreference = preference;
100         }
101
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);
105         }
106
107         public void unregister(ContentResolver cr) {
108             cr.unregisterContentObserver(this);
109         }
110
111         @Override
112         public void onChange(boolean selfChange, Uri uri) {
113             super.onChange(selfChange, uri);
114             if (ZEN_MODE_URI.equals(uri)) {
115                 updateState(mPreference);
116             }
117
118             if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
119                 updateState(mPreference);
120             }
121         }
122     }
123 }