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;
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<Void, Void, Void> 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) {
@ -85,12 +114,16 @@ public class DownloadActivity extends SherlockListActivity implements
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
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();
@ -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();
}
}

View File

@ -78,7 +78,9 @@ public class DownloadlistAdapter extends ArrayAdapter<Downloader> {
}
}
holder.title.setText(titleText);
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() + "%");

View File

@ -103,7 +103,7 @@ public class DownloadService extends Service {
/** Needed to determine the duration of a media file */
private MediaPlayer mediaplayer;
private static List<Downloader> downloads = new ArrayList<Downloader>();
private List<Downloader> 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<DownloadStatus>();
if (!downloads.isEmpty()) {
downloads.clear();
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
downloads = new ArrayList<Downloader>();
registerReceiver(downloadQueued, new IntentFilter(
ACTION_ENQUEUE_DOWNLOAD));
@ -256,11 +256,7 @@ public class DownloadService extends Service {
};
private BroadcastReceiver downloadQueued = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_ENQUEUE_DOWNLOAD)) {
private void onDownloadQueued(Intent intent) {
if (AppConfig.DEBUG)
Log.d(TAG, "Received enqueue request");
Request request = intent.getParcelableExtra(EXTRA_REQUEST);
@ -269,7 +265,8 @@ public class DownloadService extends Service {
"ACTION_ENQUEUE_DOWNLOAD intent needs request extra");
}
if (shutdownInitiated) {
if (AppConfig.DEBUG) Log.d(TAG, "Cancelling shutdown; new download was queued");
if (AppConfig.DEBUG)
Log.d(TAG, "Cancelling shutdown; new download was queued");
shutdownInitiated = false;
setupNotification();
}
@ -283,8 +280,7 @@ public class DownloadService extends Service {
if (downloader != null) {
downloads.add(downloader);
downloadExecutor.submit(downloader);
sendBroadcast(new Intent(
ACTION_DOWNLOADS_CONTENT_CHANGED));
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
} else {
Log.e(TAG,
@ -293,6 +289,12 @@ public class DownloadService extends Service {
}
}
private BroadcastReceiver downloadQueued = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onDownloadQueued(intent);
}
};
@ -461,7 +463,8 @@ public class DownloadService extends Service {
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 {
@ -728,7 +731,7 @@ public class DownloadService extends Service {
}
public static List<Downloader> getDownloads() {
public List<Downloader> getDownloads() {
return downloads;
}

View File

@ -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();

View File

@ -69,13 +69,17 @@ public class DownloadRequester {
DownloadService.Request request = new DownloadService.Request(
item.getFile_url(), item.getDownload_url());
if (!DownloadService.isRunning) {
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);
if (!DownloadService.isRunning) {
context.startService(new Intent(context, DownloadService.class));
}
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 */