Skip to content

Commit

Permalink
Merge branch 'master' into feat/developer/11054-improve-linter
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdurdin authored Aug 8, 2024
2 parents 0c6f8d6 + 464c6f9 commit cb7adbf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 58 deletions.
2 changes: 1 addition & 1 deletion developer/src/kmc/test/test-getLastGitCommitDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@
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;

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
Expand Down Expand Up @@ -107,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;
Expand All @@ -123,54 +129,86 @@ public static FVShared getInstance() {
return instance;
}

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);
}
/**
* 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();
JSONParser parser = new JSONParser();
File jsonFile = new File(keyboardsJSONPath);
if (!jsonFile.exists()) {
// Fatal error
throw new Error("keyboards.json file doesn't exist");
}
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) {
KMLog.LogError(TAG, "Unable to parse keyboards.json");
return list;
}

line = reader.readLine();
}
for (int i=0; i<keyboardsArray.length(); i++) {
JSONObject keyboardObj = keyboardsArray.getJSONObject(i);
if (keyboardObj == null) {
throw new Error("keyboard Object in keyboards.json is null");
}
JSONArray languageArray = keyboardObj.getJSONArray("languages");
if (languageArray == null) {
throw new Error("languages Array in keyboards.json is null");
}
String regionName = keyboardObj.getString("region");
FVRegion region = list.findRegion(regionName);
if(region == null) {
region = new FVRegion(regionName);
list.add(region);
}

String kbID = keyboardObj.getString("id");
String kbName = keyboardObj.getString("name");
JSONObject languageObj = languageArray.getJSONObject(0);
String lgID = languageObj.getString("id").toLowerCase(); // Normalize language Id
String lgName = languageObj.getString("name");

// Override European keyboard info
if (kbID.equalsIgnoreCase("sil_euro_latin")) {
kbName = "English";
lgID = "en";
lgName = "English";
} else if (kbID.equalsIgnoreCase("basic_kbdcan")) {
kbName = "Français";
lgID = "fr-ca";
lgName = "French";
}

FVKeyboard kbd = new FVKeyboard(
kbID,
kbName,
kbID, // unused legacy ID
keyboardObj.getString("version"),
lgID,
lgName);

region.keyboards.add(kbd);
}
} catch (Exception e) {
// loadRegionList had malformed keyboard list
throw new Error(e.getMessage());
}

inputStream.close();
} catch (Exception e) {
Log.e("createKeyboardList", "Error: " + e);
// We'll return a malformed list for now in this situation
}
// Sort by region name
Collections.sort(list, (r1, r2) -> r1.name.compareTo(r2.name));

return list;
}
for (int i=0; i<list.size(); i++) {
// Sort by keyboard name in each region
Collections.sort(list.get(i).keyboards, (k1, k2) -> k1.name.compareTo(k2.name));
}
return list;
}

private FVLoadedKeyboardList loadLoadedKeyboardList() {
FVLoadedKeyboardList data = new FVLoadedKeyboardList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 0 additions & 4 deletions oem/firstvoices/android/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb7adbf

Please sign in to comment.