refactor(android/engine): Move constants to KMManager

This commit is contained in:
Darcy Wong 2023-09-26 08:06:43 +07:00
parent b46d50fccf
commit 3df2668322
4 changed files with 86 additions and 17 deletions

View file

@ -44,8 +44,6 @@ public class SystemKeyboard extends InputMethodService implements OnKeyboardEven
private static final String TAG = "SystemKeyboard";
private static final String BANNER_THEME_WHITE="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAARCAYAAADDjbwNAAAABHNCSVQICAgIfAhkiAAAACNJREFUOI1j/P///38GOgAmelgyatGoRaMWjVo0atGoRQQAAD1MBB5gNLThAAAAAElFTkSuQmCCBT8AAcEGbxwAAAAASUVORK5CYII=";
/**
* Main initialization of the input method component. Be sure to call
* to super class.
@ -73,7 +71,8 @@ public class SystemKeyboard extends InputMethodService implements OnKeyboardEven
KMManager.SpacebarText spacebarText = KMManager.SpacebarText.fromString(prefs.getString(KeymanSettingsActivity.spacebarTextKey, KMManager.SpacebarText.LANGUAGE_KEYBOARD.toString()));
KMManager.setSpacebarText(spacebarText);
KMManager.setBannerImage(KeyboardType.KEYBOARD_TYPE_SYSTEM, BANNER_THEME_WHITE);
KMManager.setBannerImage(KeyboardType.KEYBOARD_TYPE_SYSTEM, KMManager.KM_BANNER_THEME_WHITE);
KMManager.setBanner(KeyboardType.KEYBOARD_TYPE_SYSTEM, KMManager.BannerType.IMAGE);
boolean mayHaveHapticFeedback = prefs.getBoolean(KeymanSettingsActivity.hapticFeedbackKey, false);
KMManager.setHapticFeedback(mayHaveHapticFeedback);

View file

@ -63,6 +63,15 @@ function showBanner(flag) {
}
}
// Set KMW banner to bannerType:
// @param type 'blank' | 'image' | 'suggestion' - A plain-text string
// representing the type of Banner to set active.
function setBanner(bannerType) {
if (keyman.osk) {
keyman.osk.bannerController.setBanner(bannerType);
}
}
// Set the path to use for the image banner
// path - String starting with "data:image/png;base64,"
function setBannerImage(path) {

View file

@ -87,23 +87,18 @@ final class KMKeyboard extends WebView {
/**
* Banner state value: "blank" - no banner available.
*/
protected static final String KM_BANNER_STATE_BLANK = "blank";
public static final String KM_BANNER_STATE_BLANK = "blank";
/**
* Banner state value: "image" - display an image in the banner
*/
protected static final String KM_BANNER_STATE_IMAGE = "image";
public static final String KM_BANNER_STATE_IMAGE = "image";
/**
* Banner state value: "suggestion" - dictionary suggestions are shown.
*/
protected static final String KM_BANNER_STATE_SUGGESTION = "suggestion";
public static final String KM_BANNER_STATE_SUGGESTION = "suggestion";
// Tiling black background generated from https://elmah.io/tools/base64-image-encoder/
// TODO: Replace with image/css?
protected static final String KM_BANNER_BLANK_IMAGE_PATH =
//"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAARCAIAAABM7ytaAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAVSURBVDhPYxgFo2AUjIJRQDpgYAAABT8AAcEGbxwAAAAASUVORK5CYII=";
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAARCAYAAADDjbwNAAAABHNCSVQICAgIfAhkiAAAACNJREFUOI1j9PnP8J+BDoCJHpaMWjRq0ahFoxaNWjRqEQEAAG4mAmxMRRQ1AAAAAElFTkSuQmCCBT8AAcEGbxwAAAAASUVORK5CYII=";
/**
* Current banner state.
*/
@ -118,7 +113,7 @@ final class KMKeyboard extends WebView {
// Stores the current image for use by the Banner
// when predictive text is not active
private String bannerImgPath;
protected String bannerImgPath;
// Facilitates a 'lazy init' - we'll only check the preference when it matters,
// rather than at construction time.
@ -676,6 +671,11 @@ final class KMKeyboard extends WebView {
loadJavascript(jsString);
}
public void setBanner(String bannerType) {
String jsString = KMString.format("setBanner(%s)", bannerType);
loadJavascript(jsString);
}
public void setBannerImage(String path) {
this.bannerImgPath = path; // Save the path in case delayed initialization is needed
String logString = "";

View file

@ -157,6 +157,31 @@ public final class KMManager {
}
};
public enum BannerType {
BLANK,
IMAGE,
SUGGESTION;
// Maps to enum BannerType in bannerView.ts
public static BannerType fromString(String mode) {
if (mode == null) return BLANK;
switch (mode) {
case "BLANK":
return BLANK;
case "image":
return IMAGE;
case "suggestion":
return SUGGESTION;
}
return BLANK;
}
public String toString() {
String modes[] = { "blank", "image", "suggestion"};
return modes[this.ordinal()];
}
}
protected static InputMethodService IMService;
private static boolean debugMode = false;
@ -175,6 +200,19 @@ public final class KMManager {
private static KMManager.SpacebarText spacebarText = KMManager.SpacebarText.LANGUAGE_KEYBOARD; // must match default given in kmwbase.ts
// Tiling colored background generated from https://elmah.io/tools/base64-image-encoder/
// TODO: Replace with image/css?
public static final String KM_BANNER_THEME_BLACK =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAARCAIAAABM7ytaAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAVSURBVDhPYxgFo2AUjIJRQDpgYAAABT8AAcEGbxwAAAAASUVORK5CYII=";
public static final String KM_BANNER_THEME_WHITE =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAARCAYAAADDjbwNAAAABHNCSVQICAgIfAhkiAAAACNJREFUOI1j/P///38GOgAmelgyatGoRaMWjVo0atGoRQQAAD1MBB5gNLThAAAAAElFTkSuQmCCBT8AAcEGbxwAAAAASUVORK5CYII=";
// Green banner for testing as a contrast to the Android styling.
// TODO: remove this after testing.
public static final String KM_BANNER_THEME_GREEN =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAARCAYAAADDjbwNAAAABHNCSVQICAgIfAhkiAAAACNJREFUOI1j9PnP8J+BDoCJHpaMWjRq0ahFoxaNWjRqEQEAAG4mAmxMRRQ1AAAAAElFTkSuQmCCBT8AAcEGbxwAAAAASUVORK5CYII=";
protected static KMKeyboard InAppKeyboard = null;
protected static KMKeyboard SystemKeyboard = null;
protected static KMKeyboardWebViewClient InAppKeyboardWebViewClient = null;
@ -612,13 +650,13 @@ public final class KMManager {
if (keyboardType == KeyboardType.KEYBOARD_TYPE_INAPP && InAppKeyboard == null) {
InAppKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_INAPP);
InAppKeyboardWebViewClient = new KMKeyboardWebViewClient(appContext, keyboardType);
InAppKeyboard.setBannerImage(InAppKeyboard.KM_BANNER_BLANK_IMAGE_PATH);
InAppKeyboard.setBannerImage(KM_BANNER_THEME_GREEN);
keyboard = InAppKeyboard;
webViewClient = InAppKeyboardWebViewClient;
} else if (keyboardType == KeyboardType.KEYBOARD_TYPE_SYSTEM && SystemKeyboard == null) {
SystemKeyboard = new KMKeyboard(appContext, KeyboardType.KEYBOARD_TYPE_SYSTEM);
SystemKeyboardWebViewClient = new KMKeyboardWebViewClient(appContext, keyboardType);
SystemKeyboard.setBannerImage(SystemKeyboard.KM_BANNER_BLANK_IMAGE_PATH);
SystemKeyboard.setBannerImage(KM_BANNER_THEME_WHITE);
keyboard = SystemKeyboard;
webViewClient = SystemKeyboardWebViewClient;
}
@ -1391,10 +1429,33 @@ public final class KMManager {
return true;
}
public static boolean setBannerImage(KeyboardType kType, String path) {
if (kType == KeyboardType.KEYBOARD_TYPE_INAPP) {
/**
* Update KMW banner type
* @param {KeyboardType} keyboard
* @param {BannerType} bannerType
* @return status
*/
public static boolean setBanner(KeyboardType keyboard, BannerType bannerType) {
if (keyboard == KeyboardType.KEYBOARD_TYPE_INAPP) {
InAppKeyboard.setBanner(bannerType.toString());
} else if (keyboard == KeyboardType.KEYBOARD_TYPE_SYSTEM) {
SystemKeyboard.setBanner(bannerType.toString());
} else {
return false;
}
return true;
}
/**
* Set the path to use with the image banner
* @param {KeyboardType} keyboard
* @param {String} path
* @return
*/
public static boolean setBannerImage(KeyboardType keyboard, String path) {
if (keyboard == KeyboardType.KEYBOARD_TYPE_INAPP) {
InAppKeyboard.setBannerImage(path);
} else if (kType == KeyboardType.KEYBOARD_TYPE_SYSTEM) {
} else if (keyboard == KeyboardType.KEYBOARD_TYPE_SYSTEM) {
SystemKeyboard.setBannerImage(path);
} else {
return false;