Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android/engine): Initialize index when resuming KeyboardPicker #12832

Merged
merged 2 commits into from
Dec 16, 2024

Conversation

darcywong00
Copy link
Contributor

Fixes: #11714
Fixes: KEYMAN-ANDROID-39S

From the crash reports, it's possible for KeyboardPickerActivity to resume, and have the keyboard key KMKeyboard.currentKeyboard() be uninitialized (null).

This updates KeyboardPickerActivity.onResume to get the keyboard index from the saved preference if KMKeyboard.currentKeyboard() is null.

@keymanapp-test-bot skip

Fixes: #11714
Fixes: KEYMAN-ANDROID-39S

Get the current keyboard index from shared preferences if it's uninitialized.
@keymanapp-test-bot
Copy link

User Test Results

Test specification and instructions

User tests are not required

Copy link
Contributor

@sgschantz sgschantz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a couple simple things to make this easier to read

@@ -252,7 +253,11 @@ protected void onResume() {
adapter.notifyDataSetChanged();
}

int curKbPos = KeyboardController.getInstance().getKeyboardIndex(KMKeyboard.currentKeyboard());

SharedPreferences prefs = this.getSharedPreferences(this.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefs are only used if the currentKeyboard is null, so I would not initialize this unless it is null
Instead of the ternary operator, just use a regular if-else, so the logic is easier to follow, and we only load variables that we are going to use.

Copy link
Contributor

@sgschantz sgschantz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@darcywong00 darcywong00 merged commit 4eed790 into master Dec 16, 2024
5 checks passed
@darcywong00 darcywong00 deleted the fix/android/keyboard-picker-resume branch December 16, 2024 14:10
@keyman-server
Copy link
Collaborator

Changes in this pull request will be available for download in Keyman version 18.0.160-alpha

String currentKeyboardKey = KMKeyboard.currentKeyboard();
if (currentKeyboardKey == null) {
SharedPreferences prefs = this.getSharedPreferences(this.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE);
currentKeyboardIndex = prefs.getInt(KMManager.KMKey_UserKeyboardIndex, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the pref has an invalid index? Could this ever happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The preference should only save valid indexes.
If the preference key doesn't exist, it'll fallback to 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

fix(android): crash when attempting to resume but no keyboard is set
4 participants