Restore more data.

This commit is contained in:
Alex Ning 2021-10-16 11:20:48 +08:00
parent f1dfadecb6
commit ddfc478e0e
11 changed files with 124 additions and 28 deletions

View File

@ -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);

View File

@ -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<SubscribedSubredditData> anonymousSubscribedSubreddits = getListFromFile(anonymousSubscribedSubredditsFile, new TypeToken<List<SubscribedSubredditData>>() {}.getType());
redditDataRoomDatabase.subscribedSubredditDao().insertAll(anonymousSubscribedSubreddits);
}
if (anonymousSubscribedUsersFile.exists()) {
List<SubscribedUserData> anonymousSubscribedUsers = getListFromFile(anonymousSubscribedUsersFile, new TypeToken<List<SubscribedUserData>>() {}.getType());
redditDataRoomDatabase.subscribedUserDao().insertAll(anonymousSubscribedUsers);
}
if (anonymousMultiredditsFile.exists()) {
List<MultiReddit> anonymousMultireddits = getListFromFile(anonymousMultiredditsFile, new TypeToken<List<MultiReddit>>() {}.getType());
redditDataRoomDatabase.multiRedditDao().insertAll(anonymousMultireddits);
if (anonymousMultiredditSubredditsFile.exists()) {
List<AnonymousMultiredditSubreddit> anonymousMultiredditSubreddits = getListFromFile(anonymousMultiredditSubredditsFile, new TypeToken<List<AnonymousMultiredditSubreddit>>() {}.getType());
redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(anonymousMultiredditSubreddits);
}
}
if (customThemesFile.exists()) {
List<CustomTheme> customThemes = getListFromFile(customThemesFile, new TypeToken<List<CustomTheme>>() {}.getType());
redditDataRoomDatabase.customThemeDao().insertAll(customThemes);
}
if (postFiltersFile.exists()) {
List<PostFilter> postFilters = getListFromFile(postFiltersFile, new TypeToken<List<PostFilter>>() {}.getType());
redditDataRoomDatabase.postFilterDao().insertAll(postFilters);
if (postFilterUsageFile.exists()) {
List<PostFilterUsage> postFilterUsage = getListFromFile(postFilterUsageFile, new TypeToken<List<PostFilterUsage>>() {}.getType());
redditDataRoomDatabase.postFilterUsageDao().insertAll(postFilterUsage);
}
}
}
}
} else {
@ -164,6 +228,18 @@ public class RestoreSettings {
return result;
}
@Nullable
private static <T> List<T> 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);

View File

@ -13,6 +13,9 @@ public interface CustomThemeDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(CustomTheme customTheme);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<CustomTheme> customThemes);
@Query("SELECT * FROM custom_themes")
LiveData<List<CustomTheme>> getAllCustomThemes();

View File

@ -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<MultiReddit> multiReddits);
@Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllMultiReddits(String username);

View File

@ -14,6 +14,9 @@ public interface PostFilterDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(PostFilter postFilter);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<PostFilter> postFilters);
@Query("DELETE FROM post_filter")
void deleteAllPostFilters();

View File

@ -21,7 +21,10 @@ public interface PostFilterUsageDao {
List<PostFilterUsage> getAllPostFilterUsageForBackup();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertPostFilterUsage(PostFilterUsage postFilterUsage);
void insert(PostFilterUsage postFilterUsage);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<PostFilterUsage> postFilterUsageList);
@Delete
void deletePostFilterUsage(PostFilterUsage postFilterUsage);

View File

@ -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);
}

View File

@ -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));
}
}

View File

@ -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,

View File

@ -13,6 +13,9 @@ public interface SubscribedSubredditDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(SubscribedSubredditData subscribedSubredditData);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<SubscribedSubredditData> subscribedSubredditDataList);
@Query("DELETE FROM subscribed_subreddits")
void deleteAllSubscribedSubreddits();

View File

@ -13,6 +13,9 @@ public interface SubscribedUserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(SubscribedUserData subscribedUserData);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<SubscribedUserData> subscribedUserDataList);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllSubscribedUsers(String accountName);