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

Commit b37885c8 authored by Tyler Freeman's avatar Tyler Freeman
Browse files

feat(magnification settings): add logThrottled() so we only log one scale...

feat(magnification settings): add logThrottled() so we only log one scale event when they start moving the slider

Bug: 281241589
Test: atest frameworks/base/packages/SystemUI/tests/src/com/android/systemui/accessibility/WindowMagnificationTest.java
 && atest frameworks/base/packages/SystemUI/tests/src/com/android/systemui/accessibility/AccessibilityLoggerTest.java

Change-Id: If38609f5317325945e1637903c82f78024a36cdd
parent 4dc4b842
Loading
Loading
Loading
Loading
+33 −1
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.systemui.accessibility

import com.android.internal.annotations.GuardedBy
import com.android.internal.logging.UiEvent
import com.android.internal.logging.UiEventLogger
import com.android.internal.logging.UiEventLogger.UiEventEnum
import com.android.systemui.util.time.SystemClock
import javax.inject.Inject

/**
@@ -26,7 +28,37 @@ import javax.inject.Inject
 *
 * See go/uievent
 */
class AccessibilityLogger @Inject constructor(private val uiEventLogger: UiEventLogger) {
class AccessibilityLogger
@Inject
constructor(private val uiEventLogger: UiEventLogger, private val clock: SystemClock) {

    @GuardedBy("clock") private var lastTimeThrottledMs: Long = 0
    @GuardedBy("clock") private var lastEventThrottled: UiEventEnum? = null

    /**
     * Logs the event, but any additional calls within the given delay window are ignored. The
     * window resets every time a new event is received. i.e. it will only log one time until you
     * wait at least [delayBeforeLoggingMs] before sending the next event.
     *
     * <p>Additionally, if a different type of event is passed in, the delay window for the previous
     * one is forgotten. e.g. if you send two types of events interlaced all within the delay
     * window, e.g. A->B->A within 1000ms, all three will be logged.
     */
    @JvmOverloads fun logThrottled(event: UiEventEnum, delayBeforeLoggingMs: Int = 2000) {
        synchronized(clock) {
            val currentTimeMs = clock.elapsedRealtime()
            val shouldThrottle =
                event == lastEventThrottled &&
                    currentTimeMs - lastTimeThrottledMs < delayBeforeLoggingMs
            lastEventThrottled = event
            lastTimeThrottledMs = currentTimeMs
            if (shouldThrottle) {
                return
            }
        }
        log(event)
    }

    /** Logs the given event */
    fun log(event: UiEventEnum) {
        uiEventLogger.log(event)
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.accessibility

import android.testing.AndroidTestingRunner
import androidx.test.filters.SmallTest
import com.android.internal.logging.UiEventLogger
import com.android.systemui.SysuiTestCase
import com.android.systemui.accessibility.AccessibilityLogger.MagnificationSettingsEvent.MAGNIFICATION_SETTINGS_PANEL_CLOSED
import com.android.systemui.accessibility.AccessibilityLogger.MagnificationSettingsEvent.MAGNIFICATION_SETTINGS_PANEL_OPENED
import com.android.systemui.util.time.FakeSystemClock
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit

@SmallTest
@RunWith(AndroidTestingRunner::class)
class AccessibilityLoggerTest : SysuiTestCase() {
    @JvmField @Rule val mockito = MockitoJUnit.rule()

    private val fakeClock = FakeSystemClock()
    @Mock private lateinit var fakeLogger: UiEventLogger

    private lateinit var a11yLogger: AccessibilityLogger

    @Before
    fun setup() {
        a11yLogger = AccessibilityLogger(fakeLogger, fakeClock)
    }

    @Test
    fun logThrottled_onceWithinWindow() {
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        fakeClock.advanceTime(100L)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        fakeClock.advanceTime(900L)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        fakeClock.advanceTime(1100L)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)

        verify(fakeLogger, times(2)).log(eq(MAGNIFICATION_SETTINGS_PANEL_OPENED))
    }

    @Test
    fun logThrottled_interlacedLogsAllWithinWindow() {
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_CLOSED, 1000)
        fakeClock.advanceTime(100L)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_CLOSED, 1000)
        fakeClock.advanceTime(200L)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)
        fakeClock.advanceTime(1100L)
        a11yLogger.logThrottled(MAGNIFICATION_SETTINGS_PANEL_OPENED, 1000)

        verify(fakeLogger, times(3)).log(eq(MAGNIFICATION_SETTINGS_PANEL_OPENED))
        verify(fakeLogger).log(eq(MAGNIFICATION_SETTINGS_PANEL_CLOSED))
    }
}