DownloadActivity should now display the content correctly

This commit is contained in:
daniel oeh 2012-08-17 12:59:30 +02:00
parent d420e67a07
commit 27afa983ac
5 changed files with 102 additions and 56 deletions

View File

@ -1,12 +1,17 @@
package de.danoeh.antennapod.activity; package de.danoeh.antennapod.activity;
import java.util.List;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
@ -22,6 +27,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.DownloadlistAdapter; import de.danoeh.antennapod.adapter.DownloadlistAdapter;
import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.asynctask.DownloadStatus;
import de.danoeh.antennapod.service.download.DownloadService; import de.danoeh.antennapod.service.download.DownloadService;
import de.danoeh.antennapod.service.download.Downloader;
import de.danoeh.antennapod.storage.DownloadRequester; import de.danoeh.antennapod.storage.DownloadRequester;
/** /**
@ -40,6 +46,9 @@ public class DownloadActivity extends SherlockListActivity implements
private ActionMode mActionMode; private ActionMode mActionMode;
private DownloadStatus selectedDownload; private DownloadStatus selectedDownload;
private DownloadService downloadService = null;
boolean mIsBound;
private AsyncTask<Void, Void, Void> contentRefresher; private AsyncTask<Void, Void, Void> contentRefresher;
@Override @Override
@ -49,12 +58,12 @@ public class DownloadActivity extends SherlockListActivity implements
Log.d(TAG, "Creating Activity"); Log.d(TAG, "Creating Activity");
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
dla = new DownloadlistAdapter(this, 0, DownloadService.getDownloads());
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
unbindService(mConnection);
unregisterReceiver(contentChanged); unregisterReceiver(contentChanged);
} }
@ -63,6 +72,7 @@ public class DownloadActivity extends SherlockListActivity implements
super.onResume(); super.onResume();
registerReceiver(contentChanged, new IntentFilter( registerReceiver(contentChanged, new IntentFilter(
DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED)); DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED));
bindService(new Intent(this, DownloadService.class), mConnection, 0);
startContentRefresher(); startContentRefresher();
} }
@ -74,6 +84,25 @@ public class DownloadActivity extends SherlockListActivity implements
stopContentRefresher(); 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") @SuppressLint("NewApi")
private void startContentRefresher() { private void startContentRefresher() {
if (contentRefresher != null) { if (contentRefresher != null) {
@ -81,16 +110,20 @@ public class DownloadActivity extends SherlockListActivity implements
} }
contentRefresher = new AsyncTask<Void, Void, Void>() { contentRefresher = new AsyncTask<Void, Void, Void>() {
private final int WAITING_INTERVALL = 1000; private final int WAITING_INTERVALL = 1000;
@Override @Override
protected void onProgressUpdate(Void... values) { protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values); super.onProgressUpdate(values);
dla.notifyDataSetChanged(); if (dla != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Refreshing content automatically");
dla.notifyDataSetChanged();
}
} }
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
while(!isCancelled()) { while (!isCancelled()) {
try { try {
Thread.sleep(WAITING_INTERVALL); Thread.sleep(WAITING_INTERVALL);
publishProgress(); publishProgress();
@ -107,7 +140,7 @@ public class DownloadActivity extends SherlockListActivity implements
contentRefresher.execute(); contentRefresher.execute();
} }
} }
private void stopContentRefresher() { private void stopContentRefresher() {
if (contentRefresher != null) { if (contentRefresher != null) {
contentRefresher.cancel(true); contentRefresher.cancel(true);
@ -202,6 +235,8 @@ public class DownloadActivity extends SherlockListActivity implements
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (dla != null) { if (dla != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Refreshing content");
dla.notifyDataSetChanged(); dla.notifyDataSetChanged();
} }
} }

View File

@ -78,7 +78,9 @@ public class DownloadlistAdapter extends ArrayAdapter<Downloader> {
} }
} }
holder.title.setText(titleText); 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()) holder.downloaded.setText(Converter.byteToString(status.getSoFar())
+ " / " + Converter.byteToString(status.getSize())); + " / " + Converter.byteToString(status.getSize()));
holder.percent.setText(status.getProgressPercent() + "%"); holder.percent.setText(status.getProgressPercent() + "%");

View File

@ -103,7 +103,7 @@ public class DownloadService extends Service {
/** Needed to determine the duration of a media file */ /** Needed to determine the duration of a media file */
private MediaPlayer mediaplayer; private MediaPlayer mediaplayer;
private static List<Downloader> downloads = new ArrayList<Downloader>(); private List<Downloader> downloads;
private volatile boolean shutdownInitiated = false; private volatile boolean shutdownInitiated = false;
/** True if service is running. */ /** True if service is running. */
@ -119,6 +119,9 @@ public class DownloadService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getParcelableExtra(EXTRA_REQUEST) != null) {
onDownloadQueued(intent);
}
return Service.START_NOT_STICKY; return Service.START_NOT_STICKY;
} }
@ -129,10 +132,7 @@ public class DownloadService extends Service {
Log.d(TAG, "Service started"); Log.d(TAG, "Service started");
isRunning = true; isRunning = true;
completedDownloads = new ArrayList<DownloadStatus>(); completedDownloads = new ArrayList<DownloadStatus>();
if (!downloads.isEmpty()) { downloads = new ArrayList<Downloader>();
downloads.clear();
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
registerReceiver(downloadQueued, new IntentFilter( registerReceiver(downloadQueued, new IntentFilter(
ACTION_ENQUEUE_DOWNLOAD)); 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() { private BroadcastReceiver downloadQueued = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_ENQUEUE_DOWNLOAD)) { onDownloadQueued(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();
}
}
} }
}; };
@ -459,9 +461,10 @@ public class DownloadService extends Service {
Log.d(TAG, numOfDownloads + " downloads left"); Log.d(TAG, numOfDownloads + " downloads left");
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "ShutdownInitiated: " + shutdownInitiated); Log.d(TAG, "ShutdownInitiated: " + shutdownInitiated);
if (numOfDownloads == 0) { if (numOfDownloads == 0) {
if (AppConfig.DEBUG) Log.d(TAG, "Starting shutdown"); if (AppConfig.DEBUG)
Log.d(TAG, "Starting shutdown");
shutdownInitiated = true; shutdownInitiated = true;
stopForeground(true); stopForeground(true);
} else { } else {
@ -566,7 +569,7 @@ public class DownloadService extends Service {
if (savedFeed == null) { if (savedFeed == null) {
savedFeed = feed; savedFeed = feed;
} }
saveDownloadStatus(new DownloadStatus(savedFeed, reason, successful)); saveDownloadStatus(new DownloadStatus(savedFeed, reason, successful));
sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED); sendDownloadHandledIntent(DOWNLOAD_TYPE_FEED);
queryDownloads(); queryDownloads();
@ -654,7 +657,7 @@ public class DownloadService extends Service {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Duration of file is " + media.getDuration()); Log.d(TAG, "Duration of file is " + media.getDuration());
mediaplayer.reset(); mediaplayer.reset();
status.setCompletionDate(new Date()); status.setCompletionDate(new Date());
saveDownloadStatus(status); saveDownloadStatus(status);
sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA); sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA);
@ -728,7 +731,7 @@ public class DownloadService extends Service {
} }
public static List<Downloader> getDownloads() { public List<Downloader> getDownloads() {
return downloads; return downloads;
} }

View File

@ -11,7 +11,7 @@ public abstract class Downloader extends Thread {
protected boolean finished; protected boolean finished;
protected DownloadStatus status; protected volatile DownloadStatus status;
public Downloader(DownloadService downloadService, DownloadStatus status) { public Downloader(DownloadService downloadService, DownloadStatus status) {
super(); super();

View File

@ -69,13 +69,17 @@ public class DownloadRequester {
DownloadService.Request request = new DownloadService.Request( DownloadService.Request request = new DownloadService.Request(
item.getFile_url(), item.getDownload_url()); item.getFile_url(), item.getDownload_url());
Intent queueIntent = new Intent(
DownloadService.ACTION_ENQUEUE_DOWNLOAD);
queueIntent.putExtra(DownloadService.EXTRA_REQUEST, request);
if (!DownloadService.isRunning) { 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)); context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED));
} else { } else {
Log.e(TAG, "URL " + item.getDownload_url() Log.e(TAG, "URL " + item.getDownload_url()
@ -161,7 +165,9 @@ public class DownloadRequester {
/** Remove an object from the downloads-list of the requester. */ /** Remove an object from the downloads-list of the requester. */
public void removeDownload(FeedFile f) { 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 */ /** Get the number of uncompleted Downloads */