Implemented run-Method in FeedSyncThread

This commit is contained in:
Daniel Oeh 2011-12-24 20:19:31 +01:00
parent 1f6bcb3659
commit 53daf91337
3 changed files with 29 additions and 21 deletions

View File

@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.podfetcher" package="de.podfetcher"
android:versionCode="1" android:versionCode="1"
android:versionName="1.0" > android:versionName="1.0" >
<!-- <uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS" /> --> <!-- <uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS" /> -->
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
@ -12,22 +12,21 @@
<application <application
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" > android:label="@string/app_name" >
<activity <activity
android:label="@string/app_name" android:label="@string/app_name"
android:name=".PodfetcherActivity" > android:name=".PodfetcherActivity" >
<intent-filter > <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<service android:enabled="true" android:name=".DownloadService" /> <service android:enabled="true" android:name=".DownloadService" />
<service android:enabled="true" android:name=".FeedSyncService" <service android:enabled="true" android:name=".FeedSyncService" >
<intent-filter> <intent-filter>
<action android:name="action.de.podfetcher.storage.feed_download_completed"></action> <action android:name="action.de.podfetcher.storage.feed_download_completed"></action>
</intent-filter> </intent-filter>
</service> </service>
</application> </application>
</manifest> </manifest>

View File

@ -56,18 +56,17 @@ public class FeedManager {
} }
/** Adds a new Feeditem if its not in the list /** 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) { public void addFeedItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
// Search list for feeditem // Search list for feeditem
Feed foundFeed = searchFeedByLink(item.link); Feed foundFeed = searchFeedByLink(item.link);
FeedItem foundItem = searchFeedItemByLink(foundFeed, item.link) FeedItem foundItem = searchFeedItemByLink(foundFeed, item.link);
if(foundItem != null) { if(foundItem != null) {
// Update Information, mark as read // Update Information
item.id = foundItem.id; item.id = foundItem.id;
foundItem = item foundItem = item;
item.read = foundItem.read;
adapter.setFeedItem(item); adapter.setFeedItem(item);
} else { } else {
foundFeed.items.add(item); foundFeed.items.add(item);
@ -75,6 +74,7 @@ public class FeedManager {
} }
} }
/** Get a Feed by its link */
private Feed searchFeedByLink(String link) { private Feed searchFeedByLink(String link) {
for(Feed feed : feeds) { for(Feed feed : feeds) {
if(feed.link.equals(link)) { if(feed.link.equals(link)) {
@ -84,6 +84,7 @@ public class FeedManager {
return null; return null;
} }
/** Get a FeedItem by its link */
private FeedItem searchFeedItemByLink(Feed feed, String link) { private FeedItem searchFeedItemByLink(Feed feed, String link) {
for(FeedItem item : feed.items) { for(FeedItem item : feed.items) {
if(item.link.equals(link)) { if(item.link.equals(link)) {

View File

@ -4,14 +4,13 @@
* *
* */ * */
package de.podfetcher.service package de.podfetcher.service;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.lang.Runtime; import java.lang.Runtime;
import de.podfetcher.FeedManager; import de.podfetcher.feed.*;
import de.podfetcher.Feed; import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.FeedHandler;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
@ -21,13 +20,15 @@ import android.content.Context;
public class FeedSyncService extends Service { public class FeedSyncService extends Service {
private ScheduledThreadPoolExecutor executor; private volatile ScheduledThreadPoolExecutor executor;
private FeedManager manager; private FeedManager manager;
private DownloadRequester requester;
@Override @Override
public void onCreate() { public void onCreate() {
executor = new ScheduledThreadPoolExecutor(Runtime.availableProcessors() + 2); executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 2);
manager = FeedManager.getInstance(); manager = FeedManager.getInstance();
requester = DownloadRequester.getInstance();
} }
@Override @Override
@ -37,13 +38,14 @@ public class FeedSyncService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
executor.submit(new FeedSyncThread(handleIntent(intent))); executor.submit(new FeedSyncThread(handleIntent(intent), this));
return START_STICKY;
} }
/** Extracts a Feed object from the given Intent */ /** Extracts a Feed object from the given Intent */
private Feed handleIntent(Intent intent) { private Feed handleIntent(Intent intent) {
Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1)); Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
feed.file_url = requester.getFeedfilePath(context) + requester.getFeedfileName(feed.id); feed.file_url = requester.getFeedfilePath(this) + requester.getFeedfileName(feed.id);
return feed; return feed;
} }
@ -51,9 +53,11 @@ public class FeedSyncService extends Service {
class FeedSyncThread implements Runnable { class FeedSyncThread implements Runnable {
private Feed feed; private Feed feed;
private FeedSyncService service;
public FeedSyncThread(Feed feed) { public FeedSyncThread(Feed feed, FeedSyncService service) {
this.feed = feed; this.feed = feed;
this.service = service;
} }
public void run() { public void run() {
@ -61,6 +65,10 @@ public class FeedSyncService extends Service {
FeedHandler handler = new FeedHandler(); FeedHandler handler = new FeedHandler();
feed = handler.parseFeed(feed); feed = handler.parseFeed(feed);
// Add Feeditems to the database
for(FeedItem item : feed.items) {
manager.addFeedItem(service, item);
}
} }
} }