Implemented run-Method in FeedSyncThread
This commit is contained in:
parent
1f6bcb3659
commit
53daf91337
|
@ -2,7 +2,7 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="de.podfetcher"
|
||||
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.INTERNET" />
|
||||
|
@ -12,22 +12,21 @@
|
|||
|
||||
<application
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name" >
|
||||
android:label="@string/app_name" >
|
||||
<activity
|
||||
android:label="@string/app_name"
|
||||
android:name=".PodfetcherActivity" >
|
||||
<intent-filter >
|
||||
android:name=".PodfetcherActivity" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<service android:enabled="true" android:name=".DownloadService" />
|
||||
<service android:enabled="true" android:name=".FeedSyncService"
|
||||
<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>
|
||||
|
|
|
@ -56,18 +56,17 @@ public class FeedManager {
|
|||
|
||||
}
|
||||
|
||||
/** Adds a new Feeditem if its not in the list
|
||||
* This Method is only called if the downloaded Feed was already in the feedlist
|
||||
* */
|
||||
/** Adds a new Feeditem if its not in the list */
|
||||
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)
|
||||
FeedItem foundItem = searchFeedItemByLink(foundFeed, item.link);
|
||||
if(foundItem != null) {
|
||||
// Update Information, mark as read
|
||||
// Update Information
|
||||
item.id = foundItem.id;
|
||||
foundItem = item
|
||||
foundItem = item;
|
||||
item.read = foundItem.read;
|
||||
adapter.setFeedItem(item);
|
||||
} else {
|
||||
foundFeed.items.add(item);
|
||||
|
@ -75,6 +74,7 @@ public class FeedManager {
|
|||
}
|
||||
}
|
||||
|
||||
/** Get a Feed by its link */
|
||||
private Feed searchFeedByLink(String link) {
|
||||
for(Feed feed : feeds) {
|
||||
if(feed.link.equals(link)) {
|
||||
|
@ -84,6 +84,7 @@ public class FeedManager {
|
|||
return null;
|
||||
}
|
||||
|
||||
/** Get a FeedItem by its link */
|
||||
private FeedItem searchFeedItemByLink(Feed feed, String link) {
|
||||
for(FeedItem item : feed.items) {
|
||||
if(item.link.equals(link)) {
|
||||
|
|
|
@ -4,14 +4,13 @@
|
|||
*
|
||||
* */
|
||||
|
||||
package de.podfetcher.service
|
||||
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 de.podfetcher.feed.*;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
|
@ -21,13 +20,15 @@ import android.content.Context;
|
|||
|
||||
public class FeedSyncService extends Service {
|
||||
|
||||
private ScheduledThreadPoolExecutor executor;
|
||||
private volatile ScheduledThreadPoolExecutor executor;
|
||||
private FeedManager manager;
|
||||
private DownloadRequester requester;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
executor = new ScheduledThreadPoolExecutor(Runtime.availableProcessors() + 2);
|
||||
executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 2);
|
||||
manager = FeedManager.getInstance();
|
||||
requester = DownloadRequester.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -37,13 +38,14 @@ public class FeedSyncService extends Service {
|
|||
|
||||
@Override
|
||||
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 */
|
||||
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);
|
||||
feed.file_url = requester.getFeedfilePath(this) + requester.getFeedfileName(feed.id);
|
||||
return feed;
|
||||
}
|
||||
|
||||
|
@ -51,9 +53,11 @@ public class FeedSyncService extends Service {
|
|||
class FeedSyncThread implements Runnable {
|
||||
|
||||
private Feed feed;
|
||||
private FeedSyncService service;
|
||||
|
||||
public FeedSyncThread(Feed feed) {
|
||||
public FeedSyncThread(Feed feed, FeedSyncService service) {
|
||||
this.feed = feed;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
@ -61,6 +65,10 @@ public class FeedSyncService extends Service {
|
|||
FeedHandler handler = new FeedHandler();
|
||||
|
||||
feed = handler.parseFeed(feed);
|
||||
// Add Feeditems to the database
|
||||
for(FeedItem item : feed.items) {
|
||||
manager.addFeedItem(service, item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue