DownloadActivity should now display the content correctly
This commit is contained in:
parent
d420e67a07
commit
27afa983ac
|
@ -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) {
|
||||||
|
@ -85,8 +114,12 @@ public class DownloadActivity extends SherlockListActivity implements
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Void... values) {
|
protected void onProgressUpdate(Void... values) {
|
||||||
super.onProgressUpdate(values);
|
super.onProgressUpdate(values);
|
||||||
|
if (dla != null) {
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "Refreshing content automatically");
|
||||||
dla.notifyDataSetChanged();
|
dla.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,9 @@ public class DownloadlistAdapter extends ArrayAdapter<Downloader> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
holder.title.setText(titleText);
|
holder.title.setText(titleText);
|
||||||
|
if (status.getStatusMsg() != 0) {
|
||||||
holder.message.setText(status.getStatusMsg());
|
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() + "%");
|
||||||
|
|
|
@ -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,11 +256,7 @@ public class DownloadService extends Service {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private BroadcastReceiver downloadQueued = new BroadcastReceiver() {
|
private void onDownloadQueued(Intent intent) {
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
if (intent.getAction().equals(ACTION_ENQUEUE_DOWNLOAD)) {
|
|
||||||
if (AppConfig.DEBUG)
|
if (AppConfig.DEBUG)
|
||||||
Log.d(TAG, "Received enqueue request");
|
Log.d(TAG, "Received enqueue request");
|
||||||
Request request = intent.getParcelableExtra(EXTRA_REQUEST);
|
Request request = intent.getParcelableExtra(EXTRA_REQUEST);
|
||||||
|
@ -269,7 +265,8 @@ public class DownloadService extends Service {
|
||||||
"ACTION_ENQUEUE_DOWNLOAD intent needs request extra");
|
"ACTION_ENQUEUE_DOWNLOAD intent needs request extra");
|
||||||
}
|
}
|
||||||
if (shutdownInitiated) {
|
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;
|
shutdownInitiated = false;
|
||||||
setupNotification();
|
setupNotification();
|
||||||
}
|
}
|
||||||
|
@ -283,8 +280,7 @@ public class DownloadService extends Service {
|
||||||
if (downloader != null) {
|
if (downloader != null) {
|
||||||
downloads.add(downloader);
|
downloads.add(downloader);
|
||||||
downloadExecutor.submit(downloader);
|
downloadExecutor.submit(downloader);
|
||||||
sendBroadcast(new Intent(
|
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
|
||||||
ACTION_DOWNLOADS_CONTENT_CHANGED));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG,
|
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);
|
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 {
|
||||||
|
@ -728,7 +731,7 @@ public class DownloadService extends Service {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Downloader> getDownloads() {
|
public List<Downloader> getDownloads() {
|
||||||
return downloads;
|
return downloads;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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());
|
||||||
|
|
||||||
|
if (!DownloadService.isRunning) {
|
||||||
|
Intent launchIntent = new Intent(context, DownloadService.class);
|
||||||
|
launchIntent.putExtra(DownloadService.EXTRA_REQUEST, request);
|
||||||
|
context.startService(launchIntent);
|
||||||
|
} else {
|
||||||
Intent queueIntent = new Intent(
|
Intent queueIntent = new Intent(
|
||||||
DownloadService.ACTION_ENQUEUE_DOWNLOAD);
|
DownloadService.ACTION_ENQUEUE_DOWNLOAD);
|
||||||
queueIntent.putExtra(DownloadService.EXTRA_REQUEST, request);
|
queueIntent.putExtra(DownloadService.EXTRA_REQUEST, request);
|
||||||
if (!DownloadService.isRunning) {
|
|
||||||
context.startService(new Intent(context, DownloadService.class));
|
|
||||||
}
|
|
||||||
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 */
|
||||||
|
|
Loading…
Reference in New Issue