Skip to content

Commit

Permalink
Backup settings files now in place and in use
Browse files Browse the repository at this point in the history
No more settings deleted by power failures or forced shutdowns or
whatever.
  • Loading branch information
Edw590 committed Nov 10, 2024
1 parent 05d25d7 commit 35b4b34
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
11 changes: 4 additions & 7 deletions app/src/main/java/com/edw590/visor_c_a/ApplicationClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.nio.charset.Charset;

import SettingsSync.SettingsSync;
import UtilsSWA.UtilsSWA;

/**
Expand All @@ -39,60 +40,60 @@ private UtilsSettings() {
}

/**
* <p>Gets the Generated Settings in JSON format.</p>
* <p>Reads and loads the User and Generated settings from disk.</p>
*
* @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);
}

/**
* <p>Writes the Device Settings in JSON format.</p>
* @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;
}

/**
* <p>Gets the Device Settings in JSON format.</p>
* <p>Writes the User and Generated settings to disk.</p>
*
* @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);
}

/**
* <p>Writes the Device Settings in JSON format.</p>
*
* @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()));
}
}

0 comments on commit 35b4b34

Please sign in to comment.