From 64330146790cf3b216f32801f3e19a778bbc18b8 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 24 Feb 2020 20:43:55 +0800 Subject: Fix stack overflow error when syncing subscribed subreddits and users and multireddits. Fix app crashes when creating multireddits without selecting any subreddits. Version 2.2.3. --- app/build.gradle | 4 +- .../Activity/CreateMultiRedditActivity.java | 2 + .../AsyncTask/InsertMultiRedditAsyncTask.java | 42 ++++++------ .../AsyncTask/InsertSubscribedThingsAsyncTask.java | 78 +++++++++++----------- .../MultiReddit/MultiRedditDao.java | 4 +- 5 files changed, 67 insertions(+), 63 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5145e413..4dcb03a6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "ml.docilealligator.infinityforreddit" minSdkVersion 21 targetSdkVersion 29 - versionCode 28 - versionName "2.2.2" + versionCode 29 + versionName "2.2.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java index 6db86514..7435e216 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java @@ -93,6 +93,8 @@ public class CreateMultiRedditActivity extends BaseActivity { bindView(); } } else { + mSelectedSubscribedSubreddits = new ArrayList<>(); + mSelectedOtherSubreddits = new ArrayList<>(); getCurrentAccountAndBindView(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java index c76b726e..2c15fa33 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertMultiRedditAsyncTask.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.AsyncTask; import android.os.AsyncTask; +import android.util.Log; import java.util.ArrayList; import java.util.Collections; @@ -30,9 +31,10 @@ public class InsertMultiRedditAsyncTask extends AsyncTask { List existingMultiReddits = multiRedditDao.getAllMultiRedditsList(accountName); Collections.sort(multiReddits, (multiReddit, t1) -> multiReddit.getName().compareToIgnoreCase(t1.getName())); List deletedMultiredditNames = new ArrayList<>(); - compareTwoMultiRedditList(multiReddits, existingMultiReddits, deletedMultiredditNames, 0, 0); + compareTwoMultiRedditList(multiReddits, existingMultiReddits, deletedMultiredditNames); for (String deleted : deletedMultiredditNames) { + Log.i("asdfasdfs", "s " + deleted + accountName); multiRedditDao.deleteMultiReddit(deleted, accountName); } @@ -49,27 +51,27 @@ public class InsertMultiRedditAsyncTask extends AsyncTask { } private void compareTwoMultiRedditList(List newMultiReddits, - List oldMultiReddits, - List deletedMultiReddits, int i1, int i2) { - if (newMultiReddits.size() <= i1 && oldMultiReddits.size() <= i2) { - return; - } - - if (newMultiReddits.size() <= i1) { - for (int i = 0; i < oldMultiReddits.size(); i++) { - deletedMultiReddits.add(oldMultiReddits.get(i).getName()); + List oldMultiReddits, + List deletedMultiReddits) { + int newIndex = 0; + for (int oldIndex = 0; oldIndex < oldMultiReddits.size(); oldIndex++) { + if (newIndex >= newMultiReddits.size()) { + for (; oldIndex < oldMultiReddits.size(); oldIndex++) { + deletedMultiReddits.add(oldMultiReddits.get(oldIndex).getName()); + } + return; } - return; - } - if (oldMultiReddits.size() > i2) { - if (newMultiReddits.get(i1).getName().compareToIgnoreCase(oldMultiReddits.get(i2).getName()) == 0) { - compareTwoMultiRedditList(newMultiReddits, oldMultiReddits, deletedMultiReddits, i1 + 1, i2 + 1); - } else if (newMultiReddits.get(i1).getName().compareToIgnoreCase(oldMultiReddits.get(i2).getName()) < 0) { - compareTwoMultiRedditList(newMultiReddits, oldMultiReddits, deletedMultiReddits, i1 + 1, i2); - } else { - deletedMultiReddits.add(oldMultiReddits.get(i2).getName()); - compareTwoMultiRedditList(newMultiReddits, oldMultiReddits, deletedMultiReddits, i1, i2 + 1); + MultiReddit old = oldMultiReddits.get(oldIndex); + for (; newIndex < newMultiReddits.size(); newIndex++) { + if (newMultiReddits.get(newIndex).getName().compareToIgnoreCase(old.getName()) == 0) { + newIndex++; + break; + } + if (newMultiReddits.get(newIndex).getName().compareToIgnoreCase(old.getName()) > 0) { + deletedMultiReddits.add(old.getName()); + break; + } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java index a1ea1b68..0d46da69 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java @@ -84,7 +84,7 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask Collections.sort(subscribedSubredditDataList, (subscribedSubredditData, t1) -> subscribedSubredditData.getName().compareToIgnoreCase(t1.getName())); List unsubscribedSubreddits = new ArrayList<>(); compareTwoSubscribedSubredditList(subscribedSubredditDataList, existingSubscribedSubredditDataList, - unsubscribedSubreddits, 0, 0); + unsubscribedSubreddits); for (String unsubscribed : unsubscribedSubreddits) { mSubscribedSubredditDao.deleteSubscribedSubreddit(unsubscribed, mAccountName); @@ -101,7 +101,7 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask Collections.sort(subscribedUserDataList, (subscribedUserData, t1) -> subscribedUserData.getName().compareToIgnoreCase(t1.getName())); List unsubscribedUsers = new ArrayList<>(); compareTwoSubscribedUserList(subscribedUserDataList, existingSubscribedUserDataList, - unsubscribedUsers, 0, 0); + unsubscribedUsers); for (String unsubscribed : unsubscribedUsers) { mSubscribedUserDao.deleteSubscribedUser(unsubscribed, mAccountName); @@ -128,52 +128,52 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask private void compareTwoSubscribedSubredditList(List newSubscribedSubreddits, List oldSubscribedSubreddits, - List unsubscribedSubredditNames, int i1, int i2) { - if (newSubscribedSubreddits.size() <= i1 && oldSubscribedSubreddits.size() <= i2) { - return; - } - - if (newSubscribedSubreddits.size() <= i1) { - for (int i = 0; i < oldSubscribedSubreddits.size(); i++) { - unsubscribedSubredditNames.add(oldSubscribedSubreddits.get(i).getName()); + List unsubscribedSubredditNames) { + int newIndex = 0; + for (int oldIndex = 0; oldIndex < oldSubscribedSubreddits.size(); oldIndex++) { + if (newIndex >= newSubscribedSubreddits.size()) { + for (; oldIndex < oldSubscribedSubreddits.size(); oldIndex++) { + unsubscribedSubredditNames.add(oldSubscribedSubreddits.get(oldIndex).getName()); + } + return; } - return; - } - if (oldSubscribedSubreddits.size() > i2) { - if (newSubscribedSubreddits.get(i1).getName().compareToIgnoreCase(oldSubscribedSubreddits.get(i2).getName()) == 0) { - compareTwoSubscribedSubredditList(newSubscribedSubreddits, oldSubscribedSubreddits, unsubscribedSubredditNames, i1 + 1, i2 + 1); - } else if (newSubscribedSubreddits.get(i1).getName().compareToIgnoreCase(oldSubscribedSubreddits.get(i2).getName()) < 0) { - compareTwoSubscribedSubredditList(newSubscribedSubreddits, oldSubscribedSubreddits, unsubscribedSubredditNames, i1 + 1, i2); - } else { - unsubscribedSubredditNames.add(oldSubscribedSubreddits.get(i2).getName()); - compareTwoSubscribedSubredditList(newSubscribedSubreddits, oldSubscribedSubreddits, unsubscribedSubredditNames, i1, i2 + 1); + SubscribedSubredditData old = oldSubscribedSubreddits.get(oldIndex); + for (; newIndex < newSubscribedSubreddits.size(); newIndex++) { + if (newSubscribedSubreddits.get(newIndex).getName().compareToIgnoreCase(old.getName()) == 0) { + newIndex++; + break; + } + if (newSubscribedSubreddits.get(newIndex).getName().compareToIgnoreCase(old.getName()) > 0) { + unsubscribedSubredditNames.add(old.getName()); + break; + } } } } private void compareTwoSubscribedUserList(List newSubscribedUsers, - List oldSubscribedUsers, - List unsubscribedUserNames, int i1, int i2) { - if (newSubscribedUsers.size() <= i1 && oldSubscribedUsers.size() <= i2) { - return; - } - - if (newSubscribedUsers.size() <= i1) { - for (int i = 0; i < oldSubscribedUsers.size(); i++) { - unsubscribedUserNames.add(oldSubscribedUsers.get(i).getName()); + List oldSubscribedUsers, + List unsubscribedUserNames) { + int newIndex = 0; + for (int oldIndex = 0; oldIndex < oldSubscribedUsers.size(); oldIndex++) { + if (newIndex >= newSubscribedUsers.size()) { + for (; oldIndex < oldSubscribedUsers.size(); oldIndex++) { + unsubscribedUserNames.add(oldSubscribedUsers.get(oldIndex).getName()); + } + return; } - return; - } - if (oldSubscribedUsers.size() > i2) { - if (newSubscribedUsers.get(i1).getName().compareToIgnoreCase(oldSubscribedUsers.get(i2).getName()) == 0) { - compareTwoSubscribedUserList(newSubscribedUsers, oldSubscribedUsers, unsubscribedUserNames, i1 + 1, i2 + 1); - } else if (newSubscribedUsers.get(i1).getName().compareToIgnoreCase(oldSubscribedUsers.get(i2).getName()) < 0) { - compareTwoSubscribedUserList(newSubscribedUsers, oldSubscribedUsers, unsubscribedUserNames, i1 + 1, i2); - } else { - unsubscribedUserNames.add(oldSubscribedUsers.get(i2).getName()); - compareTwoSubscribedUserList(newSubscribedUsers, oldSubscribedUsers, unsubscribedUserNames, i1, i2 + 1); + SubscribedUserData old = oldSubscribedUsers.get(oldIndex); + for (; newIndex < newSubscribedUsers.size(); newIndex++) { + if (newSubscribedUsers.get(newIndex).getName().compareToIgnoreCase(old.getName()) == 0) { + newIndex++; + break; + } + if (newSubscribedUsers.get(newIndex).getName().compareToIgnoreCase(old.getName()) > 0) { + unsubscribedUserNames.add(old.getName()); + break; + } } } } 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 198e610e..7d3654c8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditDao.java @@ -25,8 +25,8 @@ public interface MultiRedditDao { @Query("SELECT * FROM multi_reddits WHERE name = :name AND username = :username COLLATE NOCASE LIMIT 1") MultiReddit getMultiReddit(String name, String username); - @Query("DELETE FROM multi_reddits WHERE path = :multipath AND username = :username") - void deleteMultiReddit(String multipath, String username); + @Query("DELETE FROM multi_reddits WHERE name = :name AND username = :username") + void deleteMultiReddit(String name, String username); @Query("DELETE FROM multi_reddits WHERE username = :username") void deleteAllUserMultiReddits(String username); -- cgit v1.2.3