Implemented new Downloadobserver into the other classes

This commit is contained in:
daniel oeh 2012-07-06 10:37:28 +02:00
parent a7c1580eb5
commit 7b86821cd8
4 changed files with 124 additions and 44 deletions

View File

@ -1,6 +1,7 @@
package de.podfetcher.activity; package de.podfetcher.activity;
import de.podfetcher.R; import de.podfetcher.R;
import de.podfetcher.service.DownloadService;
import de.podfetcher.storage.DownloadRequester; import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.adapter.DownloadlistAdapter; import de.podfetcher.adapter.DownloadlistAdapter;
import de.podfetcher.asynctask.DownloadObserver; import de.podfetcher.asynctask.DownloadObserver;
@ -12,8 +13,12 @@ import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection;
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.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
@ -22,7 +27,7 @@ import android.widget.AdapterView.OnItemLongClickListener;
/** Shows all running downloads in a list */ /** Shows all running downloads in a list */
public class DownloadActivity extends SherlockListActivity implements public class DownloadActivity extends SherlockListActivity implements
ActionMode.Callback { ActionMode.Callback, DownloadObserver.Callback {
private static final String TAG = "DownloadActivity"; private static final String TAG = "DownloadActivity";
private static final int MENU_SHOW_LOG = 0; private static final int MENU_SHOW_LOG = 0;
@ -32,35 +37,38 @@ public class DownloadActivity extends SherlockListActivity implements
private ActionMode mActionMode; private ActionMode mActionMode;
private DownloadStatus selectedDownload; private DownloadStatus selectedDownload;
private DownloadObserver downloadObserver;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Log.d(TAG, "Creating Activity");
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
observer.execute(requester.getDownloads().toArray(
new FeedFile[requester.getDownloads().size()]));
} }
@Override
protected void onPause() {
super.onPause();
unbindService(mConnection);
if (downloadObserver != null) {
downloadObserver.unregisterCallback(DownloadActivity.this);
}
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "Trying to bind service");
bindService(new Intent(this, DownloadService.class), mConnection, 0);
}
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
Log.d(TAG, "Stopping Activity"); Log.d(TAG, "Stopping Activity");
observer.cancel(true);
} }
private final DownloadObserver observer = new DownloadObserver(this) {
@Override
protected void onProgressUpdate(DownloadStatus... values) {
dla = new DownloadlistAdapter(getContext(), 0, getStatusList());
setListAdapter(dla);
dla.notifyDataSetChanged();
}
};
@Override @Override
protected void onPostCreate(Bundle savedInstanceState) { protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState); super.onPostCreate(savedInstanceState);
@ -141,4 +149,37 @@ public class DownloadActivity extends SherlockListActivity implements
selectedDownload = null; selectedDownload = null;
dla.setSelectedItemIndex(DownloadlistAdapter.SELECTION_NONE); dla.setSelectedItemIndex(DownloadlistAdapter.SELECTION_NONE);
} }
private DownloadService downloadService = null;
boolean mIsBound;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
downloadService = ((DownloadService.LocalBinder) service)
.getService();
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 onProgressUpdate() {
dla.notifyDataSetChanged();
}
@Override
public void onFinish() {
dla.clear();
dla.notifyDataSetChanged();
}
} }

View File

@ -1,5 +1,7 @@
package de.podfetcher.adapter; package de.podfetcher.adapter;
import java.util.List;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -21,11 +23,13 @@ public class DownloadlistAdapter extends ArrayAdapter<DownloadStatus> {
public static final int SELECTION_NONE = -1; public static final int SELECTION_NONE = -1;
public DownloadlistAdapter(Context context, int textViewResourceId, public DownloadlistAdapter(Context context, int textViewResourceId,
DownloadStatus[] objects) { List<DownloadStatus> objects) {
super(context, textViewResourceId, objects); super(context, textViewResourceId, objects);
selectedItemIndex = SELECTION_NONE; this.selectedItemIndex = SELECTION_NONE;
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
Holder holder; Holder holder;

View File

@ -45,6 +45,21 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
@Override @Override
protected void onCancelled() { protected void onCancelled() {
Log.d(TAG, "Task was cancelled."); Log.d(TAG, "Task was cancelled.");
statusList.clear();
for (DownloadObserver.Callback callback : observer) {
callback.onFinish();
}
}
@Override
protected void onPostExecute(Void result) {
Log.d(TAG, "Background task has finished");
statusList.clear();
for (DownloadObserver.Callback callback : observer) {
callback.onFinish();
}
} }
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
@ -70,6 +85,9 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
} }
private void refreshStatuslist() { private void refreshStatuslist() {
ArrayList<DownloadStatus> unhandledItems = new ArrayList<DownloadStatus>(
statusList);
Cursor cursor = getDownloadCursor(); Cursor cursor = getDownloadCursor();
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
@ -80,6 +98,8 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
if (status == null) { if (status == null) {
status = new DownloadStatus(feedFile); status = new DownloadStatus(feedFile);
statusList.add(status); statusList.add(status);
} else {
unhandledItems.remove(status);
} }
// refresh status // refresh status
@ -112,6 +132,11 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
// remove unhandled items from statuslist
for (DownloadStatus status : unhandledItems) {
statusList.remove(status);
}
} }
/** Request a cursor with all running Feedfile downloads */ /** Request a cursor with all running Feedfile downloads */
@ -144,7 +169,6 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
.getLong(c .getLong(c
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
status.progressPercent = (int) (((double) status.soFar / (double) status.size) * 100); status.progressPercent = (int) (((double) status.soFar / (double) status.size) * 100);
Log.d(TAG, "Setting progress to " + status.progressPercent);
} }
public Context getContext() { public Context getContext() {
@ -166,12 +190,7 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
} }
private boolean downloadsLeft() { private boolean downloadsLeft() {
for (DownloadStatus status : statusList) { return !requester.downloads.isEmpty();
if (status.done == false) {
return true;
}
}
return false;
} }
public void registerCallback(DownloadObserver.Callback callback) { public void registerCallback(DownloadObserver.Callback callback) {
@ -184,6 +203,7 @@ public class DownloadObserver extends AsyncTask<Void, Void, Void> {
public interface Callback { public interface Callback {
public void onProgressUpdate(); public void onProgressUpdate();
public void onFinish();
} }
} }

View File

@ -17,6 +17,7 @@ import org.xml.sax.SAXException;
import de.podfetcher.activity.DownloadActivity; import de.podfetcher.activity.DownloadActivity;
import de.podfetcher.activity.MediaplayerActivity; import de.podfetcher.activity.MediaplayerActivity;
import de.podfetcher.asynctask.DownloadObserver;
import de.podfetcher.asynctask.DownloadStatus; import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.*; import de.podfetcher.feed.*;
import de.podfetcher.service.PlaybackService.LocalBinder; import de.podfetcher.service.PlaybackService.LocalBinder;
@ -60,7 +61,6 @@ public class DownloadService extends Service {
public static final String EXTRA_DOWNLOAD_ID = "extra.de.podfetcher.service.download_id"; public static final String EXTRA_DOWNLOAD_ID = "extra.de.podfetcher.service.download_id";
public static final String EXTRA_IMAGE_DOWNLOAD_ID = "extra.de.podfetcher.service.image_download_id"; public static final String EXTRA_IMAGE_DOWNLOAD_ID = "extra.de.podfetcher.service.image_download_id";
private ExecutorService syncExecutor; private ExecutorService syncExecutor;
private DownloadRequester requester; private DownloadRequester requester;
private FeedManager manager; private FeedManager manager;
@ -70,6 +70,8 @@ public class DownloadService extends Service {
private MediaPlayer mediaplayer; private MediaPlayer mediaplayer;
private DownloadManager downloadManager; private DownloadManager downloadManager;
private DownloadObserver downloadObserver;
private volatile boolean shutdownInitiated = false; private volatile boolean shutdownInitiated = false;
/** True if service is running. */ /** True if service is running. */
public static boolean isRunning = false; public static boolean isRunning = false;
@ -98,7 +100,9 @@ public class DownloadService extends Service {
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
mediaplayer = new MediaPlayer(); mediaplayer = new MediaPlayer();
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadObserver = new DownloadObserver(this);
setupNotification(); setupNotification();
downloadObserver.execute();
} }
@Override @Override
@ -113,6 +117,7 @@ public class DownloadService extends Service {
sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED)); sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED));
mediaplayer.release(); mediaplayer.release();
unregisterReceiver(downloadReceiver); unregisterReceiver(downloadReceiver);
downloadObserver.cancel(true);
} }
private IntentFilter createIntentFilter() { private IntentFilter createIntentFilter() {
@ -201,10 +206,11 @@ public class DownloadService extends Service {
Log.e(TAG, "Download failed"); Log.e(TAG, "Download failed");
Log.e(TAG, "reason code is " + reason); Log.e(TAG, "reason code is " + reason);
successful = false; successful = false;
long statusId = manager.addDownloadStatus(context, new DownloadStatus(download, long statusId = manager.addDownloadStatus(context,
reason, successful)); new DownloadStatus(download, reason, successful));
requester.removeDownload(download); requester.removeDownload(download);
sendDownloadHandledIntent(download.getDownloadId(), statusId, false, 0); sendDownloadHandledIntent(download.getDownloadId(),
statusId, false, 0);
download.setDownloadId(0); download.setDownloadId(0);
} }
@ -215,7 +221,8 @@ public class DownloadService extends Service {
}; };
private void sendDownloadHandledIntent(long downloadId, long statusId, boolean feedHasImage, long imageDownloadId) { private void sendDownloadHandledIntent(long downloadId, long statusId,
boolean feedHasImage, long imageDownloadId) {
Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED); Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId); intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(EXTRA_STATUS_ID, statusId); intent.putExtra(EXTRA_STATUS_ID, statusId);
@ -320,7 +327,8 @@ public class DownloadService extends Service {
requester.removeDownload(feed); requester.removeDownload(feed);
cleanup(); cleanup();
long statusId = manager.addDownloadStatus(service, new DownloadStatus(savedFeed, reason, successful)); long statusId = manager.addDownloadStatus(service,
new DownloadStatus(savedFeed, reason, successful));
sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId); sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId);
queryDownloads(); queryDownloads();
} }
@ -351,7 +359,8 @@ public class DownloadService extends Service {
image.setDownloaded(true); image.setDownloaded(true);
requester.removeDownload(image); requester.removeDownload(image);
long statusId = manager.addDownloadStatus(service, new DownloadStatus(image, 0, true)); long statusId = manager.addDownloadStatus(service,
new DownloadStatus(image, 0, true));
sendDownloadHandledIntent(image.getDownloadId(), statusId, false, 0); sendDownloadHandledIntent(image.getDownloadId(), statusId, false, 0);
image.setDownloadId(0); image.setDownloadId(0);
@ -385,11 +394,17 @@ public class DownloadService extends Service {
media.setDuration(mediaplayer.getDuration()); media.setDuration(mediaplayer.getDuration());
Log.d(TAG, "Duration of file is " + media.getDuration()); Log.d(TAG, "Duration of file is " + media.getDuration());
mediaplayer.reset(); mediaplayer.reset();
long statusId = manager.addDownloadStatus(service, new DownloadStatus(media, 0, true)); long statusId = manager.addDownloadStatus(service,
new DownloadStatus(media, 0, true));
sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0); sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0);
media.setDownloadId(0); media.setDownloadId(0);
manager.setFeedMedia(service, media); manager.setFeedMedia(service, media);
queryDownloads(); queryDownloads();
} }
} }
public DownloadObserver getDownloadObserver() {
return downloadObserver;
}
} }