From 34a9cee965e76efea629fba348395f5a8d20353d Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 10 Apr 2021 23:15:59 +0800 Subject: Fix only backing up post history shared preferences. Show the password of the backup zip. --- .../asynctasks/BackupSettings.java | 44 +++++++++++----------- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/BackupSettings.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/BackupSettings.java index e4f18e22..044353f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/BackupSettings.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/BackupSettings.java @@ -45,27 +45,38 @@ public class BackupSettings { SharedPreferences postHistorySharedPreferences, BackupSettingsListener backupSettingsListener) { executor.execute(() -> { - boolean res = saveSharedPreferencesToFile(context, defaultSharedPreferences, + String backupDir = context.getExternalCacheDir() + "/Backup/" + BuildConfig.VERSION_NAME; + File backupDirFile = new File(backupDir); + if (new File(backupDir).exists()) { + try { + FileUtils.deleteDirectory(backupDirFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + backupDirFile.mkdirs(); + + boolean res = saveSharedPreferencesToFile(context, defaultSharedPreferences, backupDir, SharedPreferencesUtils.DEFAULT_PREFERENCES_FILE); - boolean res1 = saveSharedPreferencesToFile(context, lightThemeSharedPreferences, + boolean res1 = saveSharedPreferencesToFile(context, lightThemeSharedPreferences, backupDir, CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE); - boolean res2 = saveSharedPreferencesToFile(context, darkThemeSharedPreferences, + boolean res2 = saveSharedPreferencesToFile(context, darkThemeSharedPreferences, backupDir, CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE); - boolean res3 = saveSharedPreferencesToFile(context, amoledThemeSharedPreferences, + boolean res3 = saveSharedPreferencesToFile(context, amoledThemeSharedPreferences, backupDir, CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE); - boolean res4 = saveSharedPreferencesToFile(context, sortTypeSharedPreferences, + boolean res4 = saveSharedPreferencesToFile(context, sortTypeSharedPreferences, backupDir, SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE); - boolean res5 = saveSharedPreferencesToFile(context, postLayoutSharedPreferences, + boolean res5 = saveSharedPreferencesToFile(context, postLayoutSharedPreferences, backupDir, SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE); - boolean res6 = saveSharedPreferencesToFile(context, postFeedScrolledPositionSharedPreferences, + boolean res6 = saveSharedPreferencesToFile(context, postFeedScrolledPositionSharedPreferences, backupDir, SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE); - boolean res7 = saveSharedPreferencesToFile(context, mainActivityTabsSharedPreferences, + boolean res7 = saveSharedPreferencesToFile(context, mainActivityTabsSharedPreferences, backupDir, SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE); - boolean res8 = saveSharedPreferencesToFile(context, nsfwAndSpoilerSharedPreferencs, + boolean res8 = saveSharedPreferencesToFile(context, nsfwAndSpoilerSharedPreferencs, backupDir, SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE); - boolean res9 = saveSharedPreferencesToFile(context, bottomAppBarSharedPreferences, + boolean res9 = saveSharedPreferencesToFile(context, bottomAppBarSharedPreferences, backupDir, SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE); - boolean res10 = saveSharedPreferencesToFile(context, postHistorySharedPreferences, + boolean res10 = saveSharedPreferencesToFile(context, postHistorySharedPreferences, backupDir, SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE); boolean zipRes = zipAndMoveToDestinationDir(context, contentResolver, destinationDirUri); @@ -92,21 +103,12 @@ public class BackupSettings { } private static boolean saveSharedPreferencesToFile(Context context, SharedPreferences sharedPreferences, - String fileName) { + String backupDir, String fileName) { boolean result = false; ObjectOutputStream output = null; try { - String backupDir = context.getExternalCacheDir() + "/Backup/" + BuildConfig.VERSION_NAME; - if (!new File(backupDir).exists()) { - new File(backupDir).mkdirs(); - } else { - File backupDirFile = new File(backupDir); - FileUtils.deleteDirectory(backupDirFile); - backupDirFile.mkdirs(); - } - output = new ObjectOutputStream(new FileOutputStream(new File(backupDir + "/" + fileName + ".txt"))); output.writeObject(sharedPreferences.getAll()); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b10909a0..e41edd82 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1062,7 +1062,7 @@ Start by joining a subreddit! - Successfully exported settings to the destination directory + Successfully exported settings to the destination directory. The password of the generated zip file is 123321. Please don\'t modify the zip file. Could not create backup zip in the destination directory Could not backup some settings but others were successfully exported to the destination directory Successfully restored settings. Restart the app to see the changes. -- cgit v1.2.3