mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-30 12:57:12 +01:00
Backup custom themes and anonymous subscribed subreddits.
This commit is contained in:
parent
4c2f1f3f70
commit
192de06e16
@ -8,6 +8,8 @@ import android.os.Handler;
|
|||||||
|
|
||||||
import androidx.documentfile.provider.DocumentFile;
|
import androidx.documentfile.provider.DocumentFile;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
import net.lingala.zip4j.ZipFile;
|
import net.lingala.zip4j.ZipFile;
|
||||||
import net.lingala.zip4j.model.ZipParameters;
|
import net.lingala.zip4j.model.ZipParameters;
|
||||||
import net.lingala.zip4j.model.enums.EncryptionMethod;
|
import net.lingala.zip4j.model.enums.EncryptionMethod;
|
||||||
@ -20,18 +22,24 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import ml.docilealligator.infinityforreddit.BuildConfig;
|
import ml.docilealligator.infinityforreddit.BuildConfig;
|
||||||
import ml.docilealligator.infinityforreddit.R;
|
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.CustomThemeSharedPreferencesUtils;
|
||||||
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
||||||
|
|
||||||
public class BackupSettings {
|
public class BackupSettings {
|
||||||
public static void backupSettings(Context context, Executor executor, Handler handler,
|
public static void backupSettings(Context context, Executor executor, Handler handler,
|
||||||
ContentResolver contentResolver, Uri destinationDirUri,
|
ContentResolver contentResolver, Uri destinationDirUri,
|
||||||
|
RedditDataRoomDatabase redditDataRoomDatabase,
|
||||||
SharedPreferences defaultSharedPreferences,
|
SharedPreferences defaultSharedPreferences,
|
||||||
SharedPreferences lightThemeSharedPreferences,
|
SharedPreferences lightThemeSharedPreferences,
|
||||||
SharedPreferences darkThemeSharedPreferences,
|
SharedPreferences darkThemeSharedPreferences,
|
||||||
@ -56,29 +64,40 @@ public class BackupSettings {
|
|||||||
}
|
}
|
||||||
backupDirFile.mkdirs();
|
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);
|
SharedPreferencesUtils.DEFAULT_PREFERENCES_FILE);
|
||||||
boolean res1 = saveSharedPreferencesToFile(context, lightThemeSharedPreferences, backupDir,
|
boolean res1 = saveSharedPreferencesToFile(lightThemeSharedPreferences, backupDir,
|
||||||
CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE);
|
CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE);
|
||||||
boolean res2 = saveSharedPreferencesToFile(context, darkThemeSharedPreferences, backupDir,
|
boolean res2 = saveSharedPreferencesToFile(darkThemeSharedPreferences, backupDir,
|
||||||
CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE);
|
CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE);
|
||||||
boolean res3 = saveSharedPreferencesToFile(context, amoledThemeSharedPreferences, backupDir,
|
boolean res3 = saveSharedPreferencesToFile(amoledThemeSharedPreferences, backupDir,
|
||||||
CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE);
|
CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE);
|
||||||
boolean res4 = saveSharedPreferencesToFile(context, sortTypeSharedPreferences, backupDir,
|
boolean res4 = saveSharedPreferencesToFile(sortTypeSharedPreferences, backupDir,
|
||||||
SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE);
|
SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE);
|
||||||
boolean res5 = saveSharedPreferencesToFile(context, postLayoutSharedPreferences, backupDir,
|
boolean res5 = saveSharedPreferencesToFile(postLayoutSharedPreferences, backupDir,
|
||||||
SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE);
|
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);
|
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);
|
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);
|
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);
|
SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE);
|
||||||
boolean res10 = saveSharedPreferencesToFile(context, postHistorySharedPreferences, backupDir,
|
boolean res10 = saveSharedPreferencesToFile(postHistorySharedPreferences, backupDir,
|
||||||
SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE);
|
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);
|
boolean zipRes = zipAndMoveToDestinationDir(context, contentResolver, destinationDirUri);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -88,7 +107,7 @@ public class BackupSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handler.post(() -> {
|
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) {
|
if (finalResult) {
|
||||||
backupSettingsListener.success();
|
backupSettingsListener.success();
|
||||||
} else {
|
} 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) {
|
String backupDir, String fileName) {
|
||||||
|
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
|
|
||||||
ObjectOutputStream output = null;
|
ObjectOutputStream output = null;
|
||||||
try {
|
try {
|
||||||
output = new ObjectOutputStream(new FileOutputStream(new File(backupDir + "/" + fileName + ".txt")));
|
output = new ObjectOutputStream(new FileOutputStream(backupDir + "/" + fileName + ".txt"));
|
||||||
output.writeObject(sharedPreferences.getAll());
|
output.writeObject(sharedPreferences.getAll());
|
||||||
|
|
||||||
result = true;
|
result = true;
|
||||||
@ -127,6 +145,21 @@ public class BackupSettings {
|
|||||||
return result;
|
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) {
|
private static boolean zipAndMoveToDestinationDir(Context context, ContentResolver contentResolver, Uri destinationDirUri) {
|
||||||
OutputStream outputStream = null;
|
OutputStream outputStream = null;
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
|
@ -126,7 +126,7 @@ public class RestoreSettings {
|
|||||||
ObjectInputStream input = null;
|
ObjectInputStream input = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
input = new ObjectInputStream(new FileInputStream(new File(uriString)));
|
input = new ObjectInputStream(new FileInputStream(uriString));
|
||||||
Object object = input.readObject();
|
Object object = input.readObject();
|
||||||
if (object instanceof Map) {
|
if (object instanceof Map) {
|
||||||
Map<String, Object> map = (Map<String, Object>) object;
|
Map<String, Object> map = (Map<String, Object>) object;
|
||||||
|
@ -16,6 +16,9 @@ public interface CustomThemeDao {
|
|||||||
@Query("SELECT * FROM custom_themes")
|
@Query("SELECT * FROM custom_themes")
|
||||||
LiveData<List<CustomTheme>> getAllCustomThemes();
|
LiveData<List<CustomTheme>> getAllCustomThemes();
|
||||||
|
|
||||||
|
@Query("SELECT * FROM custom_themes")
|
||||||
|
List<CustomTheme> getAllCustomThemesList();
|
||||||
|
|
||||||
@Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1")
|
@Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1")
|
||||||
CustomTheme getLightCustomTheme();
|
CustomTheme getLightCustomTheme();
|
||||||
|
|
||||||
|
@ -304,7 +304,7 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat {
|
|||||||
if (requestCode == SELECT_BACKUP_SETTINGS_DIRECTORY_REQUEST_CODE) {
|
if (requestCode == SELECT_BACKUP_SETTINGS_DIRECTORY_REQUEST_CODE) {
|
||||||
Uri uri = data.getData();
|
Uri uri = data.getData();
|
||||||
BackupSettings.backupSettings(activity, executor, new Handler(), activity.getContentResolver(), uri,
|
BackupSettings.backupSettings(activity, executor, new Handler(), activity.getContentResolver(), uri,
|
||||||
mSharedPreferences, lightThemeSharedPreferences, darkThemeSharedPreferences,
|
mRedditDataRoomDatabase, mSharedPreferences, lightThemeSharedPreferences, darkThemeSharedPreferences,
|
||||||
amoledThemeSharedPreferences, mSortTypeSharedPreferences, mPostLayoutSharedPreferences,
|
amoledThemeSharedPreferences, mSortTypeSharedPreferences, mPostLayoutSharedPreferences,
|
||||||
postFeedScrolledPositionSharedPreferences, mainActivityTabsSharedPreferences,
|
postFeedScrolledPositionSharedPreferences, mainActivityTabsSharedPreferences,
|
||||||
nsfwAndBlurringSharedPreferences, bottomAppBarSharedPreferences, postHistorySharedPreferences,
|
nsfwAndBlurringSharedPreferences, bottomAppBarSharedPreferences, postHistorySharedPreferences,
|
||||||
|
Loading…
Reference in New Issue
Block a user