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

Commit e3081d3a authored by Josh's avatar Josh
Browse files

Implemented Shortcut retrieval for current apps

Fixes: 341045649
Test: atest ShortcutHelperCategoriesInteractorTest
Flag: com.android.systemui.keyboard_shortcut_helper_rewrite
Change-Id: Ie2ad6e5bed462c9a804ef762e68b427d89be410d
parent 2acba2f3
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -3597,6 +3597,10 @@
         that shows the user which keyboard shortcuts they can use. The "App shortcuts" are
         for example "Open browser" or "Open calculator". [CHAR LIMIT=NONE] -->
    <string name="shortcut_helper_category_app_shortcuts">App shortcuts</string>
    <!-- Default Title of the keyboard shortcut helper category for current app. The helper is a
         component that shows the user which keyboard shortcuts they can use. The current app
         shortcuts are shortcuts provided by the currently open app. [CHAR LIMIT=NONE] -->
    <string name="shortcut_helper_category_current_app_shortcuts">Current App</string>
    <!-- Title of the keyboard shortcut helper category "Accessibility". The helper is a component
         that shows the user which keyboard shortcuts they can use. The "Accessibility" shortcuts
         are for example "Turn on talkback". [CHAR LIMIT=NONE] -->
+6 −0
Original line number Diff line number Diff line
@@ -21,11 +21,13 @@ import com.android.systemui.CoreStartable
import com.android.systemui.Flags.keyboardShortcutHelperRewrite
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
import com.android.systemui.keyboard.shortcut.data.source.KeyboardShortcutGroupsSource
import com.android.systemui.keyboard.shortcut.data.source.MultitaskingShortcutsSource
import com.android.systemui.keyboard.shortcut.data.source.SystemShortcutsSource
import com.android.systemui.keyboard.shortcut.qualifiers.AppCategoriesShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.CurrentAppShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.InputShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.MultitaskingShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.SystemShortcuts
@@ -54,6 +56,10 @@ interface ShortcutHelperModule {
    @MultitaskingShortcuts
    fun multitaskingShortcutsSource(impl: MultitaskingShortcutsSource): KeyboardShortcutGroupsSource

    @Binds
    @CurrentAppShortcuts
    fun currentAppShortcutsSource(impl: CurrentAppShortcutsSource): KeyboardShortcutGroupsSource

    @Binds
    @InputShortcuts
    fun inputShortcutsSources(impl: InputShortcutsSource): KeyboardShortcutGroupsSource
+42 −8
Original line number Diff line number Diff line
@@ -28,16 +28,18 @@ import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.keyboard.shortcut.data.source.KeyboardShortcutGroupsSource
import com.android.systemui.keyboard.shortcut.qualifiers.AppCategoriesShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.CurrentAppShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.InputShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.MultitaskingShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.SystemShortcuts
import com.android.systemui.keyboard.shortcut.shared.model.Shortcut
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategory
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.APP_CATEGORIES
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.IME
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.MULTI_TASKING
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.SYSTEM
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.AppCategories
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.CurrentApp
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.InputMethodEditor
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.MultiTasking
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.System
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCommand
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Active
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutIcon
@@ -45,6 +47,7 @@ import com.android.systemui.keyboard.shortcut.shared.model.ShortcutKey
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutSubCategory
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext

@@ -58,6 +61,7 @@ constructor(
    @MultitaskingShortcuts private val multitaskingShortcutsSource: KeyboardShortcutGroupsSource,
    @AppCategoriesShortcuts private val appCategoriesShortcutsSource: KeyboardShortcutGroupsSource,
    @InputShortcuts private val inputShortcutsSource: KeyboardShortcutGroupsSource,
    @CurrentAppShortcuts private val currentAppShortcutsSource: KeyboardShortcutGroupsSource,
    private val inputManager: InputManager,
    stateRepository: ShortcutHelperStateRepository
) {
@@ -76,7 +80,7 @@ constructor(
            if (it != null) {
                toShortcutCategory(
                    it.keyCharacterMap,
                    SYSTEM,
                    System,
                    systemShortcutsSource.shortcutGroups(it.id),
                    keepIcons = true,
                )
@@ -90,7 +94,7 @@ constructor(
            if (it != null) {
                toShortcutCategory(
                    it.keyCharacterMap,
                    MULTI_TASKING,
                    MultiTasking,
                    multitaskingShortcutsSource.shortcutGroups(it.id),
                    keepIcons = true,
                )
@@ -104,7 +108,7 @@ constructor(
            if (it != null) {
                toShortcutCategory(
                    it.keyCharacterMap,
                    APP_CATEGORIES,
                    AppCategories,
                    appCategoriesShortcutsSource.shortcutGroups(it.id),
                    keepIcons = true,
                )
@@ -118,7 +122,7 @@ constructor(
            if (it != null) {
                toShortcutCategory(
                    it.keyCharacterMap,
                    IME,
                    InputMethodEditor,
                    inputShortcutsSource.shortcutGroups(it.id),
                    keepIcons = false,
                )
@@ -127,6 +131,26 @@ constructor(
            }
        }

    val currentAppShortcutsCategory: Flow<ShortcutCategory?> =
        activeInputDevice.map {
            if (it != null) {
                val shortcutGroups = currentAppShortcutsSource.shortcutGroups(it.id)
                val categoryType = getCurrentAppShortcutCategoryType(shortcutGroups)
                if (categoryType == null) {
                    null
                } else {
                    toShortcutCategory(
                        it.keyCharacterMap,
                        categoryType,
                        shortcutGroups,
                        keepIcons = false
                    )
                }
            } else {
                null
            }
        }

    private fun toShortcutCategory(
        keyCharacterMap: KeyCharacterMap,
        type: ShortcutCategoryType,
@@ -150,6 +174,16 @@ constructor(
        }
    }

    private fun getCurrentAppShortcutCategoryType(
        shortcutGroups: List<KeyboardShortcutGroup>
    ): ShortcutCategoryType? {
        return if (shortcutGroups.isEmpty()) {
            null
        } else {
            CurrentApp(packageName = shortcutGroups[0].packageName.toString())
        }
    }

    private fun toShortcuts(
        keyCharacterMap: KeyCharacterMap,
        infoList: List<KeyboardShortcutInfo>,
+34 −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.data.source

import android.view.KeyboardShortcutGroup
import android.view.WindowManager
import android.view.WindowManager.KeyboardShortcutsReceiver
import javax.inject.Inject
import kotlinx.coroutines.suspendCancellableCoroutine

class CurrentAppShortcutsSource @Inject constructor(private val windowManager: WindowManager) :
    KeyboardShortcutGroupsSource {
    override suspend fun shortcutGroups(deviceId: Int): List<KeyboardShortcutGroup> =
        suspendCancellableCoroutine { continuation ->
            val shortcutsReceiver = KeyboardShortcutsReceiver {
                continuation.resumeWith(Result.success(it ?: emptyList()))
            }
            windowManager.requestAppKeyboardShortcuts(shortcutsReceiver, deviceId)
        }
}
+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ constructor(@Main private val resources: Resources, private val windowManager: W
    private suspend fun getImeShortcutGroup(deviceId: Int): List<KeyboardShortcutGroup> =
        suspendCancellableCoroutine { continuation ->
            val shortcutsReceiver = KeyboardShortcutsReceiver {
                continuation.resumeWith(Result.success(it))
                continuation.resumeWith(Result.success(it ?: emptyList()))
            }
            windowManager.requestImeKeyboardShortcuts(shortcutsReceiver, deviceId)
        }
Loading