Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2015 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.app.NotificationManager;
     20 import android.content.Context;
     21 import android.database.ContentObserver;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.os.Handler;
     25 import android.os.UserManager;
     26 import android.provider.Settings;
     27 import android.provider.Settings.Global;
     28 import android.service.notification.ZenModeConfig;
     29 import android.util.Log;
     30 
     31 import com.android.settings.RestrictedSettingsFragment;
     32 
     33 import java.util.Objects;
     34 
     35 abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
     36     protected static final String TAG = "ZenModeSettings";
     37     protected static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
     38 
     39     private final Handler mHandler = new Handler();
     40     private final SettingsObserver mSettingsObserver = new SettingsObserver();
     41 
     42     protected Context mContext;
     43     protected ZenModeConfig mConfig;
     44     protected int mZenMode;
     45 
     46     abstract protected void onZenModeChanged();
     47     abstract protected void onZenModeConfigChanged();
     48 
     49     public ZenModeSettingsBase() {
     50         super(UserManager.DISALLOW_ADJUST_VOLUME);
     51     }
     52 
     53     @Override
     54     public void onCreate(Bundle icicle) {
     55         super.onCreate(icicle);
     56         mContext = getActivity();
     57         updateZenMode(false /*fireChanged*/);
     58         updateZenModeConfig(false /*fireChanged*/);
     59         if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig);
     60     }
     61 
     62     @Override
     63     public void onResume() {
     64         super.onResume();
     65         updateZenMode(true /*fireChanged*/);
     66         updateZenModeConfig(true /*fireChanged*/);
     67         mSettingsObserver.register();
     68         if (isUiRestricted()) {
     69             finish();
     70         }
     71     }
     72 
     73     @Override
     74     public void onPause() {
     75         super.onPause();
     76         mSettingsObserver.unregister();
     77     }
     78 
     79     private void updateZenMode(boolean fireChanged) {
     80         final int zenMode = Settings.Global.getInt(getContentResolver(), Global.ZEN_MODE, mZenMode);
     81         if (zenMode == mZenMode) return;
     82         mZenMode = zenMode;
     83         if (DEBUG) Log.d(TAG, "updateZenMode mZenMode=" + mZenMode);
     84         if (fireChanged) {
     85             onZenModeChanged();
     86         }
     87     }
     88 
     89     private void updateZenModeConfig(boolean fireChanged) {
     90         final ZenModeConfig config = getZenModeConfig();
     91         if (Objects.equals(config, mConfig)) return;
     92         mConfig = config;
     93         if (DEBUG) Log.d(TAG, "updateZenModeConfig mConfig=" + mConfig);
     94         if (fireChanged) {
     95             onZenModeConfigChanged();
     96         }
     97     }
     98 
     99     protected boolean setZenModeConfig(ZenModeConfig config) {
    100         final String reason = getClass().getSimpleName();
    101         final boolean success = NotificationManager.from(mContext).setZenModeConfig(config, reason);
    102         if (success) {
    103             mConfig = config;
    104             if (DEBUG) Log.d(TAG, "Saved mConfig=" + mConfig);
    105             onZenModeConfigChanged();
    106         }
    107         return success;
    108     }
    109 
    110     protected void setZenMode(int zenMode, Uri conditionId) {
    111         NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG);
    112     }
    113 
    114     protected static boolean isScheduleSupported(Context context) {
    115         return NotificationManager.from(context)
    116                 .isSystemConditionProviderEnabled(ZenModeConfig.SCHEDULE_PATH);
    117     }
    118 
    119     private ZenModeConfig getZenModeConfig() {
    120         return NotificationManager.from(mContext).getZenModeConfig();
    121     }
    122 
    123     private final class SettingsObserver extends ContentObserver {
    124         private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE);
    125         private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG);
    126 
    127         private SettingsObserver() {
    128             super(mHandler);
    129         }
    130 
    131         public void register() {
    132             getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this);
    133             getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this);
    134         }
    135 
    136         public void unregister() {
    137             getContentResolver().unregisterContentObserver(this);
    138         }
    139 
    140         @Override
    141         public void onChange(boolean selfChange, Uri uri) {
    142             super.onChange(selfChange, uri);
    143             if (ZEN_MODE_URI.equals(uri)) {
    144                 updateZenMode(true /*fireChanged*/);
    145             }
    146             if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
    147                 updateZenModeConfig(true /*fireChanged*/);
    148             }
    149         }
    150     }
    151 }
    152