Content of Downloads list is now refreshed

This commit is contained in:
daniel oeh 2012-08-16 19:53:22 +02:00
parent 697c00e0eb
commit e4a2d21b9d
3 changed files with 85 additions and 74 deletions

View File

@ -1,8 +1,13 @@
package de.danoeh.antennapod.activity;
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;
@ -28,7 +33,7 @@ import de.danoeh.antennapod.storage.DownloadRequester;
* objects created by a DownloadObserver.
*/
public class DownloadActivity extends SherlockListActivity implements
ActionMode.Callback, DownloadObserver.Callback {
ActionMode.Callback {
private static final String TAG = "DownloadActivity";
private static final int MENU_SHOW_LOG = 0;
@ -38,7 +43,8 @@ public class DownloadActivity extends SherlockListActivity implements
private ActionMode mActionMode;
private DownloadStatus selectedDownload;
private DownloadObserver downloadObserver;
private AsyncTask<Void, Void, Void> contentRefresher;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -47,24 +53,21 @@ 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);
if (downloadObserver != null) {
downloadObserver.unregisterCallback(DownloadActivity.this);
}
unregisterReceiver(contentChanged);
}
@Override
protected void onResume() {
super.onResume();
if (AppConfig.DEBUG)
Log.d(TAG, "Trying to bind service");
bindService(new Intent(this, DownloadService.class), mConnection, 0);
registerReceiver(contentChanged, new IntentFilter(
DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED));
startContentRefresher();
}
@Override
@ -72,6 +75,47 @@ public class DownloadActivity extends SherlockListActivity implements
super.onStop();
if (AppConfig.DEBUG)
Log.d(TAG, "Stopping Activity");
stopContentRefresher();
}
@SuppressLint("NewApi")
private void startContentRefresher() {
if (contentRefresher != null) {
contentRefresher.cancel(true);
}
contentRefresher = new AsyncTask<Void, Void, Void>() {
private final int WAITING_INTERVALL = 1000;
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
dla.notifyDataSetChanged();
}
@Override
protected Void doInBackground(Void... params) {
while(!isCancelled()) {
try {
Thread.sleep(WAITING_INTERVALL);
publishProgress();
} catch (InterruptedException e) {
return null;
}
}
return null;
}
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
contentRefresher.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
contentRefresher.execute();
}
}
private void stopContentRefresher() {
if (contentRefresher != null) {
contentRefresher.cancel(true);
}
}
@Override
@ -82,7 +126,7 @@ public class DownloadActivity extends SherlockListActivity implements
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int position, long id) {
DownloadStatus selection = dla.getItem(position);
DownloadStatus selection = dla.getItem(position).getStatus();
if (selection != null && mActionMode != null) {
mActionMode.finish();
}
@ -157,53 +201,14 @@ public class DownloadActivity extends SherlockListActivity implements
dla.setSelectedItemIndex(DownloadlistAdapter.SELECTION_NONE);
}
private DownloadService downloadService = null;
boolean mIsBound;
private BroadcastReceiver contentChanged = new BroadcastReceiver() {
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
downloadService = ((DownloadService.LocalBinder) service)
.getService();
if (AppConfig.DEBUG)
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 onReceive(Context context, Intent intent) {
if (dla != null) {
dla.notifyDataSetChanged();
}
}
};
@Override
public void onProgressUpdate() {
runOnUiThread(new Runnable() {
@Override
public void run() {
dla.notifyDataSetChanged();
}
});
}
@Override
public void onFinish() {
if (AppConfig.DEBUG)
Log.d(TAG, "Observer has finished, clearing adapter");
runOnUiThread(new Runnable() {
@Override
public void run() {
dla.clear();
dla.notifyDataSetInvalidated();
}
});
}
}

View File

@ -14,16 +14,17 @@ import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedFile;
import de.danoeh.antennapod.feed.FeedImage;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.service.download.Downloader;
import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.R;
public class DownloadlistAdapter extends ArrayAdapter<DownloadStatus> {
public class DownloadlistAdapter extends ArrayAdapter<Downloader> {
private int selectedItemIndex;
public static final int SELECTION_NONE = -1;
public DownloadlistAdapter(Context context, int textViewResourceId,
List<DownloadStatus> objects) {
List<Downloader> objects) {
super(context, textViewResourceId, objects);
this.selectedItemIndex = SELECTION_NONE;
}
@ -31,7 +32,7 @@ public class DownloadlistAdapter extends ArrayAdapter<DownloadStatus> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
DownloadStatus status = getItem(position);
DownloadStatus status = getItem(position).getStatus();
FeedFile feedFile = status.getFeedFile();
// Inflate layout
if (convertView == null) {

View File

@ -79,6 +79,11 @@ public class DownloadService extends Service {
public static final String EXTRA_DOWNLOAD_URL = "downloadUrl";
public static final String ACTION_DOWNLOAD_HANDLED = "action.de.danoeh.antennapod.service.download_handled";
/**
* Sent by the DownloadService when the content of the downloads list
* changes.
*/
public static final String ACTION_DOWNLOADS_CONTENT_CHANGED = "action.de.danoeh.antennapod.service.downloadsContentChanged";
public static final String EXTRA_DOWNLOAD_ID = "extra.de.danoeh.antennapod.service.download_id";
@ -105,9 +110,7 @@ public class DownloadService extends Service {
private MediaPlayer mediaplayer;
private DownloadManager downloadManager;
private DownloadObserver downloadObserver;
private List<Downloader> downloads;
private static List<Downloader> downloads = new ArrayList<Downloader>();
private volatile boolean shutdownInitiated = false;
/** True if service is running. */
@ -136,8 +139,10 @@ public class DownloadService extends Service {
Log.d(TAG, "Service started");
isRunning = true;
completedDownloads = new ArrayList<DownloadStatus>();
downloads = new ArrayList<Downloader>();
if (!downloads.isEmpty()) {
downloads.clear();
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
registerReceiver(downloadQueued, new IntentFilter(
ACTION_ENQUEUE_DOWNLOAD));
@ -176,13 +181,8 @@ public class DownloadService extends Service {
requester = DownloadRequester.getInstance();
mediaplayer = new MediaPlayer();
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadObserver = new DownloadObserver(this);
setupNotification();
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
downloadObserver.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
downloadObserver.execute();
}
}
@Override
@ -198,7 +198,6 @@ public class DownloadService extends Service {
mediaplayer.release();
unregisterReceiver(cancelDownloadReceiver);
unregisterReceiver(downloadQueued);
downloadObserver.cancel(true);
createReport();
}
@ -284,10 +283,14 @@ public class DownloadService extends Service {
} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
for (Downloader d : downloads) {
d.interrupt();
removeDownload(d.getStatus());
DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile());
d.getStatus().getFeedFile().setFile_url(null);
if (AppConfig.DEBUG)
Log.d(TAG, "Cancelled all downloads");
}
downloads.clear();
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
}
@ -316,6 +319,7 @@ public class DownloadService extends Service {
if (downloader != null) {
downloads.add(downloader);
downloadExecutor.submit(downloader);
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
} else {
Log.e(TAG,
@ -389,6 +393,7 @@ public class DownloadService extends Service {
downloads.remove(status);
DownloadRequester.getInstance().removeDownload(status.getFeedFile());
status.getFeedFile().setFile_url(null);
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
/**
@ -753,8 +758,8 @@ public class DownloadService extends Service {
}
public DownloadObserver getDownloadObserver() {
return downloadObserver;
public static List<Downloader> getDownloads() {
return downloads;
}
}