1 /* 2 * Copyright (C) 2023 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 static com.android.settings.notification.PoliteNotificationGlobalPreferenceController.ON; 20 import static com.android.settings.notification.PoliteNotificationGlobalPreferenceController.OFF; 21 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.database.ContentObserver; 25 import android.net.Uri; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.provider.Settings; 31 32 import androidx.annotation.NonNull; 33 import androidx.annotation.Nullable; 34 import androidx.annotation.VisibleForTesting; 35 import androidx.lifecycle.Lifecycle; 36 import androidx.lifecycle.LifecycleEventObserver; 37 import androidx.lifecycle.LifecycleOwner; 38 import androidx.preference.Preference; 39 import androidx.preference.PreferenceScreen; 40 41 import com.android.server.notification.Flags; 42 import com.android.settings.core.TogglePreferenceController; 43 44 /** 45 * Controls the toggle that determines whether notification cooldown 46 * should apply to work profiles. 47 */ 48 public class PoliteNotifWorkProfileToggleController extends TogglePreferenceController implements 49 LifecycleEventObserver { 50 51 private final int mManagedProfileId; 52 private Preference mPreference; 53 private final ContentResolver mContentResolver; 54 55 final ContentObserver mContentObserver = new ContentObserver( 56 new Handler(Looper.getMainLooper())) { 57 @Override 58 public void onChange(boolean selfChange, @Nullable Uri uri) { 59 updateState(mPreference); 60 } 61 }; 62 PoliteNotifWorkProfileToggleController(@onNull Context context, @NonNull String preferenceKey)63 public PoliteNotifWorkProfileToggleController(@NonNull Context context, 64 @NonNull String preferenceKey) { 65 this(context, preferenceKey, new AudioHelper(context)); 66 } 67 68 @VisibleForTesting PoliteNotifWorkProfileToggleController(Context context, String preferenceKey, AudioHelper helper)69 PoliteNotifWorkProfileToggleController(Context context, String preferenceKey, 70 AudioHelper helper) { 71 super(context, preferenceKey); 72 mManagedProfileId = helper.getManagedProfileId(UserManager.get(mContext)); 73 mContentResolver = context.getContentResolver(); 74 } 75 76 @Override displayPreference(@onNull PreferenceScreen screen)77 public void displayPreference(@NonNull PreferenceScreen screen) { 78 super.displayPreference(screen); 79 mPreference = screen.findPreference(getPreferenceKey()); 80 } 81 82 @Override onStateChanged(@onNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event)83 public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, 84 @NonNull Lifecycle.Event event) { 85 if (event == Lifecycle.Event.ON_RESUME) { 86 mContentResolver.registerContentObserver( 87 Settings.System.getUriFor(Settings.System.NOTIFICATION_COOLDOWN_ENABLED), 88 /* notifyForDescendants= */ false, mContentObserver); 89 } else if (event == Lifecycle.Event.ON_PAUSE) { 90 mContentResolver.unregisterContentObserver(mContentObserver); 91 } 92 } 93 94 @Override getAvailabilityStatus()95 public int getAvailabilityStatus() { 96 // TODO: b/291897570 - remove this when the feature flag is removed! 97 if (!Flags.politeNotifications()) { 98 return CONDITIONALLY_UNAVAILABLE; 99 } 100 101 if (!isCoolDownEnabledForPrimary()) { 102 return CONDITIONALLY_UNAVAILABLE; 103 } 104 105 return (mManagedProfileId != UserHandle.USER_NULL) ? AVAILABLE : DISABLED_FOR_USER; 106 } 107 108 @Override isChecked()109 public boolean isChecked() { 110 if (!isCoolDownEnabledForPrimary()) { 111 return false; 112 } 113 return Settings.System.getIntForUser(mContext.getContentResolver(), 114 Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON, mManagedProfileId) != OFF; 115 } 116 117 @Override setChecked(boolean isChecked)118 public boolean setChecked(boolean isChecked) { 119 return Settings.System.putIntForUser(mContext.getContentResolver(), 120 Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (isChecked ? ON : OFF), 121 mManagedProfileId); 122 } 123 124 @Override getSliceHighlightMenuRes()125 public int getSliceHighlightMenuRes() { 126 // not needed since it's not sliceable 127 return NO_RES; 128 } 129 130 @Override updateState(@ullable Preference preference)131 public void updateState(@Nullable Preference preference) { 132 if (preference == null) return; 133 preference.setVisible(isAvailable()); 134 } 135 isCoolDownEnabledForPrimary()136 private boolean isCoolDownEnabledForPrimary() { 137 return Settings.System.getInt(mContext.getContentResolver(), 138 Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == ON; 139 } 140 } 141