mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2024-12-23 23:41:36 +01:00
Now using SingleThreadExecutor for Sync
This commit is contained in:
parent
bda75e6756
commit
d0cb7c7f36
@ -60,8 +60,8 @@ public class FeedManager {
|
||||
public void addFeedItem(Context context, FeedItem item) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
// Search list for feeditem
|
||||
Feed foundFeed = searchFeedByLink(item.getLink());
|
||||
FeedItem foundItem = searchFeedItemByLink(foundFeed, item.getLink());
|
||||
Feed feed = item.getFeed();
|
||||
FeedItem foundItem = searchFeedItemByLink(feed, item.getLink());
|
||||
if(foundItem != null) {
|
||||
// Update Information
|
||||
item.id = foundItem.id;
|
||||
@ -69,7 +69,7 @@ public class FeedManager {
|
||||
item.setRead(foundItem.isRead());
|
||||
adapter.setFeedItem(item);
|
||||
} else {
|
||||
foundFeed.getItems().add(item);
|
||||
feed.getItems().add(item);
|
||||
item.id = adapter.setFeedItem(item);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,8 @@
|
||||
package de.podfetcher.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import de.podfetcher.feed.*;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
@ -25,7 +26,7 @@ public class DownloadService extends Service {
|
||||
public static String ACTION_ALL_FEED_DOWNLOADS_COMPLETED = "action.de.podfetcher.storage.all_feed_downloads_completed";
|
||||
public static final String ACTION_FEED_SYNC_COMPLETED = "action.de.podfetcher.service.feed_sync_completed";
|
||||
|
||||
private volatile ScheduledThreadPoolExecutor syncExecutor;
|
||||
private ExecutorService syncExecutor;
|
||||
private DownloadRequester requester;
|
||||
private FeedManager manager;
|
||||
|
||||
@ -33,7 +34,7 @@ public class DownloadService extends Service {
|
||||
public void onCreate() {
|
||||
Log.d(this.toString(), "Service started");
|
||||
registerReceiver(downloadReceiver, createIntentFilter());
|
||||
syncExecutor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 2);
|
||||
syncExecutor = Executors.newSingleThreadExecutor();
|
||||
manager = FeedManager.getInstance();
|
||||
requester = DownloadRequester.getInstance();
|
||||
}
|
||||
@ -64,7 +65,10 @@ public class DownloadService extends Service {
|
||||
public void run() {
|
||||
syncExecutor.shutdown();
|
||||
try {
|
||||
syncExecutor.awaitTermination(20, TimeUnit.SECONDS);
|
||||
Log.d(this.toString(), "Starting to wait for termination");
|
||||
boolean b = syncExecutor.awaitTermination(20L, TimeUnit.SECONDS);
|
||||
Log.d(this.toString(), "Stopping waiting for termination; Result : "+ b);
|
||||
|
||||
stopSelf();
|
||||
}catch(InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
@ -106,9 +110,10 @@ public class DownloadService extends Service {
|
||||
manager.setFeed(context, feed);
|
||||
// Download Feed Image if provided
|
||||
if(feed.getImage() != null) {
|
||||
Log.d(this.toString(), "Feed has image; Downloading....");
|
||||
requester.downloadImage(context, feed.getImage());
|
||||
}
|
||||
syncExecutor.submit(new FeedSyncThread(feed, this));
|
||||
syncExecutor.execute(new FeedSyncThread(feed, this));
|
||||
|
||||
}
|
||||
|
||||
@ -136,10 +141,15 @@ public class DownloadService extends Service {
|
||||
|
||||
feed = handler.parseFeed(feed);
|
||||
Log.d(this.toString(), feed.getTitle() + " parsed");
|
||||
// Save information of feed in DB
|
||||
Log.d(this.toString(), "Passing new Feed to DB");
|
||||
manager.setFeed(service, feed);
|
||||
// Add Feeditems to the database
|
||||
Log.d(this.toString(), "Walking through " + feed.getItems().size() + " feeditems");
|
||||
for(FeedItem item : feed.getItems()) {
|
||||
manager.addFeedItem(service, item);
|
||||
}
|
||||
Log.d(this.toString(), "Done.");
|
||||
}
|
||||
|
||||
}
|
||||
|
115
src/de/podfetcher/service/FeedSyncService.java
Normal file
115
src/de/podfetcher/service/FeedSyncService.java
Normal file
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Syncs downloaded Feedfiles with Feeds in the database
|
||||
*
|
||||
*
|
||||
* */
|
||||
|
||||
package de.podfetcher.service;
|
||||
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.lang.Runtime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import de.podfetcher.feed.*;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.os.IBinder;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
public class FeedSyncService extends Service {
|
||||
|
||||
public static final String ACTION_FEED_SYNC_COMPLETED = "action.de.podfetcher.service.feed_sync_completed";
|
||||
|
||||
private volatile ScheduledThreadPoolExecutor executor;
|
||||
private FeedManager manager;
|
||||
private DownloadRequester requester;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.d(this.toString(), "Service started");
|
||||
executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 2);
|
||||
manager = FeedManager.getInstance();
|
||||
requester = DownloadRequester.getInstance();
|
||||
registerReceiver(allFeedsDownloaded, new IntentFilter(DownloadService.ACTION_ALL_FEED_DOWNLOADS_COMPLETED));
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED));
|
||||
unregisterReceiver(allFeedsDownloaded);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
executor.submit(new FeedSyncThread(handleIntent(intent), this));
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
/** Extracts a Feed object from the given Intent */
|
||||
private Feed handleIntent(Intent intent) {
|
||||
Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
|
||||
feed.setFile_url(requester.getFeedfilePath(this) + requester.getFeedfileName(feed.getId()));
|
||||
return feed;
|
||||
}
|
||||
|
||||
/** Prepares itself for stopping */
|
||||
private void initiateShutdown() {
|
||||
Log.d(this.toString(), "Initiating shutdown");
|
||||
// Wait until PoolExecutor is done
|
||||
Thread waiter = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
executor.shutdown();
|
||||
try {
|
||||
executor.awaitTermination(20, TimeUnit.SECONDS);
|
||||
stopSelf();
|
||||
}catch(InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
BroadcastReceiver allFeedsDownloaded = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
initiateShutdown();
|
||||
}
|
||||
};
|
||||
|
||||
/** Takes a single Feed, parses the corresponding file and refreshes information in the manager */
|
||||
class FeedSyncThread implements Runnable {
|
||||
|
||||
private Feed feed;
|
||||
private FeedSyncService service;
|
||||
|
||||
public FeedSyncThread(Feed feed, FeedSyncService service) {
|
||||
this.feed = feed;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
FeedManager manager = FeedManager.getInstance();
|
||||
FeedHandler handler = new FeedHandler();
|
||||
|
||||
feed = handler.parseFeed(feed);
|
||||
Log.d(this.toString(), feed.getTitle() + " parsed");
|
||||
// Add Feeditems to the database
|
||||
for(FeedItem item : feed.getItems()) {
|
||||
manager.addFeedItem(service, item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -106,7 +106,7 @@ public class PodDBAdapter {
|
||||
* @return the id of the entry
|
||||
* */
|
||||
public long setFeed(Feed feed) {
|
||||
open();
|
||||
open();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_TITLE, feed.getTitle());
|
||||
values.put(KEY_LINK, feed.getLink());
|
||||
@ -130,9 +130,11 @@ public class PodDBAdapter {
|
||||
|
||||
if(feed.getId() == 0) {
|
||||
// Create new entry
|
||||
Log.d(this.toString(), "Inserting new Feed into db");
|
||||
feed.setId(db.insert(TABLE_NAME_FEEDS, null, values));
|
||||
} else {
|
||||
db.update(TABLE_NAME_FEEDS, values, KEY_ID+"=?", new String[]{String.valueOf(feed.getId())});
|
||||
Log.d(this.toString(), "Updating existing Feed in db");
|
||||
db.update(TABLE_NAME_FEEDS, values, KEY_ID+"=?", new String[]{Long.toString(feed.getId())});
|
||||
}
|
||||
close();
|
||||
return feed.getId();
|
||||
|
Loading…
Reference in New Issue
Block a user