Skip to content

Commit bec9997

Browse files
committed
Implement most meta+key shortcuts using intents.
1 parent c25a0ba commit bec9997

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

app/src/main/java/io/github/rickybrent/minimal_symlayer_keyboard/InputMethodService.kt

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.rickybrent.minimal_symlayer_keyboard
22

33
import android.content.Context
4+
import android.content.Intent
5+
import android.provider.Settings
46
import android.os.VibrationEffect
57
import android.os.Vibrator
68
import android.text.InputType
@@ -533,7 +535,7 @@ class InputMethodService : AndroidInputMethodService() {
533535
}
534536

535537
/**
536-
* Handle keyboard shortcuts where emojiMeta is held. (TODO: Add search+key shortcuts)
538+
* Handle keyboard shortcuts where emojiMeta is held.
537539
*/
538540
private fun onEmojiMetaShotcut(event: KeyEvent): Boolean {
539541
// skip the extra simulateKeyTap logic with sendDownUpKeyEvents.
@@ -559,48 +561,46 @@ class InputMethodService : AndroidInputMethodService() {
559561
sendDownUpKeyEvents(KeyEvent.KEYCODE_ESCAPE)
560562
true
561563
}
562-
// TODO: System-level key events like this is not allowed and fails silently.
563-
// Replace with another approach to launch apps.
564-
KeyEvent.KEYCODE_ENTER -> {
565-
sendDownUpKeyEvents(KeyEvent.KEYCODE_HOME)
566-
true
567-
}
568564
MP01_KEYCODE_DICTATE -> {
569565
// TODO: latch control, even if disabled from dotCtrl.
570566
true
571567
}
572-
KeyEvent.KEYCODE_E -> {
573-
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENVELOPE) // Email
568+
// Use intents in place of system-level key events.
569+
KeyEvent.KEYCODE_ENTER -> { // Home
570+
launchApp(Intent.ACTION_MAIN, Intent.CATEGORY_HOME)
574571
true
575572
}
576-
KeyEvent.KEYCODE_A -> {
577-
sendDownUpKeyEvents(KeyEvent.KEYCODE_ASSIST) // Assistant
573+
KeyEvent.KEYCODE_E -> { // Email
574+
launchApp(Intent.ACTION_MAIN, Intent.CATEGORY_APP_EMAIL)
578575
true
579576
}
580-
KeyEvent.KEYCODE_C -> {
581-
sendDownUpKeyEvents(KeyEvent.KEYCODE_CONTACTS) // Contacts
577+
KeyEvent.KEYCODE_A -> { // Assistant (uses a different action)
578+
launchApp(Intent.ACTION_ASSIST)
582579
true
583580
}
584-
KeyEvent.KEYCODE_B -> {
585-
sendDownUpKeyEvents(KeyEvent.KEYCODE_EXPLORER) // Browser
581+
KeyEvent.KEYCODE_C -> { // Contacts
582+
launchApp(Intent.ACTION_MAIN, Intent.CATEGORY_APP_CONTACTS)
586583
true
587584
}
588-
KeyEvent.KEYCODE_N -> {
589-
sendDownUpKeyEvents(KeyEvent.KEYCODE_NOTIFICATION) // Notifications
585+
KeyEvent.KEYCODE_B -> { // Browser
586+
launchApp(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
590587
true
591588
}
592-
KeyEvent.KEYCODE_I -> {
593-
sendDownUpKeyEvents(KeyEvent.KEYCODE_SETTINGS) // Settings
589+
KeyEvent.KEYCODE_I -> { // Settings
590+
launchApp(Settings.ACTION_SETTINGS)
594591
true
595592
}
596-
KeyEvent.KEYCODE_P -> {
597-
sendDownUpKeyEvents(KeyEvent.KEYCODE_MUSIC) // Music
593+
KeyEvent.KEYCODE_P -> { // Music
594+
launchApp(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC)
598595
true
599596
}
600-
KeyEvent.KEYCODE_L -> {
601-
sendDownUpKeyEvents(KeyEvent.KEYCODE_CALENDAR) // Calendar
597+
KeyEvent.KEYCODE_L -> { // Calendar
598+
launchApp(Intent.ACTION_MAIN, Intent.CATEGORY_APP_CALENDAR)
602599
true
603600
}
601+
// KeyEvent.KEYCODE_N -> // Notification shade. No standard intent for this.
602+
// We may be able to use an accessibility service, but it's not a priority for me.
603+
// Menu and Escape will only work for some apps when sent like this as well.
604604
else -> false
605605
}
606606
}
@@ -842,6 +842,23 @@ class InputMethodService : AndroidInputMethodService() {
842842
return null
843843
}
844844

845+
/**
846+
* Launches an application using an Intent.
847+
*/
848+
private fun launchApp(action: String, category: String? = null) {
849+
val intent = Intent(action)
850+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
851+
if (category != null) {
852+
intent.addCategory(category)
853+
}
854+
try {
855+
startActivity(intent)
856+
} catch (e: Exception) {
857+
// Handle cases where the app isn't found or another error occurs
858+
e.printStackTrace()
859+
}
860+
}
861+
845862
fun clearModifiers() {
846863
shift.reset()
847864
alt.reset()

0 commit comments

Comments
 (0)