1 /* 2 * Copyright (C) 2024 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.modes; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.database.ContentObserver; 23 import android.net.Uri; 24 import android.os.Handler; 25 import android.os.UserManager; 26 import android.provider.Settings.Global; 27 import android.util.Log; 28 29 import com.android.settings.dashboard.RestrictedDashboardFragment; 30 31 /** 32 * Base class for all Settings pages controlling Modes behavior. 33 */ 34 abstract class ZenModesFragmentBase extends RestrictedDashboardFragment { 35 protected static final String TAG = "ZenModesSettings"; 36 protected static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 37 38 private final Handler mHandler = new Handler(); 39 private final SettingsObserver mSettingsObserver = new SettingsObserver(); 40 41 protected Context mContext; 42 43 protected ZenModesBackend mBackend; 44 45 // Individual pages must implement this method based on what they should do when 46 // the device's zen mode state changes. updateZenModeState()47 protected abstract void updateZenModeState(); 48 ZenModesFragmentBase()49 ZenModesFragmentBase() { 50 super(UserManager.DISALLOW_ADJUST_VOLUME); 51 } 52 53 @Override getLogTag()54 protected String getLogTag() { 55 return TAG; 56 } 57 58 @Override onAttach(@onNull Context context)59 public void onAttach(@NonNull Context context) { 60 mContext = context; 61 mBackend = ZenModesBackend.getInstance(context); 62 super.onAttach(context); 63 mSettingsObserver.register(); 64 } 65 66 @Override onStart()67 public void onStart() { 68 super.onStart(); 69 if (isUiRestricted()) { 70 if (isUiRestrictedByOnlyAdmin()) { 71 getPreferenceScreen().removeAll(); 72 } else { 73 finish(); 74 } 75 } 76 } 77 78 @Override onResume()79 public void onResume() { 80 super.onResume(); 81 updateZenModeState(); 82 } 83 84 @Override onDetach()85 public void onDetach() { 86 super.onDetach(); 87 mSettingsObserver.unregister(); 88 } 89 90 private final class SettingsObserver extends ContentObserver { 91 private static final Uri ZEN_MODE_URI = Global.getUriFor(Global.ZEN_MODE); 92 private static final Uri ZEN_MODE_CONFIG_ETAG_URI = Global.getUriFor( 93 Global.ZEN_MODE_CONFIG_ETAG); 94 SettingsObserver()95 private SettingsObserver() { 96 super(mHandler); 97 } 98 register()99 public void register() { 100 getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this); 101 getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false, this); 102 } 103 unregister()104 public void unregister() { 105 getContentResolver().unregisterContentObserver(this); 106 } 107 108 @Override onChange(boolean selfChange, @Nullable Uri uri)109 public void onChange(boolean selfChange, @Nullable Uri uri) { 110 super.onChange(selfChange, uri); 111 // Shouldn't have any other URIs trigger this method, but check just in case. 112 if (ZEN_MODE_URI.equals(uri) || ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) { 113 updateZenModeState(); 114 } 115 } 116 } 117 } 118