fix(android): Show WebViewError activity on SystemKeyboard exception

This commit is contained in:
Darcy Wong 2025-02-10 15:23:28 +07:00
parent 474801c239
commit 2cb0dadfa9
6 changed files with 117 additions and 5 deletions

View file

@ -57,6 +57,13 @@
android:label="@string/app_name"
android:theme="@style/AppTheme.Light.Dialog" >
</activity>
<activity
android:name="com.keyman.engine.WebViewErrorActivity"
android:exported="true"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoTitleDialog">
</activity>
<!-- Put other WebViewActivities in a separate process so the Keyboard WebView doesn't lag.
Ref https://stackoverflow.com/questions/40650643/timed-out-waiting-on-iinputcontextcallback-with-custom-keyboard-on-android -->

View file

@ -49,7 +49,6 @@ import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
@ -229,6 +228,7 @@ public final class KMManager {
private static boolean debugMode = false;
private static boolean shouldAllowSetKeyboard = true;
private static boolean didCopyAssets = false;
private static boolean didShowWebViewError = false;
private static boolean didLogHardwareKeystrokeException = false;
@ -671,6 +671,16 @@ public final class KMManager {
}
}
private static void showWebViewError() {
Intent i = new Intent(appContext, WebViewErrorActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); // Replaces FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Required to call startActivity() from outside of an Activity context
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
appContext.startActivity(i);
}
/**
* Adjust the keyboard dimensions. If the suggestion banner is active, use the
* combined banner height and keyboard height
@ -703,10 +713,7 @@ public final class KMManager {
try {
SystemKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_SYSTEM);
} catch (AndroidRuntimeException e) {
// Notify fatal error when WebView not installed/enabled
String message = appContext.getString(R.string.body_install_webview);
Toast.makeText(appContext, message,
Toast.LENGTH_LONG).show();
// Catch fatal error when WebView not installed/enabled
return;
}
SystemKeyboardWebViewClient = new KMKeyboardWebViewClient(appContext, keyboardType);
@ -809,6 +816,11 @@ public final class KMManager {
}
public static void onStartInput(EditorInfo attribute, boolean restarting) {
if (!didShowWebViewError && SystemKeyboard == null && WebViewUtils.getSystemWebViewStatus(appContext) !=
SystemWebViewStatus.FULL) {
showWebViewError();
didShowWebViewError = true;
}
if (!restarting && SystemKeyboard != null) {
String packageName = attribute.packageName;
int inputType = attribute.inputType;

View file

@ -0,0 +1,43 @@
/**
* Keyman is copyright (C) SIL Global. MIT License
*/
package com.keyman.engine;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
/**
* Activity to display when WebView is either not installed or enabled.
* Prompts user to switch system keyboard.
*/
public class WebViewErrorActivity extends BaseActivity {
private static final String TAG = "WebViewErrorActivity";
private static LinearLayout linearLayout = null;
private static Button changeIMEButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = this;
setContentView(R.layout.activity_webview_error);
linearLayout = (LinearLayout) findViewById(R.id.webViewErrorLayout);
changeIMEButton = (Button) findViewById(R.id.changeIMEButton);
changeIMEButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
InputMethodManager imManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imManager.showInputMethodPicker();
// Dismiss the View
linearLayout.setVisibility(View.GONE);
//finish(); Dismiss IME picker too fast
}
});
}
}

View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webViewErrorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<!-- Banner to display if WebView is not installed/enabled -->
<TextView
android:id="@+id/kmWebViewError"
android:drawableStart="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginStart="@dimen/fab_margin"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_weight="1"
android:background="@android:color/white"
android:text="@string/body_switch_keyboard"
android:textSize="@dimen/titlebar_label_textsize" />
<Button
android:id="@+id/changeIMEButton"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:layout_marginStart="@dimen/fab_margin"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginTop="@dimen/fab_margin"
android:layout_marginBottom="@dimen/fab_margin"
android:paddingStart="@dimen/fab_padding"
android:paddingEnd="@dimen/fab_padding"
android:background="@android:color/holo_orange_light"
android:elevation="1dp"
android:singleLine="true"
android:text="@string/label_ok"
android:textColor="@android:color/white"/>
</LinearLayout>

View file

@ -139,6 +139,12 @@
<string name="body_install_webview" comment="Notification when WebView not installed">
Keyman requires WebView to be installed."</string>
<!-- ContexT: Alert Dialog body: Switch to another keyboard -->
<string name="body_switch_keyboard" comment="Direct user to select another keyboard">
Keyman requires WebView to be installed and enabled.\n\n
Switch to a different keyboard.
</string>
<!-- Context: Alert Dialog body: System WebView installed but not enabled -->
<string name="body_enable_webview" comment="Notification when WebView installed but not enabled">
Keyman requires WebView to be enabled."</string>

View file

@ -22,6 +22,9 @@
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="AppTheme.NoTitleDialog" parent="AppTheme.Light.Dialog">
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">@android:color/black</item>