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

Commit 21d892de authored by Josh's avatar Josh
Browse files

Moved core startable logic out of repository

As part of go/optimizing-shortcut-helper-component-instances
refactoring ShortcutHelperStateRepository to do only one thing- handle
shortcut helper state, while ShortcutHelperCoreStartable, handles any
work related to starting up ShortcutHelperComponents, such as
registering broadcast listeners.

Test: this is only a refactor, existing tests cover this.
Flag: com.android.systemui.keyboard_shortcut_helper_rewrite
Bug: 385325834
Change-Id: Iedb1315aaf503e0e2102a6ac2cfcd90b7cc24fda
parent 51417ea4
Loading
Loading
Loading
Loading
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.keyboard.shortcut

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.UserHandle
import com.android.systemui.CoreStartable
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperStateRepository
import com.android.systemui.statusbar.CommandQueue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject


@SysUISingleton
class ShortcutHelperCoreStartable
@Inject constructor(
    private val commandQueue: CommandQueue,
    private val broadcastDispatcher: BroadcastDispatcher,
    private val stateRepository: ShortcutHelperStateRepository,
    @Background private val backgroundScope: CoroutineScope,
) : CoreStartable {
    override fun start() {
        registerBroadcastReceiver(
            action = Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS,
            onReceive = {
                backgroundScope.launch { stateRepository.show() }
            }
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS,
            onReceive = { stateRepository.hide() }
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_CLOSE_SYSTEM_DIALOGS,
            onReceive = { stateRepository.hide() }
        )
        commandQueue.addCallback(
            object : CommandQueue.Callbacks {
                override fun dismissKeyboardShortcutsMenu() {
                    stateRepository.hide()
                }

                override fun toggleKeyboardShortcutsMenu(deviceId: Int) {
                    backgroundScope.launch { stateRepository.toggle(deviceId) }
                }
            }
        )
    }

    private fun registerBroadcastReceiver(action: String, onReceive: () -> Unit) {
        broadcastDispatcher.registerReceiver(
            receiver =
            object : BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent) {
                    onReceive()
                }
            },
            filter = IntentFilter(action),
            flags = Context.RECEIVER_EXPORTED or Context.RECEIVER_VISIBLE_TO_INSTANT_APPS,
            user = UserHandle.ALL,
        )
    }
}
 No newline at end of file
+2 −3
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import com.android.systemui.Flags.keyboardShortcutHelperRewrite
import com.android.systemui.keyboard.shortcut.data.repository.CustomShortcutCategoriesRepository
import com.android.systemui.keyboard.shortcut.data.repository.DefaultShortcutCategoriesRepository
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutCategoriesRepository
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperStateRepository
import com.android.systemui.keyboard.shortcut.data.source.AppCategoriesShortcutsSource
import com.android.systemui.keyboard.shortcut.data.source.CurrentAppShortcutsSource
import com.android.systemui.keyboard.shortcut.data.source.InputShortcutsSource
@@ -95,8 +94,8 @@ interface ShortcutHelperModule {

        @Provides
        @IntoMap
        @ClassKey(ShortcutHelperStateRepository::class)
        fun repo(implLazy: Lazy<ShortcutHelperStateRepository>): CoreStartable {
        @ClassKey(ShortcutHelperCoreStartable::class)
        fun repo(implLazy: Lazy<ShortcutHelperCoreStartable>): CoreStartable {
            return if (keyboardShortcutHelperRewrite()) {
                implLazy.get()
            } else {
+17 −63
Original line number Diff line number Diff line
@@ -16,73 +16,44 @@

package com.android.systemui.keyboard.shortcut.data.repository

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.input.InputManager
import android.os.UserHandle
import android.view.KeyCharacterMap.VIRTUAL_KEYBOARD
import com.android.systemui.CoreStartable
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Active
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Inactive
import com.android.systemui.shared.hardware.findInputDevice
import com.android.systemui.statusbar.CommandQueue
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import com.android.app.tracing.coroutines.launchTraced as launch
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import javax.inject.Inject

@SysUISingleton
class ShortcutHelperStateRepository
@Inject
constructor(
    private val commandQueue: CommandQueue,
    private val broadcastDispatcher: BroadcastDispatcher,
    private val inputManager: InputManager,
    @Background private val backgroundScope: CoroutineScope,
    @Background private val backgroundDispatcher: CoroutineDispatcher,
) : CoreStartable {
) {
    private val _state = MutableStateFlow<ShortcutHelperState>(Inactive)
    val state = _state.asStateFlow()

    val state = MutableStateFlow<ShortcutHelperState>(Inactive)

    override fun start() {
        registerBroadcastReceiver(
            action = Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS,
            onReceive = {
                backgroundScope.launch { state.value = Active(findPhysicalKeyboardId()) }
            }
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS,
            onReceive = { state.value = Inactive }
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_CLOSE_SYSTEM_DIALOGS,
            onReceive = { state.value = Inactive }
        )
        commandQueue.addCallback(
            object : CommandQueue.Callbacks {
                override fun dismissKeyboardShortcutsMenu() {
                    state.value = Inactive
                }

                override fun toggleKeyboardShortcutsMenu(deviceId: Int) {
                    state.value =
                        if (state.value is Inactive) {
                            Active(deviceId)
    suspend fun toggle(deviceId: Int? = null) {
        if (_state.value is Inactive) {
            show(deviceId)
        } else {
                            Inactive
            hide()
        }
    }

    suspend fun show(deviceId: Int? = null) {
        _state.value = Active(deviceId ?: findPhysicalKeyboardId())
    }
        )

    fun hide() {
        _state.value = Inactive
    }

    private suspend fun findPhysicalKeyboardId() =
@@ -92,21 +63,4 @@ constructor(
            return@withContext firstEnabledPhysicalKeyboard?.id ?: VIRTUAL_KEYBOARD
        }

    fun hide() {
        state.value = Inactive
    }

    private fun registerBroadcastReceiver(action: String, onReceive: () -> Unit) {
        broadcastDispatcher.registerReceiver(
            receiver =
                object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        onReceive()
                    }
                },
            filter = IntentFilter(action),
            flags = Context.RECEIVER_EXPORTED or Context.RECEIVER_VISIBLE_TO_INSTANT_APPS,
            user = UserHandle.ALL,
        )
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -23,10 +23,9 @@ import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState
import com.android.systemui.model.SysUiState
import com.android.systemui.settings.DisplayTracker
import com.android.systemui.shared.system.QuickStepContract
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject
import com.android.app.tracing.coroutines.launchTraced as launch

@SysUISingleton
@@ -39,7 +38,7 @@ constructor(
    private val repository: ShortcutHelperStateRepository
) {

    val state: Flow<ShortcutHelperState> = repository.state.asStateFlow()
    val state: Flow<ShortcutHelperState> = repository.state

    fun onViewClosed() {
        repository.hide()
+11 −4
Original line number Diff line number Diff line
@@ -69,10 +69,7 @@ var Kosmos.shortcutHelperMultiTaskingShortcutsSource: KeyboardShortcutGroupsSour
val Kosmos.shortcutHelperStateRepository by
    Kosmos.Fixture {
        ShortcutHelperStateRepository(
            fakeCommandQueue,
            broadcastDispatcher,
            fakeInputManager.inputManager,
            testScope,
            testDispatcher,
        )
    }
@@ -153,10 +150,20 @@ val Kosmos.customShortcutCategoriesRepository by
        )
    }

val Kosmos.shortcutHelperCoreStartable by
        Kosmos.Fixture {
            ShortcutHelperCoreStartable(
                fakeCommandQueue,
                broadcastDispatcher,
                shortcutHelperStateRepository,
                testScope,
            )
        }

val Kosmos.shortcutHelperTestHelper by
    Kosmos.Fixture {
        ShortcutHelperTestHelper(
            shortcutHelperStateRepository,
            shortcutHelperCoreStartable,
            applicationContext,
            broadcastDispatcher,
            fakeCommandQueue,
Loading