diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-10-15 14:03:42 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-10-15 14:03:42 +0000 |
commit | 192de06e168530e7b0a3a8d1486ce51a07fcb508 (patch) | |
tree | 6a731ffa901e48ba57a08b91cb3eb62893d28bd6 /app/src | |
parent | 4c2f1f3f70df6408aeeb80b778bcc33ff75df957 (diff) | |
download | infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.tar infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.tar.gz infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.tar.bz2 infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.tar.lz infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.tar.xz infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.tar.zst infinity-for-reddit-192de06e168530e7b0a3a8d1486ce51a07fcb508.zip |
Backup custom themes and anonymous subscribed subreddits.
Diffstat (limited to '')
4 files changed, 53 insertions, 17 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 10cbd937..7d708298 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/BackupSettings.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/BackupSettings.java @@ -8,6 +8,8 @@ import android.os.Handler; import androidx.documentfile.provider.DocumentFile; +import com.google.gson.Gson; + import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.EncryptionMethod; @@ -20,18 +22,24 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; +import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; +import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class BackupSettings { public static void backupSettings(Context context, Executor executor, Handler handler, ContentResolver contentResolver, Uri destinationDirUri, + RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences defaultSharedPreferences, SharedPreferences lightThemeSharedPreferences, SharedPreferences darkThemeSharedPreferences, @@ -56,29 +64,40 @@ public class BackupSettings { } backupDirFile.mkdirs(); - boolean res = saveSharedPreferencesToFile(context, defaultSharedPreferences, backupDir, + File databaseDirFile = new File(backupDir + "/database"); + databaseDirFile.mkdirs(); + + boolean res = saveSharedPreferencesToFile(defaultSharedPreferences, backupDir, SharedPreferencesUtils.DEFAULT_PREFERENCES_FILE); - boolean res1 = saveSharedPreferencesToFile(context, lightThemeSharedPreferences, backupDir, + boolean res1 = saveSharedPreferencesToFile(lightThemeSharedPreferences, backupDir, CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE); - boolean res2 = saveSharedPreferencesToFile(context, darkThemeSharedPreferences, backupDir, + boolean res2 = saveSharedPreferencesToFile(darkThemeSharedPreferences, backupDir, CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE); - boolean res3 = saveSharedPreferencesToFile(context, amoledThemeSharedPreferences, backupDir, + boolean res3 = saveSharedPreferencesToFile(amoledThemeSharedPreferences, backupDir, CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE); - boolean res4 = saveSharedPreferencesToFile(context, sortTypeSharedPreferences, backupDir, + boolean res4 = saveSharedPreferencesToFile(sortTypeSharedPreferences, backupDir, SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE); - boolean res5 = saveSharedPreferencesToFile(context, postLayoutSharedPreferences, backupDir, + boolean res5 = saveSharedPreferencesToFile(postLayoutSharedPreferences, backupDir, SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE); - boolean res6 = saveSharedPreferencesToFile(context, postFeedScrolledPositionSharedPreferences, backupDir, + boolean res6 = saveSharedPreferencesToFile(postFeedScrolledPositionSharedPreferences, backupDir, SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE); - boolean res7 = saveSharedPreferencesToFile(context, mainActivityTabsSharedPreferences, backupDir, + boolean res7 = saveSharedPreferencesToFile(mainActivityTabsSharedPreferences, backupDir, SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE); - boolean res8 = saveSharedPreferencesToFile(context, nsfwAndSpoilerSharedPreferencs, backupDir, + boolean res8 = saveSharedPreferencesToFile(nsfwAndSpoilerSharedPreferencs, backupDir, SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE); - boolean res9 = saveSharedPreferencesToFile(context, bottomAppBarSharedPreferences, backupDir, + boolean res9 = saveSharedPreferencesToFile(bottomAppBarSharedPreferences, backupDir, SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE); - boolean res10 = saveSharedPreferencesToFile(context, postHistorySharedPreferences, backupDir, + boolean res10 = saveSharedPreferencesToFile(postHistorySharedPreferences, backupDir, SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE); + List<SubscribedSubredditData> anonymousSubscribedSubredditsData = redditDataRoomDatabase.subscribedSubredditDao().getAllSubscribedSubredditsList("-"); + String anonymousSubscribedSubredditsDataJson = new Gson().toJson(anonymousSubscribedSubredditsData); + boolean res11 = saveDatabaseTableToFile(anonymousSubscribedSubredditsDataJson, databaseDirFile.getAbsolutePath(), "/anonymous_subscribed_subreddits.json"); + + List<CustomTheme> customThemes = redditDataRoomDatabase.customThemeDao().getAllCustomThemesList(); + String customThemesJson = new Gson().toJson(customThemes); + boolean res12 = saveDatabaseTableToFile(customThemesJson, databaseDirFile.getAbsolutePath(), "/custom_themes.json"); + boolean zipRes = zipAndMoveToDestinationDir(context, contentResolver, destinationDirUri); try { @@ -88,7 +107,7 @@ public class BackupSettings { } handler.post(() -> { - boolean finalResult = res && res1 && res2 && res3 && res4 && res5 && res6 && res7 && res8 && res9 && res10 && zipRes; + boolean finalResult = res && res1 && res2 && res3 && res4 && res5 && res6 && res7 && res8 && res9 && res10 && res11 && res12 && zipRes; if (finalResult) { backupSettingsListener.success(); } else { @@ -102,14 +121,13 @@ public class BackupSettings { }); } - private static boolean saveSharedPreferencesToFile(Context context, SharedPreferences sharedPreferences, + private static boolean saveSharedPreferencesToFile(SharedPreferences sharedPreferences, String backupDir, String fileName) { - boolean result = false; ObjectOutputStream output = null; try { - output = new ObjectOutputStream(new FileOutputStream(new File(backupDir + "/" + fileName + ".txt"))); + output = new ObjectOutputStream(new FileOutputStream(backupDir + "/" + fileName + ".txt")); output.writeObject(sharedPreferences.getAll()); result = true; @@ -127,6 +145,21 @@ public class BackupSettings { return result; } + private static boolean saveDatabaseTableToFile(String dataJson, String backupDir, String fileName) { + File anonymousSubscribedSubredditsFile = new File(backupDir + fileName); + try { + anonymousSubscribedSubredditsFile.createNewFile(); + try (PrintWriter out = new PrintWriter(anonymousSubscribedSubredditsFile.getAbsolutePath())) { + out.println(dataJson); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return true; + } + private static boolean zipAndMoveToDestinationDir(Context context, ContentResolver contentResolver, Uri destinationDirUri) { OutputStream outputStream = null; boolean result = false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/RestoreSettings.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/RestoreSettings.java index 3b85ef9a..4a298e9c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/RestoreSettings.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/RestoreSettings.java @@ -126,7 +126,7 @@ public class RestoreSettings { ObjectInputStream input = null; try { - input = new ObjectInputStream(new FileInputStream(new File(uriString))); + input = new ObjectInputStream(new FileInputStream(uriString)); Object object = input.readObject(); if (object instanceof Map) { Map<String, Object> map = (Map<String, Object>) object; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java index 0b779316..7fd10a9a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java @@ -16,6 +16,9 @@ public interface CustomThemeDao { @Query("SELECT * FROM custom_themes") LiveData<List<CustomTheme>> getAllCustomThemes(); + @Query("SELECT * FROM custom_themes") + List<CustomTheme> getAllCustomThemesList(); + @Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1") CustomTheme getLightCustomTheme(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java index 45661b85..1cebd06c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java @@ -304,7 +304,7 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat { if (requestCode == SELECT_BACKUP_SETTINGS_DIRECTORY_REQUEST_CODE) { Uri uri = data.getData(); BackupSettings.backupSettings(activity, executor, new Handler(), activity.getContentResolver(), uri, - mSharedPreferences, lightThemeSharedPreferences, darkThemeSharedPreferences, + mRedditDataRoomDatabase, mSharedPreferences, lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences, mSortTypeSharedPreferences, mPostLayoutSharedPreferences, postFeedScrolledPositionSharedPreferences, mainActivityTabsSharedPreferences, nsfwAndBlurringSharedPreferences, bottomAppBarSharedPreferences, postHistorySharedPreferences, |