diff --git a/src/de/danoeh/antennapod/activity/DownloadActivity.java b/src/de/danoeh/antennapod/activity/DownloadActivity.java index cf3655681..7234ffcde 100644 --- a/src/de/danoeh/antennapod/activity/DownloadActivity.java +++ b/src/de/danoeh/antennapod/activity/DownloadActivity.java @@ -1,12 +1,17 @@ package de.danoeh.antennapod.activity; +import java.util.List; + import android.annotation.SuppressLint; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.ServiceConnection; import android.os.AsyncTask; import android.os.Bundle; +import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.AdapterView; @@ -22,6 +27,7 @@ import de.danoeh.antennapod.R; import de.danoeh.antennapod.adapter.DownloadlistAdapter; import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.service.download.DownloadService; +import de.danoeh.antennapod.service.download.Downloader; import de.danoeh.antennapod.storage.DownloadRequester; /** @@ -40,6 +46,9 @@ public class DownloadActivity extends SherlockListActivity implements private ActionMode mActionMode; private DownloadStatus selectedDownload; + private DownloadService downloadService = null; + boolean mIsBound; + private AsyncTask contentRefresher; @Override @@ -49,12 +58,12 @@ public class DownloadActivity extends SherlockListActivity implements Log.d(TAG, "Creating Activity"); requester = DownloadRequester.getInstance(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - dla = new DownloadlistAdapter(this, 0, DownloadService.getDownloads()); } @Override protected void onPause() { super.onPause(); + unbindService(mConnection); unregisterReceiver(contentChanged); } @@ -63,6 +72,7 @@ public class DownloadActivity extends SherlockListActivity implements super.onResume(); registerReceiver(contentChanged, new IntentFilter( DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED)); + bindService(new Intent(this, DownloadService.class), mConnection, 0); startContentRefresher(); } @@ -74,6 +84,25 @@ public class DownloadActivity extends SherlockListActivity implements stopContentRefresher(); } + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceDisconnected(ComponentName className) { + downloadService = null; + mIsBound = false; + Log.i(TAG, "Closed connection with DownloadService."); + } + + public void onServiceConnected(ComponentName name, IBinder service) { + downloadService = ((DownloadService.LocalBinder) service) + .getService(); + mIsBound = true; + if (AppConfig.DEBUG) + Log.d(TAG, "Connection to service established"); + dla = new DownloadlistAdapter(DownloadActivity.this, 0, + downloadService.getDownloads()); + setListAdapter(dla); + } + }; + @SuppressLint("NewApi") private void startContentRefresher() { if (contentRefresher != null) { @@ -81,16 +110,20 @@ public class DownloadActivity extends SherlockListActivity implements } contentRefresher = new AsyncTask() { private final int WAITING_INTERVALL = 1000; - + @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); - dla.notifyDataSetChanged(); + if (dla != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Refreshing content automatically"); + dla.notifyDataSetChanged(); + } } @Override protected Void doInBackground(Void... params) { - while(!isCancelled()) { + while (!isCancelled()) { try { Thread.sleep(WAITING_INTERVALL); publishProgress(); @@ -107,7 +140,7 @@ public class DownloadActivity extends SherlockListActivity implements contentRefresher.execute(); } } - + private void stopContentRefresher() { if (contentRefresher != null) { contentRefresher.cancel(true); @@ -202,6 +235,8 @@ public class DownloadActivity extends SherlockListActivity implements @Override public void onReceive(Context context, Intent intent) { if (dla != null) { + if (AppConfig.DEBUG) + Log.d(TAG, "Refreshing content"); dla.notifyDataSetChanged(); } } diff --git a/src/de/danoeh/antennapod/adapter/DownloadlistAdapter.java b/src/de/danoeh/antennapod/adapter/DownloadlistAdapter.java index dc6f8eb27..4346de927 100644 --- a/src/de/danoeh/antennapod/adapter/DownloadlistAdapter.java +++ b/src/de/danoeh/antennapod/adapter/DownloadlistAdapter.java @@ -78,7 +78,9 @@ public class DownloadlistAdapter extends ArrayAdapter { } } holder.title.setText(titleText); - holder.message.setText(status.getStatusMsg()); + if (status.getStatusMsg() != 0) { + holder.message.setText(status.getStatusMsg()); + } holder.downloaded.setText(Converter.byteToString(status.getSoFar()) + " / " + Converter.byteToString(status.getSize())); holder.percent.setText(status.getProgressPercent() + "%"); diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java index 13176686c..2730891ea 100644 --- a/src/de/danoeh/antennapod/service/download/DownloadService.java +++ b/src/de/danoeh/antennapod/service/download/DownloadService.java @@ -103,7 +103,7 @@ public class DownloadService extends Service { /** Needed to determine the duration of a media file */ private MediaPlayer mediaplayer; - private static List downloads = new ArrayList(); + private List downloads; private volatile boolean shutdownInitiated = false; /** True if service is running. */ @@ -119,6 +119,9 @@ public class DownloadService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + if (intent.getParcelableExtra(EXTRA_REQUEST) != null) { + onDownloadQueued(intent); + } return Service.START_NOT_STICKY; } @@ -129,10 +132,7 @@ public class DownloadService extends Service { Log.d(TAG, "Service started"); isRunning = true; completedDownloads = new ArrayList(); - if (!downloads.isEmpty()) { - downloads.clear(); - sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); - } + downloads = new ArrayList(); registerReceiver(downloadQueued, new IntentFilter( ACTION_ENQUEUE_DOWNLOAD)); @@ -256,43 +256,45 @@ public class DownloadService extends Service { }; + private void onDownloadQueued(Intent intent) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received enqueue request"); + Request request = intent.getParcelableExtra(EXTRA_REQUEST); + if (request == null) { + throw new IllegalArgumentException( + "ACTION_ENQUEUE_DOWNLOAD intent needs request extra"); + } + if (shutdownInitiated) { + if (AppConfig.DEBUG) + Log.d(TAG, "Cancelling shutdown; new download was queued"); + shutdownInitiated = false; + setupNotification(); + } + + DownloadRequester requester = DownloadRequester.getInstance(); + FeedFile feedfile = requester.getDownload(request.source); + if (feedfile != null) { + + DownloadStatus status = new DownloadStatus(feedfile); + Downloader downloader = getDownloader(status); + if (downloader != null) { + downloads.add(downloader); + downloadExecutor.submit(downloader); + sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); + } + } else { + Log.e(TAG, + "Could not find feedfile in download requester when trying to enqueue new download"); + queryDownloads(); + + } + } + private BroadcastReceiver downloadQueued = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(ACTION_ENQUEUE_DOWNLOAD)) { - if (AppConfig.DEBUG) - Log.d(TAG, "Received enqueue request"); - Request request = intent.getParcelableExtra(EXTRA_REQUEST); - if (request == null) { - throw new IllegalArgumentException( - "ACTION_ENQUEUE_DOWNLOAD intent needs request extra"); - } - if (shutdownInitiated) { - if (AppConfig.DEBUG) Log.d(TAG, "Cancelling shutdown; new download was queued"); - shutdownInitiated = false; - setupNotification(); - } - - DownloadRequester requester = DownloadRequester.getInstance(); - FeedFile feedfile = requester.getDownload(request.source); - if (feedfile != null) { - - DownloadStatus status = new DownloadStatus(feedfile); - Downloader downloader = getDownloader(status); - if (downloader != null) { - downloads.add(downloader); - downloadExecutor.submit(downloader); - sendBroadcast(new Intent( - ACTION_DOWNLOADS_CONTENT_CHANGED)); - } - } else { - Log.e(TAG, - "Could not find feedfile in download requester when trying to enqueue new download"); - queryDownloads(); - - } - } + onDownloadQueued(intent); } }; @@ -459,9 +461,10 @@ public class DownloadService extends Service { Log.d(TAG, numOfDownloads + " downloads left"); if (AppConfig.DEBUG) Log.d(TAG, "ShutdownInitiated: " + shutdownInitiated); - + if (numOfDownloads == 0) { - if (AppConfig.DEBUG) Log.d(TAG, "Starting shutdown"); + if (AppConfig.DEBUG) + Log.d(TAG, "Starting shutdown"); shutdownInitiated = true; stopForeground(true); } else { @@ -566,7 +569,7 @@ public class DownloadService extends Service { if (savedFeed == null) { savedFeed = feed; } - + saveDownloadStatus(new DownloadStatus(savedFeed, reason, successful)); sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED); queryDownloads(); @@ -654,7 +657,7 @@ public class DownloadService extends Service { if (AppConfig.DEBUG) Log.d(TAG, "Duration of file is " + media.getDuration()); mediaplayer.reset(); - + status.setCompletionDate(new Date()); saveDownloadStatus(status); sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA); @@ -728,7 +731,7 @@ public class DownloadService extends Service { } - public static List getDownloads() { + public List getDownloads() { return downloads; } diff --git a/src/de/danoeh/antennapod/service/download/Downloader.java b/src/de/danoeh/antennapod/service/download/Downloader.java index 33a70ab08..83cdeb921 100644 --- a/src/de/danoeh/antennapod/service/download/Downloader.java +++ b/src/de/danoeh/antennapod/service/download/Downloader.java @@ -11,7 +11,7 @@ public abstract class Downloader extends Thread { protected boolean finished; - protected DownloadStatus status; + protected volatile DownloadStatus status; public Downloader(DownloadService downloadService, DownloadStatus status) { super(); diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index cdee44f64..1a76520c7 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -69,13 +69,17 @@ public class DownloadRequester { DownloadService.Request request = new DownloadService.Request( item.getFile_url(), item.getDownload_url()); - Intent queueIntent = new Intent( - DownloadService.ACTION_ENQUEUE_DOWNLOAD); - queueIntent.putExtra(DownloadService.EXTRA_REQUEST, request); + if (!DownloadService.isRunning) { - context.startService(new Intent(context, DownloadService.class)); + Intent launchIntent = new Intent(context, DownloadService.class); + launchIntent.putExtra(DownloadService.EXTRA_REQUEST, request); + context.startService(launchIntent); + } else { + Intent queueIntent = new Intent( + DownloadService.ACTION_ENQUEUE_DOWNLOAD); + queueIntent.putExtra(DownloadService.EXTRA_REQUEST, request); + context.sendBroadcast(queueIntent); } - context.sendBroadcast(queueIntent); context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); } else { Log.e(TAG, "URL " + item.getDownload_url() @@ -161,7 +165,9 @@ public class DownloadRequester { /** Remove an object from the downloads-list of the requester. */ public void removeDownload(FeedFile f) { - downloads.remove(f.getDownload_url()); + if (downloads.remove(f.getDownload_url()) == null) { + Log.e(TAG, "Could not remove object with url " + f.getDownload_url()); + } } /** Get the number of uncompleted Downloads */