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