Fixed Service starting process + Bugs

This commit is contained in:
Daniel Oeh 2011-12-29 13:05:32 +01:00
parent 5a9798680b
commit d1db888141
8 changed files with 93 additions and 12 deletions

View File

@ -2,16 +2,45 @@ package de.podfetcher.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import de.podfetcher.R;
import de.podfetcher.feed.FeedManager;
/** Activity for adding/editing a Feed */
public class AddFeedActivity extends Activity {
private EditText etxtFeedurl;
private Button butConfirm;
private FeedManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addfeed);
manager = FeedManager.getInstance();
etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl);
butConfirm = (Button) findViewById(R.id.butConfirm);
butConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewFeed();
finish();
}
});
}
private void addNewFeed() {
String url = etxtFeedurl.getText().toString();
manager.addFeed(this, url);
}

View File

@ -1,24 +1,55 @@
package de.podfetcher.activity;
import de.podfetcher.R;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.gui.FeedlistAdapter;
import de.podfetcher.service.FeedSyncService;
import de.podfetcher.storage.DownloadRequester;
import greendroid.app.GDListActivity;
import android.os.Bundle;
import android.view.View;
import greendroid.widget.ActionBarItem.Type;
import greendroid.widget.ActionBarItem;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.Context;
public class FeedlistActivity extends GDListActivity {
private FeedManager manager;
private FeedlistAdapter fla;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager = FeedManager.getInstance();
fla = new FeedlistAdapter(this, R.layout.feedlist_item, 0, manager.getFeeds());
setListAdapter(fla);
addActionBarItem(Type.Add, R.id.action_bar_add);
addActionBarItem(Type.Refresh, R.id.action_bar_refresh);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(FeedSyncService.ACTION_FEED_SYNC_COMPLETED);
filter.addAction(DownloadRequester.ACTION_IMAGE_DOWNLOAD_COMPLETED);
registerReceiver(contentUpdate, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(contentUpdate);
}
@Override
public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
switch(item.getItemId()) {
@ -29,4 +60,11 @@ public class FeedlistActivity extends GDListActivity {
return super.onHandleActionBarItemClick(item, position);
}
}
private BroadcastReceiver contentUpdate = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
fla.notifyDataSetChanged();
}
};
}

View File

@ -182,6 +182,10 @@ public class FeedManager {
return items;
}
public ArrayList<Feed> getFeeds() {
return feeds;
}

View File

@ -26,6 +26,8 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout feedlistView;
@ -45,8 +47,9 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
ImageView imageView = (ImageView)feedlistView.findViewById(R.id.imgvFeedimage);
TextView txtvFeedname = (TextView)feedlistView.findViewById(R.id.txtvFeedname);
TextView txtvNewEpisodes = (TextView)feedlistView.findViewById(R.id.txtvNewEpisodes);
imageView.setImageURI(Uri.fromFile(new File(feed.getFile_url()))); // TODO select default picture when no image downloaded
if(feed.getImage() != null) {
imageView.setImageURI(Uri.fromFile(new File(feed.getFile_url()))); // TODO select default picture when no image downloaded
}
txtvFeedname.setText(feed.getTitle());
// TODO find new Episodes txtvNewEpisodes.setText(feed)
return feedlistView;

View File

@ -6,6 +6,7 @@
package de.podfetcher.service;
import java.io.File;
import de.podfetcher.feed.*;
import de.podfetcher.storage.DownloadRequester;
import android.app.Service;
@ -15,6 +16,7 @@ import android.content.IntentFilter;
import android.os.IBinder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.util.Log;
public class DownloadService extends Service {
@ -25,6 +27,7 @@ public class DownloadService extends Service {
@Override
public void onCreate() {
Log.d(this.toString(), "Service started");
registerReceiver(receiver, createIntentFilter());
}
@ -78,15 +81,14 @@ public class DownloadService extends Service {
requester.removeFeedByID(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
// Get Feed Information
Feed feed = manager.getFeed(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
feed.setFile_url(requester.getFeedfilePath(context) + requester.getFeedfileName(feed.getId()));
feed.setFile_url((new File(requester.getFeedfilePath(context), requester.getFeedfileName(feed.getId()))).toString());
// Update Information in Database
manager.setFeed(context, feed);
// Download Feed Image if provided
if(feed.getImage() != null) {
requester.downloadImage(context, feed.getImage());
}
// Notify FeedSyncService about the new Feed
sendBroadcast(intent);
context.startService(intent);
}
@ -95,7 +97,6 @@ public class DownloadService extends Service {
requester.removeImageByID(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
FeedImage image = manager.getFeedImage(intent.getLongExtra(DownloadRequester.EXTRA_ITEM_ID, -1));
image.setFile_url(requester.getImagefilePath(context) + requester.getImagefileName(image.getId()));
sendBroadcast(intent);
}
}

View File

@ -19,6 +19,7 @@ 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 {
@ -31,6 +32,7 @@ public class FeedSyncService extends Service {
@Override
public void onCreate() {
Log.d(this.toString(), "Service started");
executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 2);
manager = FeedManager.getInstance();
requester = DownloadRequester.getInstance();
@ -44,6 +46,7 @@ public class FeedSyncService extends Service {
@Override
public void onDestroy() {
sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED));
unregisterReceiver(allFeedsDownloaded);
}
@ -100,6 +103,7 @@ public class FeedSyncService extends Service {
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);

View File

@ -6,6 +6,7 @@ import java.io.File;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.service.DownloadService;
import android.app.DownloadManager;
import android.content.Context;
@ -22,9 +23,9 @@ public class DownloadRequester {
public static String ACTION_IMAGE_DOWNLOAD_COMPLETED = "action.de.podfetcher.storage.image_download_completed";
private static boolean STORE_ON_SD = true;
public static String IMAGE_DOWNLOADPATH = "images";
public static String FEED_DOWNLOADPATH = "cache";
public static String MEDIA_DOWNLOADPATH = "media";
public static String IMAGE_DOWNLOADPATH = "images/";
public static String FEED_DOWNLOADPATH = "cache/";
public static String MEDIA_DOWNLOADPATH = "media/";
private static DownloadRequester downloader;
@ -56,6 +57,7 @@ public class DownloadRequester {
// TODO Set Allowed Network Types
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Intent i = new Intent(action);
context.startService(new Intent(context, DownloadService.class));
i.putExtra(EXTRA_DOWNLOAD_ID, manager.enqueue(request));
i.putExtra(EXTRA_ITEM_ID, id);
type.add(i);
@ -139,7 +141,7 @@ public class DownloadRequester {
}
public String getFeedfilePath(Context context) {
return context.getExternalFilesDir(FEED_DOWNLOADPATH).toString();
return context.getExternalFilesDir(FEED_DOWNLOADPATH).toString() + "/";
}
public String getFeedfileName(long id) {
@ -147,7 +149,7 @@ public class DownloadRequester {
}
public String getImagefilePath(Context context) {
return context.getExternalFilesDir(IMAGE_DOWNLOADPATH).toString();
return context.getExternalFilesDir(IMAGE_DOWNLOADPATH).toString() + "/";
}
public String getImagefileName(long id) {