From 7b86821cd83d8051da0a1fd2777d33837b1fa325 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Fri, 6 Jul 2012 10:37:28 +0200 Subject: Implemented new Downloadobserver into the other classes --- src/de/podfetcher/activity/DownloadActivity.java | 73 +++++++++++++++++----- src/de/podfetcher/adapter/DownloadlistAdapter.java | 8 ++- src/de/podfetcher/asynctask/DownloadObserver.java | 34 +++++++--- src/de/podfetcher/service/DownloadService.java | 53 ++++++++++------ 4 files changed, 124 insertions(+), 44 deletions(-) (limited to 'src/de/podfetcher') diff --git a/src/de/podfetcher/activity/DownloadActivity.java b/src/de/podfetcher/activity/DownloadActivity.java index b9f814d94..c71c74743 100644 --- a/src/de/podfetcher/activity/DownloadActivity.java +++ b/src/de/podfetcher/activity/DownloadActivity.java @@ -1,6 +1,7 @@ package de.podfetcher.activity; import de.podfetcher.R; +import de.podfetcher.service.DownloadService; import de.podfetcher.storage.DownloadRequester; import de.podfetcher.adapter.DownloadlistAdapter; import de.podfetcher.asynctask.DownloadObserver; @@ -12,8 +13,12 @@ import com.actionbarsherlock.view.ActionMode; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; +import android.content.ComponentName; +import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.os.Bundle; +import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnLongClickListener; @@ -22,7 +27,7 @@ import android.widget.AdapterView.OnItemLongClickListener; /** Shows all running downloads in a list */ public class DownloadActivity extends SherlockListActivity implements - ActionMode.Callback { + ActionMode.Callback, DownloadObserver.Callback { private static final String TAG = "DownloadActivity"; private static final int MENU_SHOW_LOG = 0; @@ -32,35 +37,38 @@ public class DownloadActivity extends SherlockListActivity implements private ActionMode mActionMode; private DownloadStatus selectedDownload; + private DownloadObserver downloadObserver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + Log.d(TAG, "Creating Activity"); requester = DownloadRequester.getInstance(); - observer.execute(requester.getDownloads().toArray( - new FeedFile[requester.getDownloads().size()])); } + @Override + protected void onPause() { + super.onPause(); + unbindService(mConnection); + if (downloadObserver != null) { + downloadObserver.unregisterCallback(DownloadActivity.this); + } + } + + @Override + protected void onResume() { + super.onResume(); + Log.d(TAG, "Trying to bind service"); + bindService(new Intent(this, DownloadService.class), mConnection, 0); + } + @Override protected void onStop() { super.onStop(); Log.d(TAG, "Stopping Activity"); - observer.cancel(true); } - private final DownloadObserver observer = new DownloadObserver(this) { - @Override - protected void onProgressUpdate(DownloadStatus... values) { - - dla = new DownloadlistAdapter(getContext(), 0, getStatusList()); - setListAdapter(dla); - dla.notifyDataSetChanged(); - - } - }; - @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); @@ -141,4 +149,37 @@ public class DownloadActivity extends SherlockListActivity implements selectedDownload = null; dla.setSelectedItemIndex(DownloadlistAdapter.SELECTION_NONE); } + + private DownloadService downloadService = null; + boolean mIsBound; + + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder service) { + downloadService = ((DownloadService.LocalBinder) service) + .getService(); + Log.d(TAG, "Connection to service established"); + dla = new DownloadlistAdapter(DownloadActivity.this, 0, + downloadService.getDownloadObserver().getStatusList()); + setListAdapter(dla); + downloadObserver = downloadService.getDownloadObserver(); + downloadObserver.registerCallback(DownloadActivity.this); + } + + public void onServiceDisconnected(ComponentName className) { + downloadService = null; + mIsBound = false; + Log.i(TAG, "Closed connection with DownloadService."); + } + }; + + @Override + public void onProgressUpdate() { + dla.notifyDataSetChanged(); + } + + @Override + public void onFinish() { + dla.clear(); + dla.notifyDataSetChanged(); + } } diff --git a/src/de/podfetcher/adapter/DownloadlistAdapter.java b/src/de/podfetcher/adapter/DownloadlistAdapter.java index b128bcf16..91060562d 100644 --- a/src/de/podfetcher/adapter/DownloadlistAdapter.java +++ b/src/de/podfetcher/adapter/DownloadlistAdapter.java @@ -1,5 +1,7 @@ package de.podfetcher.adapter; +import java.util.List; + import android.content.Context; import android.view.LayoutInflater; import android.view.View; @@ -21,11 +23,13 @@ public class DownloadlistAdapter extends ArrayAdapter { public static final int SELECTION_NONE = -1; public DownloadlistAdapter(Context context, int textViewResourceId, - DownloadStatus[] objects) { + List objects) { super(context, textViewResourceId, objects); - selectedItemIndex = SELECTION_NONE; + this.selectedItemIndex = SELECTION_NONE; } + + @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder; diff --git a/src/de/podfetcher/asynctask/DownloadObserver.java b/src/de/podfetcher/asynctask/DownloadObserver.java index f94c3c457..65d3e6129 100644 --- a/src/de/podfetcher/asynctask/DownloadObserver.java +++ b/src/de/podfetcher/asynctask/DownloadObserver.java @@ -45,6 +45,21 @@ public class DownloadObserver extends AsyncTask { @Override protected void onCancelled() { Log.d(TAG, "Task was cancelled."); + statusList.clear(); + for (DownloadObserver.Callback callback : observer) { + callback.onFinish(); + } + } + + + + @Override + protected void onPostExecute(Void result) { + Log.d(TAG, "Background task has finished"); + statusList.clear(); + for (DownloadObserver.Callback callback : observer) { + callback.onFinish(); + } } protected Void doInBackground(Void... params) { @@ -70,6 +85,9 @@ public class DownloadObserver extends AsyncTask { } private void refreshStatuslist() { + ArrayList unhandledItems = new ArrayList( + statusList); + Cursor cursor = getDownloadCursor(); if (cursor.moveToFirst()) { do { @@ -80,6 +98,8 @@ public class DownloadObserver extends AsyncTask { if (status == null) { status = new DownloadStatus(feedFile); statusList.add(status); + } else { + unhandledItems.remove(status); } // refresh status @@ -112,6 +132,11 @@ public class DownloadObserver extends AsyncTask { } while (cursor.moveToNext()); } cursor.close(); + + // remove unhandled items from statuslist + for (DownloadStatus status : unhandledItems) { + statusList.remove(status); + } } /** Request a cursor with all running Feedfile downloads */ @@ -144,7 +169,6 @@ public class DownloadObserver extends AsyncTask { .getLong(c .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); status.progressPercent = (int) (((double) status.soFar / (double) status.size) * 100); - Log.d(TAG, "Setting progress to " + status.progressPercent); } public Context getContext() { @@ -166,12 +190,7 @@ public class DownloadObserver extends AsyncTask { } private boolean downloadsLeft() { - for (DownloadStatus status : statusList) { - if (status.done == false) { - return true; - } - } - return false; + return !requester.downloads.isEmpty(); } public void registerCallback(DownloadObserver.Callback callback) { @@ -184,6 +203,7 @@ public class DownloadObserver extends AsyncTask { public interface Callback { public void onProgressUpdate(); + public void onFinish(); } } diff --git a/src/de/podfetcher/service/DownloadService.java b/src/de/podfetcher/service/DownloadService.java index 5af129663..8d36df888 100644 --- a/src/de/podfetcher/service/DownloadService.java +++ b/src/de/podfetcher/service/DownloadService.java @@ -17,6 +17,7 @@ import org.xml.sax.SAXException; import de.podfetcher.activity.DownloadActivity; import de.podfetcher.activity.MediaplayerActivity; +import de.podfetcher.asynctask.DownloadObserver; import de.podfetcher.asynctask.DownloadStatus; import de.podfetcher.feed.*; import de.podfetcher.service.PlaybackService.LocalBinder; @@ -51,7 +52,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"; - + public static final String ACTION_DOWNLOAD_HANDLED = "action.de.podfetcher.service.download_handled"; /** True if handled feed has an image. */ public static final String EXTRA_FEED_HAS_IMAGE = "extra.de.podfetcher.service.feed_has_image"; @@ -59,7 +60,6 @@ public class DownloadService extends Service { public static final String EXTRA_STATUS_ID = "extra.de.podfetcher.service.feedfile_id"; public static final String EXTRA_DOWNLOAD_ID = "extra.de.podfetcher.service.download_id"; public static final String EXTRA_IMAGE_DOWNLOAD_ID = "extra.de.podfetcher.service.image_download_id"; - private ExecutorService syncExecutor; private DownloadRequester requester; @@ -70,6 +70,8 @@ public class DownloadService extends Service { private MediaPlayer mediaplayer; private DownloadManager downloadManager; + private DownloadObserver downloadObserver; + private volatile boolean shutdownInitiated = false; /** True if service is running. */ public static boolean isRunning = false; @@ -98,7 +100,9 @@ public class DownloadService extends Service { requester = DownloadRequester.getInstance(); mediaplayer = new MediaPlayer(); downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + downloadObserver = new DownloadObserver(this); setupNotification(); + downloadObserver.execute(); } @Override @@ -113,6 +117,7 @@ public class DownloadService extends Service { sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED)); mediaplayer.release(); unregisterReceiver(downloadReceiver); + downloadObserver.cancel(true); } private IntentFilter createIntentFilter() { @@ -194,19 +199,20 @@ public class DownloadService extends Service { (FeedMedia) download); } successful = true; - + } else if (status == DownloadManager.STATUS_FAILED) { reason = c.getInt(c .getColumnIndex(DownloadManager.COLUMN_REASON)); Log.e(TAG, "Download failed"); Log.e(TAG, "reason code is " + reason); successful = false; - long statusId = manager.addDownloadStatus(context, new DownloadStatus(download, - reason, successful)); + long statusId = manager.addDownloadStatus(context, + new DownloadStatus(download, reason, successful)); requester.removeDownload(download); - sendDownloadHandledIntent(download.getDownloadId(), statusId, false, 0); + sendDownloadHandledIntent(download.getDownloadId(), + statusId, false, 0); download.setDownloadId(0); - + } queryDownloads(); c.close(); @@ -214,8 +220,9 @@ public class DownloadService extends Service { } }; - - private void sendDownloadHandledIntent(long downloadId, long statusId, boolean feedHasImage, long imageDownloadId) { + + private void sendDownloadHandledIntent(long downloadId, long statusId, + boolean feedHasImage, long imageDownloadId) { Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED); intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId); intent.putExtra(EXTRA_STATUS_ID, statusId); @@ -286,7 +293,7 @@ public class DownloadService extends Service { FeedManager manager = FeedManager.getInstance(); FeedHandler handler = new FeedHandler(); feed.setDownloaded(true); - + try { feed = handler.parseFeed(feed); Log.d(TAG, feed.getTitle() + " parsed"); @@ -296,7 +303,7 @@ public class DownloadService extends Service { imageId = requester.downloadImage(service, feed.getImage()); hasImage = true; } - + feed.setDownloadId(0); // Save information of feed in DB savedFeed = manager.updateFeed(service, feed); @@ -317,12 +324,13 @@ public class DownloadService extends Service { successful = false; reason = DownloadError.ERROR_UNSUPPORTED_TYPE; } - + requester.removeDownload(feed); - cleanup(); - long statusId = manager.addDownloadStatus(service, new DownloadStatus(savedFeed, reason, successful)); + cleanup(); + long statusId = manager.addDownloadStatus(service, + new DownloadStatus(savedFeed, reason, successful)); sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId); - queryDownloads(); + queryDownloads(); } /** Delete files that aren't needed anymore */ @@ -350,11 +358,12 @@ public class DownloadService extends Service { public void run() { image.setDownloaded(true); requester.removeDownload(image); - - long statusId = manager.addDownloadStatus(service, new DownloadStatus(image, 0, true)); + + long statusId = manager.addDownloadStatus(service, + new DownloadStatus(image, 0, true)); sendDownloadHandledIntent(image.getDownloadId(), statusId, false, 0); image.setDownloadId(0); - + manager.setFeedImage(service, image); queryDownloads(); } @@ -385,11 +394,17 @@ public class DownloadService extends Service { media.setDuration(mediaplayer.getDuration()); Log.d(TAG, "Duration of file is " + media.getDuration()); mediaplayer.reset(); - long statusId = manager.addDownloadStatus(service, new DownloadStatus(media, 0, true)); + long statusId = manager.addDownloadStatus(service, + new DownloadStatus(media, 0, true)); sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0); media.setDownloadId(0); manager.setFeedMedia(service, media); queryDownloads(); } } + + public DownloadObserver getDownloadObserver() { + return downloadObserver; + } + } -- cgit v1.2.3