Skip to content

Commit

Permalink
refactor(android/engine): Pass contents of banner.html to KMW
Browse files Browse the repository at this point in the history
  • Loading branch information
darcywong00 committed Nov 13, 2023
1 parent 8961e9d commit 6170e04
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion android/KMAPro/kMAPro/src/main/assets/svg/banner.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Custom banner theme -->
<div style="background: white; width: 100%; height: 100%; position: absolute; left: 0; top: 0">
<!--<div style="height: 1px; left: 0; position: absolute; top: 0; width: 100%; background: black"></div>-->
<img src="keyman_banner.svg" style="height:100%; background: white; display: block">
<img src="$BANNER" style="height:100%; background: white; display: block">

<!-- Tri-color line -->
<div style="height: 5%; left: 0; position: absolute; bottom: 0; width: 56%; background: #F68924"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class SystemKeyboard extends InputMethodService implements OnKeyboardEven
private KMHardwareKeyboardInterpreter interpreter = null;

// Paths relative to assets folder for banner themes
public static final String KM_BANNER_THEME_KEYMAN = "svg/keyman_banner.svg";

public static final String KM_BANNER_THEME_KEYMAN = "svg/banner.html";
public static final String KM_BANNER_THEME_KEYMAN_SVG = "svg/keyman_banner.svg";
private static final String TAG = "SystemKeyboard";

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ public void onCreate() {
KMManager.SpacebarText spacebarText = KMManager.SpacebarText.fromString(prefs.getString(KeymanSettingsActivity.spacebarTextKey, KMManager.SpacebarText.LANGUAGE_KEYBOARD.toString()));
KMManager.setSpacebarText(spacebarText);

KMManager.setBannerImage(KeyboardType.KEYBOARD_TYPE_SYSTEM, KM_BANNER_THEME_KEYMAN);
KMManager.setBannerImage(KeyboardType.KEYBOARD_TYPE_SYSTEM, KM_BANNER_THEME_KEYMAN, KM_BANNER_THEME_KEYMAN_SVG);
KMManager.setBanner(KeyboardType.KEYBOARD_TYPE_SYSTEM, KMManager.BannerType.IMAGE);

boolean mayHaveHapticFeedback = prefs.getBoolean(KeymanSettingsActivity.hapticFeedbackKey, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public void onKeyboardChanged(String newKeyboard) {
@Override
public void onKeyboardShown() {
// Set the in-app banner image here. Doesn't work in KMManager.init
KMManager.setBannerImage(KeyboardType.KEYBOARD_TYPE_INAPP, KMManager.KM_BANNER_THEME_WHITE);
KMManager.setBannerImage(KeyboardType.KEYBOARD_TYPE_INAPP, "svg/banner.html", KMManager.KM_BANNER_THEME_WHITE);
KMManager.setBanner(KeyboardType.KEYBOARD_TYPE_INAPP, KMManager.BannerType.IMAGE);
resizeTextView(true);
}
Expand Down
4 changes: 2 additions & 2 deletions android/KMEA/app/src/main/assets/keyboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<script src="sentry.min.js"></script>
<script src="keyman-sentry.js"></script>
<script src="android-host.js"></script>
<style type="text/css">
<!--style type="text/css">
body {background-color:#000000;}
</style>
<link rel="stylesheet" type="text/css" rel="noopener" target="_blank" href="imagebanner.css"></link>
<link rel="stylesheet" type="text/css" rel="noopener" target="_blank" href="imagebanner.css"></link-->
</head>
<body class="kmw-embedded keyman-app">
</body>
Expand Down
19 changes: 14 additions & 5 deletions android/KMEA/app/src/main/java/com/keyman/engine/KMKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,16 +663,25 @@ public String getBannerImage() {
return this.bannerImagePath;
}

public void setBannerImage(String path) {
this.bannerImagePath = path; // Save the path in case delayed initialization is needed
public void setBannerImage(String htmlPath, String svgPath) {
// Read the banner html contents
String contents = FileUtils.readContents(context, htmlPath);

// If $BANNER string exists, replace with actual path
File bannerPath = new File(KMManager.getResourceRoot(), svgPath);
if (bannerPath.exists()) {
contents = contents.replace("$BANNER", bannerPath.getAbsolutePath());
}

this.bannerImagePath = contents; // Save the path in case delayed initialization is needed
String logString = "";
if (path != null && path.contains("base64") || path.length() > 256) {
if (contents != null && contents.contains("base64") || contents.length() > 256) {
logString = "<base64 image>";
} else {
logString = path;
logString = contents;
}
KMLog.LogInfo(TAG, KMString.format("Banner image path: (%s).", logString));
String jsString = KMString.format("setBannerImage('%s')", path);
String jsString = KMString.format("setBannerImage('%s')", contents);
loadJavascript(jsString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ private static void copyAssets(Context context) {
copyAsset(context, KMFilename_JSPolyfill, "", true);

// Copy image banner css
copyAsset(context, KMFilename_ImageBannerCss, "", true);
//copyAsset(context, KMFilename_ImageBannerCss, "", true);

// SVG directory for banner themes
File svgDir = new File(getSVGDir());
Expand Down Expand Up @@ -1463,11 +1463,11 @@ public static boolean setBanner(KeyboardType keyboard, BannerType bannerType) {
* @param {String} path
* @return
*/
public static boolean setBannerImage(KeyboardType keyboard, String path) {
public static boolean setBannerImage(KeyboardType keyboard, String htmlPath, String svgPath) {
if (keyboard == KeyboardType.KEYBOARD_TYPE_INAPP && InAppKeyboard != null) {
InAppKeyboard.setBannerImage(path);
InAppKeyboard.setBannerImage(htmlPath, svgPath);
} else if (keyboard == KeyboardType.KEYBOARD_TYPE_SYSTEM && SystemKeyboard != null) {
SystemKeyboard.setBannerImage(path);
SystemKeyboard.setBannerImage(htmlPath, svgPath);
} else {
Log.d(TAG, "setBannerImage but keyboard is null");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
package com.keyman.engine.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.keyman.engine.KMManager;

import org.json.JSONObject;
import org.json.JSONArray;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -248,6 +254,35 @@ public static boolean saveList(File filepath, Object obj) {
return result;
}

/**
* Read the contents of asset file as a string
* Reference: https://stackoverflow.com/questions/16110002/read-assets-file-as-string
* @param context
* @param path - path of file relative to assets folder
* @return String
*/
public static String readContents(Context context, String path) {
StringBuilder sb = new StringBuilder();
String str = "";
AssetManager assetManager = context.getAssets();
try {
InputStream inputStream = assetManager.open(path);
if (inputStream == null) {
KMLog.LogInfo(TAG, "Unable to read contents of asset: " + path);
return str;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
while ((str = reader.readLine()) != null) {
sb.append(str);
}
reader.close();
} catch (Exception e) {
KMLog.LogException(TAG, "Error reading asset file", e);
return str;
}
return sb.toString();
}

/**
* Utility to parse a URL and extract the filename
* @param urlStr String
Expand Down

0 comments on commit 6170e04

Please sign in to comment.