From 65a9173d575b2f5e14a19c95bdee87554a95c53c Mon Sep 17 00:00:00 2001 From: Angela Wang Date: Fri, 4 Aug 2023 09:43:33 +0000 Subject: [PATCH] Fix flash notification preview not working for second user issue When switching to second user and try to preview the flash notification effect it'll not work. The root cause is the receiver is registered in system process and the broadcast is sent as second user. Needs to send the broadcast as system user to let the receiver receive the preview broadcast. Bug: 286039180 Test: atest FlashNotificationsPreviewPreferenceControllerTest Test: switch to second user and test the preview function Change-Id: I7dd553dac9fd66dab59eaec25849aac9f92989b6 --- .../FlashNotificationsPreviewPreferenceController.java | 3 ++- .../ScreenFlashNotificationColorDialogFragment.java | 6 ++++-- .../FlashNotificationsPreviewPreferenceControllerTest.java | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceController.java b/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceController.java index f13758439fd..ae1adfa85a7 100644 --- a/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceController.java +++ b/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceController.java @@ -28,6 +28,7 @@ import android.database.ContentObserver; import android.net.Uri; import android.os.Handler; import android.os.Looper; +import android.os.UserHandle; import android.provider.Settings; import androidx.annotation.NonNull; @@ -81,7 +82,7 @@ public class FlashNotificationsPreviewPreferenceController extends if (getPreferenceKey().equals(preference.getKey())) { Intent intent = new Intent(ACTION_FLASH_NOTIFICATION_START_PREVIEW); intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_SHORT_PREVIEW); - mContext.sendBroadcast(intent); + mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM); return true; } diff --git a/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragment.java b/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragment.java index 968396686ee..ca77f16f104 100644 --- a/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragment.java +++ b/src/com/android/settings/accessibility/ScreenFlashNotificationColorDialogFragment.java @@ -27,6 +27,7 @@ import android.app.Dialog; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; +import android.os.UserHandle; import android.view.View; import androidx.annotation.ColorInt; @@ -41,6 +42,7 @@ import java.util.Timer; import java.util.TimerTask; import java.util.function.Consumer; + /** * DialogFragment for Screen flash notification color picker. */ @@ -166,14 +168,14 @@ public class ScreenFlashNotificationColorDialogFragment extends DialogFragment i Intent intent = new Intent(ACTION_FLASH_NOTIFICATION_START_PREVIEW); intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_LONG_PREVIEW); intent.putExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR, mCurrentColor); - getContext().sendBroadcast(intent); + getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM); } private void stopPreviewLocked() { if (getContext() == null) return; Intent stopIntent = new Intent(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW); - getContext().sendBroadcast(stopIntent); + getContext().sendBroadcastAsUser(stopIntent, UserHandle.SYSTEM); mIsPreview = false; } diff --git a/tests/robotests/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceControllerTest.java index 98da926b3f2..0f32f31ac1b 100644 --- a/tests/robotests/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/FlashNotificationsPreviewPreferenceControllerTest.java @@ -131,7 +131,7 @@ public class FlashNotificationsPreviewPreferenceControllerTest { @Test public void testHandlePreferenceTreeClick_invalidPreference() { mController.handlePreferenceTreeClick(mock(Preference.class)); - verify(mContext, never()).sendBroadcast(any()); + verify(mContext, never()).sendBroadcastAsUser(any(), any()); } @Test @@ -139,7 +139,7 @@ public class FlashNotificationsPreviewPreferenceControllerTest { mController.handlePreferenceTreeClick(mPreference); ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); - verify(mContext).sendBroadcast(captor.capture()); + verify(mContext).sendBroadcastAsUser(captor.capture(), any()); Intent captured = captor.getValue(); assertThat(captured.getAction()).isEqualTo(ACTION_FLASH_NOTIFICATION_START_PREVIEW); @@ -150,7 +150,7 @@ public class FlashNotificationsPreviewPreferenceControllerTest { mController.handlePreferenceTreeClick(mPreference); ArgumentCaptor captor = ArgumentCaptor.forClass(Intent.class); - verify(mContext).sendBroadcast(captor.capture()); + verify(mContext).sendBroadcastAsUser(captor.capture(), any()); Intent captured = captor.getValue(); assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_LONG_PREVIEW))