1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.keyboard.shortcut.data.source 18 19 import android.content.res.Resources 20 import android.hardware.input.InputManager 21 import android.hardware.input.KeyGlyphMap 22 import android.view.KeyEvent.KEYCODE_A 23 import android.view.KeyEvent.KEYCODE_BACK 24 import android.view.KeyEvent.KEYCODE_DPAD_LEFT 25 import android.view.KeyEvent.KEYCODE_ESCAPE 26 import android.view.KeyEvent.KEYCODE_H 27 import android.view.KeyEvent.KEYCODE_HOME 28 import android.view.KeyEvent.KEYCODE_I 29 import android.view.KeyEvent.KEYCODE_L 30 import android.view.KeyEvent.KEYCODE_N 31 import android.view.KeyEvent.KEYCODE_RECENT_APPS 32 import android.view.KeyEvent.KEYCODE_S 33 import android.view.KeyEvent.KEYCODE_SLASH 34 import android.view.KeyEvent.KEYCODE_TAB 35 import android.view.KeyEvent.META_ALT_ON 36 import android.view.KeyEvent.META_CTRL_ON 37 import android.view.KeyEvent.META_META_ON 38 import android.view.KeyEvent.META_SHIFT_ON 39 import android.view.KeyboardShortcutGroup 40 import android.view.KeyboardShortcutInfo 41 import com.android.systemui.Flags.shortcutHelperKeyGlyph 42 import com.android.systemui.dagger.qualifiers.Main 43 import com.android.systemui.keyboard.shortcut.data.model.shortcutInfo 44 import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperKeys 45 import com.android.systemui.res.R 46 import javax.inject.Inject 47 48 class SystemShortcutsSource 49 @Inject 50 constructor(@Main private val resources: Resources, private val inputManager: InputManager) : 51 KeyboardShortcutGroupsSource { 52 53 override suspend fun shortcutGroups(deviceId: Int) = 54 listOf( 55 KeyboardShortcutGroup( 56 resources.getString(R.string.shortcut_helper_category_system_controls), 57 systemControlsShortcuts() + hardwareShortcuts(deviceId), 58 ), 59 KeyboardShortcutGroup( 60 resources.getString(R.string.shortcut_helper_category_system_apps), 61 systemAppsShortcuts(), 62 ), 63 ) 64 65 private fun hardwareShortcuts(deviceId: Int): List<KeyboardShortcutInfo> = 66 if (shortcutHelperKeyGlyph()) { 67 val keyGlyphMap = inputManager.getKeyGlyphMap(deviceId) 68 if (keyGlyphMap != null) { 69 functionRowKeys(keyGlyphMap) + keyCombinationShortcuts(keyGlyphMap) 70 } else { 71 // Not add function row keys if it is not supported by keyboard 72 emptyList() 73 } 74 } else { 75 defaultFunctionRowKeys() 76 } 77 78 private fun defaultFunctionRowKeys(): List<KeyboardShortcutInfo> = 79 listOf( 80 shortcutInfo(resources.getString(R.string.group_system_access_home_screen)) { 81 command(modifiers = 0, KEYCODE_HOME) 82 }, 83 shortcutInfo(resources.getString(R.string.group_system_go_back)) { 84 command(modifiers = 0, KEYCODE_BACK) 85 }, 86 shortcutInfo(resources.getString(R.string.group_system_overview_open_apps)) { 87 command(modifiers = 0, KEYCODE_RECENT_APPS) 88 }, 89 ) 90 91 private fun functionRowKeys(keyGlyphMap: KeyGlyphMap): List<KeyboardShortcutInfo> { 92 val functionRowKeys = mutableListOf<KeyboardShortcutInfo>() 93 keyGlyphMap.functionRowKeys.forEach { keyCode -> 94 val labelResId = ShortcutHelperKeys.keyLabelResIds[keyCode] 95 if (labelResId != null) { 96 functionRowKeys.add( 97 shortcutInfo(resources.getString(labelResId)) { 98 command(modifiers = 0, keyCode) 99 } 100 ) 101 } 102 } 103 return functionRowKeys 104 } 105 106 private fun keyCombinationShortcuts(keyGlyphMap: KeyGlyphMap): List<KeyboardShortcutInfo> { 107 val shortcuts = mutableListOf<KeyboardShortcutInfo>() 108 keyGlyphMap.hardwareShortcuts.forEach { (keyCombination, keyCode) -> 109 val labelResId = ShortcutHelperKeys.keyLabelResIds[keyCode] 110 if (labelResId != null) { 111 val info = 112 shortcutInfo(resources.getString(labelResId)) { 113 command(keyCombination.modifierState, keyCombination.keycode) 114 } 115 shortcuts.add(info) 116 } 117 } 118 return shortcuts 119 } 120 121 private fun systemControlsShortcuts() = 122 listOf( 123 // Access list of all apps and search (i.e. Search/Launcher): 124 // - Meta 125 shortcutInfo(resources.getString(R.string.group_system_access_all_apps_search)) { 126 command(META_META_ON) 127 }, 128 // Access home screen: 129 // - Meta + H 130 shortcutInfo(resources.getString(R.string.group_system_access_home_screen)) { 131 command(META_META_ON, KEYCODE_H) 132 }, 133 // Overview of open apps: 134 // - Meta + Tab 135 shortcutInfo(resources.getString(R.string.group_system_overview_open_apps)) { 136 command(META_META_ON, KEYCODE_TAB) 137 }, 138 // Cycle through recent apps (forward): 139 // - Alt + Tab 140 shortcutInfo(resources.getString(R.string.group_system_cycle_forward)) { 141 command(META_ALT_ON, KEYCODE_TAB) 142 }, 143 // Cycle through recent apps (back): 144 // - Shift + Alt + Tab 145 shortcutInfo(resources.getString(R.string.group_system_cycle_back)) { 146 command(META_SHIFT_ON or META_ALT_ON, KEYCODE_TAB) 147 }, 148 // Back: go back to previous state (back button) 149 // - Meta + Escape OR 150 // - Meta + Left arrow 151 shortcutInfo(resources.getString(R.string.group_system_go_back)) { 152 command(META_META_ON, KEYCODE_ESCAPE) 153 }, 154 shortcutInfo(resources.getString(R.string.group_system_go_back)) { 155 command(META_META_ON, KEYCODE_DPAD_LEFT) 156 }, 157 // Take a full screenshot: 158 // - Meta + S 159 shortcutInfo(resources.getString(R.string.group_system_full_screenshot)) { 160 command(META_META_ON, KEYCODE_S) 161 }, 162 // Access list of system / apps shortcuts: 163 // - Meta + / 164 shortcutInfo(resources.getString(R.string.group_system_access_system_app_shortcuts)) { 165 command(META_META_ON, KEYCODE_SLASH) 166 }, 167 // Access notification shade: 168 // - Meta + N 169 shortcutInfo(resources.getString(R.string.group_system_access_notification_shade)) { 170 command(META_META_ON, KEYCODE_N) 171 }, 172 // Lock screen: 173 // - Meta + L 174 shortcutInfo(resources.getString(R.string.group_system_lock_screen)) { 175 command(META_META_ON, KEYCODE_L) 176 }, 177 ) 178 179 private fun systemAppsShortcuts() = 180 listOf( 181 // Access system settings: 182 // - Meta + I 183 shortcutInfo(resources.getString(R.string.group_system_access_system_settings)) { 184 command(META_META_ON, KEYCODE_I) 185 }, 186 // Access Assistant: 187 // - Meta + A 188 shortcutInfo(resources.getString(R.string.group_system_access_google_assistant)) { 189 command(META_META_ON, KEYCODE_A) 190 }, 191 ) 192 } 193