Skip to content

Commit

Permalink
feat: keyboard shortcuts helper
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjaySargam committed Aug 16, 2024
1 parent 4af8ad3 commit ee30b44
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 0 deletions.
23 changes: 23 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import android.media.AudioManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyboardShortcutGroup
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -54,6 +57,7 @@ import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.workarounds.AppLoadedFromBackupWorkaround.showedActivityFailedScreen
import com.ichi2.async.CollectionLoader
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.customtabs.CustomTabActivityHelper
import com.ichi2.compat.customtabs.CustomTabsFallback
import com.ichi2.compat.customtabs.CustomTabsHelper
Expand Down Expand Up @@ -102,6 +106,25 @@ open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener {
}
}

override fun onProvideKeyboardShortcuts(
data: MutableList<KeyboardShortcutGroup>,
menu: Menu?,
deviceId: Int
) {
val shortcutGroups = CompatHelper.compat.getAllShortcuts(this)
data.addAll(shortcutGroups)
super.onProvideKeyboardShortcuts(data, menu, deviceId)
}

override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_K && event.isAltPressed) {
// Alt+K: Show keyboard shortcuts dialog
CompatHelper.compat.showKeyboardShortcutsDialog(this)
return true
}
return super.onKeyUp(keyCode, event)
}

override fun onStart() {
super.onStart()
customTabActivityHelper.bindCustomTabsService(this)
Expand Down
13 changes: 13 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/Compat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.graphics.Bitmap.CompressFormat
import android.media.MediaRecorder
import android.net.Uri
import android.os.Bundle
import android.view.KeyboardShortcutGroup
import android.view.View
import androidx.annotation.CheckResult
import androidx.core.view.OnReceiveContentListener
Expand Down Expand Up @@ -217,6 +218,18 @@ interface Compat {
onReceiveContentListener: OnReceiveContentListener
)

/**
* To show keyboard shortcuts dialog
*/
fun showKeyboardShortcutsDialog(
activity: Activity
)

/**
* Get all keyboard shortcuts
*/
fun getAllShortcuts(activity: Activity): MutableList<KeyboardShortcutGroup>

/**
* Converts a locale to a 'two letter' code (ISO-639-1 + ISO 3166-1 alpha-2)
* Locale("spa", "MEX", "001") => Locale("es", "MX", "001")
Expand Down
12 changes: 12 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/compat/CompatV23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import android.os.Bundle
import android.os.Environment
import android.os.Vibrator
import android.provider.MediaStore
import android.view.KeyboardShortcutGroup
import android.view.View
import androidx.appcompat.widget.TooltipCompat
import androidx.core.view.OnReceiveContentListener
Expand Down Expand Up @@ -160,6 +161,17 @@ open class CompatV23 : Compat {
// No implementation possible.
}

// Until API 24
override fun showKeyboardShortcutsDialog(activity: Activity) {
// No implementation available
}

// Until API 24
override fun getAllShortcuts(activity: Activity): MutableList<KeyboardShortcutGroup> {
// No implementation available
return mutableListOf()
}

// Until API 26
@Throws(IOException::class)
override fun deleteFile(file: File) {
Expand Down
Loading

0 comments on commit ee30b44

Please sign in to comment.