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

Commit c0ff9ea6 authored by Joshua Mokut's avatar Joshua Mokut Committed by Android (Google) Code Review
Browse files

Merge "Implemented Shortcut retrieval for current apps" into main

parents 732fe397 e3081d3a
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -3597,6 +3597,10 @@
         that shows the user which keyboard shortcuts they can use. The "App shortcuts" are
         that shows the user which keyboard shortcuts they can use. The "App shortcuts" are
         for example "Open browser" or "Open calculator". [CHAR LIMIT=NONE] -->
         for example "Open browser" or "Open calculator". [CHAR LIMIT=NONE] -->
    <string name="shortcut_helper_category_app_shortcuts">App shortcuts</string>
    <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
    <!-- 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
         that shows the user which keyboard shortcuts they can use. The "Accessibility" shortcuts
         are for example "Turn on talkback". [CHAR LIMIT=NONE] -->
         are for example "Turn on talkback". [CHAR LIMIT=NONE] -->
+6 −0
Original line number Original line Diff line number Diff line
@@ -21,11 +21,13 @@ import com.android.systemui.CoreStartable
import com.android.systemui.Flags.keyboardShortcutHelperRewrite
import com.android.systemui.Flags.keyboardShortcutHelperRewrite
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperStateRepository
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.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.InputShortcutsSource
import com.android.systemui.keyboard.shortcut.data.source.KeyboardShortcutGroupsSource
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.MultitaskingShortcutsSource
import com.android.systemui.keyboard.shortcut.data.source.SystemShortcutsSource
import com.android.systemui.keyboard.shortcut.data.source.SystemShortcutsSource
import com.android.systemui.keyboard.shortcut.qualifiers.AppCategoriesShortcuts
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.InputShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.MultitaskingShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.MultitaskingShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.SystemShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.SystemShortcuts
@@ -54,6 +56,10 @@ interface ShortcutHelperModule {
    @MultitaskingShortcuts
    @MultitaskingShortcuts
    fun multitaskingShortcutsSource(impl: MultitaskingShortcutsSource): KeyboardShortcutGroupsSource
    fun multitaskingShortcutsSource(impl: MultitaskingShortcutsSource): KeyboardShortcutGroupsSource


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

    @Binds
    @Binds
    @InputShortcuts
    @InputShortcuts
    fun inputShortcutsSources(impl: InputShortcutsSource): KeyboardShortcutGroupsSource
    fun inputShortcutsSources(impl: InputShortcutsSource): KeyboardShortcutGroupsSource
+42 −8
Original line number Original line 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.dagger.qualifiers.Background
import com.android.systemui.keyboard.shortcut.data.source.KeyboardShortcutGroupsSource
import com.android.systemui.keyboard.shortcut.data.source.KeyboardShortcutGroupsSource
import com.android.systemui.keyboard.shortcut.qualifiers.AppCategoriesShortcuts
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.InputShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.MultitaskingShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.MultitaskingShortcuts
import com.android.systemui.keyboard.shortcut.qualifiers.SystemShortcuts
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.Shortcut
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategory
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
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.APP_CATEGORIES
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.AppCategories
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.IME
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.CurrentApp
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.MULTI_TASKING
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.InputMethodEditor
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutCategoryType.SYSTEM
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.ShortcutCommand
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Active
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Active
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutIcon
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 com.android.systemui.keyboard.shortcut.shared.model.ShortcutSubCategory
import javax.inject.Inject
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withContext


@@ -58,6 +61,7 @@ constructor(
    @MultitaskingShortcuts private val multitaskingShortcutsSource: KeyboardShortcutGroupsSource,
    @MultitaskingShortcuts private val multitaskingShortcutsSource: KeyboardShortcutGroupsSource,
    @AppCategoriesShortcuts private val appCategoriesShortcutsSource: KeyboardShortcutGroupsSource,
    @AppCategoriesShortcuts private val appCategoriesShortcutsSource: KeyboardShortcutGroupsSource,
    @InputShortcuts private val inputShortcutsSource: KeyboardShortcutGroupsSource,
    @InputShortcuts private val inputShortcutsSource: KeyboardShortcutGroupsSource,
    @CurrentAppShortcuts private val currentAppShortcutsSource: KeyboardShortcutGroupsSource,
    private val inputManager: InputManager,
    private val inputManager: InputManager,
    stateRepository: ShortcutHelperStateRepository
    stateRepository: ShortcutHelperStateRepository
) {
) {
@@ -76,7 +80,7 @@ constructor(
            if (it != null) {
            if (it != null) {
                toShortcutCategory(
                toShortcutCategory(
                    it.keyCharacterMap,
                    it.keyCharacterMap,
                    SYSTEM,
                    System,
                    systemShortcutsSource.shortcutGroups(it.id),
                    systemShortcutsSource.shortcutGroups(it.id),
                    keepIcons = true,
                    keepIcons = true,
                )
                )
@@ -90,7 +94,7 @@ constructor(
            if (it != null) {
            if (it != null) {
                toShortcutCategory(
                toShortcutCategory(
                    it.keyCharacterMap,
                    it.keyCharacterMap,
                    MULTI_TASKING,
                    MultiTasking,
                    multitaskingShortcutsSource.shortcutGroups(it.id),
                    multitaskingShortcutsSource.shortcutGroups(it.id),
                    keepIcons = true,
                    keepIcons = true,
                )
                )
@@ -104,7 +108,7 @@ constructor(
            if (it != null) {
            if (it != null) {
                toShortcutCategory(
                toShortcutCategory(
                    it.keyCharacterMap,
                    it.keyCharacterMap,
                    APP_CATEGORIES,
                    AppCategories,
                    appCategoriesShortcutsSource.shortcutGroups(it.id),
                    appCategoriesShortcutsSource.shortcutGroups(it.id),
                    keepIcons = true,
                    keepIcons = true,
                )
                )
@@ -118,7 +122,7 @@ constructor(
            if (it != null) {
            if (it != null) {
                toShortcutCategory(
                toShortcutCategory(
                    it.keyCharacterMap,
                    it.keyCharacterMap,
                    IME,
                    InputMethodEditor,
                    inputShortcutsSource.shortcutGroups(it.id),
                    inputShortcutsSource.shortcutGroups(it.id),
                    keepIcons = false,
                    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(
    private fun toShortcutCategory(
        keyCharacterMap: KeyCharacterMap,
        keyCharacterMap: KeyCharacterMap,
        type: ShortcutCategoryType,
        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(
    private fun toShortcuts(
        keyCharacterMap: KeyCharacterMap,
        keyCharacterMap: KeyCharacterMap,
        infoList: List<KeyboardShortcutInfo>,
        infoList: List<KeyboardShortcutInfo>,
+34 −0
Original line number Original line 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 Original line 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> =
    private suspend fun getImeShortcutGroup(deviceId: Int): List<KeyboardShortcutGroup> =
        suspendCancellableCoroutine { continuation ->
        suspendCancellableCoroutine { continuation ->
            val shortcutsReceiver = KeyboardShortcutsReceiver {
            val shortcutsReceiver = KeyboardShortcutsReceiver {
                continuation.resumeWith(Result.success(it))
                continuation.resumeWith(Result.success(it ?: emptyList()))
            }
            }
            windowManager.requestImeKeyboardShortcuts(shortcutsReceiver, deviceId)
            windowManager.requestImeKeyboardShortcuts(shortcutsReceiver, deviceId)
        }
        }
Loading