Merge pull request #1039 from TomHennen/fix_concurrentmodexception

Fix locking in GpodnetPreferences
This commit is contained in:
Tom Hennen 2015-08-05 18:44:03 -04:00
commit c7d2975039
2 changed files with 15 additions and 30 deletions

View File

@ -426,30 +426,4 @@ public class PreferencesTest extends ActivityInstrumentationTestCase2<Preference
} }
}, Timeout.getLargeTimeout()); }, Timeout.getLargeTimeout());
} }
@FlakyTest(tolerance = 3)
public void testAbout() throws IOException {
int numViews = 0, numLinks = 0;
InputStream input = getActivity().getResources().getAssets().open("about.html");
List<String> lines = IOUtils.readLines(input);
input.close();
for(String line : lines) {
if(line.contains("(View)")) {
numViews++;
} else if(line.contains("(Link)")) {
numLinks++;
}
}
for(int i=0; i < numViews; i++) {
solo.clickOnText(solo.getString(R.string.about_pref));
solo.clickOnText("(View)", i);
solo.goBack();
}
for(int i=0; i < numLinks; i++) {
solo.clickOnText(solo.getString(R.string.about_pref));
solo.clickOnText("(Link)", i);
solo.goBack();
}
}
} }

View File

@ -8,7 +8,6 @@ import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -217,26 +216,36 @@ public class GpodnetPreferences {
public static void removeRemovedFeeds(Collection<String> removed) { public static void removeRemovedFeeds(Collection<String> removed) {
ensurePreferencesLoaded(); ensurePreferencesLoaded();
feedListLock.lock();
removedFeeds.removeAll(removed); removedFeeds.removeAll(removed);
writePreference(PREF_SYNC_REMOVED, removedFeeds); writePreference(PREF_SYNC_REMOVED, removedFeeds);
feedListLock.unlock();
} }
public static synchronized void enqueueEpisodeAction(GpodnetEpisodeAction action) { public static void enqueueEpisodeAction(GpodnetEpisodeAction action) {
ensurePreferencesLoaded(); ensurePreferencesLoaded();
feedListLock.lock();
queuedEpisodeActions.add(action); queuedEpisodeActions.add(action);
writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions)); writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions));
feedListLock.unlock();
GpodnetSyncService.sendSyncActionsIntent(ClientConfig.applicationCallbacks.getApplicationInstance()); GpodnetSyncService.sendSyncActionsIntent(ClientConfig.applicationCallbacks.getApplicationInstance());
} }
public static List<GpodnetEpisodeAction> getQueuedEpisodeActions() { public static List<GpodnetEpisodeAction> getQueuedEpisodeActions() {
ensurePreferencesLoaded(); ensurePreferencesLoaded();
return Collections.unmodifiableList(queuedEpisodeActions); List<GpodnetEpisodeAction> copy = new ArrayList();
feedListLock.lock();
copy.addAll(queuedEpisodeActions);
feedListLock.unlock();
return copy;
} }
public static synchronized void removeQueuedEpisodeActions(Collection<GpodnetEpisodeAction> queued) { public static void removeQueuedEpisodeActions(Collection<GpodnetEpisodeAction> queued) {
ensurePreferencesLoaded(); ensurePreferencesLoaded();
feedListLock.lock();
queuedEpisodeActions.removeAll(queued); queuedEpisodeActions.removeAll(queued);
writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions)); writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions));
feedListLock.unlock();
} }
/** /**
@ -252,12 +261,14 @@ public class GpodnetPreferences {
setUsername(null); setUsername(null);
setPassword(null); setPassword(null);
setDeviceID(null); setDeviceID(null);
feedListLock.lock();
addedFeeds.clear(); addedFeeds.clear();
writePreference(PREF_SYNC_ADDED, addedFeeds); writePreference(PREF_SYNC_ADDED, addedFeeds);
removedFeeds.clear(); removedFeeds.clear();
writePreference(PREF_SYNC_REMOVED, removedFeeds); writePreference(PREF_SYNC_REMOVED, removedFeeds);
queuedEpisodeActions.clear(); queuedEpisodeActions.clear();
writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions)); writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions));
feedListLock.unlock();
setLastSubscriptionSyncTimestamp(0); setLastSubscriptionSyncTimestamp(0);
} }