From 0d75a26a68f360fa8ac3254377cccffab3a55b21 Mon Sep 17 00:00:00 2001 From: Yining Liu Date: Tue, 19 Nov 2024 22:53:42 +0000 Subject: [PATCH] Implement the main switch of notifications on lock screen Add the ability of controlling whether to show notifs on the lock screen (secure setting: lock_screen_show_notifications) to the new lock screen notification settings page main switch. This switch also listens to the change of lock_screen_show_notifications to update its status in case of other places change the value. Bug: 367455695 Flag: com.android.server.notification.notification_lock_screen_settings Test: atest LockScreenNotificationsPreferenceControllerTest Change-Id: Id598d508ac383563a0d9413578251eb22ed4ca90 --- .../lock_screen_notifications_settings.xml | 4 + ...tificationsGlobalPreferenceController.java | 117 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java diff --git a/res/xml/lock_screen_notifications_settings.xml b/res/xml/lock_screen_notifications_settings.xml index 8eed90e9c61..076e7f5e941 100644 --- a/res/xml/lock_screen_notifications_settings.xml +++ b/res/xml/lock_screen_notifications_settings.xml @@ -20,5 +20,9 @@ xmlns:settings="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"> + diff --git a/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java b/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java new file mode 100644 index 00000000000..7c7664e5da9 --- /dev/null +++ b/src/com/android/settings/notification/LockScreenNotificationsGlobalPreferenceController.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.notification; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.ContentObserver; +import android.net.Uri; +import android.os.Handler; +import android.os.Looper; +import android.provider.Settings; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleEventObserver; +import androidx.lifecycle.LifecycleOwner; +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; + +import com.android.server.notification.Flags; +import com.android.settings.widget.SettingsMainSwitchPreferenceController; + +public class LockScreenNotificationsGlobalPreferenceController + extends SettingsMainSwitchPreferenceController + implements LifecycleEventObserver { + + public static final int ON = 1; + public static final int OFF = 0; + private final ContentResolver mContentResolver; + private final ContentObserver mContentObserver; + @Nullable private Preference mPreference; + + + public LockScreenNotificationsGlobalPreferenceController( + @NonNull Context context, + @NonNull String preferenceKey + ) { + super(context, preferenceKey); + mContentResolver = context.getContentResolver(); + mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { + @Override + public void onChange(boolean selfChange, @Nullable Uri uri) { + if (mPreference == null) return; + updateState(mPreference); + } + }; + } + + @Override + public void displayPreference(@NonNull PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = screen.findPreference(getPreferenceKey()); + } + + @Override + public void updateState(@NonNull Preference preference) { + super.updateState(preference); + setChecked(lockScreenShowNotifications()); + } + + @Override + public int getAvailabilityStatus() { + // TODO: b/367455695 - remove this when the feature flag is removed! + return Flags.notificationLockScreenSettings() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; + } + + @Override + public boolean isChecked() { + return lockScreenShowNotifications(); + } + + private boolean lockScreenShowNotifications() { + return Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, OFF) == ON; + } + + @Override + public boolean setChecked(boolean isChecked) { + return Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, (isChecked ? ON : OFF)); + } + + @Override + public int getSliceHighlightMenuRes() { + // not needed since it's not sliceable + return NO_RES; + } + + @Override + public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, + @NonNull Lifecycle.Event event) { + if (event == Lifecycle.Event.ON_RESUME) { + mContentResolver.registerContentObserver( + Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS), + /* notifyForDescendants= */ false, + mContentObserver + ); + } else { + mContentResolver.unregisterContentObserver(mContentObserver); + } + } +}