Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb36215d authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

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
parent 79cf8d64
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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();
+5 −0
Original line number Diff line number Diff line
@@ -88,6 +88,11 @@
                  android:excludeFromRecents="true"
                  />

        <activity android:name=".settings.brightness.BrightnessDialogTest$TestDialog"
            android:exported="false"
            android:excludeFromRecents="true"
            />

        <activity android:name="com.android.systemui.screenshot.ScrollViewActivity"
                  android:exported="false" />

+107 −0
Original line number Diff line number Diff line
/*
 * 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>(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<View>(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)
}