From abaa00ce7b0097052fc6a68c37b44e203520aa9d Mon Sep 17 00:00:00 2001 From: Peter Kalauskas Date: Tue, 3 May 2022 23:03:58 +0000 Subject: [PATCH] Cache SHOW_MEDIA_ON_QUICK_SETTINGS Store value of SHOW_MEDIA_ON_QUICK_SETTINGS the first time it is queried to avoid binder calls on critical path. SHOW_MEDIA_ON_QUICK_SETTINGS should not change at runtime. Test: Ensure Settings.Global.getInt is not called during Lockscreen PIN disappear CUJ Test: adb shell settings put global qs_media_controls 1 && \ atest SystemUITests:NotificationSectionsFeatureManagerTest Test: adb shell settings put global qs_media_controls 0 && \ atest SystemUITests:MediaResumeListenerTest Bug: 230620480 Change-Id: Ie3264573071b6daa1cc7393d99aad5359bc2fd1a --- .../src/com/android/systemui/util/Utils.java | 17 ++++++++++++----- .../NotificationSectionsFeatureManagerTest.kt | 18 +++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/util/Utils.java b/packages/SystemUI/src/com/android/systemui/util/Utils.java index 5b5dca30620a6..d54de3fa9a3f6 100644 --- a/packages/SystemUI/src/com/android/systemui/util/Utils.java +++ b/packages/SystemUI/src/com/android/systemui/util/Utils.java @@ -21,9 +21,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Resources; -import android.content.res.TypedArray; import android.provider.Settings; -import android.view.ContextThemeWrapper; import android.view.DisplayCutout; import com.android.internal.policy.SystemBarUtils; @@ -35,6 +33,8 @@ import java.util.function.Consumer; public class Utils { + private static Boolean sUseQsMediaPlayer = null; + /** * Allows lambda iteration over a list. It is done in reverse order so it is safe * to add or remove items during the iteration. Skips over null items. @@ -81,9 +81,16 @@ public class Utils { * Off by default, but can be disabled by setting to 0 */ public static boolean useQsMediaPlayer(Context context) { - int flag = Settings.Global.getInt(context.getContentResolver(), - Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1); - return flag > 0; + // TODO(b/192412820): Replace SHOW_MEDIA_ON_QUICK_SETTINGS with compile-time value + // Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS can't be toggled at runtime, so simply + // cache the first result we fetch and use that going forward. Do this to avoid unnecessary + // binder calls which may happen on the critical path. + if (sUseQsMediaPlayer == null) { + int flag = Settings.Global.getInt(context.getContentResolver(), + Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1); + sUseQsMediaPlayer = flag > 0; + } + return sUseQsMediaPlayer; } /** diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt index 540d2918319fa..1cce3b5b9e776 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt @@ -21,10 +21,13 @@ import android.provider.Settings import android.testing.AndroidTestingRunner import androidx.test.filters.SmallTest +import com.android.dx.mockito.inline.extended.ExtendedMockito import com.android.internal.config.sysui.SystemUiDeviceConfigFlags.NOTIFICATIONS_USE_PEOPLE_FILTERING import com.android.systemui.SysuiTestCase import com.android.systemui.util.DeviceConfigProxyFake +import com.android.systemui.util.Utils +import com.android.systemui.util.mockito.any import org.junit.After import org.junit.Assert.assertFalse @@ -32,28 +35,33 @@ import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.mockito.MockitoSession +import org.mockito.quality.Strictness @RunWith(AndroidTestingRunner::class) @SmallTest class NotificationSectionsFeatureManagerTest : SysuiTestCase() { var manager: NotificationSectionsFeatureManager? = null val proxyFake = DeviceConfigProxyFake() - var originalQsMediaPlayer: Int = 0 + private lateinit var staticMockSession: MockitoSession @Before public fun setup() { manager = NotificationSectionsFeatureManager(proxyFake, mContext) manager!!.clearCache() - originalQsMediaPlayer = Settings.Global.getInt(context.getContentResolver(), - Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1) + staticMockSession = ExtendedMockito.mockitoSession() + .mockStatic(Utils::class.java) + .strictness(Strictness.LENIENT) + .startMocking() + `when`(Utils.useQsMediaPlayer(any())).thenReturn(false) Settings.Global.putInt(context.getContentResolver(), Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 0) } @After public fun teardown() { - Settings.Global.putInt(context.getContentResolver(), - Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, originalQsMediaPlayer) + staticMockSession.finishMocking() } @Test