Improved the way the AddFeedActivity is waiting for the download to

finish
This commit is contained in:
daniel oeh 2012-06-18 16:05:23 +02:00
parent 113ccff68f
commit 245f272a35
3 changed files with 140 additions and 42 deletions

View File

@ -5,12 +5,18 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.view.View; import android.view.View;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log; import android.util.Log;
import de.podfetcher.R; import de.podfetcher.R;
import de.podfetcher.feed.Feed; import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.storage.DownloadRequester; import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.util.URLChecker; import de.podfetcher.util.URLChecker;
import de.podfetcher.service.DownloadObserver; import de.podfetcher.service.DownloadObserver;
import de.podfetcher.service.DownloadService;
import de.podfetcher.service.DownloadStatus; import de.podfetcher.service.DownloadStatus;
import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.app.SherlockActivity;
@ -26,14 +32,16 @@ public class AddFeedActivity extends SherlockActivity {
private static final String TAG = "AddFeedActivity"; private static final String TAG = "AddFeedActivity";
private DownloadRequester requester; private DownloadRequester requester;
private FeedManager manager;
private EditText etxtFeedurl; private EditText etxtFeedurl;
private Button butConfirm; private Button butConfirm;
private Button butCancel; private Button butCancel;
private long downloadId; private long downloadId;
private boolean hasImage;
private DownloadObserver observer; private boolean isWaitingForImage = false;
private long imageDownloadId;
private ProgressDialog progDialog; private ProgressDialog progDialog;
@ -43,14 +51,13 @@ public class AddFeedActivity extends SherlockActivity {
setContentView(R.layout.addfeed); setContentView(R.layout.addfeed);
requester = DownloadRequester.getInstance(); requester = DownloadRequester.getInstance();
manager = FeedManager.getInstance();
createObserver();
progDialog = new ProgressDialog(this) { progDialog = new ProgressDialog(this) {
@Override @Override
public void onBackPressed() { public void onBackPressed() {
requester.cancelDownload(getContext(), downloadId); requester.cancelDownload(getContext(), downloadId);
observer.cancel(true); unregisterReceiver(downloadCompleted);
createObserver();
dismiss(); dismiss();
} }
@ -76,35 +83,17 @@ public class AddFeedActivity extends SherlockActivity {
}); });
} }
private void createObserver() {
observer = new DownloadObserver(this) {
@Override
protected void onPostExecute(Boolean result) {
progDialog.dismiss();
finish();
}
@Override
protected void onProgressUpdate(DownloadStatus... values) {
DownloadStatus progr = values[0];
progDialog.setMessage(getContext().getString(progr.getStatusMsg())
+ " (" + progr.getProgressPercent() + "%)");
}
};
}
@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 void addNewFeed() { private void addNewFeed() {
String url = etxtFeedurl.getText().toString(); String url = etxtFeedurl.getText().toString();
url = URLChecker.prepareURL(url); url = URLChecker.prepareURL(url);
if(url != null) { if (url != null) {
Feed feed = new Feed(url, new Date()); Feed feed = new Feed(url, new Date());
downloadId = requester.downloadFeed(this, feed); downloadId = requester.downloadFeed(this, feed);
observeDownload(feed); observeDownload(feed);
@ -113,7 +102,65 @@ public class AddFeedActivity extends SherlockActivity {
private void observeDownload(Feed feed) { private void observeDownload(Feed feed) {
progDialog.show(); progDialog.show();
observer.execute(feed); progDialog.setMessage("Downloading Feed");
registerReceiver(downloadCompleted, new IntentFilter(DownloadService.ACTION_DOWNLOAD_HANDLED));
} }
private void updateProgDialog(final String msg) {
if (progDialog.isShowing()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progDialog.setMessage(msg);
}
});
}
}
private void handleDownloadError(DownloadStatus status) {
}
private BroadcastReceiver downloadCompleted = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long receivedDownloadId = intent.getLongExtra(
DownloadService.EXTRA_DOWNLOAD_ID, -1);
if (receivedDownloadId == downloadId
|| (isWaitingForImage && receivedDownloadId == imageDownloadId)) {
long statusId = intent.getLongExtra(
DownloadService.EXTRA_STATUS_ID, 0);
DownloadStatus status = manager.getDownloadStatus(statusId);
if (status.isSuccessful()) {
if (!isWaitingForImage) {
hasImage = intent.getBooleanExtra(
DownloadService.EXTRA_FEED_HAS_IMAGE, false);
if (!hasImage) {
progDialog.dismiss();
finish();
} else {
imageDownloadId = intent
.getLongExtra(
DownloadService.EXTRA_IMAGE_DOWNLOAD_ID,
-1);
isWaitingForImage = true;
updateProgDialog("Downloading Image");
}
} else {
progDialog.dismiss();
finish();
}
} else {
handleDownloadError(status);
}
}
}
};
} }

View File

@ -263,6 +263,15 @@ public class FeedManager {
return null; return null;
} }
public DownloadStatus getDownloadStatus(long statusId) {
for (DownloadStatus status : downloadLog) {
if (status.getId() == statusId) {
return status;
}
}
return null;
}
/** Reads the database */ /** Reads the database */
public void loadDBData(Context context) { public void loadDBData(Context context) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
@ -375,10 +384,14 @@ public class FeedManager {
feedfile = getFeedMedia(feedfileId); feedfile = getFeedMedia(feedfileId);
} }
if (feedfile != null) { // otherwise ignore status if (feedfile != null) { // otherwise ignore status
boolean successful = logCursor.getInt(logCursor.getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL)) > 0; boolean successful = logCursor.getInt(logCursor
int reason = logCursor.getInt(logCursor.getColumnIndex(PodDBAdapter.KEY_REASON)); .getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL)) > 0;
Date completionDate = new Date(logCursor.getLong(logCursor.getColumnIndex(PodDBAdapter.KEY_COMPLETION_DATE))); int reason = logCursor.getInt(logCursor
downloadLog.add(new DownloadStatus(id, feedfile, successful, reason, completionDate)); .getColumnIndex(PodDBAdapter.KEY_REASON));
Date completionDate = new Date(logCursor.getLong(logCursor
.getColumnIndex(PodDBAdapter.KEY_COMPLETION_DATE)));
downloadLog.add(new DownloadStatus(id, feedfile,
successful, reason, completionDate));
} }
} while (logCursor.moveToNext()); } while (logCursor.moveToNext());

View File

@ -44,6 +44,15 @@ public class DownloadService extends Service {
public static String ACTION_ALL_FEED_DOWNLOADS_COMPLETED = "action.de.podfetcher.storage.all_feed_downloads_completed"; public static String ACTION_ALL_FEED_DOWNLOADS_COMPLETED = "action.de.podfetcher.storage.all_feed_downloads_completed";
public static final String ACTION_FEED_SYNC_COMPLETED = "action.de.podfetcher.service.feed_sync_completed"; public static final String ACTION_FEED_SYNC_COMPLETED = "action.de.podfetcher.service.feed_sync_completed";
public static final String ACTION_DOWNLOAD_HANDLED = "action.de.podfetcher.service.download_handled";
/** True if handled feed has an image. */
public static final String EXTRA_FEED_HAS_IMAGE = "extra.de.podfetcher.service.feed_has_image";
/** ID of DownloadStatus. */
public static final String EXTRA_STATUS_ID = "extra.de.podfetcher.service.feedfile_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";
private ExecutorService syncExecutor; private ExecutorService syncExecutor;
private DownloadRequester requester; private DownloadRequester requester;
private FeedManager manager; private FeedManager manager;
@ -179,15 +188,30 @@ public class DownloadService extends Service {
.getColumnIndex(DownloadManager.COLUMN_REASON)); .getColumnIndex(DownloadManager.COLUMN_REASON));
Log.d(TAG, "reason code is " + reason); Log.d(TAG, "reason code is " + reason);
successful = false; successful = false;
} long statusId = manager.addDownloadStatus(context, new DownloadStatus(download,
manager.addDownloadStatus(context, new DownloadStatus(download,
reason, successful)); reason, successful));
sendDownloadHandledIntent(download.getDownloadId(), statusId, false, 0);
download.setDownloadId(0);
}
c.close(); c.close();
} }
} }
}; };
private void sendDownloadHandledIntent(long downloadId, long statusId, boolean feedHasImage, long imageDownloadId) {
Intent intent = new Intent(ACTION_DOWNLOAD_HANDLED);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(EXTRA_STATUS_ID, statusId);
intent.putExtra(EXTRA_FEED_HAS_IMAGE, feedHasImage);
if (feedHasImage) {
intent.putExtra(EXTRA_IMAGE_DOWNLOAD_ID, imageDownloadId);
}
sendBroadcast(intent);
}
/** Check if there's something else to download, otherwise stop */ /** Check if there's something else to download, otherwise stop */
public synchronized void queryDownloads() { public synchronized void queryDownloads() {
int numOfDownloads = requester.getNumberOfDownloads(); int numOfDownloads = requester.getNumberOfDownloads();
@ -239,6 +263,9 @@ public class DownloadService extends Service {
} }
public void run() { public void run() {
long imageId = 0;
boolean hasImage = false;
FeedManager manager = FeedManager.getInstance(); FeedManager manager = FeedManager.getInstance();
FeedHandler handler = new FeedHandler(); FeedHandler handler = new FeedHandler();
feed.setDownloaded(true); feed.setDownloaded(true);
@ -247,12 +274,15 @@ public class DownloadService extends Service {
// Download Feed Image if provided // Download Feed Image if provided
if (feed.getImage() != null) { if (feed.getImage() != null) {
Log.d(TAG, "Feed has image; Downloading...."); Log.d(TAG, "Feed has image; Downloading....");
requester.downloadImage(service, feed.getImage()); imageId = requester.downloadImage(service, feed.getImage());
hasImage = true;
} }
requester.removeDownload(feed); requester.removeDownload(feed);
cleanup(); cleanup();
long statusId = manager.addDownloadStatus(service, new DownloadStatus(feed, 0, true));
sendDownloadHandledIntent(feed.getDownloadId(), statusId, hasImage, imageId);
feed.setDownloadId(0);
// Save information of feed in DB // Save information of feed in DB
manager.updateFeed(service, feed); manager.updateFeed(service, feed);
queryDownloads(); queryDownloads();
@ -283,6 +313,11 @@ public class DownloadService extends Service {
public void run() { public void run() {
image.setDownloaded(true); image.setDownloaded(true);
requester.removeDownload(image); requester.removeDownload(image);
long statusId = manager.addDownloadStatus(service, new DownloadStatus(image, 0, true));
sendDownloadHandledIntent(image.getDownloadId(), statusId, false, 0);
image.setDownloadId(0);
manager.setFeedImage(service, image); manager.setFeedImage(service, image);
queryDownloads(); queryDownloads();
} }
@ -313,6 +348,9 @@ 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));
sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0);
media.setDownloadId(0);
manager.setFeedMedia(service, media); manager.setFeedMedia(service, media);
queryDownloads(); queryDownloads();
} }