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

Commit e60ebb42 authored by Steve Elliott's avatar Steve Elliott
Browse files

Add ActivatableNotificationViewBinder hierarchy

Bug: 271161129
Test: atest SystemUITests
Change-Id: Ia1a06a400ce5f978f6147b74b9a9afb7c787efdd
parent 3a428a53
Loading
Loading
Loading
Loading
+55 −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.data.repository

import android.view.accessibility.AccessibilityManager
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged

/** Exposes accessibility-related state. */
interface AccessibilityRepository {
    /** @see [AccessibilityManager.isTouchExplorationEnabled] */
    val isTouchExplorationEnabled: Flow<Boolean>

    companion object {
        operator fun invoke(a11yManager: AccessibilityManager): AccessibilityRepository =
            AccessibilityRepositoryImpl(a11yManager)
    }
}

private class AccessibilityRepositoryImpl(
    manager: AccessibilityManager,
) : AccessibilityRepository {
    override val isTouchExplorationEnabled: Flow<Boolean> =
        conflatedCallbackFlow {
                val listener = TouchExplorationStateChangeListener(::trySend)
                manager.addTouchExplorationStateChangeListener(listener)
                trySend(manager.isTouchExplorationEnabled)
                awaitClose { manager.removeTouchExplorationStateChangeListener(listener) }
            }
            .distinctUntilChanged()
}

@Module
object AccessibilityRepositoryModule {
    @Provides fun provideRepo(manager: AccessibilityManager) = AccessibilityRepository(manager)
}
+33 −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.domain.interactor

import com.android.systemui.accessibility.data.repository.AccessibilityRepository
import com.android.systemui.dagger.SysUISingleton
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow

@SysUISingleton
class AccessibilityInteractor
@Inject
constructor(
    private val a11yRepo: AccessibilityRepository,
) {
    /** @see [android.view.accessibility.AccessibilityManager.isTouchExplorationEnabled] */
    val isTouchExplorationEnabled: Flow<Boolean>
        get() = a11yRepo.isTouchExplorationEnabled
}
+2 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.keyguard.dagger.KeyguardBouncerComponent;
import com.android.systemui.BootCompleteCache;
import com.android.systemui.BootCompleteCacheImpl;
import com.android.systemui.accessibility.AccessibilityModule;
import com.android.systemui.accessibility.data.repository.AccessibilityRepositoryModule;
import com.android.systemui.appops.dagger.AppOpsModule;
import com.android.systemui.assist.AssistModule;
import com.android.systemui.biometrics.AlternateUdfpsTouchProvider;
@@ -140,6 +141,7 @@ import javax.inject.Named;
 */
@Module(includes = {
            AccessibilityModule.class,
            AccessibilityRepositoryModule.class,
            AppOpsModule.class,
            AssistModule.class,
            BiometricsModule.class,
+2 −2
Original line number Diff line number Diff line
@@ -196,7 +196,7 @@ public abstract class ActivatableNotificationView extends ExpandableOutlineView
    public void onTap() {}

    /** Sets the last action up time this view was touched. */
    void setLastActionUpTime(long eventTime) {
    public void setLastActionUpTime(long eventTime) {
        mLastActionUpTime = eventTime;
    }

@@ -705,7 +705,7 @@ public abstract class ActivatableNotificationView extends ExpandableOutlineView
        return mRefocusOnDismiss || isAccessibilityFocused();
    }

    void setTouchHandler(Gefingerpoken touchHandler) {
    public void setTouchHandler(Gefingerpoken touchHandler) {
        mTouchHandler = touchHandler;
    }

+98 −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.statusbar.notification.row.ui.viewbinder

import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.android.systemui.Gefingerpoken
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.plugins.FalsingManager
import com.android.systemui.statusbar.notification.row.ActivatableNotificationView
import com.android.systemui.statusbar.notification.row.ui.viewmodel.ActivatableNotificationViewModel
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.launch

/** Binds an [ActivatableNotificationView] to its [view model][ActivatableNotificationViewModel]. */
object ActivatableNotificationViewBinder {

    fun bind(
        viewModel: ActivatableNotificationViewModel,
        view: ActivatableNotificationView,
        falsingManager: FalsingManager,
    ) {
        ExpandableOutlineViewBinder.bind(viewModel, view)
        val touchHandler = TouchHandler(view, falsingManager)
        view.repeatWhenAttached {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.isTouchable.collect { isTouchable ->
                        touchHandler.isTouchEnabled = isTouchable
                    }
                }
                view.registerListenersWhileAttached(touchHandler)
            }
        }
    }

    private suspend fun ActivatableNotificationView.registerListenersWhileAttached(
        touchHandler: TouchHandler,
    ): Unit =
        try {
            setOnTouchListener(touchHandler)
            setTouchHandler(touchHandler)
            awaitCancellation()
        } finally {
            setTouchHandler(null)
            setOnTouchListener(null)
        }
}

private class TouchHandler(
    private val view: ActivatableNotificationView,
    private val falsingManager: FalsingManager,
) : Gefingerpoken, OnTouchListener {

    var isTouchEnabled = false

    override fun onTouch(v: View, ev: MotionEvent): Boolean {
        val result = false
        if (ev.action == MotionEvent.ACTION_UP) {
            view.setLastActionUpTime(ev.eventTime)
        }
        // With a11y, just do nothing.
        if (!isTouchEnabled) {
            return false
        }
        if (ev.action == MotionEvent.ACTION_UP) {
            // If this is a false tap, capture the even so it doesn't result in a click.
            val falseTap: Boolean = falsingManager.isFalseTap(FalsingManager.LOW_PENALTY)
            if (!falseTap && v is ActivatableNotificationView) {
                v.onTap()
            }
            return falseTap
        }
        return result
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean = false

    /** Use [onTouch] instead. */
    override fun onTouchEvent(ev: MotionEvent): Boolean = false
}
Loading