25
25
26
26
import java .nio .charset .Charset ;
27
27
28
+ import SettingsSync .SettingsSync ;
28
29
import UtilsSWA .UtilsSWA ;
29
30
30
31
/**
@@ -39,60 +40,60 @@ private UtilsSettings() {
39
40
}
40
41
41
42
/**
42
- * <p>Gets the Generated Settings in JSON format .</p>
43
+ * <p>Reads and loads the User and Generated settings from disk .</p>
43
44
*
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
58
46
*
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
62
48
*/
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 ;
68
81
}
69
82
70
83
/**
71
- * <p>Gets the Device Settings in JSON format .</p>
84
+ * <p>Writes the User and Generated settings to disk .</p>
72
85
*
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
74
88
*/
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" ;
79
92
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 );
95
95
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 ()));
97
98
}
98
99
}
0 commit comments