Skip to content

Commit 35b4b34

Browse files
committed
Backup settings files now in place and in use
No more settings deleted by power failures or forced shutdowns or whatever.
1 parent 05d25d7 commit 35b4b34

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

app/src/main/java/com/edw590/visor_c_a/ActivitiesFragments/Tabs/TabCommunicatorMemories.java

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
6969
editTxt_memories_text.setHint("Stored memories on the smart LLM");
7070
if (UtilsSWA.isCommunicatorConnectedSERVER()) {
7171
editTxt_memories_text.setText(GPTComm.getMemories());
72+
} else {
73+
editTxt_memories_text.setText("[Not connected to the server to get the memories]");
7274
}
7375

7476
AppCompatButton btn_save = new AppCompatButton(requireContext());

app/src/main/java/com/edw590/visor_c_a/ApplicationClass.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable
100100

101101
/////////////////////////////////////////////////////////////
102102

103-
if (!SettingsSync.loadGenSettings(UtilsSettings.readJsonGenSettings())) {
103+
if (!UtilsSettings.loadSettingsFile(false)) {
104104
System.out.println("Failed to load generated settings. Using empty ones...");
105105
}
106106

107-
try {
108-
SettingsSync.loadUserSettings(UtilsSettings.readJsonUserSettings());
109-
} catch (final Exception e) {
107+
if (!UtilsSettings.loadSettingsFile(true)) {
110108
System.out.println("Failed to load user settings. Using empty ones...");
111-
e.printStackTrace();
112109
}
113110

114111
infinity_thread.start();
@@ -132,9 +129,9 @@ public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable
132129
while (true) {
133130
// Write user and gen settings every 5 seconds
134131

135-
UtilsSettings.writeUserSettings(SettingsSync.getJsonUserSettings());
132+
UtilsSettings.writeSettingsFile(SettingsSync.getJsonUserSettings(), true);
136133

137-
UtilsSettings.writeGenSettings(SettingsSync.getJsonGenSettings());
134+
UtilsSettings.writeSettingsFile(SettingsSync.getJsonGenSettings(), false);
138135

139136
try {
140137
Thread.sleep(5000);

app/src/main/java/com/edw590/visor_c_a/GlobalUtils/UtilsSettings.java

+46-45
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.nio.charset.Charset;
2727

28+
import SettingsSync.SettingsSync;
2829
import UtilsSWA.UtilsSWA;
2930

3031
/**
@@ -39,60 +40,60 @@ private UtilsSettings() {
3940
}
4041

4142
/**
42-
* <p>Gets the Generated Settings in JSON format.</p>
43+
* <p>Reads and loads the User and Generated settings from disk.</p>
4344
*
44-
* @return the Generated Settings in JSON format
45-
*/
46-
@NonNull
47-
public static String readJsonGenSettings() {
48-
GPath gen_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH);
49-
gen_settings_path.add2(true, UtilsSWA.GEN_SETTINGS_FILE_CLIENT);
50-
51-
byte[] file_bytes = UtilsFilesDirs.readFileBytes(gen_settings_path);
52-
53-
return UtilsSWA.bytesToPrintableDATACONV(file_bytes, false);
54-
}
55-
56-
/**
57-
* <p>Writes the Device Settings in JSON format.</p>
45+
* @param user_settings true if the user settings should be loaded, false if the generated settings should be loaded
5846
*
59-
* @param json the Device Settings in JSON format
60-
*
61-
* @return true if the operation completed successfully, false otherwise
47+
* @return true if the settings were read and loaded successfully, false otherwise
6248
*/
63-
public static boolean writeGenSettings(@NonNull final String json) {
64-
GPath gen_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH);
65-
gen_settings_path.add2(true, UtilsSWA.GEN_SETTINGS_FILE_CLIENT);
66-
67-
return UtilsFilesDirs.writeFile(gen_settings_path, json.getBytes(Charset.defaultCharset())) == 0;
49+
public static boolean loadSettingsFile(final boolean user_settings) {
50+
String settings_file_str = user_settings ? UtilsSWA.USER_SETTINGS_FILE : UtilsSWA.GEN_SETTINGS_FILE_CLIENT;
51+
String backup_file_str = settings_file_str + ".bak";
52+
53+
GPath settings_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, settings_file_str);
54+
GPath backup_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, backup_file_str);
55+
56+
byte[] file_bytes = UtilsFilesDirs.readFileBytes(settings_file);
57+
try {
58+
if (user_settings) {
59+
SettingsSync.loadUserSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false));
60+
} else {
61+
SettingsSync.loadGenSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false));
62+
}
63+
} catch (final Exception e) {
64+
file_bytes = UtilsFilesDirs.readFileBytes(backup_file);
65+
try {
66+
if (user_settings) {
67+
SettingsSync.loadUserSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false));
68+
} else {
69+
SettingsSync.loadGenSettings(UtilsSWA.bytesToPrintableDATACONV(file_bytes, false));
70+
}
71+
} catch (final Exception e2) {
72+
String user_generated = user_settings ? "user" : "generated";
73+
System.out.println("Failed to load " + user_generated + " settings. Using empty ones...");
74+
e2.printStackTrace();
75+
76+
return false;
77+
}
78+
}
79+
80+
return true;
6881
}
6982

7083
/**
71-
* <p>Gets the Device Settings in JSON format.</p>
84+
* <p>Writes the User and Generated settings to disk.</p>
7285
*
73-
* @return the Device Settings in JSON format
86+
* @param json the JSON string to write
87+
* @param user_settings true if the user settings should be saved, false if the generated settings should be saved
7488
*/
75-
@NonNull
76-
public static String readJsonUserSettings() {
77-
GPath user_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH);
78-
user_settings_path.add2(true, UtilsSWA.USER_SETTINGS_FILE);
89+
public static void writeSettingsFile(@NonNull final String json, final boolean user_settings) {
90+
String settings_file_str = user_settings ? UtilsSWA.USER_SETTINGS_FILE : UtilsSWA.GEN_SETTINGS_FILE_CLIENT;
91+
String backup_file_str = settings_file_str + ".bak";
7992

80-
byte[] file_bytes = UtilsFilesDirs.readFileBytes(user_settings_path);
81-
82-
return UtilsSWA.bytesToPrintableDATACONV(file_bytes, false);
83-
}
84-
85-
/**
86-
* <p>Writes the Device Settings in JSON format.</p>
87-
*
88-
* @param json the Device Settings in JSON format
89-
*
90-
* @return true if the operation completed successfully, false otherwise
91-
*/
92-
public static boolean writeUserSettings(@NonNull final String json) {
93-
GPath user_settings_path = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH);
94-
user_settings_path.add2(true, UtilsSWA.USER_SETTINGS_FILE);
93+
GPath settings_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, settings_file_str);
94+
GPath backup_file = new GPath(true, GL_CONSTS.VISOR_EXT_FOLDER_PATH).add2(false, backup_file_str);
9595

96-
return UtilsFilesDirs.writeFile(user_settings_path, json.getBytes(Charset.defaultCharset())) == 0;
96+
UtilsFilesDirs.writeFile(settings_file, json.getBytes(Charset.defaultCharset()));
97+
UtilsFilesDirs.writeFile(backup_file, json.getBytes(Charset.defaultCharset()));
9798
}
9899
}

0 commit comments

Comments
 (0)