1- /**
2- * Copyright (C) 2017-2018 SIL International. All rights reserved .
1+ /*
2+ * Keyman is copyright (C) SIL Global. MIT License .
33 */
44
55package com .keyman .engine ;
2323import com .keyman .engine .util .FileUtils ;
2424import com .keyman .engine .util .KMLog ;
2525import com .keyman .engine .util .KMString ;
26+ import com .keyman .engine .util .WebViewUtils ;
2627
2728import android .annotation .SuppressLint ;
2829import android .content .Context ;
@@ -85,7 +86,8 @@ final class KMKeyboard extends WebView {
8586
8687 private static String txtFont = "" ;
8788 private static String oskFont = null ;
88- private static String keyboardRoot = "" ;
89+ private static String dataRoot = "" ;
90+ private static String packageRoot = "" ;
8991 private final String fontUndefined = "undefined" ;
9092 private GestureDetector gestureDetector ;
9193 private static ArrayList <OnKeyboardEventListener > kbEventListeners = null ;
@@ -366,8 +368,7 @@ public void loadKeyboard() {
366368 } else {
367369 KMManager .SystemKeyboardWebViewClient .setKeyboardLoaded (false );
368370 }
369-
370- String htmlPath = "file://" + getContext ().getDir ("data" , Context .MODE_PRIVATE ) + "/" + KMManager .KMFilename_KeyboardHtml ;
371+ String htmlPath = WebViewUtils .buildAssetUrl (KMManager .KMFilename_KeyboardHtml );
371372 loadUrl (htmlPath );
372373 setBackgroundColor (0 );
373374 }
@@ -561,6 +562,7 @@ public static String oskFontFilename() {
561562 return oskFont ;
562563 }
563564
565+ // REVIEW: this method seems to be unused
564566 /**
565567 * Return the full path to the special OSK font,
566568 * which is with all the keyboard assets at the root app_data folder
@@ -646,7 +648,7 @@ public boolean prepareKeyboardSwitch(String packageID, String keyboardID, String
646648 }
647649 String kbKey = KMString .format ("%s_%s" , languageID , keyboardID );
648650
649- setKeyboardRoot (packageID );
651+ setPackageRoot (packageID );
650652
651653 // Escape single-quoted names for javascript call
652654 keyboardName = keyboardName .replaceAll ("\' " , "\\ \\ '" ); // Double-escaped-backslash b/c regex.
@@ -702,28 +704,28 @@ public boolean setKeyboard(String packageID, String keyboardID, String languageI
702704 KMManager .getLatestKeyboardFileVersion (getContext (), packageID , keyboardID ) : null ;
703705 }
704706
705- setKeyboardRoot (packageID );
707+ setPackageRoot (packageID );
706708
707709 if (kOskFont == null || kOskFont .isEmpty ())
708710 kOskFont = kFont ;
709711
710- JSONObject jDisplayFont = makeFontPaths (kFont );
711- JSONObject jOskFont = makeFontPaths (kOskFont );
712+ JSONObject jDisplayFont = makeFontObject (kFont );
713+ JSONObject jOskFont = makeFontObject (kOskFont );
712714
713715 txtFont = getFontFilename (jDisplayFont );
714716 oskFont = getFontFilename (jOskFont );
715717
716718 String kbKey = KMString .format ("%s_%s" , languageID , keyboardID );
717719
718- String keyboardPath = makeKeyboardPath (packageID , keyboardID , keyboardVersion );
720+ String keyboardUrl = makeKeyboardUrl (packageID , keyboardID , keyboardVersion );
719721
720722 JSONObject reg = new JSONObject ();
721723 try {
722724 reg .put ("KN" , keyboardName );
723725 reg .put ("KI" , "Keyboard_" + keyboardID );
724726 reg .put ("KLC" , languageID );
725727 reg .put ("KL" , languageName );
726- reg .put ("KF" , keyboardPath );
728+ reg .put ("KF" , keyboardUrl );
727729 reg .put ("KP" , packageID );
728730
729731 if (jDisplayFont != null ) reg .put ("KFont" , jDisplayFont );
@@ -809,28 +811,31 @@ private void sendError(String packageID, String keyboardID, String languageID, b
809811 }
810812
811813 // Set the base path of the keyboard depending on the package ID
812- private void setKeyboardRoot (String packageID ) {
814+ private void setPackageRoot (String packageID ) {
815+ this .dataRoot = WebViewUtils .buildAssetUrl ("" );
813816 if (packageID .equals (KMManager .KMDefault_UndefinedPackageID )) {
814- this .keyboardRoot = (context .getDir ("data" , Context .MODE_PRIVATE ).toString () +
815- File .separator + KMManager .KMDefault_UndefinedPackageID + File .separator );
817+ this .packageRoot = this .dataRoot + KMManager .KMDefault_UndefinedPackageID + "/" ;
816818 } else {
817- this .keyboardRoot = (context .getDir ("data" , Context .MODE_PRIVATE ).toString () +
818- File .separator + KMManager .KMDefault_AssetPackages + File .separator + packageID + File .separator );
819+ this .packageRoot = this .dataRoot + KMManager .KMDefault_AssetPackages + "/" + packageID + "/" ;
819820 }
820821 }
821822
822- public String getKeyboardRoot () {
823- return this .keyboardRoot ;
823+ private String getDataRoot () {
824+ return this .dataRoot ;
825+ }
826+
827+ private String getPackageRoot () {
828+ return this .packageRoot ;
824829 }
825830
826- private String makeKeyboardPath (String packageID , String keyboardID , String keyboardVersion ) {
827- String keyboardPath ;
831+ private String makeKeyboardUrl (String packageID , String keyboardID , String keyboardVersion ) {
832+ String keyboardUrl = getPackageRoot () ;
828833 if (packageID .equals (KMManager .KMDefault_UndefinedPackageID )) {
829- keyboardPath = getKeyboardRoot () + keyboardID + "-" + keyboardVersion + ".js" ;
834+ keyboardUrl += keyboardID + "-" + keyboardVersion + ".js" ;
830835 } else {
831- keyboardPath = getKeyboardRoot () + keyboardID + ".js" ;
836+ keyboardUrl += keyboardID + ".js" ;
832837 }
833- return keyboardPath ;
838+ return keyboardUrl ;
834839 }
835840
836841 private void sendKMWError (int lineNumber , String sourceId , String message ) {
@@ -1042,13 +1047,26 @@ public void onDismiss() {
10421047 }
10431048
10441049 /**
1045- * Take a font JSON object and adjust to pass to JS
1046- * 1. Replace "source" keys for "files" keys
1047- * 2. Create full font paths for .ttf or .svg
1048- * @param font String font JSON object as a string
1049- * @return JSONObject of modified font information with full paths. If font is invalid, return `null`
1050+ * Create a JSON object consisting of the font family and the URLs of the
1051+ * font files on the local device.
1052+ *
1053+ * The `font` parameter can either be the filename of the font (with an
1054+ * extension recognized as font), or a Font object or JSON string.
1055+ * In the former case a new JSON object is created with the font family
1056+ * derived from the filename, and the font filename prefixed with path
1057+ * to the fonts.
1058+ * In the latter case the legacy `sources` key is renamed to `files`.
1059+ * If `files` is a single string it will be prefixed with the path to the
1060+ * fonts. If `files` is an array, the array is iterated until finding
1061+ * the first file with a font extension which is then prefixed with the
1062+ * path to the fonts.
1063+ *
1064+ * @param font A string containing either the font filename or a font JSON
1065+ * object as a string
1066+ * @return JSONObject of modified font information with full paths. If font
1067+ * is invalid, return `null`.
10501068 */
1051- private JSONObject makeFontPaths (String font ) {
1069+ private JSONObject makeFontObject (String font ) {
10521070
10531071 if (font == null || font .equals ("" )) {
10541072 return null ;
@@ -1059,14 +1077,13 @@ private JSONObject makeFontPaths(String font) {
10591077 JSONObject jfont = new JSONObject ();
10601078 jfont .put (KMManager .KMKey_FontFamily , font .substring (0 , font .length ()-4 ));
10611079 JSONArray jfiles = new JSONArray ();
1062- jfiles .put (keyboardRoot + font );
1080+ String fontRoot = KMManager .isDefaultFont (font ) ? getDataRoot () : getPackageRoot ();
1081+ jfiles .put (fontRoot + font );
10631082 jfont .put (KMManager .KMKey_FontFiles , jfiles );
10641083 return jfont ;
10651084 }
10661085
10671086 JSONObject fontObj = new JSONObject (font );
1068- JSONArray sourceArray ;
1069- String fontFile ;
10701087
10711088 // Replace "sources" key with "files"
10721089 if (fontObj .has (KMManager .KMKey_FontSource )) {
@@ -1076,16 +1093,18 @@ private JSONObject makeFontPaths(String font) {
10761093
10771094 Object obj = fontObj .get (KMManager .KMKey_FontFiles );
10781095 if (obj instanceof String ) {
1079- fontFile = fontObj .getString (KMManager .KMKey_FontFiles );
1080- fontObj .put (KMManager .KMKey_FontFiles , keyboardRoot + obj );
1096+ String fontFile = fontObj .getString (KMManager .KMKey_FontFiles );
1097+ String fontRoot = KMManager .isDefaultFont (fontFile ) ? getDataRoot () : getPackageRoot ();
1098+ fontObj .put (KMManager .KMKey_FontFiles , fontRoot + obj );
10811099 return fontObj ;
10821100 } else if (obj instanceof JSONArray ) {
1083- sourceArray = fontObj .optJSONArray (KMManager .KMKey_FontFiles );
1101+ JSONArray sourceArray = fontObj .optJSONArray (KMManager .KMKey_FontFiles );
10841102 if (sourceArray != null ) {
10851103 for (int i = 0 ; i < sourceArray .length (); i ++) {
1086- fontFile = sourceArray .getString (i );
1104+ String fontFile = sourceArray .getString (i );
10871105 if (FileUtils .hasFontExtension (fontFile )) {
1088- fontObj .put (KMManager .KMKey_FontFiles , keyboardRoot + fontFile );
1106+ String fontRoot = KMManager .isDefaultFont (fontFile ) ? getDataRoot () : getPackageRoot ();
1107+ fontObj .put (KMManager .KMKey_FontFiles , fontRoot + fontFile );
10891108 fontObj .remove (KMManager .KMKey_FontSource );
10901109 return fontObj ;
10911110 }
@@ -1094,7 +1113,6 @@ private JSONObject makeFontPaths(String font) {
10941113 }
10951114 } catch (JSONException e ) {
10961115 KMLog .LogException (TAG , "Failed to make font for '" +font +"'" , e );
1097- return null ;
10981116 }
10991117
11001118 return null ;
0 commit comments