Home | History | Annotate | Download | only in notification
      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.settings.core.PreferenceControllerMixin;
     31 import com.android.settingslib.core.AbstractPreferenceController;
     32 import com.android.settingslib.core.lifecycle.Lifecycle;
     33 import com.android.settingslib.core.lifecycle.LifecycleObserver;
     34 import com.android.settingslib.core.lifecycle.events.OnPause;
     35 import com.android.settingslib.core.lifecycle.events.OnResume;
     36 
     37 public class ZenModePreferenceController extends AbstractPreferenceController
     38         implements LifecycleObserver, OnResume, OnPause, PreferenceControllerMixin {
     39 
     40     private final String mKey;
     41     private SettingObserver mSettingObserver;
     42     private ZenModeSettings.SummaryBuilder mSummaryBuilder;
     43 
     44     public ZenModePreferenceController(Context context, Lifecycle lifecycle, String key) {
     45         super(context);
     46         mSummaryBuilder = new ZenModeSettings.SummaryBuilder(context);
     47 
     48         if (lifecycle != null) {
     49             lifecycle.addObserver(this);
     50         }
     51         mKey = key;
     52     }
     53 
     54     @Override
     55     public void displayPreference(PreferenceScreen screen) {
     56         super.displayPreference(screen);
     57         mSettingObserver = new SettingObserver(screen.findPreference(mKey));
     58     }
     59 
     60     @Override
     61     public void onResume() {
     62         if (mSettingObserver != null) {
     63             mSettingObserver.register(mContext.getContentResolver());
     64         }
     65     }
     66 
     67     @Override
     68     public void onPause() {
     69         if (mSettingObserver != null) {
     70             mSettingObserver.unregister(mContext.getContentResolver());
     71         }
     72     }
     73 
     74     @Override
     75     public String getPreferenceKey() {
     76         return mKey;
     77     }
     78 
     79     @Override
     80     public boolean isAvailable() {
     81         return true;
     82     }
     83 
     84     @Override
     85     public void updateState(Preference preference) {
     86         super.updateState(preference);
     87         if (preference.isEnabled()) {
     88             preference.setSummary(mSummaryBuilder.getSoundSummary());
     89         }
     90     }
     91 
     92     class SettingObserver extends ContentObserver {
     93         private final Uri ZEN_MODE_URI = Settings.Global.getUriFor(Settings.Global.ZEN_MODE);
     94         private final Uri ZEN_MODE_CONFIG_ETAG_URI = Settings.Global.getUriFor(
     95                 Settings.Global.ZEN_MODE_CONFIG_ETAG);
     96 
     97         private final Preference mPreference;
     98 
     99         public SettingObserver(Preference preference) {
    100             super(new Handler());
    101             mPreference = preference;
    102         }
    103 
    104         public void register(ContentResolver cr) {
    105             cr.registerContentObserver(ZEN_MODE_URI, false, this, UserHandle.USER_ALL);
    106             cr.registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this, UserHandle.USER_ALL);
    107         }
    108 
    109         public void unregister(ContentResolver cr) {
    110             cr.unregisterContentObserver(this);
    111         }
    112 
    113         @Override
    114         public void onChange(boolean selfChange, Uri uri) {
    115             super.onChange(selfChange, uri);
    116             if (ZEN_MODE_URI.equals(uri)) {
    117                 updateState(mPreference);
    118             }
    119 
    120             if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
    121                 updateState(mPreference);
    122             }
    123         }
    124     }
    125 }
    126