From eb36215d41b544198e93f33970351307008eee36 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Mon, 17 Oct 2022 13:25:02 -0400 Subject: [PATCH] Add exclusion rect to the BrightnessDialog slider This prevents drags that are close to the screen from triggering back gesture. It includes the horizontal margin to give some room for starting the drag slightly outside of the view. Fixes: 247049806 Test: manual: small and large screen Test: atest BrightnessDialogTest Change-Id: I0a206eda72f6eb15a5d887f01b468a8ceba15d1f --- .../settings/brightness/BrightnessDialog.java | 12 ++ packages/SystemUI/tests/AndroidManifest.xml | 5 + .../brightness/BrightnessDialogTest.kt | 107 ++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 packages/SystemUI/tests/src/com/android/systemui/settings/brightness/BrightnessDialogTest.kt diff --git a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessDialog.java b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessDialog.java index 6e9f859c202bd..d5a3954362716 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessDialog.java +++ b/packages/SystemUI/src/com/android/systemui/settings/brightness/BrightnessDialog.java @@ -20,6 +20,7 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; import android.app.Activity; +import android.graphics.Rect; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; @@ -36,6 +37,8 @@ import com.android.systemui.R; import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.dagger.qualifiers.Background; +import java.util.List; + import javax.inject.Inject; /** A dialog that provides controls for adjusting the screen brightness. */ @@ -83,6 +86,15 @@ public class BrightnessDialog extends Activity { lp.leftMargin = horizontalMargin; lp.rightMargin = horizontalMargin; frame.setLayoutParams(lp); + Rect bounds = new Rect(); + frame.addOnLayoutChangeListener( + (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { + // Exclude this view (and its horizontal margins) from triggering gestures. + // This prevents back gesture from being triggered by dragging close to the + // edge of the slider (0% or 100%). + bounds.set(-horizontalMargin, 0, right - left + horizontalMargin, bottom - top); + v.setSystemGestureExclusionRects(List.of(bounds)); + }); BrightnessSliderController controller = mToggleSliderFactory.create(this, frame); controller.init(); diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml index ba2804572ef5c..1b404a82145b8 100644 --- a/packages/SystemUI/tests/AndroidManifest.xml +++ b/packages/SystemUI/tests/AndroidManifest.xml @@ -88,6 +88,11 @@ android:excludeFromRecents="true" /> + + diff --git a/packages/SystemUI/tests/src/com/android/systemui/settings/brightness/BrightnessDialogTest.kt b/packages/SystemUI/tests/src/com/android/systemui/settings/brightness/BrightnessDialogTest.kt new file mode 100644 index 0000000000000..1130bda9b8810 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/settings/brightness/BrightnessDialogTest.kt @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2022 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.systemui.settings.brightness + +import android.content.Intent +import android.graphics.Rect +import android.os.Handler +import android.testing.AndroidTestingRunner +import android.testing.TestableLooper +import android.view.View +import android.view.ViewGroup +import androidx.test.filters.SmallTest +import androidx.test.rule.ActivityTestRule +import androidx.test.runner.intercepting.SingleActivityFactory +import com.android.systemui.R +import com.android.systemui.SysuiTestCase +import com.android.systemui.broadcast.BroadcastDispatcher +import com.android.systemui.util.mockito.any +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations + +@SmallTest +@RunWith(AndroidTestingRunner::class) +@TestableLooper.RunWithLooper +class BrightnessDialogTest : SysuiTestCase() { + + @Mock private lateinit var brightnessSliderControllerFactory: BrightnessSliderController.Factory + @Mock private lateinit var backgroundHandler: Handler + @Mock private lateinit var brightnessSliderController: BrightnessSliderController + + @Rule + @JvmField + var activityRule = + ActivityTestRule( + object : SingleActivityFactory(TestDialog::class.java) { + override fun create(intent: Intent?): TestDialog { + return TestDialog( + fakeBroadcastDispatcher, + brightnessSliderControllerFactory, + backgroundHandler + ) + } + }, + false, + false + ) + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + `when`(brightnessSliderControllerFactory.create(any(), any())) + .thenReturn(brightnessSliderController) + `when`(brightnessSliderController.rootView).thenReturn(View(context)) + + activityRule.launchActivity(null) + } + + @After + fun tearDown() { + activityRule.finishActivity() + } + + @Test + fun testGestureExclusion() { + val frame = activityRule.activity.requireViewById(R.id.brightness_mirror_container) + + val lp = frame.layoutParams as ViewGroup.MarginLayoutParams + val horizontalMargin = + activityRule.activity.resources.getDimensionPixelSize( + R.dimen.notification_side_paddings + ) + assertThat(lp.leftMargin).isEqualTo(horizontalMargin) + assertThat(lp.rightMargin).isEqualTo(horizontalMargin) + + assertThat(frame.systemGestureExclusionRects.size).isEqualTo(1) + val exclusion = frame.systemGestureExclusionRects[0] + assertThat(exclusion) + .isEqualTo(Rect(-horizontalMargin, 0, frame.width + horizontalMargin, frame.height)) + } + + class TestDialog( + broadcastDispatcher: BroadcastDispatcher, + brightnessSliderControllerFactory: BrightnessSliderController.Factory, + backgroundHandler: Handler + ) : BrightnessDialog(broadcastDispatcher, brightnessSliderControllerFactory, backgroundHandler) +}