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 androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.dashboard.RestrictedDashboardFragment; 33 import com.android.settingslib.core.AbstractPreferenceController; 34 35 abstract public class ZenModeSettingsBase extends RestrictedDashboardFragment { 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 int mZenMode; 44 45 protected ZenModeBackend mBackend; 46 onZenModeConfigChanged()47 protected void onZenModeConfigChanged() {}; 48 ZenModeSettingsBase()49 public ZenModeSettingsBase() { 50 super(UserManager.DISALLOW_ADJUST_VOLUME); 51 } 52 53 @Override getLogTag()54 protected String getLogTag() { 55 return TAG; 56 } 57 58 @Override onAttach(Context context)59 public void onAttach(Context context) { 60 super.onAttach(context); 61 mContext = context; 62 mBackend = ZenModeBackend.getInstance(mContext); 63 } 64 65 @Override onCreate(Bundle icicle)66 public void onCreate(Bundle icicle) { 67 super.onCreate(icicle); 68 updateZenMode(false /*fireChanged*/); 69 } 70 71 @Override onResume()72 public void onResume() { 73 super.onResume(); 74 updateZenMode(true /*fireChanged*/); 75 mSettingsObserver.register(); 76 if (isUiRestricted()) { 77 if (isUiRestrictedByOnlyAdmin()) { 78 getPreferenceScreen().removeAll(); 79 return; 80 } else { 81 finish(); 82 } 83 } 84 } 85 86 @Override onPause()87 public void onPause() { 88 super.onPause(); 89 mSettingsObserver.unregister(); 90 } 91 updateZenMode(boolean fireChanged)92 private void updateZenMode(boolean fireChanged) { 93 final int zenMode = Settings.Global.getInt(getContentResolver(), Global.ZEN_MODE, mZenMode); 94 if (zenMode == mZenMode) return; 95 mZenMode = zenMode; 96 if (DEBUG) Log.d(TAG, "updateZenMode mZenMode=" + mZenMode + " " + fireChanged); 97 } 98 99 private final class SettingsObserver extends ContentObserver { 100 private final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE); 101 private final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor(Global.ZEN_MODE_CONFIG_ETAG); 102 SettingsObserver()103 private SettingsObserver() { 104 super(mHandler); 105 } 106 register()107 public void register() { 108 getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this); 109 getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this); 110 } 111 unregister()112 public void unregister() { 113 getContentResolver().unregisterContentObserver(this); 114 } 115 116 @Override onChange(boolean selfChange, Uri uri)117 public void onChange(boolean selfChange, Uri uri) { 118 super.onChange(selfChange, uri); 119 if (ZEN_MODE_URI.equals(uri)) { 120 updateZenMode(true /*fireChanged*/); 121 } 122 if (ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { 123 mBackend.updatePolicy(); 124 onZenModeConfigChanged(); 125 } 126 } 127 } 128 updatePreference(AbstractPreferenceController controller)129 void updatePreference(AbstractPreferenceController controller) { 130 final PreferenceScreen screen = getPreferenceScreen(); 131 if (!controller.isAvailable()) { 132 return; 133 } 134 final String key = controller.getPreferenceKey(); 135 136 final Preference preference = screen.findPreference(key); 137 if (preference == null) { 138 Log.d(TAG, String.format("Cannot find preference with key %s in Controller %s", 139 key, controller.getClass().getSimpleName())); 140 return; 141 } 142 controller.updateState(preference); 143 } 144 } 145