Refactor database access

This commit is contained in:
Martin Fietz 2015-09-09 14:05:58 +02:00
parent 2c3f7921d2
commit 75dfc89a44
65 changed files with 1509 additions and 1698 deletions

View File

@ -2,6 +2,7 @@ package de.test.antennapod;
import android.test.InstrumentationTestRunner;
import android.test.suitebuilder.TestSuiteBuilder;
import junit.framework.TestSuite;
public class AntennaPodTestRunner extends InstrumentationTestRunner {
@ -13,4 +14,5 @@ public class AntennaPodTestRunner extends InstrumentationTestRunner {
.excludePackages("de.test.antennapod.gpodnet")
.build();
}
}

View File

@ -1,7 +1,6 @@
package de.test.antennapod.service.playback;
import android.content.Context;
import android.media.RemoteControlClient;
import android.test.InstrumentationTestCase;
import junit.framework.AssertionFailedError;
@ -45,7 +44,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext());
PodDBAdapter.deleteDatabase();
httpServer.stop();
}
@ -54,16 +53,16 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase {
super.setUp();
assertionError = null;
final Context context = getInstrumentation().getTargetContext();
context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
// make sure database is created
PodDBAdapter adapter = new PodDBAdapter(context);
// create new database
PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
httpServer = new HTTPBin();
httpServer.start();
final Context context = getInstrumentation().getTargetContext();
File cacheDir = context.getExternalFilesDir("testFiles");
if (cacheDir == null)
cacheDir = context.getExternalFilesDir("testFiles");
@ -119,12 +118,12 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase {
Feed f = new Feed(0, new Date(), "f", "l", "d", null, null, null, null, "i", null, null, "l", false);
FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NO, null, null);
f.setPreferences(prefs);
f.setItems(new ArrayList<FeedItem>());
f.setItems(new ArrayList<>());
FeedItem i = new FeedItem(0, "t", "i", "l", new Date(), FeedItem.UNPLAYED, f);
f.getItems().add(i);
FeedMedia media = new FeedMedia(0, i, 0, 0, 0, "audio/wav", fileUrl, downloadUrl, fileUrl != null, null, 0);
i.setMedia(media);
PodDBAdapter adapter = new PodDBAdapter(c);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(f);
assertTrue(media.getId() != 0);

View File

@ -26,16 +26,16 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
assertTrue(PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext()));
PodDBAdapter.deleteDatabase();
}
@Override
protected void setUp() throws Exception {
super.setUp();
final Context context = getInstrumentation().getTargetContext();
context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
// make sure database is created
PodDBAdapter adapter = new PodDBAdapter(context);
// create new database
PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
}
@ -49,11 +49,11 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
final Context c = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10;
Feed f = new Feed(0, new Date(), "title", "link", "d", null, null, null, null, "id", null, "null", "url", false);
f.setItems(new ArrayList<FeedItem>());
f.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
f.getItems().add(new FeedItem(0, pref + i, pref + i, "link", new Date(), FeedItem.PLAYED, f));
}
PodDBAdapter adapter = new PodDBAdapter(c);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(f);
adapter.setQueue(f.getItems());

View File

@ -15,7 +15,6 @@ import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.FeedItemStatistics;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.flattr.FlattrStatus;
import static de.test.antennapod.storage.DBTestUtils.saveFeedlist;
@ -27,25 +26,23 @@ public class DBReaderTest extends InstrumentationTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
final Context context = getInstrumentation().getTargetContext();
assertTrue(PodDBAdapter.deleteDatabase(context));
assertTrue(PodDBAdapter.deleteDatabase());
}
@Override
protected void setUp() throws Exception {
super.setUp();
final Context context = getInstrumentation().getTargetContext();
context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
// make sure database is created
PodDBAdapter adapter = new PodDBAdapter(context);
// create new database
PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
}
public void testGetFeedList() {
final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, 10, 0, false);
List<Feed> savedFeeds = DBReader.getFeedList(context);
List<Feed> feeds = saveFeedlist(10, 0, false);
List<Feed> savedFeeds = DBReader.getFeedList();
assertNotNull(savedFeeds);
assertEquals(feeds.size(), savedFeeds.size());
for (int i = 0; i < feeds.size(); i++) {
@ -54,8 +51,7 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetFeedListSortOrder() {
final Context context = getInstrumentation().getTargetContext();
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
Feed feed1 = new Feed(0, new Date(), "A", "link", "d", null, null, null, "rss", "A", null, "", "", true);
@ -73,7 +69,7 @@ public class DBReaderTest extends InstrumentationTestCase {
adapter.close();
List<Feed> saved = DBReader.getFeedList(context);
List<Feed> saved = DBReader.getFeedList();
assertNotNull(saved);
assertEquals("Wrong size: ", 4, saved.size());
@ -84,9 +80,8 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testFeedListDownloadUrls() {
final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, 10, 0, false);
List<String> urls = DBReader.getFeedListDownloadUrls(context);
List<Feed> feeds = saveFeedlist(10, 0, false);
List<String> urls = DBReader.getFeedListDownloadUrls();
assertNotNull(urls);
assertTrue(urls.size() == feeds.size());
for (int i = 0; i < urls.size(); i++) {
@ -98,8 +93,8 @@ public class DBReaderTest extends InstrumentationTestCase {
final Context context = getInstrumentation().getTargetContext();
final int numFeeds = 10;
final int numItems = 1;
List<Feed> feeds = saveFeedlist(context, numFeeds, numItems, false);
List<FeedItem> items = new ArrayList<FeedItem>();
List<Feed> feeds = saveFeedlist(numFeeds, numItems, false);
List<FeedItem> items = new ArrayList<>();
for (Feed f : feeds) {
for (FeedItem item : f.getItems()) {
item.setFeed(null);
@ -107,7 +102,7 @@ public class DBReaderTest extends InstrumentationTestCase {
items.add(item);
}
}
DBReader.loadFeedDataOfFeedItemlist(context, items);
DBReader.loadFeedDataOfFeedItemlist(items);
for (int i = 0; i < numFeeds; i++) {
for (int j = 0; j < numItems; j++) {
FeedItem item = feeds.get(i).getItems().get(j);
@ -119,13 +114,12 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetFeedItemList() {
final Context context = getInstrumentation().getTargetContext();
final int numFeeds = 1;
final int numItems = 10;
Feed feed = saveFeedlist(context, numFeeds, numItems, false).get(0);
Feed feed = saveFeedlist(numFeeds, numItems, false).get(0);
List<FeedItem> items = feed.getItems();
feed.setItems(null);
List<FeedItem> savedItems = DBReader.getFeedItemList(context, feed);
List<FeedItem> savedItems = DBReader.getFeedItemList(feed);
assertNotNull(savedItems);
assertTrue(savedItems.size() == items.size());
for (int i = 0; i < savedItems.size(); i++) {
@ -137,22 +131,21 @@ public class DBReaderTest extends InstrumentationTestCase {
if (numItems <= 0) {
throw new IllegalArgumentException("numItems<=0");
}
final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, numItems, numItems, false);
List<FeedItem> allItems = new ArrayList<FeedItem>();
List<Feed> feeds = saveFeedlist(numItems, numItems, false);
List<FeedItem> allItems = new ArrayList<>();
for (Feed f : feeds) {
allItems.addAll(f.getItems());
}
// take random items from every feed
Random random = new Random();
List<FeedItem> queue = new ArrayList<FeedItem>();
List<FeedItem> queue = new ArrayList<>();
while (queue.size() < numItems) {
int index = random.nextInt(numItems);
if (!queue.contains(allItems.get(index))) {
queue.add(allItems.get(index));
}
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setQueue(queue);
adapter.close();
@ -160,10 +153,9 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetQueueIDList() {
final Context context = getInstrumentation().getTargetContext();
final int numItems = 10;
List<FeedItem> queue = saveQueue(numItems);
LongList ids = DBReader.getQueueIDList(context);
LongList ids = DBReader.getQueueIDList();
assertNotNull(ids);
assertTrue(queue.size() == ids.size());
for (int i = 0; i < queue.size(); i++) {
@ -173,10 +165,9 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetQueue() {
final Context context = getInstrumentation().getTargetContext();
final int numItems = 10;
List<FeedItem> queue = saveQueue(numItems);
List<FeedItem> savedQueue = DBReader.getQueue(context);
List<FeedItem> savedQueue = DBReader.getQueue();
assertNotNull(savedQueue);
assertTrue(queue.size() == savedQueue.size());
for (int i = 0; i < queue.size(); i++) {
@ -189,13 +180,12 @@ public class DBReaderTest extends InstrumentationTestCase {
if (numItems <= 0) {
throw new IllegalArgumentException("numItems<=0");
}
final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, numItems, numItems, true);
List<FeedItem> items = new ArrayList<FeedItem>();
List<Feed> feeds = saveFeedlist(numItems, numItems, true);
List<FeedItem> items = new ArrayList<>();
for (Feed f : feeds) {
items.addAll(f.getItems());
}
List<FeedItem> downloaded = new ArrayList<FeedItem>();
List<FeedItem> downloaded = new ArrayList<>();
Random random = new Random();
while (downloaded.size() < numItems) {
@ -207,7 +197,7 @@ public class DBReaderTest extends InstrumentationTestCase {
downloaded.add(item);
}
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setFeedItemlist(downloaded);
adapter.close();
@ -215,10 +205,9 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetDownloadedItems() {
final Context context = getInstrumentation().getTargetContext();
final int numItems = 10;
List<FeedItem> downloaded = saveDownloadedItems(numItems);
List<FeedItem> downloaded_saved = DBReader.getDownloadedItems(context);
List<FeedItem> downloaded_saved = DBReader.getDownloadedItems();
assertNotNull(downloaded_saved);
assertTrue(downloaded_saved.size() == downloaded.size());
for (FeedItem item : downloaded_saved) {
@ -232,13 +221,12 @@ public class DBReaderTest extends InstrumentationTestCase {
if (numItems <= 0) {
throw new IllegalArgumentException("numItems<=0");
}
final Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, numItems, numItems, true);
List<FeedItem> items = new ArrayList<FeedItem>();
List<Feed> feeds = saveFeedlist(numItems, numItems, true);
List<FeedItem> items = new ArrayList<>();
for (Feed f : feeds) {
items.addAll(f.getItems());
}
List<FeedItem> unread = new ArrayList<FeedItem>();
List<FeedItem> unread = new ArrayList<>();
Random random = new Random();
while (unread.size() < numItems) {
@ -249,7 +237,7 @@ public class DBReaderTest extends InstrumentationTestCase {
unread.add(item);
}
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setFeedItemlist(unread);
adapter.close();
@ -257,11 +245,10 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetUnreadItemsList() {
final Context context = getInstrumentation().getTargetContext();
final int numItems = 10;
List<FeedItem> unread = saveUnreadItems(numItems);
List<FeedItem> unreadSaved = DBReader.getUnreadItemsList(context);
List<FeedItem> unreadSaved = DBReader.getUnreadItemsList();
assertNotNull(unreadSaved);
assertTrue(unread.size() == unreadSaved.size());
for (FeedItem item : unreadSaved) {
@ -270,7 +257,6 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetNewItemIds() {
final Context context = getInstrumentation().getTargetContext();
final int numItems = 10;
List<FeedItem> unread = saveUnreadItems(numItems);
@ -278,7 +264,7 @@ public class DBReaderTest extends InstrumentationTestCase {
for (int i = 0; i < unread.size(); i++) {
unreadIds[i] = unread.get(i).getId();
}
List<FeedItem> unreadSaved = DBReader.getUnreadItemsList(context);
List<FeedItem> unreadSaved = DBReader.getUnreadItemsList();
assertNotNull(unreadSaved);
assertTrue(unread.size() == unreadSaved.size());
for(int i=0; i < unreadSaved.size(); i++) {
@ -295,16 +281,15 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetPlaybackHistory() {
final Context context = getInstrumentation().getTargetContext();
final int numItems = (DBReader.PLAYBACK_HISTORY_SIZE + 1) * 2;
final int playedItems = DBReader.PLAYBACK_HISTORY_SIZE + 1;
final int numReturnedItems = Math.min(playedItems, DBReader.PLAYBACK_HISTORY_SIZE);
final int numFeeds = 1;
Feed feed = DBTestUtils.saveFeedlist(context, numFeeds, numItems, true).get(0);
Feed feed = DBTestUtils.saveFeedlist(numFeeds, numItems, true).get(0);
long[] ids = new long[playedItems];
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
for (int i = 0; i < playedItems; i++) {
FeedMedia m = feed.getItems().get(i).getMedia();
@ -314,7 +299,7 @@ public class DBReaderTest extends InstrumentationTestCase {
}
adapter.close();
List<FeedItem> saved = DBReader.getPlaybackHistory(context);
List<FeedItem> saved = DBReader.getPlaybackHistory();
assertNotNull(saved);
assertEquals("Wrong size: ", numReturnedItems, saved.size());
for (int i = 0; i < numReturnedItems; i++) {
@ -325,11 +310,10 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetFeedStatisticsCheckOrder() {
final Context context = getInstrumentation().getTargetContext();
final int NUM_FEEDS = 10;
final int NUM_ITEMS = 10;
List<Feed> feeds = DBTestUtils.saveFeedlist(context, NUM_FEEDS, NUM_ITEMS, false);
List<FeedItemStatistics> statistics = DBReader.getFeedStatisticsList(context);
List<Feed> feeds = DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, false);
List<FeedItemStatistics> statistics = DBReader.getFeedStatisticsList();
assertNotNull(statistics);
assertEquals(feeds.size(), statistics.size());
for (int i = 0; i < NUM_FEEDS; i++) {
@ -338,31 +322,29 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetNavDrawerDataQueueEmptyNoUnreadItems() {
final Context context = getInstrumentation().getTargetContext();
final int NUM_FEEDS = 10;
final int NUM_ITEMS = 10;
List<Feed> feeds = DBTestUtils.saveFeedlist(context, NUM_FEEDS, NUM_ITEMS, true);
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData(context);
List<Feed> feeds = DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true);
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
assertEquals(NUM_FEEDS, navDrawerData.feeds.size());
assertEquals(0, navDrawerData.numNewItems);
assertEquals(0, navDrawerData.queueSize);
}
public void testGetNavDrawerDataQueueNotEmptyWithUnreadItems() {
final Context context = getInstrumentation().getTargetContext();
final int NUM_FEEDS = 10;
final int NUM_ITEMS = 10;
final int NUM_QUEUE = 1;
final int NUM_NEW = 2;
List<Feed> feeds = DBTestUtils.saveFeedlist(context, NUM_FEEDS, NUM_ITEMS, true);
PodDBAdapter adapter = new PodDBAdapter(context);
List<Feed> feeds = DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
for (int i = 0; i < NUM_NEW; i++) {
FeedItem item = feeds.get(0).getItems().get(i);
item.setNew();
adapter.setSingleFeedItem(item);
}
List<FeedItem> queue = new ArrayList<FeedItem>();
List<FeedItem> queue = new ArrayList<>();
for (int i = 0; i < NUM_QUEUE; i++) {
FeedItem item = feeds.get(1).getItems().get(i);
queue.add(item);
@ -371,7 +353,7 @@ public class DBReaderTest extends InstrumentationTestCase {
adapter.close();
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData(context);
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
assertEquals(NUM_FEEDS, navDrawerData.feeds.size());
assertEquals(NUM_NEW, navDrawerData.numNewItems);
assertEquals(NUM_QUEUE, navDrawerData.queueSize);
@ -379,7 +361,7 @@ public class DBReaderTest extends InstrumentationTestCase {
public void testGetFeedItemlistCheckChaptersFalse() throws Exception {
Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = DBTestUtils.saveFeedlist(context, 10, 10, false, false, 0);
List<Feed> feeds = DBTestUtils.saveFeedlist(10, 10, false, false, 0);
for (Feed feed : feeds) {
for (FeedItem item : feed.getItems()) {
assertFalse(item.hasChapters());
@ -388,8 +370,7 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetFeedItemlistCheckChaptersTrue() throws Exception {
Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, 10, 10, false, true, 10);
List<Feed> feeds = saveFeedlist(10, 10, false, true, 10);
for (Feed feed : feeds) {
for (FeedItem item : feed.getItems()) {
assertTrue(item.hasChapters());
@ -398,13 +379,12 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testLoadChaptersOfFeedItemNoChapters() throws Exception {
Context context = getInstrumentation().getTargetContext();
List<Feed> feeds = saveFeedlist(context, 1, 3, false, false, 0);
saveFeedlist(context, 1, 3, false, true, 3);
List<Feed> feeds = saveFeedlist(1, 3, false, false, 0);
saveFeedlist(1, 3, false, true, 3);
for (Feed feed : feeds) {
for (FeedItem item : feed.getItems()) {
assertFalse(item.hasChapters());
DBReader.loadChaptersOfFeedItem(context, item);
DBReader.loadChaptersOfFeedItem(item);
assertFalse(item.hasChapters());
assertNull(item.getChapters());
}
@ -412,14 +392,13 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testLoadChaptersOfFeedItemWithChapters() throws Exception {
Context context = getInstrumentation().getTargetContext();
final int NUM_CHAPTERS = 3;
DBTestUtils.saveFeedlist(context, 1, 3, false, false, 0);
List<Feed> feeds = saveFeedlist(context, 1, 3, false, true, NUM_CHAPTERS);
DBTestUtils.saveFeedlist(1, 3, false, false, 0);
List<Feed> feeds = saveFeedlist(1, 3, false, true, NUM_CHAPTERS);
for (Feed feed : feeds) {
for (FeedItem item : feed.getItems()) {
assertTrue(item.hasChapters());
DBReader.loadChaptersOfFeedItem(context, item);
DBReader.loadChaptersOfFeedItem(item);
assertTrue(item.hasChapters());
assertNotNull(item.getChapters());
assertEquals(NUM_CHAPTERS, item.getChapters().size());
@ -428,11 +407,10 @@ public class DBReaderTest extends InstrumentationTestCase {
}
public void testGetItemWithChapters() throws Exception {
Context context = getInstrumentation().getTargetContext();
final int NUM_CHAPTERS = 3;
List<Feed> feeds = saveFeedlist(context, 1, 1, false, true, NUM_CHAPTERS);
List<Feed> feeds = saveFeedlist(1, 1, false, true, NUM_CHAPTERS);
FeedItem item1 = feeds.get(0).getItems().get(0);
FeedItem item2 = DBReader.getFeedItem(context, item1.getId());
FeedItem item2 = DBReader.getFeedItem(item1.getId());
assertTrue(item2.hasChapters());
assertEquals(item1.getChapters(), item2.getChapters());
}

View File

@ -12,7 +12,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
@ -21,7 +20,6 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.flattr.FlattrStatus;
import static de.test.antennapod.storage.DBTestUtils.saveFeedlist;
@ -40,7 +38,8 @@ public class DBTasksTest extends InstrumentationTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
assertTrue(PodDBAdapter.deleteDatabase(context));
assertTrue(PodDBAdapter.deleteDatabase());
for (File f : destFolder.listFiles()) {
assertTrue(f.delete());
@ -58,9 +57,9 @@ public class DBTasksTest extends InstrumentationTestCase {
assertTrue(destFolder.exists());
assertTrue(destFolder.canWrite());
context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
// make sure database is created
PodDBAdapter adapter = new PodDBAdapter(context);
// create new database
PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
@ -76,9 +75,9 @@ public class DBTasksTest extends InstrumentationTestCase {
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
Feed feed = new Feed("url", new Date(), "title");
List<FeedItem> items = new ArrayList<FeedItem>();
List<FeedItem> items = new ArrayList<>();
feed.setItems(items);
List<File> files = new ArrayList<File>();
List<File> files = new ArrayList<>();
for (int i = 0; i < NUM_ITEMS; i++) {
FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed);
@ -89,7 +88,7 @@ public class DBTasksTest extends InstrumentationTestCase {
items.add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -128,7 +127,7 @@ public class DBTasksTest extends InstrumentationTestCase {
items.add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -149,9 +148,9 @@ public class DBTasksTest extends InstrumentationTestCase {
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
Feed feed = new Feed("url", new Date(), "title");
List<FeedItem> items = new ArrayList<FeedItem>();
List<FeedItem> items = new ArrayList<>();
feed.setItems(items);
List<File> files = new ArrayList<File>();
List<File> files = new ArrayList<>();
for (int i = 0; i < NUM_ITEMS; i++) {
FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed);
@ -163,7 +162,7 @@ public class DBTasksTest extends InstrumentationTestCase {
items.add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.setQueue(items);
@ -188,14 +187,14 @@ public class DBTasksTest extends InstrumentationTestCase {
@FlakyTest(tolerance = 3)
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException {
// add feed with no enclosures so that item ID != media ID
saveFeedlist(context, 1, 10, false);
saveFeedlist(1, 10, false);
// add candidate for performAutoCleanup
List<Feed> feeds = saveFeedlist(context, 1, 1, true);
List<Feed> feeds = saveFeedlist(1, 1, true);
FeedMedia m = feeds.get(0).getItems().get(0).getMedia();
m.setDownloaded(true);
m.setFile_url("file");
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setMedia(m);
adapter.close();
@ -208,7 +207,7 @@ public class DBTasksTest extends InstrumentationTestCase {
final int NUM_ITEMS = 10;
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(), FeedItem.UNPLAYED, feed));
}
@ -228,8 +227,8 @@ public class DBTasksTest extends InstrumentationTestCase {
Feed feed1 = new Feed("url1", new Date(), "title");
Feed feed2 = new Feed("url2", new Date(), "title");
feed1.setItems(new ArrayList<FeedItem>());
feed2.setItems(new ArrayList<FeedItem>());
feed1.setItems(new ArrayList<>());
feed2.setItems(new ArrayList<>());
Feed savedFeed1 = DBTasks.updateFeed(context, feed1)[0];
Feed savedFeed2 = DBTasks.updateFeed(context, feed2)[0];
@ -242,11 +241,11 @@ public class DBTasksTest extends InstrumentationTestCase {
final int NUM_ITEMS_NEW = 10;
final Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS_OLD; i++) {
feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(i), FeedItem.PLAYED, feed));
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -271,7 +270,7 @@ public class DBTasksTest extends InstrumentationTestCase {
updatedFeedTest(newFeed, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW);
final Feed feedFromDB = DBReader.getFeed(context, newFeed.getId());
final Feed feedFromDB = DBReader.getFeed(newFeed.getId());
assertNotNull(feedFromDB);
assertTrue(feedFromDB.getId() == newFeed.getId());
updatedFeedTest(feedFromDB, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW);

View File

@ -1,7 +1,5 @@
package de.test.antennapod.storage;
import android.content.Context;
import junit.framework.Assert;
import java.util.ArrayList;
@ -26,14 +24,14 @@ public class DBTestUtils {
/**
* Use this method when tests don't involve chapters.
*/
public static List<Feed> saveFeedlist(Context context, int numFeeds, int numItems, boolean withMedia) {
return saveFeedlist(context, numFeeds, numItems, withMedia, false, 0);
public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia) {
return saveFeedlist(numFeeds, numItems, withMedia, false, 0);
}
/**
* Use this method when tests involve chapters.
*/
public static List<Feed> saveFeedlist(Context context, int numFeeds, int numItems, boolean withMedia,
public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia,
boolean withChapters, int numChapters) {
if (numFeeds <= 0) {
throw new IllegalArgumentException("numFeeds<=0");
@ -42,13 +40,13 @@ public class DBTestUtils {
throw new IllegalArgumentException("numItems<0");
}
List<Feed> feeds = new ArrayList<Feed>();
PodDBAdapter adapter = new PodDBAdapter(context);
List<Feed> feeds = new ArrayList<>();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
for (int i = 0; i < numFeeds; i++) {
Feed f = new Feed(0, new Date(), "feed " + i, "link" + i, "descr", null, null,
null, null, "id" + i, null, null, "url" + i, false, new FlattrStatus(), false, null, null, false);
f.setItems(new ArrayList<FeedItem>());
f.setItems(new ArrayList<>());
for (int j = 0; j < numItems; j++) {
FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(),
FeedItem.PLAYED, f, withChapters);

View File

@ -29,6 +29,7 @@ import de.danoeh.antennapod.core.storage.PodDBAdapter;
* Test class for DBWriter
*/
public class DBWriterTest extends InstrumentationTestCase {
private static final String TAG = "DBWriterTest";
private static final String TEST_FOLDER = "testDBWriter";
private static final long TIMEOUT = 5L;
@ -36,9 +37,10 @@ public class DBWriterTest extends InstrumentationTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
final Context context = getInstrumentation().getTargetContext();
assertTrue(PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext()));
assertTrue(PodDBAdapter.deleteDatabase());
final Context context = getInstrumentation().getTargetContext();
File testDir = context.getExternalFilesDir(TEST_FOLDER);
assertNotNull(testDir);
for (File f : testDir.listFiles()) {
@ -49,10 +51,10 @@ public class DBWriterTest extends InstrumentationTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
final Context context = getInstrumentation().getTargetContext();
context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
// make sure database is created
PodDBAdapter adapter = new PodDBAdapter(context);
// create new database
PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
}
@ -63,7 +65,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertTrue(dest.createNewFile());
Feed feed = new Feed("url", new Date(), "title");
List<FeedItem> items = new ArrayList<FeedItem>();
List<FeedItem> items = new ArrayList<>();
feed.setItems(items);
FeedItem item = new FeedItem(0, "Item", "Item", "url", new Date(), FeedItem.PLAYED, feed);
@ -72,7 +74,7 @@ public class DBWriterTest extends InstrumentationTestCase {
items.add(item);
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -80,7 +82,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertTrue(item.getId() != 0);
DBWriter.deleteFeedMediaOfItem(getInstrumentation().getTargetContext(), media.getId()).get();
media = DBReader.getFeedMedia(getInstrumentation().getTargetContext(), media.getId());
media = DBReader.getFeedMedia(media.getId());
assertNotNull(media);
assertFalse(dest.exists());
assertFalse(media.isDownloaded());
@ -92,7 +94,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertNotNull(destFolder);
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
// create Feed image
File imgFile = new File(destFolder, "image");
@ -118,7 +120,7 @@ public class DBWriterTest extends InstrumentationTestCase {
item.getChapters().add(new SimpleChapter(0, "item " + i, item, "example.com"));
}
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -139,7 +141,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertFalse(f.exists());
}
adapter = new PodDBAdapter(getInstrumentation().getContext());
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor c = adapter.getFeedCursor(feed.getId());
assertEquals(0, c.getCount());
@ -164,7 +166,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertNotNull(destFolder);
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
feed.setImage(null);
@ -182,7 +184,7 @@ public class DBWriterTest extends InstrumentationTestCase {
item.setMedia(media);
}
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -200,7 +202,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertFalse(f.exists());
}
adapter = new PodDBAdapter(getInstrumentation().getContext());
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor c = adapter.getFeedCursor(feed.getId());
assertTrue(c.getCount() == 0);
@ -229,7 +231,7 @@ public class DBWriterTest extends InstrumentationTestCase {
image.setOwner(feed);
feed.setImage(image);
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -242,7 +244,7 @@ public class DBWriterTest extends InstrumentationTestCase {
// check if files still exist
assertFalse(imgFile.exists());
adapter = new PodDBAdapter(getInstrumentation().getContext());
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor c = adapter.getFeedCursor(feed.getId());
assertTrue(c.getCount() == 0);
@ -257,7 +259,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertNotNull(destFolder);
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
// create Feed image
File imgFile = new File(destFolder, "image");
@ -273,7 +275,7 @@ public class DBWriterTest extends InstrumentationTestCase {
}
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -289,7 +291,7 @@ public class DBWriterTest extends InstrumentationTestCase {
// check if files still exist
assertFalse(imgFile.exists());
adapter = new PodDBAdapter(getInstrumentation().getContext());
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor c = adapter.getFeedCursor(feed.getId());
assertTrue(c.getCount() == 0);
@ -309,7 +311,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertNotNull(destFolder);
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
// create Feed image
File imgFile = new File(destFolder, "image");
@ -327,7 +329,7 @@ public class DBWriterTest extends InstrumentationTestCase {
item.setImage(itemImage);
}
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -344,7 +346,7 @@ public class DBWriterTest extends InstrumentationTestCase {
// check if files still exist
assertFalse(imgFile.exists());
adapter = new PodDBAdapter(getInstrumentation().getContext());
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor c = adapter.getFeedCursor(feed.getId());
assertTrue(c.getCount() == 0);
@ -367,7 +369,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertNotNull(destFolder);
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
// create Feed image
File imgFile = new File(destFolder, "image");
@ -388,7 +390,7 @@ public class DBWriterTest extends InstrumentationTestCase {
item.setMedia(media);
}
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -460,7 +462,7 @@ public class DBWriterTest extends InstrumentationTestCase {
item.setMedia(media);
}
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getContext());
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -474,7 +476,7 @@ public class DBWriterTest extends InstrumentationTestCase {
DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
adapter = new PodDBAdapter(getInstrumentation().getContext());
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor c = adapter.getFeedCursor(feed.getId());
assertTrue(c.getCount() == 0);
@ -500,7 +502,7 @@ public class DBWriterTest extends InstrumentationTestCase {
FeedMedia media = new FeedMedia(0, item, 10, 0, 1, "mime", null, "url", false, playbackCompletionDate, 0);
feed.getItems().add(item);
item.setMedia(media);
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -509,13 +511,11 @@ public class DBWriterTest extends InstrumentationTestCase {
}
public void testAddItemToPlaybackHistoryNotPlayedYet() throws ExecutionException, InterruptedException {
final Context context = getInstrumentation().getTargetContext();
FeedMedia media = playbackHistorySetup(null);
DBWriter.addItemToPlaybackHistory(context, media).get();
PodDBAdapter adapter = new PodDBAdapter(context);
DBWriter.addItemToPlaybackHistory(media).get();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
media = DBReader.getFeedMedia(context, media.getId());
media = DBReader.getFeedMedia(media.getId());
adapter.close();
assertNotNull(media);
@ -524,13 +524,12 @@ public class DBWriterTest extends InstrumentationTestCase {
public void testAddItemToPlaybackHistoryAlreadyPlayed() throws ExecutionException, InterruptedException {
final long OLD_DATE = 0;
final Context context = getInstrumentation().getTargetContext();
FeedMedia media = playbackHistorySetup(new Date(OLD_DATE));
DBWriter.addItemToPlaybackHistory(getInstrumentation().getTargetContext(), media).get();
PodDBAdapter adapter = new PodDBAdapter(context);
DBWriter.addItemToPlaybackHistory(media).get();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
media = DBReader.getFeedMedia(context, media.getId());
media = DBReader.getFeedMedia(media.getId());
adapter.close();
assertNotNull(media);
@ -541,13 +540,13 @@ public class DBWriterTest extends InstrumentationTestCase {
private Feed queueTestSetupMultipleItems(final int NUM_ITEMS) throws InterruptedException, ExecutionException, TimeoutException {
final Context context = getInstrumentation().getTargetContext();
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.PLAYED, feed);
feed.getItems().add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -568,11 +567,11 @@ public class DBWriterTest extends InstrumentationTestCase {
public void testAddQueueItemSingleItem() throws InterruptedException, ExecutionException, TimeoutException {
final Context context = getInstrumentation().getTargetContext();
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed);
feed.getItems().add(item);
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -580,7 +579,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertTrue(item.getId() != 0);
DBWriter.addQueueItem(context, item.getId()).get(TIMEOUT, TimeUnit.SECONDS);
adapter = new PodDBAdapter(context);
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor cursor = adapter.getQueueIDCursor();
assertTrue(cursor.moveToFirst());
@ -592,11 +591,11 @@ public class DBWriterTest extends InstrumentationTestCase {
public void testAddQueueItemSingleItemAlreadyInQueue() throws InterruptedException, ExecutionException, TimeoutException {
final Context context = getInstrumentation().getTargetContext();
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed);
feed.getItems().add(item);
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -604,7 +603,7 @@ public class DBWriterTest extends InstrumentationTestCase {
assertTrue(item.getId() != 0);
DBWriter.addQueueItem(context, item.getId()).get(TIMEOUT, TimeUnit.SECONDS);
adapter = new PodDBAdapter(context);
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor cursor = adapter.getQueueIDCursor();
assertTrue(cursor.moveToFirst());
@ -613,7 +612,7 @@ public class DBWriterTest extends InstrumentationTestCase {
adapter.close();
DBWriter.addQueueItem(context, item.getId()).get(TIMEOUT, TimeUnit.SECONDS);
adapter = new PodDBAdapter(context);
adapter = PodDBAdapter.getInstance();
adapter.open();
cursor = adapter.getQueueIDCursor();
assertTrue(cursor.moveToFirst());
@ -628,7 +627,7 @@ public class DBWriterTest extends InstrumentationTestCase {
final int NUM_ITEMS = 10;
Feed feed = queueTestSetupMultipleItems(NUM_ITEMS);
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor cursor = adapter.getQueueIDCursor();
assertTrue(cursor.moveToFirst());
@ -642,12 +641,11 @@ public class DBWriterTest extends InstrumentationTestCase {
}
public void testClearQueue() throws InterruptedException, ExecutionException, TimeoutException {
final Context context = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10;
Feed feed = queueTestSetupMultipleItems(NUM_ITEMS);
DBWriter.clearQueue(context).get(TIMEOUT, TimeUnit.SECONDS);
PodDBAdapter adapter = new PodDBAdapter(context);
DBWriter.clearQueue().get(TIMEOUT, TimeUnit.SECONDS);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor cursor = adapter.getQueueIDCursor();
assertFalse(cursor.moveToFirst());
@ -659,13 +657,13 @@ public class DBWriterTest extends InstrumentationTestCase {
final int NUM_ITEMS = 10;
final Context context = getInstrumentation().getTargetContext();
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.PLAYED, feed);
feed.getItems().add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -675,13 +673,13 @@ public class DBWriterTest extends InstrumentationTestCase {
}
for (int removeIndex = 0; removeIndex < NUM_ITEMS; removeIndex++) {
final FeedItem item = feed.getItems().get(removeIndex);
adapter = new PodDBAdapter(context);
adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setQueue(feed.getItems());
adapter.close();
DBWriter.removeQueueItem(context, item, false).get(TIMEOUT, TimeUnit.SECONDS);
adapter = new PodDBAdapter(context);
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor queue = adapter.getQueueIDCursor();
assertTrue(queue.getCount() == NUM_ITEMS - 1);
@ -703,15 +701,14 @@ public class DBWriterTest extends InstrumentationTestCase {
public void testMoveQueueItem() throws InterruptedException, ExecutionException, TimeoutException {
final int NUM_ITEMS = 10;
final Context context = getInstrumentation().getTargetContext();
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.PLAYED, feed);
feed.getItems().add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -727,13 +724,13 @@ public class DBWriterTest extends InstrumentationTestCase {
Log.d(TAG, String.format("testMoveQueueItem: From=%d, To=%d", from, to));
final long fromID = feed.getItems().get(from).getId();
adapter = new PodDBAdapter(context);
adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setQueue(feed.getItems());
adapter.close();
DBWriter.moveQueueItem(context, from, to, false).get(TIMEOUT, TimeUnit.SECONDS);
adapter = new PodDBAdapter(context);
DBWriter.moveQueueItem(from, to, false).get(TIMEOUT, TimeUnit.SECONDS);
adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor queue = adapter.getQueueIDCursor();
assertTrue(queue.getCount() == NUM_ITEMS);
@ -749,7 +746,6 @@ public class DBWriterTest extends InstrumentationTestCase {
}
public void testMarkFeedRead() throws InterruptedException, ExecutionException, TimeoutException {
final Context context = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10;
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
@ -758,7 +754,7 @@ public class DBWriterTest extends InstrumentationTestCase {
feed.getItems().add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -768,24 +764,23 @@ public class DBWriterTest extends InstrumentationTestCase {
assertTrue(item.getId() != 0);
}
DBWriter.markFeedRead(context, feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
List<FeedItem> loadedItems = DBReader.getFeedItemList(context, feed);
DBWriter.markFeedRead(feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
List<FeedItem> loadedItems = DBReader.getFeedItemList(feed);
for (FeedItem item : loadedItems) {
assertTrue(item.isPlayed());
}
}
public void testMarkAllItemsReadSameFeed() throws InterruptedException, ExecutionException, TimeoutException {
final Context context = getInstrumentation().getTargetContext();
final int NUM_ITEMS = 10;
Feed feed = new Feed("url", new Date(), "title");
feed.setItems(new ArrayList<FeedItem>());
feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.UNPLAYED, feed);
feed.getItems().add(item);
}
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(feed);
adapter.close();
@ -795,8 +790,8 @@ public class DBWriterTest extends InstrumentationTestCase {
assertTrue(item.getId() != 0);
}
DBWriter.markAllItemsRead(context).get(TIMEOUT, TimeUnit.SECONDS);
List<FeedItem> loadedItems = DBReader.getFeedItemList(context, feed);
DBWriter.markAllItemsRead().get(TIMEOUT, TimeUnit.SECONDS);
List<FeedItem> loadedItems = DBReader.getFeedItemList(feed);
for (FeedItem item : loadedItems) {
assertTrue(item.isPlayed());
}

View File

@ -44,8 +44,10 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActiv
solo = new Solo(getInstrumentation(), getActivity());
uiTestUtils = new UITestUtils(getInstrumentation().getTargetContext());
uiTestUtils.setup();
// create database
PodDBAdapter adapter = new PodDBAdapter(getInstrumentation().getTargetContext());
// create new database
PodDBAdapter.deleteDatabase();
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
@ -59,7 +61,7 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActiv
uiTestUtils.tearDown();
solo.finishOpenedActivities();
PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext());
PodDBAdapter.deleteDatabase();
// reset preferences
prefs.edit().clear().commit();

View File

@ -1,12 +1,14 @@
package de.test.antennapod.ui;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.FlakyTest;
import android.widget.ImageButton;
import com.robotium.solo.Condition;
import com.robotium.solo.Solo;
import com.robotium.solo.Timeout;
@ -15,22 +17,120 @@ import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
/**
* Test cases for starting and ending playback from the MainActivity and AudioPlayerActivity
* test cases for starting and ending playback from the MainActivity and AudioPlayerActivity
*/
public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity> {
private static final String TAG = PlaybackTest.class.getSimpleName();
private Solo solo;
private UITestUtils uiTestUtils;
private Context context;
private PlaybackController controller;
protected FeedMedia currentMedia;
private PlaybackController createController(Activity activity) {
return new PlaybackController(activity, false) {
@Override
public void setupGUI() {
}
@Override
public void onPositionObserverUpdate() {
}
@Override
public void onBufferStart() {
}
@Override
public void onBufferEnd() {
}
@Override
public void onBufferUpdate(float progress) {
}
@Override
public void handleError(int code) {
}
@Override
public void onReloadNotification(int code) {
}
@Override
public void onSleepTimerUpdate() {
}
@Override
public ImageButton getPlayButton() {
return null;
}
@Override
public void postStatusMsg(int msg) {
}
@Override
public void clearStatusMsg() {
}
@Override
public boolean loadMediaInfo() {
Playable playable = controller.getMedia();
if(playable == null) {
currentMedia = null;
return true;
} else if(playable instanceof FeedMedia) {
currentMedia = (FeedMedia) playable;
return true;
} else {
return false;
}
}
@Override
public void onAwaitingVideoSurface() {
}
@Override
public void onServiceQueried() {
}
@Override
public void onShutdownNotification() {
}
@Override
public void onPlaybackEnd() {
currentMedia = null;
}
@Override
public void onPlaybackSpeedChange() {
}
@Override
protected void setScreenOn(boolean enable) {
}
};
}
public PlaybackTest() {
super(MainActivity.class);
}
@ -38,30 +138,36 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
@Override
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
context = getInstrumentation().getContext();
PodDBAdapter.deleteDatabase();
controller = createController(getActivity());
controller.init();
solo = new Solo(getInstrumentation(), getActivity());
context = getInstrumentation().getTargetContext();
uiTestUtils = new UITestUtils(context);
uiTestUtils.setup();
// create database
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.close();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit()
.clear()
.putBoolean(UserPreferences.PREF_UNPAUSE_ON_HEADSET_RECONNECT, false)
.putBoolean(UserPreferences.PREF_PAUSE_ON_HEADSET_DISCONNECT, false)
.putString(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS, "")
.commit();
}
@Override
public void tearDown() throws Exception {
uiTestUtils.tearDown();
controller.release();
solo.finishOpenedActivities();
PodDBAdapter.deleteDatabase(context);
uiTestUtils.tearDown();
// shut down playback service
skipEpisode();
@ -85,87 +191,106 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
}
private void startLocalPlayback() {
openNavDrawer();
solo.clickOnText(solo.getString(R.string.episodes_label));
solo.clickOnText(solo.getString(R.string.all_episodes_short_label));
final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(context, 10);
String allEpisodesLabel = solo.getString(R.string.all_episodes_label);
if(!getActivity().getSupportActionBar().getTitle().equals(allEpisodesLabel)) {
openNavDrawer();
solo.clickOnText(solo.getString(R.string.episodes_label));
solo.clickOnText(solo.getString(R.string.all_episodes_short_label));
}
final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(10);
assertTrue(solo.waitForView(solo.getView(R.id.butSecondaryAction)));
solo.clickOnView(solo.getView(R.id.butSecondaryAction));
assertTrue(solo.waitForView(solo.getView(R.id.butPlay)));
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return episodes.get(0).getMedia().isCurrentlyPlaying();
long mediaId = episodes.get(0).getMedia().getId();
boolean playing = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == mediaId;
} else {
return false;
}
}, Timeout.getLargeTimeout());
}, Timeout.getSmallTimeout());
assertTrue(playing);
}
private void startLocalPlaybackFromQueue() {
openNavDrawer();
solo.clickOnText(solo.getString(R.string.queue_label));
assertTrue(solo.waitForView(solo.getView(R.id.butSecondaryAction)));
final List<FeedItem> queue = DBReader.getQueue(context);
final List<FeedItem> queue = DBReader.getQueue();
solo.clickOnImageButton(1);
assertTrue(solo.waitForView(solo.getView(R.id.butPlay)));
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return queue.get(0).getMedia().isCurrentlyPlaying();
long mediaId = queue.get(0).getMedia().getId();
boolean playing = solo.waitForCondition(() -> {
if(currentMedia != null) {
return currentMedia.getId() == mediaId;
} else {
return false;
}
}, Timeout.getLargeTimeout());
}, Timeout.getSmallTimeout());
assertTrue(playing);
}
public void testStartLocal() throws Exception {
uiTestUtils.addLocalFeedData(true);
DBWriter.clearQueue(context).get();
DBWriter.clearQueue().get();
startLocalPlayback();
}
public void testContinousPlaybackOffSingleEpisode() throws Exception {
setContinuousPlaybackPreference(false);
uiTestUtils.addLocalFeedData(true);
DBWriter.clearQueue(context).get();
DBWriter.clearQueue().get();
startLocalPlayback();
}
@FlakyTest(tolerance = 3)
public void testContinousPlaybackOffMultipleEpisodes() throws Exception {
setContinuousPlaybackPreference(false);
uiTestUtils.addLocalFeedData(true);
List<FeedItem> queue = DBReader.getQueue(context);
List<FeedItem> queue = DBReader.getQueue();
final FeedItem first = queue.get(0);
final FeedItem second = queue.get(1);
startLocalPlaybackFromQueue();
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return first.getMedia().isCurrentlyPlaying() == false;
boolean stopped = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() != first.getMedia().getId();
} else {
return false;
}
}, 10000);
}, Timeout.getSmallTimeout());
assertTrue(stopped);
Thread.sleep(1000);
assertTrue(second.getMedia().isCurrentlyPlaying() == false);
PlayerStatus status = controller.getStatus();
assertFalse(status.equals(PlayerStatus.PLAYING));
}
@FlakyTest(tolerance = 3)
public void testContinuousPlaybackOnMultipleEpisodes() throws Exception {
setContinuousPlaybackPreference(true);
uiTestUtils.addLocalFeedData(true);
List<FeedItem> queue = DBReader.getQueue(context);
List<FeedItem> queue = DBReader.getQueue();
final FeedItem first = queue.get(0);
final FeedItem second = queue.get(1);
startLocalPlaybackFromQueue();
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return first.getMedia().isCurrentlyPlaying() == false;
boolean firstPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == first.getMedia().getId();
} else {
return false;
}
}, 10000);
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return second.getMedia().isCurrentlyPlaying() == true;
}, Timeout.getSmallTimeout());
assertTrue(firstPlaying);
boolean secondPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == second.getMedia().getId();
} else {
return false;
}
}, 10000);
}, Timeout.getLargeTimeout());
assertTrue(secondPlaying);
}
/**
@ -174,24 +299,34 @@ public class PlaybackTest extends ActivityInstrumentationTestCase2<MainActivity>
private void replayEpisodeCheck(boolean followQueue) throws Exception {
setContinuousPlaybackPreference(followQueue);
uiTestUtils.addLocalFeedData(true);
DBWriter.clearQueue(context).get();
final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(context, 10);
DBWriter.clearQueue().get();
final List<FeedItem> episodes = DBReader.getRecentlyPublishedEpisodes(10);
startLocalPlayback();
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return false == episodes.get(0).getMedia().isCurrentlyPlaying();
long mediaId = episodes.get(0).getMedia().getId();
boolean startedPlaying = solo.waitForCondition(() -> {
if (currentMedia != null) {
return currentMedia.getId() == mediaId;
} else {
return false;
}
}, Timeout.getSmallTimeout());
assertTrue(startedPlaying);
boolean stoppedPlaying = solo.waitForCondition(() -> {
return currentMedia == null || currentMedia.getId() != mediaId;
}, Timeout.getLargeTimeout());
assertTrue(stoppedPlaying);
startLocalPlayback();
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {
return false == episodes.get(0).getMedia().isCurrentlyPlaying();
boolean startedReplay = solo.waitForCondition(() -> {
if(currentMedia != null) {
return currentMedia.getId() == mediaId;
} else {
return false;
}
}, Timeout.getLargeTimeout());
assertTrue(startedReplay);
}
public void testReplayEpisodeContinuousPlaybackOn() throws Exception {

View File

@ -4,6 +4,7 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import junit.framework.Assert;
@ -38,6 +39,8 @@ import de.test.antennapod.util.syndication.feedgenerator.RSS2Generator;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class UITestUtils {
private static final String TAG = UITestUtils.class.getSimpleName();
private static final String DATA_FOLDER = "test/UITestUtils";
public static final int NUM_FEEDS = 5;
@ -79,7 +82,7 @@ public class UITestUtils {
server.stop();
if (localFeedDataAdded) {
PodDBAdapter.deleteDatabase(context);
PodDBAdapter.deleteDatabase();
}
}
@ -174,16 +177,15 @@ public class UITestUtils {
*/
public void addLocalFeedData(boolean downloadEpisodes) throws Exception {
if (localFeedDataAdded) {
throw new IllegalStateException("addLocalFeedData was called twice on the same instance");
Log.w(TAG, "addLocalFeedData was called twice on the same instance");
// might be a flaky test, this is actually not that severe
return;
}
if (!feedDataHosted) {
addHostedFeedData();
}
List<FeedItem> queue = new ArrayList<FeedItem>();
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
List<FeedItem> queue = new ArrayList<>();
for (Feed feed : hostedFeeds) {
feed.setDownloaded(true);
if (feed.getImage() != null) {
@ -206,6 +208,10 @@ public class UITestUtils {
queue.add(feed.getItems().get(0));
feed.getItems().get(1).getMedia().setPlaybackCompletionDate(new Date());
}
localFeedDataAdded = true;
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.setCompleteFeed(hostedFeeds.toArray(new Feed[hostedFeeds.size()]));
adapter.setQueue(queue);
adapter.close();

View File

@ -9,6 +9,7 @@ import com.joanzapata.iconify.fonts.FontAwesomeModule;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.spa.SPAUtil;
@ -40,6 +41,7 @@ public class PodcastApp extends Application {
singleton = this;
LOGICAL_DENSITY = getResources().getDisplayMetrics().density;
PodDBAdapter.init(this);
UpdateManager.init(this);
UserPreferences.init(this);
PlaybackPreferences.init(this);

View File

@ -65,9 +65,9 @@ public class UpdateManager {
// from now on, Glide will handle caching images
new Thread() {
public void run() {
List<Feed> feeds = DBReader.getFeedList(context);
List<Feed> feeds = DBReader.getFeedList();
for (Feed podcast : feeds) {
List<FeedItem> episodes = DBReader.getFeedItemList(context, podcast);
List<FeedItem> episodes = DBReader.getFeedItemList(podcast);
for (FeedItem episode : episodes) {
FeedImage image = episode.getImage();
if (image != null && image.isDownloaded() && image.getFile_url() != null) {
@ -76,7 +76,7 @@ public class UpdateManager {
imageFile.delete();
}
image.setFile_url(null); // calls setDownloaded(false)
DBWriter.setFeedImage(context, image);
DBWriter.setFeedImage(image);
}
}
}

View File

@ -6,7 +6,6 @@ import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
@ -60,6 +59,10 @@ import de.danoeh.antennapod.fragment.CoverFragment;
import de.danoeh.antennapod.fragment.ItemDescriptionFragment;
import de.danoeh.antennapod.menuhandler.NavDrawerActivity;
import de.danoeh.antennapod.preferences.PreferenceController;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Activity for playing audio files.
@ -104,6 +107,8 @@ public class AudioplayerActivity extends MediaplayerActivity implements ItemDesc
private ImageButton butNavChaptersShownotes;
private ImageButton butShowCover;
private Subscription subscription;
private PopupWindow popupWindow;
private void resetFragmentView() {
@ -145,7 +150,9 @@ public class AudioplayerActivity extends MediaplayerActivity implements ItemDesc
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop()");
cancelLoadTask();
if(subscription != null) {
subscription.unsubscribe();
}
EventDistributor.getInstance().unregister(contentUpdate);
}
@ -716,10 +723,10 @@ public class AudioplayerActivity extends MediaplayerActivity implements ItemDesc
Feed feed = navDrawerData.feeds.get(position - navAdapter.getSubscriptionOffset());
switch(item.getItemId()) {
case R.id.mark_all_seen_item:
DBWriter.markFeedSeen(this, feed.getId());
DBWriter.markFeedSeen(feed.getId());
return true;
case R.id.mark_all_read_item:
DBWriter.markFeedRead(this, feed.getId());
DBWriter.markFeedRead(feed.getId());
return true;
case R.id.remove_item:
final FeedRemover remover = new FeedRemover(this, feed) {
@ -747,32 +754,22 @@ public class AudioplayerActivity extends MediaplayerActivity implements ItemDesc
private DBReader.NavDrawerData navDrawerData;
private AsyncTask<Void, Void, DBReader.NavDrawerData> loadTask;
private void loadData() {
loadTask = new AsyncTask<Void, Void, DBReader.NavDrawerData>() {
@Override
protected DBReader.NavDrawerData doInBackground(Void... params) {
return DBReader.getNavDrawerData(AudioplayerActivity.this);
}
@Override
protected void onPostExecute(DBReader.NavDrawerData result) {
super.onPostExecute(result);
navDrawerData = result;
if (navAdapter != null) {
navAdapter.notifyDataSetChanged();
}
}
};
loadTask.execute();
subscription = Observable.defer(() -> Observable.just(DBReader.getNavDrawerData()))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
navDrawerData = result;
if (navAdapter != null) {
navAdapter.notifyDataSetChanged();
}
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
});
}
private void cancelLoadTask() {
if (loadTask != null) {
loadTask.cancel(true);
}
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

View File

@ -26,7 +26,6 @@ import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.joanzapata.iconify.Iconify;
import de.danoeh.antennapod.R;
@ -111,7 +110,7 @@ public class FeedInfoActivity extends ActionBarActivity {
@Override
protected Feed doInBackground(Long... params) {
return DBReader.getFeed(FeedInfoActivity.this, params[0]);
return DBReader.getFeed(params[0]);
}
@Override
@ -239,7 +238,7 @@ public class FeedInfoActivity extends ActionBarActivity {
prefs.setPassword(etxtPassword.getText().toString());
}
if (authInfoChanged || autoDeleteChanged) {
DBWriter.setFeedPreferences(this, prefs);
DBWriter.setFeedPreferences(prefs);
}
authInfoChanged = false;
autoDeleteChanged = false;
@ -299,7 +298,7 @@ public class FeedInfoActivity extends ActionBarActivity {
@Override
public void onConfirmButtonPressed(DialogInterface dialog) {
DBWriter.setFeedsItemsAutoDownload(context, feed, autoDownload);
DBWriter.setFeedsItemsAutoDownload(feed, autoDownload);
}
}

View File

@ -58,6 +58,10 @@ import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.menuhandler.NavDrawerActivity;
import de.danoeh.antennapod.preferences.PreferenceController;
import de.greenrobot.event.EventBus;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* The activity that is shown when the user launches the app.
@ -106,6 +110,8 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
private ProgressDialog pd;
private Subscription subscription;
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getNoTitleTheme());
@ -478,9 +484,11 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
@Override
protected void onStop() {
super.onStop();
cancelLoadTask();
EventDistributor.getInstance().unregister(contentUpdate);
EventBus.getDefault().unregister(this);
if(subscription != null) {
subscription.unsubscribe();
}
if(pd != null) {
pd.dismiss();
}
@ -551,10 +559,10 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
Feed feed = navDrawerData.feeds.get(position - navAdapter.getSubscriptionOffset());
switch(item.getItemId()) {
case R.id.mark_all_seen_item:
DBWriter.markFeedSeen(this, feed.getId());
DBWriter.markFeedSeen(feed.getId());
return true;
case R.id.mark_all_read_item:
DBWriter.markFeedRead(this, feed.getId());
DBWriter.markFeedRead(feed.getId());
return true;
case R.id.remove_item:
final FeedRemover remover = new FeedRemover(this, feed) {
@ -629,33 +637,21 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
};
private void loadData() {
cancelLoadTask();
loadTask = new AsyncTask<Void, Void, DBReader.NavDrawerData>() {
@Override
protected DBReader.NavDrawerData doInBackground(Void... params) {
return DBReader.getNavDrawerData(MainActivity.this);
}
subscription = Observable.defer(() -> Observable.just(DBReader.getNavDrawerData()))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
boolean handleIntent = (navDrawerData == null);
@Override
protected void onPostExecute(DBReader.NavDrawerData result) {
super.onPostExecute(navDrawerData);
boolean handleIntent = (navDrawerData == null);
navDrawerData = result;
navAdapter.notifyDataSetChanged();
navDrawerData = result;
navAdapter.notifyDataSetChanged();
if (handleIntent) {
handleNavIntent();
}
}
};
loadTask.execute();
}
private void cancelLoadTask() {
if (loadTask != null) {
loadTask.cancel(true);
}
if (handleIntent) {
handleNavIntent();
}
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
});
}
public void onEvent(QueueEvent event) {

View File

@ -109,7 +109,7 @@ public class OnlineFeedViewActivity extends ActionBarActivity {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((arg & EventDistributor.FEED_LIST_UPDATE) != 0) {
updater = Observable.defer(() -> Observable.just(DBReader.getFeedList(OnlineFeedViewActivity.this)))
updater = Observable.defer(() -> Observable.just(DBReader.getFeedList()))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(feeds -> {
@ -263,7 +263,7 @@ public class OnlineFeedViewActivity extends ActionBarActivity {
download = Observable.create(new Observable.OnSubscribe<DownloadStatus>() {
@Override
public void call(Subscriber<? super DownloadStatus> subscriber) {
feeds = DBReader.getFeedList(OnlineFeedViewActivity.this);
feeds = DBReader.getFeedList();
downloader = new HttpDownloader(request);
downloader.call();
Log.d(TAG, "Download was completed");

View File

@ -64,7 +64,7 @@ public class ActionButtonUtils {
butSecondary.setContentDescription(context.getString(labels[1]));
} else {
// item is not downloaded and not being downloaded
LongList queueIds = DBReader.getQueueIDList(context);
LongList queueIds = DBReader.getQueueIDList();
if(DefaultActionButtonCallback.userAllowedMobileDownloads() ||
!DefaultActionButtonCallback.userChoseAddToQueue() || queueIds.contains(item.getId())) {
butSecondary.setVisibility(View.VISIBLE);

View File

@ -57,7 +57,7 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
final FeedMedia media = item.getMedia();
boolean isDownloading = DownloadRequester.getInstance().isDownloadingFile(media);
if (!isDownloading && !media.isDownloaded()) {
LongList queueIds = DBReader.getQueueIDList(context);
LongList queueIds = DBReader.getQueueIDList();
if (NetworkUtils.isDownloadAllowed() || userAllowedMobileDownloads()) {
try {
DBTasks.downloadFeedItems(context, item);
@ -75,7 +75,7 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
} else if (isDownloading) {
DownloadRequester.getInstance().cancelDownload(context, media);
if(UserPreferences.isEnableAutodownload()) {
DBWriter.setFeedItemAutoDownload(context, media.getItem(), false);
DBWriter.setFeedItemAutoDownload(media.getItem(), false);
Toast.makeText(context, R.string.download_canceled_autodownload_enabled_msg, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, R.string.download_canceled_msg, Toast.LENGTH_LONG).show();
@ -93,7 +93,7 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
}
} else {
if (!item.isPlayed()) {
DBWriter.markItemPlayed(context, item, FeedItem.PLAYED, true);
DBWriter.markItemPlayed(item, FeedItem.PLAYED, true);
}
}
}
@ -117,7 +117,7 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
}
}
});
LongList queueIds = DBReader.getQueueIDList(context);
LongList queueIds = DBReader.getQueueIDList();
if(!queueIds.contains(item.getId())) {
builder.setNeutralButton(context.getText(R.string.confirm_mobile_download_dialog_only_add_to_queue),
new DialogInterface.OnClickListener() {

View File

@ -123,7 +123,7 @@ public class DownloadLogAdapter extends BaseAdapter {
public void onClick(View v) {
ButtonHolder holder = (ButtonHolder) v.getTag();
if(holder.typeId == Feed.FEEDFILETYPE_FEED) {
Feed feed = DBReader.getFeed(context, holder.id);
Feed feed = DBReader.getFeed(holder.id);
if (feed != null) {
feed.setLastUpdate(new Date(0)); // force refresh
try {
@ -135,7 +135,7 @@ public class DownloadLogAdapter extends BaseAdapter {
Log.wtf(TAG, "Could not find feed for feed id: " + holder.id);
}
} else if(holder.typeId == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
FeedMedia media = DBReader.getFeedMedia(context, holder.id);
FeedMedia media = DBReader.getFeedMedia(holder.id);
try {
DBTasks.downloadFeedItems(context, media.getItem());
Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show();

View File

@ -59,7 +59,7 @@ public class OpmlExportWorker extends AsyncTask<Void, Void, Void> {
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(output), LangUtils.UTF_8);
opmlWriter.writeDocument(DBReader.getFeedList(context), writer);
opmlWriter.writeDocument(DBReader.getFeedList(), writer);
} catch (IOException e) {
e.printStackTrace();
exception = e;

View File

@ -372,12 +372,12 @@ public class EpisodesApplyActionFragment extends Fragment {
}
private void markedCheckedPlayed() {
DBWriter.markItemPlayed(getActivity(), FeedItem.PLAYED, checkedIds.toArray());
DBWriter.markItemPlayed(FeedItem.PLAYED, checkedIds.toArray());
close();
}
private void markedCheckedUnplayed() {
DBWriter.markItemPlayed(getActivity(), FeedItem.UNPLAYED, checkedIds.toArray());
DBWriter.markItemPlayed(FeedItem.UNPLAYED, checkedIds.toArray());
close();
}

View File

@ -38,6 +38,7 @@ import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.QueueEvent;
import de.danoeh.antennapod.core.service.download.DownloadService;
import de.danoeh.antennapod.core.service.download.Downloader;
import de.danoeh.antennapod.core.storage.DBReader;
@ -57,6 +58,7 @@ public class AllEpisodesFragment extends Fragment {
public static final String TAG = "AllEpisodesFragment";
private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED |
EventDistributor.FEED_LIST_UPDATE |
EventDistributor.DOWNLOAD_QUEUED |
EventDistributor.UNREAD_ITEMS_UPDATE |
EventDistributor.PLAYER_STATUS_UPDATE;
@ -251,7 +253,7 @@ public class AllEpisodesFragment extends Fragment {
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
DBWriter.markAllItemsRead(getActivity());
DBWriter.markAllItemsRead();
Toast.makeText(getActivity(), R.string.mark_all_read_msg, Toast.LENGTH_SHORT).show();
}
};
@ -485,14 +487,14 @@ public class AllEpisodesFragment extends Fragment {
if (context != null) {
if(showOnlyNewEpisodes) {
return new Object[] {
DBReader.getNewItemsList(context),
DBReader.getQueueIDList(context),
DBReader.getNewItemsList(),
DBReader.getQueueIDList(),
null // see ItemAccess.isNew
};
} else {
return new Object[]{
DBReader.getRecentlyPublishedEpisodes(context, RECENT_EPISODES_LIMIT),
DBReader.getQueueIDList(context)
DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT),
DBReader.getQueueIDList()
};
}
} else {

View File

@ -179,7 +179,7 @@ public class CompletedDownloadsFragment extends ListFragment {
protected List<FeedItem> doInBackground(Void... params) {
Context context = getActivity();
if (context != null) {
return DBReader.getDownloadedItems(context);
return DBReader.getDownloadedItems();
}
return Collections.emptyList();
}

View File

@ -142,7 +142,7 @@ public class DownloadLogFragment extends ListFragment {
if (!super.onOptionsItemSelected(item)) {
switch (item.getItemId()) {
case R.id.clear_history_item:
DBWriter.clearDownloadLog(getActivity());
DBWriter.clearDownloadLog();
return true;
default:
return false;
@ -170,7 +170,7 @@ public class DownloadLogFragment extends ListFragment {
protected List<DownloadStatus> doInBackground(Void... params) {
Context context = getActivity();
if (context != null) {
return DBReader.getDownloadLog(context);
return DBReader.getDownloadLog();
}
return null;
}

View File

@ -210,7 +210,7 @@ public class ItemDescriptionFragment extends Fragment {
@Override
protected FeedItem doInBackground(Void... voids) {
return DBReader.getFeedItem(getActivity(), getArguments().getLong(ARG_FEEDITEM_ID));
return DBReader.getFeedItem(getArguments().getLong(ARG_FEEDITEM_ID));
}
@Override

View File

@ -36,7 +36,6 @@ import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.List;
@ -497,12 +496,12 @@ public class ItemFragment extends Fragment implements LoaderManager.LoaderCallba
return new DBTaskLoader<Pair<FeedItem,LongList>>(getActivity()) {
@Override
public Pair<FeedItem,LongList> loadInBackground() {
FeedItem data1 = DBReader.getFeedItem(getContext(), itemID);
FeedItem data1 = DBReader.getFeedItem(itemID);
if (data1 != null) {
Timeline t = new Timeline(getActivity(), data1);
webviewData = t.processShownotes(false);
}
LongList data2 = DBReader.getQueueIDList(getContext());
LongList data2 = DBReader.getQueueIDList();
return Pair.create(data1, data2);
}
};

View File

@ -630,12 +630,12 @@ public class ItemlistFragment extends ListFragment {
long feedID = params[0];
Context context = getActivity();
if (context != null) {
Feed feed = DBReader.getFeed(context, feedID);
Feed feed = DBReader.getFeed(feedID);
if(feed != null && feed.getItemFilter() != null) {
FeedItemFilter filter = feed.getItemFilter();
feed.setItems(filter.filter(context, feed.getItems()));
}
LongList queuedItemsIds = DBReader.getQueueIDList(context);
LongList queuedItemsIds = DBReader.getQueueIDList();
return new Object[] { feed, queuedItemsIds };
} else {
return null;

View File

@ -73,7 +73,7 @@ public class NewEpisodesFragment extends AllEpisodesFragment {
FeedItem item = (FeedItem) listView.getAdapter().getItem(which);
// we're marking it as unplayed since the user didn't actually play it
// but they don't want it considered 'NEW' anymore
DBWriter.markItemPlayed(getActivity(), FeedItem.UNPLAYED, item.getId());
DBWriter.markItemPlayed(FeedItem.UNPLAYED, item.getId());
undoBarController.showUndoBar(false,
getString(R.string.marked_as_read_label), new FeedItemUndoToken(item,
which)
@ -89,14 +89,14 @@ public class NewEpisodesFragment extends AllEpisodesFragment {
public void onUndo(FeedItemUndoToken token) {
if (token != null) {
long itemId = token.getFeedItemId();
DBWriter.markItemPlayed(context, FeedItem.NEW, itemId);
DBWriter.markItemPlayed(FeedItem.NEW, itemId);
}
}
@Override
public void onHide(FeedItemUndoToken token) {
if (token != null && context != null) {
long itemId = token.getFeedItemId();
FeedItem item = DBReader.getFeedItem(context, itemId);
FeedItem item = DBReader.getFeedItem(itemId);
FeedMedia media = item.getMedia();
if(media != null && media.hasAlmostEnded() && item.getFeed().getPreferences().getCurrentAutoDelete()) {
DBWriter.deleteFeedMediaOfItem(context, media.getId());

View File

@ -164,7 +164,7 @@ public class PlaybackHistoryFragment extends ListFragment {
if (!super.onOptionsItemSelected(item)) {
switch (item.getItemId()) {
case R.id.clear_history_item:
DBWriter.clearPlaybackHistory(getActivity());
DBWriter.clearPlaybackHistory();
return true;
default:
return false;
@ -267,9 +267,9 @@ public class PlaybackHistoryFragment extends ListFragment {
protected Pair<List<FeedItem>,LongList> doInBackground(Void... params) {
Context context = activity.get();
if (context != null) {
List<FeedItem> history = DBReader.getPlaybackHistory(context);
LongList queue = DBReader.getQueueIDList(context);
DBReader.loadFeedDataOfFeedItemlist(context, history);
List<FeedItem> history = DBReader.getPlaybackHistory();
LongList queue = DBReader.getQueueIDList();
DBReader.loadFeedDataOfFeedItemlist(history);
return Pair.create(history, queue);
} else {
return null;

View File

@ -269,7 +269,7 @@ public class QueueFragment extends Fragment {
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
DBWriter.clearQueue(getActivity());
DBWriter.clearQueue();
}
};
conDialog.createNewDialog().show();
@ -402,7 +402,7 @@ public class QueueFragment extends Fragment {
final FeedItem item = queue.remove(from);
queue.add(to, item);
listAdapter.notifyDataSetChanged();
DBWriter.moveQueueItem(getActivity(), from, to, true);
DBWriter.moveQueueItem(from, to, true);
}
@Override
@ -432,10 +432,12 @@ public class QueueFragment extends Fragment {
public void onHide(FeedItemUndoToken token) {
if (token != null && context != null) {
long itemId = token.getFeedItemId();
FeedItem item = DBReader.getFeedItem(context, itemId);
FeedMedia media = item.getMedia();
if(media != null && media.hasAlmostEnded() && item.getFeed().getPreferences().getCurrentAutoDelete()) {
DBWriter.deleteFeedMediaOfItem(context, media.getId());
FeedItem item = DBReader.getFeedItem(itemId);
if(item != null) {
FeedMedia media = item.getMedia();
if (media != null && media.hasAlmostEnded() && item.getFeed().getPreferences().getCurrentAutoDelete()) {
DBWriter.deleteFeedMediaOfItem(context, media.getId());
}
}
}
}
@ -608,7 +610,7 @@ public class QueueFragment extends Fragment {
protected List<FeedItem> doInBackground(Void... params) {
Context context = activity.get();
if (context != null) {
return DBReader.getQueue(context);
return DBReader.getQueue();
}
return null;
}

View File

@ -81,8 +81,8 @@ public class RunningDownloadsFragment extends ListFragment {
if(downloadRequest.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA &&
UserPreferences.isEnableAutodownload()) {
FeedMedia media = DBReader.getFeedMedia(getActivity(), downloadRequest.getFeedfileId());
DBWriter.setFeedItemAutoDownload(getActivity(), media.getItem(), false);
FeedMedia media = DBReader.getFeedMedia(downloadRequest.getFeedfileId());
DBWriter.setFeedItemAutoDownload(media.getItem(), false);
Toast.makeText(getActivity(), R.string.download_canceled_autodownload_enabled_msg, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), R.string.download_canceled_msg, Toast.LENGTH_SHORT).show();

View File

@ -155,7 +155,7 @@ public class FeedItemMenuHandler {
break;
case R.id.mark_read_item:
selectedItem.setPlayed(true);
DBWriter.markItemPlayed(context, selectedItem, FeedItem.PLAYED, false);
DBWriter.markItemPlayed(selectedItem, FeedItem.PLAYED, false);
if(GpodnetPreferences.loggedIn()) {
FeedMedia media = selectedItem.getMedia();
// not all items have media, Gpodder only cares about those that do
@ -173,7 +173,7 @@ public class FeedItemMenuHandler {
break;
case R.id.mark_unread_item:
selectedItem.setPlayed(false);
DBWriter.markItemPlayed(context, selectedItem, FeedItem.UNPLAYED, false);
DBWriter.markItemPlayed(selectedItem, FeedItem.UNPLAYED, false);
if(GpodnetPreferences.loggedIn()) {
GpodnetEpisodeAction actionNew = new GpodnetEpisodeAction.Builder(selectedItem, Action.NEW)
.currentDeviceId()
@ -183,10 +183,10 @@ public class FeedItemMenuHandler {
}
break;
case R.id.move_to_top_item:
DBWriter.moveQueueItemToTop(context, selectedItem.getId(), true);
DBWriter.moveQueueItemToTop(selectedItem.getId(), true);
return true;
case R.id.move_to_bottom_item:
DBWriter.moveQueueItemToBottom(context, selectedItem.getId(), true);
DBWriter.moveQueueItemToBottom(selectedItem.getId(), true);
case R.id.add_to_queue_item:
DBWriter.addQueueItem(context, selectedItem.getId());
break;
@ -195,15 +195,15 @@ public class FeedItemMenuHandler {
break;
case R.id.reset_position:
selectedItem.getMedia().setPosition(0);
DBWriter.markItemPlayed(context, selectedItem, FeedItem.UNPLAYED, true);
DBWriter.markItemPlayed(selectedItem, FeedItem.UNPLAYED, true);
break;
case R.id.activate_auto_download:
selectedItem.setAutoDownload(true);
DBWriter.setFeedItemAutoDownload(context, selectedItem, true);
DBWriter.setFeedItemAutoDownload(selectedItem, true);
break;
case R.id.deactivate_auto_download:
selectedItem.setAutoDownload(false);
DBWriter.setFeedItemAutoDownload(context, selectedItem, false);
DBWriter.setFeedItemAutoDownload(selectedItem, false);
break;
case R.id.visit_website_item:
Uri uri = Uri.parse(selectedItem.getLink());

View File

@ -78,7 +78,7 @@ public class FeedMenuHandler {
public void onConfirmButtonPressed(
DialogInterface dialog) {
dialog.dismiss();
DBWriter.markFeedRead(context, selectedFeed.getId());
DBWriter.markFeedRead(selectedFeed.getId());
}
};
conDialog.createNewDialog().show();
@ -138,7 +138,7 @@ public class FeedMenuHandler {
@Override
public void onClick(DialogInterface dialog, int which) {
feed.setHiddenItemProperties(hidden.toArray(new String[hidden.size()]));
DBWriter.setFeedItemsFilter(context, feed.getId(), hidden);
DBWriter.setFeedItemsFilter(feed.getId(), hidden);
}
});
builder.setNegativeButton(R.string.cancel_label, null);

View File

@ -58,9 +58,9 @@ public class PlayerWidgetService extends Service {
if (media.hasAlmostEnded()) {
Log.d(TAG, "smart mark as read");
FeedItem item = media.getItem();
DBWriter.markItemPlayed(this, item, FeedItem.PLAYED, false);
DBWriter.markItemPlayed(item, FeedItem.PLAYED, false);
DBWriter.removeQueueItem(this, item, false);
DBWriter.addItemToPlaybackHistory(this, media);
DBWriter.addItemToPlaybackHistory(media);
if (item.getFeed().getPreferences().getCurrentAutoDelete()) {
Log.d(TAG, "Delete " + media.toString());
DBWriter.deleteFeedMediaOfItem(this, media.getId());

View File

@ -94,7 +94,7 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
return ExitCode.NO_NETWORK;
}
final List<FlattrThing> flattrQueue = DBReader.getFlattrQueue(context);
final List<FlattrThing> flattrQueue = DBReader.getFlattrQueue();
if (extraFlattrThing != null) {
flattrQueue.add(extraFlattrThing);
} else if (flattrQueue.size() == 1) {

View File

@ -32,7 +32,7 @@ public class FlattrStatusFetcher extends Thread {
try {
List<Flattr> flattredThings = FlattrUtils.retrieveFlattredThings();
DBWriter.setFlattredStatus(context, flattredThings).get();
DBWriter.setFlattredStatus(flattredThings).get();
} catch (FlattrException e) {
e.printStackTrace();
Log.d(TAG, "flattrQueue exception retrieving list with flattred items " + e.getMessage());

View File

@ -89,7 +89,7 @@ public class OpmlBackupAgent extends BackupAgentHelper {
try {
// Write OPML
new OpmlWriter().writeDocument(DBReader.getFeedList(mContext), writer);
new OpmlWriter().writeDocument(DBReader.getFeedList(), writer);
// Compare checksum of new and old file to see if we need to perform a backup at all
if (digester != null) {

View File

@ -1,5 +1,9 @@
package de.danoeh.antennapod.core.feed;
import android.database.Cursor;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
public abstract class Chapter extends FeedComponent {
/** Defines starting point in milliseconds. */
@ -22,6 +26,33 @@ public abstract class Chapter extends FeedComponent {
this.link = link;
}
public static Chapter fromCursor(Cursor cursor, FeedItem item) {
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_TITLE);
int indexStart = cursor.getColumnIndex(PodDBAdapter.KEY_START);
int indexLink = cursor.getColumnIndex(PodDBAdapter.KEY_LINK);
int indexChapterType = cursor.getColumnIndex(PodDBAdapter.KEY_CHAPTER_TYPE);
String title = cursor.getString(indexTitle);
long start = cursor.getLong(indexStart);
String link = cursor.getString(indexLink);
int chapterType = cursor.getInt(indexChapterType);
Chapter chapter = null;
switch (chapterType) {
case SimpleChapter.CHAPTERTYPE_SIMPLECHAPTER:
chapter = new SimpleChapter(start, title, item, link);
break;
case ID3Chapter.CHAPTERTYPE_ID3CHAPTER:
chapter = new ID3Chapter(start, title, item, link);
break;
case VorbisCommentChapter.CHAPTERTYPE_VORBISCOMMENT_CHAPTER:
chapter = new VorbisCommentChapter(start, title, item, link);
break;
}
return chapter;
}
public abstract int getChapterType();
public long getStart() {

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.feed;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
@ -12,6 +13,7 @@ import java.util.List;
import de.danoeh.antennapod.core.asynctask.ImageResource;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.flattr.FlattrStatus;
import de.danoeh.antennapod.core.util.flattr.FlattrThing;
@ -170,11 +172,60 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
preferences = new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, username, password);
}
public static Feed fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexLastUpdate = cursor.getColumnIndex(PodDBAdapter.KEY_LASTUPDATE);
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_TITLE);
int indexLink = cursor.getColumnIndex(PodDBAdapter.KEY_LINK);
int indexDescription = cursor.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION);
int indexPaymentLink = cursor.getColumnIndex(PodDBAdapter.KEY_PAYMENT_LINK);
int indexAuthor = cursor.getColumnIndex(PodDBAdapter.KEY_AUTHOR);
int indexLanguage = cursor.getColumnIndex(PodDBAdapter.KEY_LANGUAGE);
int indexType = cursor.getColumnIndex(PodDBAdapter.KEY_TYPE);
int indexFeedIdentifier = cursor.getColumnIndex(PodDBAdapter.KEY_FEED_IDENTIFIER);
int indexFileUrl = cursor.getColumnIndex(PodDBAdapter.KEY_FILE_URL);
int indexDownloadUrl = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL);
int indexDownloaded = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOADED);
int indexFlattrStatus = cursor.getColumnIndex(PodDBAdapter.KEY_FLATTR_STATUS);
int indexIsPaged = cursor.getColumnIndex(PodDBAdapter.KEY_IS_PAGED);
int indexNextPageLink = cursor.getColumnIndex(PodDBAdapter.KEY_NEXT_PAGE_LINK);
int indexHide = cursor.getColumnIndex(PodDBAdapter.KEY_HIDE);
int indexLastUpdateFailed = cursor.getColumnIndex(PodDBAdapter.KEY_LAST_UPDATE_FAILED);
/**
* Returns true if at least one item in the itemlist is unread.
*
*/
Date lastUpdate = new Date(cursor.getLong(indexLastUpdate));
Feed feed = new Feed(
cursor.getLong(indexId),
lastUpdate,
cursor.getString(indexTitle),
cursor.getString(indexLink),
cursor.getString(indexDescription),
cursor.getString(indexPaymentLink),
cursor.getString(indexAuthor),
cursor.getString(indexLanguage),
cursor.getString(indexType),
cursor.getString(indexFeedIdentifier),
null,
cursor.getString(indexFileUrl),
cursor.getString(indexDownloadUrl),
cursor.getInt(indexDownloaded) > 0,
new FlattrStatus(cursor.getLong(indexFlattrStatus)),
cursor.getInt(indexIsPaged) > 0,
cursor.getString(indexNextPageLink),
cursor.getString(indexHide),
cursor.getInt(indexLastUpdateFailed) > 0
);
FeedPreferences preferences = FeedPreferences.fromCursor(cursor);
feed.setPreferences(preferences);
return feed;
}
/**
* Returns true if at least one item in the itemlist is unread.
*
*/
public boolean hasNewItems() {
for (FeedItem item : items) {
if (item.isNew()) {
@ -444,7 +495,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
}
public void savePreferences(Context context) {
DBWriter.setFeedPreferences(context, preferences);
DBWriter.setFeedPreferences(preferences);
}
@Override

View File

@ -1,10 +1,12 @@
package de.danoeh.antennapod.core.feed;
import android.database.Cursor;
import android.net.Uri;
import java.io.File;
import de.danoeh.antennapod.core.asynctask.ImageResource;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
public class FeedImage extends FeedFile implements ImageResource {
@ -31,6 +33,23 @@ public class FeedImage extends FeedFile implements ImageResource {
super();
}
public static FeedImage fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_TITLE);
int indexFileUrl = cursor.getColumnIndex(PodDBAdapter.KEY_FILE_URL);
int indexDownloadUrl = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL);
int indexDownloaded = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOADED);
return new FeedImage(
cursor.getLong(indexId),
cursor.getString(indexTitle),
cursor.getString(indexFileUrl),
cursor.getString(indexDownloadUrl),
cursor.getInt(indexDownloaded) > 0
);
}
@Override
public String getHumanReadableIdentifier() {
if (owner != null && owner.getHumanReadableIdentifier() != null) {

View File

@ -1,5 +1,6 @@
package de.danoeh.antennapod.core.feed;
import android.database.Cursor;
import android.net.Uri;
import org.apache.commons.lang3.builder.ToStringBuilder;
@ -9,9 +10,9 @@ import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.asynctask.ImageResource;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.ShownotesProvider;
import de.danoeh.antennapod.core.util.flattr.FlattrStatus;
import de.danoeh.antennapod.core.util.flattr.FlattrThing;
@ -125,6 +126,37 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr
this.hasChapters = hasChapters;
}
public static FeedItem fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_TITLE);
int indexLink = cursor.getColumnIndex(PodDBAdapter.KEY_LINK);
int indexPubDate = cursor.getColumnIndex(PodDBAdapter.KEY_PUBDATE);
int indexPaymentLink = cursor.getColumnIndex(PodDBAdapter.KEY_PAYMENT_LINK);
int indexFeedId = cursor.getColumnIndex(PodDBAdapter.KEY_FEED);
int indexFlattrStatus = cursor.getColumnIndex(PodDBAdapter.KEY_FLATTR_STATUS);
int indexHasChapters = cursor.getColumnIndex(PodDBAdapter.KEY_HAS_CHAPTERS);
int indexRead = cursor.getColumnIndex(PodDBAdapter.KEY_READ);
int indexItemIdentifier = cursor.getColumnIndex(PodDBAdapter.KEY_ITEM_IDENTIFIER);
int indexAutoDownload = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DOWNLOAD);
long id = cursor.getInt(indexId);
assert(id > 0);
String title = cursor.getString(indexTitle);
String link = cursor.getString(indexLink);
Date pubDate = new Date(cursor.getLong(indexPubDate));
String paymentLink = cursor.getString(indexPaymentLink);
long feedId = cursor.getLong(indexFeedId);
boolean hasChapters = cursor.getInt(indexHasChapters) > 0;
FlattrStatus flattrStatus = new FlattrStatus(cursor.getLong(indexFlattrStatus));
int state = cursor.getInt(indexRead);
String itemIdentifier = cursor.getString(indexItemIdentifier);
boolean autoDownload = cursor.getInt(indexAutoDownload) > 0;
FeedItem item = new FeedItem(id, title, link, pubDate, paymentLink, feedId, flattrStatus,
hasChapters, null, state, itemIdentifier, autoDownload);
return item;
}
public void updateFromOther(FeedItem other) {
super.updateFromOther(other);
if (other.title != null) {
@ -321,7 +353,7 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr
public String call() throws Exception {
if (contentEncoded == null || description == null) {
DBReader.loadExtraInformationOfFeedItem(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedItem.this);
DBReader.loadExtraInformationOfFeedItem(FeedItem.this);
}
return (contentEncoded != null) ? contentEncoded : description;

View File

@ -64,7 +64,7 @@ public class FeedItemFilter {
if(hideUnplayed && false == item.isPlayed()) continue;
if(hidePaused && item.getState() == FeedItem.State.IN_PROGRESS) continue;
if(hidePlayed && item.isPlayed()) continue;
boolean isQueued = DBReader.getQueueIDList(context).contains(item.getId());
boolean isQueued = DBReader.getQueueIDList().contains(item.getId());
if(hideQueued && isQueued) continue;
if(hideNotQueued && false == isQueued) continue;
boolean isDownloaded = item.getMedia() != null && item.getMedia().isDownloaded();

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.core.feed;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Parcel;
@ -11,11 +12,11 @@ import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.ChapterUtils;
import de.danoeh.antennapod.core.util.playback.Playable;
@ -83,6 +84,55 @@ public class FeedMedia extends FeedFile implements Playable {
this.hasEmbeddedPicture = hasEmbeddedPicture;
}
public static FeedMedia fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexPlaybackCompletionDate = cursor.getColumnIndex(PodDBAdapter.KEY_PLAYBACK_COMPLETION_DATE);
int indexDuration = cursor.getColumnIndex(PodDBAdapter.KEY_DURATION);
int indexPosition = cursor.getColumnIndex(PodDBAdapter.KEY_POSITION);
int indexSize = cursor.getColumnIndex(PodDBAdapter.KEY_SIZE);
int indexMimeType = cursor.getColumnIndex(PodDBAdapter.KEY_MIME_TYPE);
int indexFileUrl = cursor.getColumnIndex(PodDBAdapter.KEY_FILE_URL);
int indexDownloadUrl = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL);
int indexDownloaded = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOADED);
int indexPlayedDuration = cursor.getColumnIndex(PodDBAdapter.KEY_PLAYED_DURATION);
long mediaId = cursor.getLong(indexId);
Date playbackCompletionDate = null;
long playbackCompletionTime = cursor.getLong(indexPlaybackCompletionDate);
if (playbackCompletionTime > 0) {
playbackCompletionDate = new Date(playbackCompletionTime);
}
Boolean hasEmbeddedPicture;
switch(cursor.getInt(cursor.getColumnIndex(PodDBAdapter.KEY_HAS_EMBEDDED_PICTURE))) {
case 1:
hasEmbeddedPicture = Boolean.TRUE;
break;
case 0:
hasEmbeddedPicture = Boolean.FALSE;
break;
default:
hasEmbeddedPicture = null;
break;
}
return new FeedMedia(
mediaId,
null,
cursor.getInt(indexDuration),
cursor.getInt(indexPosition),
cursor.getLong(indexSize),
cursor.getString(indexMimeType),
cursor.getString(indexFileUrl),
cursor.getString(indexDownloadUrl),
cursor.getInt(indexDownloaded) > 0,
playbackCompletionDate,
cursor.getInt(indexPlayedDuration),
hasEmbeddedPicture
);
}
@Override
public String getHumanReadableIdentifier() {
if (item != null && item.getTitle() != null) {
@ -297,22 +347,22 @@ public class FeedMedia extends FeedFile implements Playable {
@Override
public void loadMetadata() throws PlayableException {
if (item == null && itemID != 0) {
item = DBReader.getFeedItem(ClientConfig.applicationCallbacks.getApplicationInstance(), itemID);
item = DBReader.getFeedItem(itemID);
}
}
@Override
public void loadChapterMarks() {
if (item == null && itemID != 0) {
item = DBReader.getFeedItem(ClientConfig.applicationCallbacks.getApplicationInstance(), itemID);
item = DBReader.getFeedItem(itemID);
}
// check if chapters are stored in db and not loaded yet.
if (item != null && item.hasChapters() && item.getChapters() == null) {
DBReader.loadChaptersOfFeedItem(ClientConfig.applicationCallbacks.getApplicationInstance(), item);
DBReader.loadChaptersOfFeedItem(item);
} else if (item != null && item.getChapters() == null && !localFileAvailable()) {
ChapterUtils.loadChaptersFromStreamUrl(this);
if (getChapters() != null && item != null) {
DBWriter.setFeedItem(ClientConfig.applicationCallbacks.getApplicationInstance(),
DBWriter.setFeedItem(
item);
}
}
@ -389,9 +439,9 @@ public class FeedMedia extends FeedFile implements Playable {
@Override
public void saveCurrentPosition(SharedPreferences pref, int newPosition) {
DBWriter.setFeedMediaPlaybackInformation(ClientConfig.applicationCallbacks.getApplicationInstance(), this);
DBWriter.setFeedMediaPlaybackInformation(this);
if(item.isNew()) {
DBWriter.markItemPlayed(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedItem.UNPLAYED, item.getId());
DBWriter.markItemPlayed(FeedItem.UNPLAYED, item.getId());
}
setPosition(newPosition);
}
@ -421,11 +471,11 @@ public class FeedMedia extends FeedFile implements Playable {
public String call() throws Exception {
if (item == null) {
item = DBReader.getFeedItem(
ClientConfig.applicationCallbacks.getApplicationInstance(), itemID);
itemID);
}
if (item.getContentEncoded() == null || item.getDescription() == null) {
DBReader.loadExtraInformationOfFeedItem(
ClientConfig.applicationCallbacks.getApplicationInstance(), item);
item);
}
return (item.getContentEncoded() != null) ? item.getContentEncoded() : item.getDescription();

View File

@ -1,9 +1,13 @@
package de.danoeh.antennapod.core.feed;
import android.content.Context;
import de.danoeh.antennapod.core.storage.DBWriter;
import android.database.Cursor;
import org.apache.commons.lang3.StringUtils;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
/**
* Contains preferences for a single feed.
@ -29,6 +33,23 @@ public class FeedPreferences {
this.password = password;
}
public static FeedPreferences fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexAutoDownload = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DOWNLOAD);
int indexAutoDeleteAction = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DELETE_ACTION);
int indexUsername = cursor.getColumnIndex(PodDBAdapter.KEY_USERNAME);
int indexPassword = cursor.getColumnIndex(PodDBAdapter.KEY_PASSWORD);
long feedId = cursor.getLong(indexId);
boolean autoDownload = cursor.getInt(indexAutoDownload) > 0;
int autoDeleteActionIndex = cursor.getInt(indexAutoDeleteAction);
AutoDeleteAction autoDeleteAction = AutoDeleteAction.values()[autoDeleteActionIndex];
String username = cursor.getString(indexUsername);
String password = cursor.getString(indexPassword);
return new FeedPreferences(feedId, autoDownload, autoDeleteAction, username, password);
}
/**
* Compare another FeedPreferences with this one. The feedID, autoDownload and AutoDeleteAction attribute are excluded from the
@ -98,7 +119,7 @@ public class FeedPreferences {
}
public void save(Context context) {
DBWriter.setFeedPreferences(context, this);
DBWriter.setFeedPreferences(this);
}
public String getUsername() {

View File

@ -108,7 +108,7 @@ public class ApOkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
com.squareup.okhttp.Request request = chain.request();
String url = request.urlString();
Context context = ClientConfig.applicationCallbacks.getApplicationInstance();
String authentication = DBReader.getImageAuthentication(context, url);
String authentication = DBReader.getImageAuthentication(url);
if(TextUtils.isEmpty(authentication)) {
Log.d(TAG, "no credentials for '" + url + "'");

View File

@ -126,7 +126,7 @@ public class GpodnetSyncService extends Service {
private synchronized void syncSubscriptionChanges() {
final long timestamp = GpodnetPreferences.getLastSubscriptionSyncTimestamp();
try {
final List<String> localSubscriptions = DBReader.getFeedListDownloadUrls(this);
final List<String> localSubscriptions = DBReader.getFeedListDownloadUrls();
Collection<String> localAdded = GpodnetPreferences.getAddedFeedsCopy();
Collection<String> localRemoved = GpodnetPreferences.getRemovedFeedsCopy();
GpodnetService service = tryLogin();
@ -242,9 +242,9 @@ public class GpodnetSyncService extends Service {
for (GpodnetEpisodeAction action : remoteActions) {
switch (action.getAction()) {
case NEW:
FeedItem newItem = DBReader.getFeedItem(this, action.getPodcast(), action.getEpisode());
FeedItem newItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode());
if(newItem != null) {
DBWriter.markItemPlayed(this, newItem, FeedItem.UNPLAYED, true);
DBWriter.markItemPlayed(newItem, FeedItem.UNPLAYED, true);
} else {
Log.i(TAG, "Unknown feed item: " + action);
}
@ -273,14 +273,14 @@ public class GpodnetSyncService extends Service {
}
}
for (GpodnetEpisodeAction action : mostRecentPlayAction.values()) {
FeedItem playItem = DBReader.getFeedItem(this, action.getPodcast(), action.getEpisode());
FeedItem playItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode());
if (playItem != null) {
FeedMedia media = playItem.getMedia();
media.setPosition(action.getPosition() * 1000);
DBWriter.setFeedMedia(this, media);
DBWriter.setFeedMedia(media);
if(playItem.getMedia().hasAlmostEnded()) {
DBWriter.markItemPlayed(this, playItem, FeedItem.PLAYED, true);
DBWriter.addItemToPlaybackHistory(this, playItem.getMedia());
DBWriter.markItemPlayed(playItem, FeedItem.PLAYED, true);
DBWriter.addItemToPlaybackHistory(playItem.getMedia());
}
}
}

View File

@ -20,7 +20,6 @@ import android.util.Log;
import android.webkit.URLUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.http.HttpStatus;
@ -504,7 +503,7 @@ public class DownloadService extends Service {
*/
private void saveDownloadStatus(DownloadStatus status) {
reportQueue.add(status);
DBWriter.addDownloadStatus(this, status);
DBWriter.addDownloadStatus(status);
}
private void sendDownloadHandledIntent() {
@ -888,7 +887,7 @@ public class DownloadService extends Service {
if (successful) {
// we create a 'successful' download log if the feed's last refresh failed
List<DownloadStatus> log = DBReader.getFeedDownloadLog(DownloadService.this, feed);
List<DownloadStatus> log = DBReader.getFeedDownloadLog(feed);
if(log.size() > 0 && log.get(0).isSuccessful() == false) {
saveDownloadStatus(new DownloadStatus(feed,
feed.getHumanReadableIdentifier(), DownloadError.SUCCESS, successful,
@ -1011,17 +1010,17 @@ public class DownloadService extends Service {
@Override
public void run() {
if(request.getFeedfileType() == Feed.FEEDFILETYPE_FEED) {
DBWriter.setFeedLastUpdateFailed(DownloadService.this, request.getFeedfileId(), true);
DBWriter.setFeedLastUpdateFailed(request.getFeedfileId(), true);
} else if (request.isDeleteOnFailure()) {
Log.d(TAG, "Ignoring failed download, deleteOnFailure=true");
} else {
File dest = new File(request.getDestination());
if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
Log.d(TAG, "File has been partially downloaded. Writing file url");
FeedMedia media = DBReader.getFeedMedia(DownloadService.this, request.getFeedfileId());
FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId());
media.setFile_url(request.getDestination());
try {
DBWriter.setFeedMedia(DownloadService.this, media).get();
DBWriter.setFeedMedia(media).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
@ -1050,7 +1049,7 @@ public class DownloadService extends Service {
@Override
public void run() {
FeedImage image = DBReader.getFeedImage(DownloadService.this, request.getFeedfileId());
FeedImage image = DBReader.getFeedImage(request.getFeedfileId());
if (image == null) {
throw new IllegalStateException("Could not find downloaded image in database");
}
@ -1060,7 +1059,7 @@ public class DownloadService extends Service {
saveDownloadStatus(status);
sendDownloadHandledIntent();
DBWriter.setFeedImage(DownloadService.this, image);
DBWriter.setFeedImage(image);
numberOfDownloads.decrementAndGet();
queryDownloadsAsync();
}
@ -1084,7 +1083,7 @@ public class DownloadService extends Service {
@Override
public void run() {
FeedMedia media = DBReader.getFeedMedia(DownloadService.this,
FeedMedia media = DBReader.getFeedMedia(
request.getFeedfileId());
if (media == null) {
throw new IllegalStateException(
@ -1126,9 +1125,9 @@ public class DownloadService extends Service {
item.setAutoDownload(false);
// update the db
DBWriter.setFeedItem(DownloadService.this, item).get();
DBWriter.setFeedItem(item).get();
DBWriter.setFeedMedia(DownloadService.this, media).get();
DBWriter.setFeedMedia(media).get();
if (!DBTasks.isInQueue(DownloadService.this, item.getId())) {
DBWriter.addQueueItem(DownloadService.this, item.getId()).get();
}

View File

@ -1,12 +1,15 @@
package de.danoeh.antennapod.core.service.download;
import android.database.Cursor;
import org.apache.commons.lang3.Validate;
import de.danoeh.antennapod.core.feed.FeedFile;
import de.danoeh.antennapod.core.util.DownloadError;
import java.util.Date;
import de.danoeh.antennapod.core.feed.FeedFile;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.DownloadError;
/** Contains status attributes for one download */
public class DownloadStatus {
/**
@ -101,6 +104,30 @@ public class DownloadStatus {
this.reasonDetailed = reasonDetailed;
}
public static DownloadStatus fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOADSTATUS_TITLE);
int indexFeedFile = cursor.getColumnIndex(PodDBAdapter.KEY_FEEDFILE);
int indexFileFileType = cursor.getColumnIndex(PodDBAdapter.KEY_FEEDFILETYPE);
int indexSuccessful = cursor.getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL);
int indexReason = cursor.getColumnIndex(PodDBAdapter.KEY_REASON);
int indexCompletionDate = cursor.getColumnIndex(PodDBAdapter.KEY_COMPLETION_DATE);
int indexReasonDetailed = cursor.getColumnIndex(PodDBAdapter.KEY_REASON_DETAILED);
long id = cursor.getLong(indexId);
String title = cursor.getString(indexTitle);
long feedfileId = cursor.getLong(indexFeedFile);
int feedfileType = cursor.getInt(indexFileFileType);
boolean successful = cursor.getInt(indexSuccessful) > 0;
int reason = cursor.getInt(indexReason);
Date completionDate = new Date(cursor.getLong(indexCompletionDate));
String reasonDetailed = cursor.getString(indexReasonDetailed);
return new DownloadStatus(id, title, feedfileId,
feedfileType, successful, DownloadError.fromCode(reason), completionDate,
reasonDetailed);
}
@Override
public String toString() {
return "DownloadStatus [id=" + id + ", title=" + title + ", reason="

View File

@ -564,7 +564,7 @@ public class PlaybackService extends Service {
if (playable instanceof FeedMedia) {
FeedMedia media = (FeedMedia) playable;
FeedItem item = media.getItem();
DBWriter.markItemPlayed(PlaybackService.this, item, FeedItem.PLAYED, true);
DBWriter.markItemPlayed(item, FeedItem.PLAYED, true);
try {
final List<FeedItem> queue = taskManager.getQueue();
@ -577,7 +577,7 @@ public class PlaybackService extends Service {
if (isInQueue) {
DBWriter.removeQueueItem(PlaybackService.this, item, true);
}
DBWriter.addItemToPlaybackHistory(PlaybackService.this, media);
DBWriter.addItemToPlaybackHistory(media);
// auto-flattr if enabled
if (isAutoFlattrable(media) && UserPreferences.getAutoFlattrPlayedDurationThreshold() == 1.0f) {

View File

@ -201,9 +201,9 @@ public class PlaybackServiceMediaPlayer {
if(oldMedia.hasAlmostEnded()) {
Log.d(TAG, "smart mark as read");
FeedItem item = oldMedia.getItem();
DBWriter.markItemPlayed(context, item, FeedItem.PLAYED, false);
DBWriter.markItemPlayed(item, FeedItem.PLAYED, false);
DBWriter.removeQueueItem(context, item, false);
DBWriter.addItemToPlaybackHistory(context, oldMedia);
DBWriter.addItemToPlaybackHistory(oldMedia);
if (item.getFeed().getPreferences().getCurrentAutoDelete()) {
Log.d(TAG, "Delete " + oldMedia.toString());
DBWriter.deleteFeedMediaOfItem(context, oldMedia.getId());

View File

@ -100,7 +100,7 @@ public class PlaybackServiceTaskManager {
queueFuture = schedExecutor.submit(new Callable<List<FeedItem>>() {
@Override
public List<FeedItem> call() throws Exception {
return DBReader.getQueue(context);
return DBReader.getQueue();
}
});
}

View File

@ -5,7 +5,6 @@ import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
@ -18,13 +17,14 @@ import de.danoeh.antennapod.core.util.LongList;
* Implementation of the EpisodeCleanupAlgorithm interface used by AntennaPod.
*/
public class APCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> {
private static final String TAG = "APCleanupAlgorithm";
@Override
public int performCleanup(Context context, Integer episodeNumber) {
List<FeedItem> candidates = new ArrayList<FeedItem>();
List<FeedItem> downloadedItems = DBReader.getDownloadedItems(context);
LongList queue = DBReader.getQueueIDList(context);
List<FeedItem> candidates = new ArrayList<>();
List<FeedItem> downloadedItems = DBReader.getDownloadedItems();
LongList queue = DBReader.getQueueIDList();
List<FeedItem> delete;
for (FeedItem item : downloadedItems) {
if (item.hasMedia() && item.getMedia().isDownloaded()
@ -34,20 +34,17 @@ public class APCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> {
}
Collections.sort(candidates, new Comparator<FeedItem>() {
@Override
public int compare(FeedItem lhs, FeedItem rhs) {
Date l = lhs.getMedia().getPlaybackCompletionDate();
Date r = rhs.getMedia().getPlaybackCompletionDate();
Collections.sort(candidates, (lhs, rhs) -> {
Date l = lhs.getMedia().getPlaybackCompletionDate();
Date r = rhs.getMedia().getPlaybackCompletionDate();
if (l == null) {
l = new Date();
}
if (r == null) {
r = new Date();
}
return l.compareTo(r);
if (l == null) {
l = new Date();
}
if (r == null) {
r = new Date();
}
return l.compareTo(r);
});
if (candidates.size() > episodeNumber) {
@ -75,22 +72,21 @@ public class APCleanupAlgorithm implements EpisodeCleanupAlgorithm<Integer> {
}
@Override
public Integer getDefaultCleanupParameter(Context context) {
return getPerformAutoCleanupArgs(context, 0);
public Integer getDefaultCleanupParameter() {
return getPerformAutoCleanupArgs(0);
}
@Override
public Integer getPerformCleanupParameter(Context context, List<FeedItem> items) {
return getPerformAutoCleanupArgs(context, items.size());
public Integer getPerformCleanupParameter(List<FeedItem> items) {
return getPerformAutoCleanupArgs(items.size());
}
static int getPerformAutoCleanupArgs(Context context,
final int episodeNumber) {
static int getPerformAutoCleanupArgs(final int episodeNumber) {
if (episodeNumber >= 0
&& UserPreferences.getEpisodeCacheSize() != UserPreferences
.getEpisodeCacheSizeUnlimited()) {
int downloadedEpisodes = DBReader
.getNumberOfDownloadedEpisodes(context);
.getNumberOfDownloadedEpisodes();
if (downloadedEpisodes + episodeNumber >= UserPreferences
.getEpisodeCacheSize()) {

View File

@ -51,8 +51,8 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm {
Log.d(TAG, "Performing auto-dl of undownloaded episodes");
List<FeedItem> candidates;
final List<FeedItem> queue = DBReader.getQueue(context);
final List<FeedItem> newItems = DBReader.getNewItemsList(context);
final List<FeedItem> queue = DBReader.getQueue();
final List<FeedItem> newItems = DBReader.getNewItemsList();
candidates = new ArrayList<FeedItem>(queue.size() + newItems.size());
candidates.addAll(queue);
for(FeedItem newItem : newItems) {
@ -71,9 +71,9 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm {
}
int autoDownloadableEpisodes = candidates.size();
int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes(context);
int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes();
int deletedEpisodes = cleanupAlgorithm.performCleanup(context,
APCleanupAlgorithm.getPerformAutoCleanupArgs(context, autoDownloadableEpisodes));
APCleanupAlgorithm.getPerformAutoCleanupArgs(autoDownloadableEpisodes));
boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences
.getEpisodeCacheSizeUnlimited();
int episodeCacheSize = UserPreferences.getEpisodeCacheSize();

View File

@ -68,7 +68,7 @@ public final class DBTasks {
* @param downloadUrl URL of the feed.
*/
public static void removeFeedWithDownloadUrl(Context context, String downloadUrl) {
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor cursor = adapter.getFeedCursorDownloadUrls();
long feedID = 0;
@ -163,7 +163,7 @@ public final class DBTasks {
if (feeds != null) {
refreshFeeds(context, feeds);
} else {
refreshFeeds(context, DBReader.getFeedList(context));
refreshFeeds(context, DBReader.getFeedList());
}
isRefreshing.set(false);
@ -196,7 +196,6 @@ public final class DBTasks {
} catch (DownloadRequestException e) {
e.printStackTrace();
DBWriter.addDownloadStatus(
context,
new DownloadStatus(feed, feed
.getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR, false, e
@ -220,7 +219,6 @@ public final class DBTasks {
} catch (DownloadRequestException e) {
e.printStackTrace();
DBWriter.addDownloadStatus(
context,
new DownloadStatus(feed, feed
.getHumanReadableIdentifier(),
DownloadError.ERROR_REQUEST_ERROR, false, e
@ -287,7 +285,7 @@ public final class DBTasks {
"The feedmanager was notified about a missing episode. It will update its database now.");
media.setDownloaded(false);
media.setFile_url(null);
DBWriter.setFeedMedia(context, media);
DBWriter.setFeedMedia(media);
EventDistributor.getInstance().sendFeedUpdateBroadcast();
}
@ -299,7 +297,7 @@ public final class DBTasks {
public static void downloadAllItemsInQueue(final Context context) {
new Thread() {
public void run() {
List<FeedItem> queue = DBReader.getQueue(context);
List<FeedItem> queue = DBReader.getQueue();
if (!queue.isEmpty()) {
try {
downloadFeedItems(context,
@ -336,7 +334,7 @@ public final class DBTasks {
ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm()
.performCleanup(context,
ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm()
.getPerformCleanupParameter(context, Arrays.asList(items)));
.getPerformCleanupParameter(Arrays.asList(items)));
}
}.start();
@ -350,7 +348,7 @@ public final class DBTasks {
requester.downloadMedia(context, item.getMedia());
} catch (DownloadRequestException e) {
e.printStackTrace();
DBWriter.addDownloadStatus(context,
DBWriter.addDownloadStatus(
new DownloadStatus(item.getMedia(), item
.getMedia()
.getHumanReadableIdentifier(),
@ -393,7 +391,7 @@ public final class DBTasks {
*/
public static void performAutoCleanup(final Context context) {
ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm().performCleanup(context,
ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm().getDefaultCleanupParameter(context));
ClientConfig.dbTasksCallbacks.getEpisodeCacheCleanupAlgorithm().getDefaultCleanupParameter());
}
/**
@ -409,7 +407,7 @@ public final class DBTasks {
final long itemId, List<FeedItem> queue) {
FeedItem result = null;
if (queue == null) {
queue = DBReader.getQueue(context);
queue = DBReader.getQueue();
}
if (queue != null) {
Iterator<FeedItem> iterator = queue.iterator();
@ -434,19 +432,19 @@ public final class DBTasks {
* @param feedItemId ID of the FeedItem
*/
public static boolean isInQueue(Context context, final long feedItemId) {
LongList queue = DBReader.getQueueIDList(context);
LongList queue = DBReader.getQueueIDList();
return queue.contains(feedItemId);
}
private static Feed searchFeedByIdentifyingValueOrID(Context context, PodDBAdapter adapter,
Feed feed) {
if (feed.getId() != 0) {
return DBReader.getFeed(context, feed.getId(), adapter);
return DBReader.getFeed(feed.getId(), adapter);
} else {
List<Feed> feeds = DBReader.getFeedList(context);
List<Feed> feeds = DBReader.getFeedList();
for (Feed f : feeds) {
if (f.getIdentifyingValue().equals(feed.getIdentifyingValue())) {
f.setItems(DBReader.getFeedItemList(context, f));
f.setItems(DBReader.getFeedItemList(f));
return f;
}
}
@ -485,7 +483,7 @@ public final class DBTasks {
List<Feed> newFeedsList = new ArrayList<Feed>();
List<Feed> updatedFeedsList = new ArrayList<Feed>();
Feed[] resultFeeds = new Feed[newFeeds.length];
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
for (int feedIdx = 0; feedIdx < newFeeds.length; feedIdx++) {
@ -574,7 +572,7 @@ public final class DBTasks {
try {
DBWriter.addNewFeed(context, newFeedsList.toArray(new Feed[newFeedsList.size()])).get();
DBWriter.setCompleteFeed(context, updatedFeedsList.toArray(new Feed[updatedFeedsList.size()])).get();
DBWriter.setCompleteFeed(updatedFeedsList.toArray(new Feed[updatedFeedsList.size()])).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
@ -602,8 +600,8 @@ public final class DBTasks {
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemTitles(feedID,
query);
List<FeedItem> items = DBReader.extractItemlistFromCursor(context, searchResult);
DBReader.loadFeedDataOfFeedItemlist(context, items);
List<FeedItem> items = DBReader.extractItemlistFromCursor(searchResult);
DBReader.loadFeedDataOfFeedItemlist(items);
setResult(items);
searchResult.close();
}
@ -626,8 +624,8 @@ public final class DBTasks {
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemDescriptions(feedID,
query);
List<FeedItem> items = DBReader.extractItemlistFromCursor(context, searchResult);
DBReader.loadFeedDataOfFeedItemlist(context, items);
List<FeedItem> items = DBReader.extractItemlistFromCursor(searchResult);
DBReader.loadFeedDataOfFeedItemlist(items);
setResult(items);
searchResult.close();
}
@ -650,8 +648,8 @@ public final class DBTasks {
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemContentEncoded(feedID,
query);
List<FeedItem> items = DBReader.extractItemlistFromCursor(context, searchResult);
DBReader.loadFeedDataOfFeedItemlist(context, items);
List<FeedItem> items = DBReader.extractItemlistFromCursor(searchResult);
DBReader.loadFeedDataOfFeedItemlist(items);
setResult(items);
searchResult.close();
}
@ -673,8 +671,8 @@ public final class DBTasks {
public void execute(PodDBAdapter adapter) {
Cursor searchResult = adapter.searchItemChapters(feedID,
query);
List<FeedItem> items = DBReader.extractItemlistFromCursor(context, searchResult);
DBReader.loadFeedDataOfFeedItemlist(context, items);
List<FeedItem> items = DBReader.extractItemlistFromCursor(searchResult);
DBReader.loadFeedDataOfFeedItemlist(items);
setResult(items);
searchResult.close();
}
@ -697,7 +695,7 @@ public final class DBTasks {
@Override
public T call() throws Exception {
PodDBAdapter adapter = new PodDBAdapter(context);
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
execute(adapter);
adapter.close();

View File

@ -24,7 +24,7 @@ public interface EpisodeCleanupAlgorithm<T> {
* space to free to satisfy the episode cache conditions. If the conditions are already satisfied, this
* method should not have any effects.
*/
public T getDefaultCleanupParameter(Context context);
public T getDefaultCleanupParameter();
/**
* Returns a parameter for performCleanup.
@ -32,5 +32,5 @@ public interface EpisodeCleanupAlgorithm<T> {
* @param items A list of FeedItems that are about to be downloaded. The implementation of this interface
* should decide how much space to free to satisfy the episode cache conditions.
*/
public T getPerformCleanupParameter(Context context, List<FeedItem> items);
public T getPerformCleanupParameter(List<FeedItem> items);
}

View File

@ -1,5 +1,7 @@
package de.danoeh.antennapod.core.storage;
import android.database.Cursor;
import java.util.Date;
/**
@ -36,6 +38,15 @@ public class FeedItemStatistics {
}
}
public static FeedItemStatistics fromCursor(Cursor cursor) {
return new FeedItemStatistics(
cursor.getLong(0),
cursor.getInt(1),
cursor.getInt(2),
cursor.getInt(4),
new Date(cursor.getLong(3)));
}
public long getFeedID() {
return feedID;
}

View File

@ -17,8 +17,8 @@ import org.apache.commons.lang3.Validate;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.ProgressEvent;
import de.danoeh.antennapod.core.feed.Chapter;
@ -40,6 +40,7 @@ import de.greenrobot.event.EventBus;
* Implements methods for accessing the database
*/
public class PodDBAdapter {
private static final String TAG = "PodDBAdapter";
public static final String DATABASE_NAME = "Antennapod.db";
@ -53,63 +54,6 @@ public class PodDBAdapter {
*/
public static final int SEARCH_LIMIT = 30;
// ----------- Column indices
// ----------- General indices
public static final int KEY_ID_INDEX = 0;
public static final int KEY_TITLE_INDEX = 1;
public static final int KEY_FILE_URL_INDEX = 2;
public static final int KEY_DOWNLOAD_URL_INDEX = 3;
public static final int KEY_DOWNLOADED_INDEX = 4;
public static final int KEY_LINK_INDEX = 5;
public static final int KEY_DESCRIPTION_INDEX = 6;
public static final int KEY_PAYMENT_LINK_INDEX = 7;
// ----------- Feed indices
public static final int KEY_LAST_UPDATE_INDEX = 8;
public static final int KEY_LANGUAGE_INDEX = 9;
public static final int KEY_AUTHOR_INDEX = 10;
public static final int KEY_IMAGE_INDEX = 11;
public static final int KEY_TYPE_INDEX = 12;
public static final int KEY_FEED_IDENTIFIER_INDEX = 13;
public static final int KEY_FEED_FLATTR_STATUS_INDEX = 14;
public static final int KEY_FEED_USERNAME_INDEX = 15;
public static final int KEY_FEED_PASSWORD_INDEX = 16;
public static final int KEY_IS_PAGED_INDEX = 17;
public static final int KEY_LOAD_ALL_PAGES_INDEX = 18;
public static final int KEY_NEXT_PAGE_LINK_INDEX = 19;
// ----------- FeedItem indices
public static final int KEY_CONTENT_ENCODED_INDEX = 2;
public static final int KEY_PUBDATE_INDEX = 3;
public static final int KEY_READ_INDEX = 4;
public static final int KEY_MEDIA_INDEX = 8;
public static final int KEY_FEED_INDEX = 9;
public static final int KEY_HAS_SIMPLECHAPTERS_INDEX = 10;
public static final int KEY_ITEM_IDENTIFIER_INDEX = 11;
public static final int KEY_ITEM_FLATTR_STATUS_INDEX = 12;
// ---------- FeedMedia indices
public static final int KEY_DURATION_INDEX = 1;
public static final int KEY_POSITION_INDEX = 5;
public static final int KEY_SIZE_INDEX = 6;
public static final int KEY_MIME_TYPE_INDEX = 7;
public static final int KEY_PLAYBACK_COMPLETION_DATE_INDEX = 8;
public static final int KEY_MEDIA_FEEDITEM_INDEX = 9;
public static final int KEY_PLAYED_DURATION_INDEX = 10;
// --------- Download log indices
public static final int KEY_FEEDFILE_INDEX = 1;
public static final int KEY_FEEDFILETYPE_INDEX = 2;
public static final int KEY_REASON_INDEX = 3;
public static final int KEY_SUCCESSFUL_INDEX = 4;
public static final int KEY_COMPLETION_DATE_INDEX = 5;
public static final int KEY_REASON_DETAILED_INDEX = 6;
public static final int KEY_DOWNLOADSTATUS_TITLE_INDEX = 7;
// --------- Queue indices
public static final int KEY_FEEDITEM_INDEX = 1;
public static final int KEY_QUEUE_FEED_INDEX = 2;
// --------- Chapters indices
public static final int KEY_CHAPTER_START_INDEX = 2;
public static final int KEY_CHAPTER_FEEDITEM_INDEX = 3;
public static final int KEY_CHAPTER_LINK_INDEX = 4;
public static final int KEY_CHAPTER_TYPE_INDEX = 5;
// Key-constants
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
@ -253,12 +197,7 @@ public class PodDBAdapter {
public static final String CREATE_INDEX_SIMPLECHAPTERS_FEEDITEM = "CREATE INDEX "
+ TABLE_NAME_SIMPLECHAPTERS + "_" + KEY_FEEDITEM + " ON " + TABLE_NAME_SIMPLECHAPTERS + " ("
+ KEY_FEEDITEM + ")";
private SQLiteDatabase db;
private final Context context;
private PodDBHelper helper;
/**
* Select all columns from the feed-table
*/
@ -287,30 +226,7 @@ public class PodDBAdapter {
TABLE_NAME_FEEDS + "." + KEY_LAST_UPDATE_FAILED,
TABLE_NAME_FEEDS + "." + KEY_AUTO_DELETE_ACTION,
};
// column indices for FEED_SEL_STD
public static final int IDX_FEED_SEL_STD_ID = 0;
public static final int IDX_FEED_SEL_STD_TITLE = 1;
public static final int IDX_FEED_SEL_STD_FILE_URL = 2;
public static final int IDX_FEED_SEL_STD_DOWNLOAD_URL = 3;
public static final int IDX_FEED_SEL_STD_DOWNLOADED = 4;
public static final int IDX_FEED_SEL_STD_LINK = 5;
public static final int IDX_FEED_SEL_STD_DESCRIPTION = 6;
public static final int IDX_FEED_SEL_STD_PAYMENT_LINK = 7;
public static final int IDX_FEED_SEL_STD_LASTUPDATE = 8;
public static final int IDX_FEED_SEL_STD_LANGUAGE = 9;
public static final int IDX_FEED_SEL_STD_AUTHOR = 10;
public static final int IDX_FEED_SEL_STD_IMAGE = 11;
public static final int IDX_FEED_SEL_STD_TYPE = 12;
public static final int IDX_FEED_SEL_STD_FEED_IDENTIFIER = 13;
public static final int IDX_FEED_SEL_PREFERENCES_AUTO_DOWNLOAD = 14;
public static final int IDX_FEED_SEL_STD_FLATTR_STATUS = 15;
public static final int IDX_FEED_SEL_STD_IS_PAGED = 16;
public static final int IDX_FEED_SEL_STD_NEXT_PAGE_LINK = 17;
public static final int IDX_FEED_SEL_PREFERENCES_USERNAME = 18;
public static final int IDX_FEED_SEL_PREFERENCES_PASSWORD = 19;
public static final int IDX_FEED_SEL_PREFERENCES_AUTO_DELETE_ACTION = 22;
/**
* Select all columns from the feeditems-table except description and
* content-encoded.
@ -321,7 +237,8 @@ public class PodDBAdapter {
TABLE_NAME_FEED_ITEMS + "." + KEY_PUBDATE,
TABLE_NAME_FEED_ITEMS + "." + KEY_READ,
TABLE_NAME_FEED_ITEMS + "." + KEY_LINK,
TABLE_NAME_FEED_ITEMS + "." + KEY_PAYMENT_LINK, KEY_MEDIA,
TABLE_NAME_FEED_ITEMS + "." + KEY_PAYMENT_LINK,
TABLE_NAME_FEED_ITEMS + "." + KEY_MEDIA,
TABLE_NAME_FEED_ITEMS + "." + KEY_FEED,
TABLE_NAME_FEED_ITEMS + "." + KEY_HAS_CHAPTERS,
TABLE_NAME_FEED_ITEMS + "." + KEY_ITEM_IDENTIFIER,
@ -340,73 +257,59 @@ public class PodDBAdapter {
SEL_FI_SMALL_STR = selFiSmall.substring(1, selFiSmall.length() - 1);
}
// column indices for FEEDITEM_SEL_FI_SMALL
public static final int IDX_FI_SMALL_ID = 0;
public static final int IDX_FI_SMALL_TITLE = 1;
public static final int IDX_FI_SMALL_PUBDATE = 2;
public static final int IDX_FI_SMALL_READ = 3;
public static final int IDX_FI_SMALL_LINK = 4;
public static final int IDX_FI_SMALL_PAYMENT_LINK = 5;
public static final int IDX_FI_SMALL_MEDIA = 6;
public static final int IDX_FI_SMALL_FEED = 7;
public static final int IDX_FI_SMALL_HAS_CHAPTERS = 8;
public static final int IDX_FI_SMALL_ITEM_IDENTIFIER = 9;
public static final int IDX_FI_SMALL_FLATTR_STATUS = 10;
public static final int IDX_FI_SMALL_IMAGE = 11;
/**
* Select id, description and content-encoded column from feeditems.
*/
private static final String[] SEL_FI_EXTRA = {KEY_ID, KEY_DESCRIPTION,
KEY_CONTENT_ENCODED, KEY_FEED};
// column indices for SEL_FI_EXTRA
public static final int IDX_FI_EXTRA_ID = 0;
public static final int IDX_FI_EXTRA_DESCRIPTION = 1;
public static final int IDX_FI_EXTRA_CONTENT_ENCODED = 2;
public static final int IDX_FI_EXTRA_FEED = 3;
private SQLiteDatabase db;
private static Context context;
private static PodDBHelper dbHelper;
private static AtomicInteger counter = new AtomicInteger(0);
static PodDBHelper dbHelperSingleton;
public static void init(Context context) {
PodDBAdapter.context = context.getApplicationContext();
}
private static synchronized PodDBHelper getDbHelperSingleton(Context appContext) {
if (dbHelperSingleton == null) {
dbHelperSingleton = new PodDBHelper(appContext, DATABASE_NAME, null);
public static synchronized PodDBAdapter getInstance() {
if(dbHelper == null) {
dbHelper = new PodDBHelper(PodDBAdapter.context, DATABASE_NAME, null);
}
return dbHelperSingleton;
return new PodDBAdapter();
}
public PodDBAdapter(Context c) {
this.context = c;
helper = getDbHelperSingleton(c.getApplicationContext());
}
private PodDBAdapter() {}
public PodDBAdapter open() {
counter.incrementAndGet();
if (db == null || !db.isOpen() || db.isReadOnly()) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Opening DB");
Log.v(TAG, "Opening DB");
try {
db = helper.getWritableDatabase();
db = dbHelper.getWritableDatabase();
} catch (SQLException ex) {
ex.printStackTrace();
db = helper.getReadableDatabase();
Log.e(TAG, Log.getStackTraceString(ex));
db = dbHelper.getReadableDatabase();
}
}
return this;
}
public void close() {
if (BuildConfig.DEBUG)
Log.d(TAG, "Closing DB");
//db.close();
if(counter.decrementAndGet() == 0) {
Log.v(TAG, "Closing DB");
db.close();
}
db = null;
}
public static boolean deleteDatabase(Context context) {
Log.w(TAG, "Deleting database");
dbHelperSingleton.close();
dbHelperSingleton = null;
return context.deleteDatabase(DATABASE_NAME);
public static boolean deleteDatabase() {
if(dbHelper != null) {
dbHelper.close();
dbHelper = null;
}
return context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
}
/**
@ -484,7 +387,12 @@ public class PodDBAdapter {
* @return the id of the entry
*/
public long setImage(FeedImage image) {
db.beginTransaction();
boolean startedTransaction = false;
if(false == db.inTransaction()) {
db.beginTransaction();
startedTransaction = true;
}
ContentValues values = new ContentValues();
values.put(KEY_TITLE, image.getTitle());
values.put(KEY_DOWNLOAD_URL, image.getDownload_url());
@ -505,8 +413,10 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(image.getOwner().getId())});
}
}
db.setTransactionSuccessful();
db.endTransaction();
if(startedTransaction) {
db.setTransactionSuccessful();
db.endTransaction();
}
return image.getId();
}
@ -527,8 +437,7 @@ public class PodDBAdapter {
values.put(KEY_HAS_EMBEDDED_PICTURE, media.hasEmbeddedPicture());
if (media.getPlaybackCompletionDate() != null) {
values.put(KEY_PLAYBACK_COMPLETION_DATE, media
.getPlaybackCompletionDate().getTime());
values.put(KEY_PLAYBACK_COMPLETION_DATE, media.getPlaybackCompletionDate().getTime());
} else {
values.put(KEY_PLAYBACK_COMPLETION_DATE, 0);
}
@ -822,8 +731,7 @@ public class PodDBAdapter {
values.put(KEY_LINK, chapter.getLink());
values.put(KEY_CHAPTER_TYPE, chapter.getChapterType());
if (chapter.getId() == 0) {
chapter.setId(db
.insert(TABLE_NAME_SIMPLECHAPTERS, null, values));
chapter.setId(db.insert(TABLE_NAME_SIMPLECHAPTERS, null, values));
} else {
db.update(TABLE_NAME_SIMPLECHAPTERS, values, KEY_ID + "=?",
new String[]{String.valueOf(chapter.getId())});
@ -884,14 +792,6 @@ public class PodDBAdapter {
return count;
}
public void removeDownloadLogItems(long count) {
if (count > 0) {
final String sql = String.format("DELETE FROM %s WHERE %s in (SELECT %s from %s ORDER BY %s ASC LIMIT %d)",
TABLE_NAME_DOWNLOAD_LOG, KEY_ID, KEY_ID, TABLE_NAME_DOWNLOAD_LOG, KEY_COMPLETION_DATE, count);
db.execSQL(sql, null);
}
}
public void setQueue(List<FeedItem> queue) {
ContentValues values = new ContentValues();
db.beginTransaction();
@ -963,11 +863,6 @@ public class PodDBAdapter {
db.endTransaction();
}
public void removeDownloadStatus(DownloadStatus remove) {
db.delete(TABLE_NAME_DOWNLOAD_LOG, KEY_ID + "=?",
new String[]{String.valueOf(remove.getId())});
}
public void clearPlaybackHistory() {
ContentValues values = new ContentValues();
values.put(KEY_PLAYBACK_COMPLETION_DATE, 0);
@ -1075,21 +970,14 @@ public class PodDBAdapter {
* cursor uses the FEEDITEM_SEL_FI_SMALL selection.
*/
public final Cursor getQueueCursor() {
Object[] args = (Object[]) new String[]{
SEL_FI_SMALL_STR + "," + TABLE_NAME_QUEUE + "." + KEY_ID,
Object[] args = new String[] {
SEL_FI_SMALL_STR,
TABLE_NAME_FEED_ITEMS, TABLE_NAME_QUEUE,
TABLE_NAME_FEED_ITEMS + "." + KEY_ID,
TABLE_NAME_QUEUE + "." + KEY_FEEDITEM,
TABLE_NAME_QUEUE + "." + KEY_ID};
String query = String.format(
"SELECT %s FROM %s INNER JOIN %s ON %s=%s ORDER BY %s", args);
TABLE_NAME_QUEUE + "." + KEY_ID };
String query = String.format("SELECT %s FROM %s INNER JOIN %s ON %s=%s ORDER BY %s", args);
Cursor c = db.rawQuery(query, null);
/*
* Cursor c = db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL,
* "INNER JOIN ? ON ?=?", new String[] { TABLE_NAME_QUEUE,
* TABLE_NAME_FEED_ITEMS + "." + KEY_ID, TABLE_NAME_QUEUE + "." +
* KEY_FEEDITEM }, null, null, TABLE_NAME_QUEUE + "." + KEY_FEEDITEM);
*/
return c;
}

View File

@ -148,7 +148,7 @@ public class NetworkUtils {
}
subscriber.onNext(size);
subscriber.onCompleted();
DBWriter.setFeedMedia(context, media);
DBWriter.setFeedMedia(media);
}
})
.subscribeOn(Schedulers.newThread())

View File

@ -83,7 +83,7 @@ public class QueueSorter {
}
if (comparator != null) {
DBWriter.sortQueue(context, comparator, broadcastUpdate);
DBWriter.sortQueue(comparator, broadcastUpdate);
}
}
}

View File

@ -167,7 +167,7 @@ public class FlattrUtils {
deleteToken();
FlattrServiceCreator.deleteFlattrService();
showRevokeDialog(context);
DBWriter.clearAllFlattrStatus(context);
DBWriter.clearAllFlattrStatus();
}
// ------------------------------------------------ DIALOGS

View File

@ -89,7 +89,9 @@ public class UndoBarController<T> {
public void close() {
hideUndoBar(true);
mUndoListener.onHide(mUndoToken);
if(mUndoListener != null) {
mUndoListener.onHide(mUndoToken);
}
}
public void hideUndoBar(boolean immediate) {

View File

@ -164,7 +164,7 @@ public interface Playable extends Parcelable,
case FeedMedia.PLAYABLE_TYPE_FEEDMEDIA:
long mediaId = pref.getLong(FeedMedia.PREF_MEDIA_ID, -1);
if (mediaId != -1) {
return DBReader.getFeedMedia(context, mediaId);
return DBReader.getFeedMedia(mediaId);
}
break;
case ExternalMedia.PLAYABLE_TYPE_EXTERNAL_MEDIA:

View File

@ -56,7 +56,7 @@ public abstract class PlaybackController {
private final Activity activity;
private PlaybackService playbackService;
private Playable media;
protected Playable media;
private PlayerStatus status;
private ScheduledThreadPoolExecutor schedExecutor;
@ -479,8 +479,10 @@ public abstract class PlaybackController {
private void updatePlayButtonAppearance(int resource, CharSequence contentDescription) {
ImageButton butPlay = getPlayButton();
butPlay.setImageResource(resource);
butPlay.setContentDescription(contentDescription);
if(butPlay != null) {
butPlay.setImageResource(resource);
butPlay.setContentDescription(contentDescription);
}
}
public abstract ImageButton getPlayButton();