Created FeedSyncService

This commit is contained in:
Daniel Oeh 2011-12-24 13:48:11 +01:00
parent 1f39a1a0f1
commit 1f6bcb3659
5 changed files with 117 additions and 4 deletions

View File

@ -23,6 +23,11 @@
</intent-filter>
</activity>
<service android:enabled="true" android:name=".DownloadService" />
<service android:enabled="true" android:name=".FeedSyncService"
<intent-filter>
<action android:name="action.de.podfetcher.storage.feed_download_completed"></action>
</intent-filter>
</service>
</application>
</manifest>

View File

@ -44,12 +44,55 @@ public class FeedManager {
Feed feed = new Feed(url);
feed.download_url = url;
feed.id = adapter.setFeed(feed);
// Add Feed to Feedlist if not available
Feed foundFeed = getFeed(feed.id);
if(foundFeed == null) {
feeds.add(feed);
}else {
feed = foundFeed;
}
DownloadRequester req = DownloadRequester.getInstance();
req.downloadFeed(context, feed);
}
/** Adds a new Feeditem if its not in the list
* This Method is only called if the downloaded Feed was already in the feedlist
* */
public void addFeedItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context);
// Search list for feeditem
Feed foundFeed = searchFeedByLink(item.link);
FeedItem foundItem = searchFeedItemByLink(foundFeed, item.link)
if(foundItem != null) {
// Update Information, mark as read
item.id = foundItem.id;
foundItem = item
adapter.setFeedItem(item);
} else {
foundFeed.items.add(item);
item.id = adapter.setFeedItem(item);
}
}
private Feed searchFeedByLink(String link) {
for(Feed feed : feeds) {
if(feed.link.equals(link)) {
return feed;
}
}
return null;
}
private FeedItem searchFeedItemByLink(Feed feed, String link) {
for(FeedItem item : feed.items) {
if(item.link.equals(link)) {
return item;
}
}
return null;
}
/** Updates Information of an existing Feed */
public void setFeed(Context context, Feed feed) {
PodDBAdapter adapter = new PodDBAdapter(context);

View File

@ -0,0 +1,67 @@
/*
* Syncs downloaded Feedfiles with Feeds in the database
*
*
* */
package de.podfetcher.service
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.lang.Runtime;
import de.podfetcher.FeedManager;
import de.podfetcher.Feed;
import de.podfetcher.FeedHandler;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.content.Context;
public class FeedSyncService extends Service {
private ScheduledThreadPoolExecutor executor;
private FeedManager manager;
@Override
public void onCreate() {
executor = new ScheduledThreadPoolExecutor(Runtime.availableProcessors() + 2);
manager = FeedManager.getInstance();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
executor.submit(new FeedSyncThread(handleIntent(intent)));
}
/** 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.file_url = requester.getFeedfilePath(context) + requester.getFeedfileName(feed.id);
return feed;
}
/** Takes a single Feed, parses the corresponding file and refreshes information in the manager */
class FeedSyncThread implements Runnable {
private Feed feed;
public FeedSyncThread(Feed feed) {
this.feed = feed;
}
public void run() {
FeedManager manager = FeedManager.getInstance();
FeedHandler handler = new FeedHandler();
feed = handler.parseFeed(feed);
}
}
}

View File

@ -69,7 +69,7 @@ public class DownloadRequester {
public void downloadImage(Context context, FeedImage image) {
download(context, images, image.download_url,
new File(getImagefilePath, getImagefileName(image.id)),
new File(getImagefilePath(context), getImagefileName(image.id)),
true, ACTION_IMAGE_DOWNLOAD_COMPLETED, image.id);
}

View File

@ -22,8 +22,6 @@ public class DownloadService extends Service {
@Override
public void onCreate() {
receiver = new DownloadReceiver();
registerReceiver(receiver, createIntentFilter());
}