diff --git a/app/src/main/java/com/edw590/visor_c_a/ActivitiesFragments/Tabs/TabCommunicatorMemories.java b/app/src/main/java/com/edw590/visor_c_a/ActivitiesFragments/Tabs/TabCommunicatorMemories.java index fd41dec..04232bb 100644 --- a/app/src/main/java/com/edw590/visor_c_a/ActivitiesFragments/Tabs/TabCommunicatorMemories.java +++ b/app/src/main/java/com/edw590/visor_c_a/ActivitiesFragments/Tabs/TabCommunicatorMemories.java @@ -69,6 +69,8 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved editTxt_memories_text.setHint("Stored memories on the smart LLM"); if (UtilsSWA.isCommunicatorConnectedSERVER()) { editTxt_memories_text.setText(GPTComm.getMemories()); + } else { + editTxt_memories_text.setText("[Not connected to the server to get the memories]"); } AppCompatButton btn_save = new AppCompatButton(requireContext()); diff --git a/app/src/main/java/com/edw590/visor_c_a/ApplicationClass.java b/app/src/main/java/com/edw590/visor_c_a/ApplicationClass.java index 34ead55..d7fb748 100644 --- a/app/src/main/java/com/edw590/visor_c_a/ApplicationClass.java +++ b/app/src/main/java/com/edw590/visor_c_a/ApplicationClass.java @@ -100,15 +100,12 @@ public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable ///////////////////////////////////////////////////////////// - if (!SettingsSync.loadGenSettings(UtilsSettings.readJsonGenSettings())) { + if (!UtilsSettings.loadSettingsFile(false)) { System.out.println("Failed to load generated settings. Using empty ones..."); } - try { - SettingsSync.loadUserSettings(UtilsSettings.readJsonUserSettings()); - } catch (final Exception e) { + if (!UtilsSettings.loadSettingsFile(true)) { System.out.println("Failed to load user settings. Using empty ones..."); - e.printStackTrace(); } infinity_thread.start(); @@ -132,9 +129,9 @@ public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable while (true) { // Write user and gen settings every 5 seconds - UtilsSettings.writeUserSettings(SettingsSync.getJsonUserSettings()); + UtilsSettings.writeSettingsFile(SettingsSync.getJsonUserSettings(), true); - UtilsSettings.writeGenSettings(SettingsSync.getJsonGenSettings()); + UtilsSettings.writeSettingsFile(SettingsSync.getJsonGenSettings(), false); try { Thread.sleep(5000); diff --git a/app/src/main/java/com/edw590/visor_c_a/GlobalUtils/UtilsSettings.java b/app/src/main/java/com/edw590/visor_c_a/GlobalUtils/UtilsSettings.java index 17352a9..e0b3e43 100644 --- a/app/src/main/java/com/edw590/visor_c_a/GlobalUtils/UtilsSettings.java +++ b/app/src/main/java/com/edw590/visor_c_a/GlobalUtils/UtilsSettings.java @@ -25,6 +25,7 @@ import java.nio.charset.Charset; +import SettingsSync.SettingsSync; import UtilsSWA.UtilsSWA; /** @@ -39,60 +40,60 @@ private UtilsSettings() { } /** - *
Gets the Generated Settings in JSON format.
+ *Reads and loads the User and Generated settings from disk.
* - * @return the Generated Settings in JSON format - */ - @NonNull - public static String readJsonGenSettings() { - GPath gen_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH); - gen_settings_path.add2(true, UtilsSWA.GEN_SETTINGS_FILE_CLIENT); - - byte[] file_bytes = UtilsFilesDirs.readFileBytes(gen_settings_path); - - return UtilsSWA.bytesToPrintableDATACONV(file_bytes, false); - } - - /** - *Writes the Device Settings in JSON format.
+ * @param user_settings true if the user settings should be loaded, false if the generated settings should be loaded * - * @param json the Device Settings in JSON format - * - * @return true if the operation completed successfully, false otherwise + * @return true if the settings were read and loaded successfully, false otherwise */ - public static boolean writeGenSettings(@NonNull final String json) { - GPath gen_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH); - gen_settings_path.add2(true, UtilsSWA.GEN_SETTINGS_FILE_CLIENT); - - return UtilsFilesDirs.writeFile(gen_settings_path, json.getBytes(Charset.defaultCharset())) == 0; + public static boolean loadSettingsFile(final boolean user_settings) { + String settings_file_str = user_settings ? UtilsSWA.USER_SETTINGS_FILE : UtilsSWA.GEN_SETTINGS_FILE_CLIENT; + String backup_file_str = settings_file_str + ".bak"; + + GPath settings_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, settings_file_str); + GPath backup_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, backup_file_str); + + byte[] file_bytes = UtilsFilesDirs.readFileBytes(settings_file); + try { + if (user_settings) { + SettingsSync.loadUserSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false)); + } else { + SettingsSync.loadGenSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false)); + } + } catch (final Exception e) { + file_bytes = UtilsFilesDirs.readFileBytes(backup_file); + try { + if (user_settings) { + SettingsSync.loadUserSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false)); + } else { + SettingsSync.loadGenSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false)); + } + } catch (final Exception e2) { + String user_generated = user_settings ? "user" : "generated"; + System.out.println("Failed to load " + user_generated + " settings. Using empty ones..."); + e2.printStackTrace(); + + return false; + } + } + + return true; } /** - *Gets the Device Settings in JSON format.
+ *Writes the User and Generated settings to disk.
* - * @return the Device Settings in JSON format + * @param json the JSON string to write + * @param user_settings true if the user settings should be saved, false if the generated settings should be saved */ - @NonNull - public static String readJsonUserSettings() { - GPath user_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH); - user_settings_path.add2(true, UtilsSWA.USER_SETTINGS_FILE); + public static void writeSettingsFile(@NonNull final String json, final boolean user_settings) { + String settings_file_str = user_settings ? UtilsSWA.USER_SETTINGS_FILE : UtilsSWA.GEN_SETTINGS_FILE_CLIENT; + String backup_file_str = settings_file_str + ".bak"; - byte[] file_bytes = UtilsFilesDirs.readFileBytes(user_settings_path); - - return UtilsSWA.bytesToPrintableDATACONV(file_bytes, false); - } - - /** - *Writes the Device Settings in JSON format.
- * - * @param json the Device Settings in JSON format - * - * @return true if the operation completed successfully, false otherwise - */ - public static boolean writeUserSettings(@NonNull final String json) { - GPath user_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH); - user_settings_path.add2(true, UtilsSWA.USER_SETTINGS_FILE); + GPath settings_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, settings_file_str); + GPath backup_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, backup_file_str); - return UtilsFilesDirs.writeFile(user_settings_path, json.getBytes(Charset.defaultCharset())) == 0; + UtilsFilesDirs.writeFile(settings_file, json.getBytes(Charset.defaultCharset())); + UtilsFilesDirs.writeFile(backup_file, json.getBytes(Charset.defaultCharset())); } }