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): Catch when WebView provider unavailable #13101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.AndroidRuntimeException;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
Expand Down Expand Up @@ -110,7 +111,14 @@ final class KMKeyboard extends WebView {

public String specialOskFont = "";

public KMKeyboard(Context context) {
/**
* Constructor for KMKeyboard.
* If System WebView not installed/enabled, will throw AndroidRuntimeException.
* Can't catch the exception here because super(context) must be the first line.
* @param context
* @throws AndroidRuntimeException
*/
public KMKeyboard(Context context) throws AndroidRuntimeException {
super(context);
this.context = context;
this.keyboardType = KeyboardType.KEYBOARD_TYPE_INAPP;
Expand Down
30 changes: 28 additions & 2 deletions android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.os.Build;
import android.os.IBinder;
import android.text.InputType;
import android.util.AndroidRuntimeException;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
Expand All @@ -48,6 +49,7 @@
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

Expand Down Expand Up @@ -687,12 +689,22 @@ private static void initKeyboard(Context appContext, KeyboardType keyboardType)
KMKeyboardWebViewClient webViewClient = null;

if (keyboardType == KeyboardType.KEYBOARD_TYPE_INAPP && InAppKeyboard == null) {
InAppKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_INAPP);
try {
InAppKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_INAPP);
} catch (AndroidRuntimeException e) {
// Fatal error: Notify WebView not installed/enabled and exit Keyman
notifyWebViewFailed(appContext);
}
InAppKeyboardWebViewClient = new KMKeyboardWebViewClient(appContext, keyboardType);
keyboard = InAppKeyboard;
webViewClient = InAppKeyboardWebViewClient;
} else if (keyboardType == KeyboardType.KEYBOARD_TYPE_SYSTEM && SystemKeyboard == null) {
SystemKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_SYSTEM);
try {
SystemKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_SYSTEM);
} catch (AndroidRuntimeException e) {
// Fatal error: Notify WebView not installed/enabled and exit Keyman
notifyWebViewFailed(appContext);
}
SystemKeyboardWebViewClient = new KMKeyboardWebViewClient(appContext, keyboardType);
keyboard = SystemKeyboard;
webViewClient = SystemKeyboardWebViewClient;
Expand Down Expand Up @@ -726,6 +738,20 @@ private static void initKeyboard(Context appContext, KeyboardType keyboardType)
setEngineWebViewVersionStatus(appContext, keyboard);
}

private static void notifyWebViewFailed(Context appContext) {
KMLog.LogInfo(TAG, "System WebView not installed/enabled");
Toast.makeText(appContext, R.string.webview_failed_text, Toast.LENGTH_LONG).show();
try {
Thread.sleep(3500);
} catch (InterruptedException e) {
KMLog.LogException(TAG, "Timer exception", e);
}

// Exit Keyman since WebView failed
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}

public static String getLanguagePredictionPreferenceKey(String langID) {
return langID + predictionPrefSuffix;
}
Expand Down
9 changes: 7 additions & 2 deletions android/KMEA/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@
<string name="fatal_keyboard_error" comment="Fatal keyboard error (keyboard ID::packageID for language). Will load default keyboard">
Fatal keyboard error with %1$s:%2$s for %3$s language. Loading default keyboard.</string>

<!-- Context: Fatal keyboard error. -->
<string name="fatal_keyboard_error_short" comment="Error in keyboard (keyboard ID::packageID for language).">
<!-- Context: Fatal keyboard error. -->
<string name="fatal_keyboard_error_short" comment="Error in keyboard (keyboard ID::packageID for language).">
Error in keyboard %1$s:%2$s for %3$s language.</string>

<!-- Context: Fatal keyboard error. System WebView not installed/enabled -->
<string name="webview_failed_text" comment="Notification when WebView failed because it's uninstalled or not enabled">
Fatal Error: WebView must be installed or enabled. Exiting"</string>

<!-- Context: Query for associated dictionary -->
<string name="query_associated_model" comment="Check if there's an available dictionary to download">Checking for associated dictionary to download</string>

Expand Down Expand Up @@ -278,4 +282,5 @@

<!-- Context: anywhere -->
<string name="unable_to_open_browser" comment="Notification when a browser activity cannot be launched">Unable to launch web browser</string>

</resources>