From 4c5b6213d104efba91ecd27628c775ab21bc6fc7 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Mon, 8 Jul 2024 16:26:50 -0500 Subject: [PATCH 1/5] fix(android/engine): Parse keyboards.json for FirstVoices app --- .../com/firstvoices/keyboards/FVShared.java | 127 +++++++++++------- .../firstvoices/keyboards/MainActivity.java | 10 +- oem/firstvoices/android/build.sh | 4 - 3 files changed, 86 insertions(+), 55 deletions(-) diff --git a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java index 95158826ba5..4ae998500a5 100644 --- a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java +++ b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java @@ -7,22 +7,25 @@ import android.net.Uri; import android.util.Log; import android.widget.Toast; +import com.keyman.engine.JSONParser; import com.keyman.engine.KMManager; import com.keyman.engine.data.Keyboard; import com.keyman.engine.packages.PackageProcessor; import com.keyman.engine.util.KMLog; -import java.io.BufferedReader; +import org.json.JSONArray; +import org.json.JSONObject; + 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.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -30,6 +33,8 @@ final class FVShared { private static FVShared instance = null; private boolean isInitialized = false; + // File containing keyboard+region info for each keyboard + private static final String FVKeyboards_JSON = "keyboards.json"; private static final String FVLoadedKeyboardList = "loaded_keyboards.dat"; // Keys from earlier versions of app, used only in the upgrade process @@ -124,53 +129,83 @@ public static FVShared getInstance() { } private FVRegionList loadRegionList() { - FVRegionList list = new FVRegionList(); - try { - // At this point in initialization, fv_all.kmp hasn't been extracted, so - // we get all the keyboard info from keyboards.csv - InputStream inputStream = context.getAssets().open("keyboards.csv"); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - - reader.readLine(); // skip header row - String line = reader.readLine(); - - while (line != null) { - while (line.contains(",,")) - line = line.replace(",,", ", ,"); - - String[] values = line.split(","); - if (values != null && values.length > 0) { - // Read in column info - String kbId = values[1]; - String kbName = values[2]; - String regionName = values[3]; - String legacyId = values[4]; - String version = values[5]; - String lgId = values[6].toLowerCase(); // Normalize language ID - String lgName = values[7]; - - FVRegion region = list.findRegion(regionName); - if(region == null) { - region = new FVRegion(regionName); - list.add(region); - } - - FVKeyboard keyboard = new FVKeyboard(kbId, kbName, legacyId, version, lgId, lgName); - - region.keyboards.add(keyboard); - } + FVRegionList list = new FVRegionList(); + File resourceRoot = new File(getResourceRoot()); + PackageProcessor kmpProcessor = new PackageProcessor(resourceRoot); + JSONParser parser = new JSONParser(); + File jsonFile = new File(getPackagesDir() + FVDefault_PackageID + File.separator + FVKeyboards_JSON); + if (!jsonFile.exists()) { + return list; + } + try { + // At this point in initialization, fv_all.kmp is now extracted, so + // populate keyboard info from keyboards.json + JSONArray keyboardsArray = parser.getJSONObjectFromFile(jsonFile, JSONArray.class); + + if (keyboardsArray == null) { + Log.d("loadRegionList", "unable to load keyboards.json"); + return list; + } - line = reader.readLine(); - } + for (int i=0; i r1.name.compareTo(r2.name)); - return list; - } + for (int i=0; i k1.name.compareTo(k2.name)); + } + return list; + } private FVLoadedKeyboardList loadLoadedKeyboardList() { FVLoadedKeyboardList data = new FVLoadedKeyboardList(); diff --git a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/MainActivity.java b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/MainActivity.java index f91657203bc..d38d5b9bc07 100644 --- a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/MainActivity.java +++ b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/MainActivity.java @@ -49,17 +49,17 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); + if (BuildConfig.DEBUG) { + KMManager.setDebugMode(true); + } + KMManager.initialize(getApplicationContext(), KMManager.KeyboardType.KEYBOARD_TYPE_INAPP); + FVShared.getInstance().initialize(this); FVShared.getInstance().upgradeTo12(); FVShared.getInstance().upgradeTo14(); FVShared.getInstance().preloadPackages(); - if (BuildConfig.DEBUG) { - KMManager.setDebugMode(true); - } - KMManager.initialize(getApplicationContext(), KMManager.KeyboardType.KEYBOARD_TYPE_INAPP); - /** * We need to set the default (fallback) keyboard to sil_euro_latin inside the fv_all package * rather than the normal default of sil_euro_latin inside the sil_euro_latin package. diff --git a/oem/firstvoices/android/build.sh b/oem/firstvoices/android/build.sh index 3e10a79ae66..e27be069747 100755 --- a/oem/firstvoices/android/build.sh +++ b/oem/firstvoices/android/build.sh @@ -61,13 +61,9 @@ if builder_start_action clean; then fi if builder_start_action configure; then - KEYBOARDS_CSV="$KEYMAN_ROOT/oem/firstvoices/keyboards.csv" - KEYBOARDS_CSV_TARGET="$KEYMAN_ROOT/oem/firstvoices/android/app/src/main/assets/keyboards.csv" - KEYBOARD_PACKAGE_ID="fv_all" KEYBOARDS_TARGET="$KEYMAN_ROOT/oem/firstvoices/android/app/src/main/assets/${KEYBOARD_PACKAGE_ID}.kmp" - cp "$KEYBOARDS_CSV" "$KEYBOARDS_CSV_TARGET" downloadKeyboardPackage "$KEYBOARD_PACKAGE_ID" "$KEYBOARDS_TARGET" builder_finish_action success configure From fe758cb441acc150e7606a44f19a7a87419f29a5 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Mon, 5 Aug 2024 09:07:13 +0700 Subject: [PATCH 2/5] change(oem/fv/android): Add keyboard JSON path to loadRegionList() --- .../java/com/firstvoices/keyboards/FVShared.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java index 4ae998500a5..b7d7def3a83 100644 --- a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java +++ b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java @@ -112,7 +112,8 @@ public synchronized void initialize(Context context) { } this.context = context.getApplicationContext(); - this.regionList = loadRegionList(); + String keyboardsJSONPath = getPackagesDir() + FVDefault_PackageID + File.separator + FVKeyboards_JSON; + this.regionList = loadRegionList(keyboardsJSONPath); this.loadedKeyboards = loadLoadedKeyboardList(); isInitialized = true; @@ -128,12 +129,15 @@ public static FVShared getInstance() { return instance; } - private FVRegionList loadRegionList() { + /** + * Parse keyboards.json file to associate region info for each keyboard + * @param keyboardsJSONPath - path to the keyboards.json file containing region info for each keyboard + * @return FVRegionList + */ + private FVRegionList loadRegionList(String keyboardsJSONPath) { FVRegionList list = new FVRegionList(); - File resourceRoot = new File(getResourceRoot()); - PackageProcessor kmpProcessor = new PackageProcessor(resourceRoot); JSONParser parser = new JSONParser(); - File jsonFile = new File(getPackagesDir() + FVDefault_PackageID + File.separator + FVKeyboards_JSON); + File jsonFile = new File(keyboardsJSONPath); if (!jsonFile.exists()) { return list; } From 564308c3c63bf44a5e67e447f59c42b54bffa663 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Mon, 5 Aug 2024 09:07:33 +0700 Subject: [PATCH 3/5] fix(oem/fv/android): Add error logging --- .../java/com/firstvoices/keyboards/FVShared.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java index b7d7def3a83..8eb51aadd13 100644 --- a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java +++ b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java @@ -139,7 +139,8 @@ private FVRegionList loadRegionList(String keyboardsJSONPath) { JSONParser parser = new JSONParser(); File jsonFile = new File(keyboardsJSONPath); if (!jsonFile.exists()) { - return list; + // Fatal error + throw new Error("keyboards.json file doesn't exist"); } try { // At this point in initialization, fv_all.kmp is now extracted, so @@ -147,19 +148,19 @@ private FVRegionList loadRegionList(String keyboardsJSONPath) { JSONArray keyboardsArray = parser.getJSONObjectFromFile(jsonFile, JSONArray.class); if (keyboardsArray == null) { - Log.d("loadRegionList", "unable to load keyboards.json"); + KMLog.LogError(TAG, "Unable to parse keyboards.json"); return list; } for (int i=0; i Date: Tue, 6 Aug 2024 14:19:02 +0700 Subject: [PATCH 4/5] fix(oem/fv/android): Throw errors if keyboards.json can't be read --- .../java/com/firstvoices/keyboards/FVShared.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java index 8eb51aadd13..9b75b3eea95 100644 --- a/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java +++ b/oem/firstvoices/android/app/src/main/java/com/firstvoices/keyboards/FVShared.java @@ -155,13 +155,11 @@ private FVRegionList loadRegionList(String keyboardsJSONPath) { for (int i=0; i Date: Thu, 8 Aug 2024 04:37:05 +0700 Subject: [PATCH 5/5] fix(developer): update date for last git commit date fixture --- developer/src/kmc/test/test-getLastGitCommitDate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/developer/src/kmc/test/test-getLastGitCommitDate.ts b/developer/src/kmc/test/test-getLastGitCommitDate.ts index 90de59f6897..917cfdc7ad0 100644 --- a/developer/src/kmc/test/test-getLastGitCommitDate.ts +++ b/developer/src/kmc/test/test-getLastGitCommitDate.ts @@ -21,6 +21,6 @@ describe('getLastGitCommitDate', function () { // The expected date was manually extracted using the following command, with msec appended: // TZ=UTC git log --date=iso-strict-local -- fixtures/get-last-git-commit-date/README.md // If the fixture modified, then this will also need to be updated. - assert.equal(date, '2024-08-01T00:07:07.000Z'); + assert.equal(date, '2024-06-17T20:43:48.000Z'); }); });