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.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.os.UserManager; 25 import android.provider.Settings; 26 import android.provider.Settings.Global; 27 import android.util.Log; 28 29 import com.android.settings.dashboard.RestrictedDashboardFragment; 30 31 abstract public class ZenModeSettingsBase extends RestrictedDashboardFragment { 32 protected static final String TAG = "ZenModeSettings"; 33 protected static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 34 35 private final Handler mHandler = new Handler(); 36 private final SettingsObserver mSettingsObserver = new SettingsObserver(); 37 38 protected Context mContext; 39 protected int mZenMode; 40 41 protected ZenModeBackend mBackend; 42 onZenModeConfigChanged()43 protected void onZenModeConfigChanged() {}; 44 ZenModeSettingsBase()45 public ZenModeSettingsBase() { 46 super(UserManager.DISALLOW_ADJUST_VOLUME); 47 } 48 49 @Override getLogTag()50 protected String getLogTag() { 51 return TAG; 52 } 53 54 @Override onCreate(Bundle icicle)55 public void onCreate(Bundle icicle) { 56 mContext = getActivity(); 57 mBackend = ZenModeBackend.getInstance(mContext); 58 super.onCreate(icicle); 59 updateZenMode(false /*fireChanged*/); 60 } 61 62 @Override onResume()63 public void onResume() { 64 super.onResume(); 65 updateZenMode(true /*fireChanged*/); 66 mSettingsObserver.register(); 67 if (isUiRestricted()) { 68 if (isUiRestrictedByOnlyAdmin()) { 69 getPreferenceScreen().removeAll(); 70 return; 71 } else { 72 finish(); 73 } 74 } 75 } 76 77 @Override onPause()78 public void onPause() { 79 super.onPause(); 80 mSettingsObserver.unregister(); 81 } 82 updateZenMode(boolean fireChanged)83 private void updateZenMode(boolean fireChanged) { 84 final int zenMode = Settings.Global.getInt(getContentResolver(), Global.ZEN_MODE, mZenMode); 85 if (zenMode == mZenMode) return; 86 mZenMode = zenMode; 87 if (DEBUG) Log.d(TAG, "updateZenMode mZenMode=" + mZenMode + " " + fireChanged); 88 } 89 90 private final class SettingsObserver extends ContentObserver { 91 private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE); 92 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG); 93 SettingsObserver()94 private SettingsObserver() { 95 super(mHandler); 96 } 97 register()98 public void register() { 99 getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this); 100 getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this); 101 } 102 unregister()103 public void unregister() { 104 getContentResolver().unregisterContentObserver(this); 105 } 106 107 @Override onChange(boolean selfChange, Uri uri)108 public void onChange(boolean selfChange, Uri uri) { 109 super.onChange(selfChange, uri); 110 if (ZEN_MODE_URI.equals(uri)) { 111 updateZenMode(true /*fireChanged*/); 112 } 113 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { 114 mBackend.updatePolicy(); 115 onZenModeConfigChanged(); 116 } 117 } 118 } 119 } 120