From ddfc478e0e9f95b8630a16dd3a179bb4acfca10a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 16 Oct 2021 11:20:48 +0800 Subject: Restore more data. --- .../activities/FilteredPostsActivity.java | 4 +- .../asynctasks/RestoreSettings.java | 120 +++++++++++++++++---- .../customtheme/CustomThemeDao.java | 3 + .../multireddit/MultiRedditDao.java | 5 +- .../postfilter/PostFilterDao.java | 3 + .../postfilter/PostFilterUsageDao.java | 5 +- .../postfilter/SavePostFilter.java | 2 +- .../postfilter/SavePostFilterUsage.java | 2 +- .../settings/AdvancedPreferenceFragment.java | 2 +- .../SubscribedSubredditDao.java | 3 + .../subscribeduser/SubscribedUserDao.java | 3 + 11 files changed, 124 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index 7d9ef895..29991f5c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -277,6 +277,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec } break; case PostPagingSource.TYPE_MULTI_REDDIT: + case PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT: String multiRedditName; if (name.endsWith("/")) { multiRedditName = name.substring(0, name.length() - 1); @@ -302,7 +303,8 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec if (postType == PostPagingSource.TYPE_USER) { bundle.putString(PostFragment.EXTRA_USER_NAME, name); bundle.putString(PostFragment.EXTRA_USER_WHERE, userWhere); - } else if (postType == PostPagingSource.TYPE_SUBREDDIT || postType == PostPagingSource.TYPE_MULTI_REDDIT) { + } else if (postType == PostPagingSource.TYPE_SUBREDDIT || postType == PostPagingSource.TYPE_MULTI_REDDIT + || postType == PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT) { bundle.putString(PostFragment.EXTRA_NAME, name); } else if (postType == PostPagingSource.TYPE_SEARCH) { bundle.putString(PostFragment.EXTRA_NAME, name); 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 4a298e9c..a14e93d3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/RestoreSettings.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/RestoreSettings.java @@ -6,6 +6,12 @@ import android.content.SharedPreferences; import android.net.Uri; import android.os.Handler; +import androidx.annotation.Nullable; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; + import net.lingala.zip4j.ZipFile; import org.apache.commons.io.FileUtils; @@ -13,20 +19,33 @@ import org.apache.commons.io.FileUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; +import java.lang.reflect.Type; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.account.Account; +import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; +import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubreddit; +import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; +import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class RestoreSettings { public static void restoreSettings(Context context, Executor executor, Handler handler, ContentResolver contentResolver, Uri zipFileUri, + RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences defaultSharedPreferences, SharedPreferences lightThemeSharedPreferences, SharedPreferences darkThemeSharedPreferences, @@ -77,28 +96,73 @@ public class RestoreSettings { boolean result = true; if (restoreFiles != null) { for (File f : restoreFiles) { - if (f.getName().startsWith(SharedPreferencesUtils.DEFAULT_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(defaultSharedPreferences, f.toString()); - } else if (f.getName().startsWith(CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(lightThemeSharedPreferences, f.toString()); - } else if (f.getName().startsWith(CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(darkThemeSharedPreferences, f.toString()); - } else if (f.getName().startsWith(CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(amoledThemeSharedPreferences, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(sortTypeSharedPreferences, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(postLayoutSharedPreferences, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(postFeedScrolledPositionSharedPreferences, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(mainActivityTabsSharedPreferences, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(nsfwAndSpoilerSharedPreferencs, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(bottomAppBarSharedPreferences, f.toString()); - } else if (f.getName().startsWith(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE)) { - result = result & importSharedPreferencsFromFile(postHistorySharedPreferences, f.toString()); + if (f.isFile()) { + if (f.getName().startsWith(SharedPreferencesUtils.DEFAULT_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(defaultSharedPreferences, f.toString()); + } else if (f.getName().startsWith(CustomThemeSharedPreferencesUtils.LIGHT_THEME_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(lightThemeSharedPreferences, f.toString()); + } else if (f.getName().startsWith(CustomThemeSharedPreferencesUtils.DARK_THEME_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(darkThemeSharedPreferences, f.toString()); + } else if (f.getName().startsWith(CustomThemeSharedPreferencesUtils.AMOLED_THEME_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(amoledThemeSharedPreferences, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.SORT_TYPE_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(sortTypeSharedPreferences, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(postLayoutSharedPreferences, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(postFeedScrolledPositionSharedPreferences, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(mainActivityTabsSharedPreferences, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(nsfwAndSpoilerSharedPreferencs, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.BOTTOM_APP_BAR_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(bottomAppBarSharedPreferences, f.toString()); + } else if (f.getName().startsWith(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE)) { + result = result & importSharedPreferencsFromFile(postHistorySharedPreferences, f.toString()); + } + } else if (f.isDirectory() && f.getName().equals("database")) { + if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { + redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); + } + + File anonymousSubscribedSubredditsFile = new File(f.getAbsolutePath() + "/anonymous_subscribed_subreddits.json"); + File anonymousSubscribedUsersFile = new File(f.getAbsolutePath() + "/anonymous_subscribed_users.json"); + File anonymousMultiredditsFile = new File(f.getAbsolutePath() + "/anonymous_multireddits.json"); + File anonymousMultiredditSubredditsFile = new File(f.getAbsolutePath() + "/anonymous_multireddit_subreddits.json"); + File customThemesFile = new File(f.getAbsolutePath() + "/custom_themes.json"); + File postFiltersFile = new File(f.getAbsolutePath() + "/post_filters.json"); + File postFilterUsageFile = new File(f.getAbsolutePath() + "/post_filter_usage.json"); + + if (anonymousSubscribedSubredditsFile.exists()) { + List anonymousSubscribedSubreddits = getListFromFile(anonymousSubscribedSubredditsFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.subscribedSubredditDao().insertAll(anonymousSubscribedSubreddits); + } + if (anonymousSubscribedUsersFile.exists()) { + List anonymousSubscribedUsers = getListFromFile(anonymousSubscribedUsersFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.subscribedUserDao().insertAll(anonymousSubscribedUsers); + } + if (anonymousMultiredditsFile.exists()) { + List anonymousMultireddits = getListFromFile(anonymousMultiredditsFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.multiRedditDao().insertAll(anonymousMultireddits); + + if (anonymousMultiredditSubredditsFile.exists()) { + List anonymousMultiredditSubreddits = getListFromFile(anonymousMultiredditSubredditsFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(anonymousMultiredditSubreddits); + } + } + if (customThemesFile.exists()) { + List customThemes = getListFromFile(customThemesFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.customThemeDao().insertAll(customThemes); + } + if (postFiltersFile.exists()) { + List postFilters = getListFromFile(postFiltersFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.postFilterDao().insertAll(postFilters); + + if (postFilterUsageFile.exists()) { + List postFilterUsage = getListFromFile(postFilterUsageFile, new TypeToken>() {}.getType()); + redditDataRoomDatabase.postFilterUsageDao().insertAll(postFilterUsage); + } + } } } } else { @@ -164,6 +228,18 @@ public class RestoreSettings { return result; } + @Nullable + private static List getListFromFile(File file, Type dataType) { + try (JsonReader reader = new JsonReader(new FileReader(file))) { + Gson gson = new Gson(); + return gson.fromJson(reader, dataType); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + public interface RestoreSettingsListener { void success(); void failed(String errorMessage); 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 7fd10a9a..172d36e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeDao.java @@ -13,6 +13,9 @@ public interface CustomThemeDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(CustomTheme customTheme); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List customThemes); + @Query("SELECT * FROM custom_themes") LiveData> getAllCustomThemes(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java index a5099e85..4d99e7a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java @@ -11,7 +11,10 @@ import java.util.List; @Dao public interface MultiRedditDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(MultiReddit MultiReddit); + void insert(MultiReddit multiReddit); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List multiReddits); @Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC") LiveData> getAllMultiReddits(String username); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java index 5dc37da9..ec53d185 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java @@ -14,6 +14,9 @@ public interface PostFilterDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(PostFilter postFilter); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List postFilters); + @Query("DELETE FROM post_filter") void deleteAllPostFilters(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java index 652fa04c..b0d0f03d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java @@ -21,7 +21,10 @@ public interface PostFilterUsageDao { List getAllPostFilterUsageForBackup(); @Insert(onConflict = OnConflictStrategy.REPLACE) - void insertPostFilterUsage(PostFilterUsage postFilterUsage); + void insert(PostFilterUsage postFilterUsage); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List postFilterUsageList); @Delete void deletePostFilterUsage(PostFilterUsage postFilterUsage); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java index 2f14ba6f..fd021cba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java @@ -27,7 +27,7 @@ public class SavePostFilter { redditDataRoomDatabase.postFilterDao().insert(postFilter); for (PostFilterUsage postFilterUsage : postFilterUsages) { postFilterUsage.name = postFilter.name; - redditDataRoomDatabase.postFilterUsageDao().insertPostFilterUsage(postFilterUsage); + redditDataRoomDatabase.postFilterUsageDao().insert(postFilterUsage); } handler.post(savePostFilterListener::success); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java index 567da3b1..53a87726 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java @@ -7,6 +7,6 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class SavePostFilterUsage { public static void savePostFilterUsage(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, PostFilterUsage postFilterUsage) { - executor.execute(() -> redditDataRoomDatabase.postFilterUsageDao().insertPostFilterUsage(postFilterUsage)); + executor.execute(() -> redditDataRoomDatabase.postFilterUsageDao().insert(postFilterUsage)); } } 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 1cebd06c..2b826d4f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java @@ -322,7 +322,7 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat { } else if (requestCode == SELECT_RESTORE_SETTINGS_DIRECTORY_REQUEST_CODE) { Uri uri = data.getData(); RestoreSettings.restoreSettings(activity, executor, new Handler(), activity.getContentResolver(), uri, - mSharedPreferences, lightThemeSharedPreferences, darkThemeSharedPreferences, + mRedditDataRoomDatabase, mSharedPreferences, lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences, mSortTypeSharedPreferences, mPostLayoutSharedPreferences, postFeedScrolledPositionSharedPreferences, mainActivityTabsSharedPreferences, nsfwAndBlurringSharedPreferences, bottomAppBarSharedPreferences, postHistorySharedPreferences, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java index f7452100..09d866ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java @@ -13,6 +13,9 @@ public interface SubscribedSubredditDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(SubscribedSubredditData subscribedSubredditData); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List subscribedSubredditDataList); + @Query("DELETE FROM subscribed_subreddits") void deleteAllSubscribedSubreddits(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java index 9ae7c895..18754a6b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java @@ -13,6 +13,9 @@ public interface SubscribedUserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(SubscribedUserData subscribedUserData); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List subscribedUserDataList); + @Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC") LiveData> getAllSubscribedUsers(String accountName); -- cgit v1.2.3