mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2024-12-25 08:21:59 +01:00
Improved the way the AddFeedActivity is waiting for the download to
finish
This commit is contained in:
parent
113ccff68f
commit
245f272a35
@ -5,12 +5,18 @@ import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.view.View;
|
||||
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 de.podfetcher.R;
|
||||
import de.podfetcher.feed.Feed;
|
||||
import de.podfetcher.feed.FeedManager;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
import de.podfetcher.util.URLChecker;
|
||||
import de.podfetcher.service.DownloadObserver;
|
||||
import de.podfetcher.service.DownloadService;
|
||||
import de.podfetcher.service.DownloadStatus;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockActivity;
|
||||
@ -26,36 +32,37 @@ public class AddFeedActivity extends SherlockActivity {
|
||||
private static final String TAG = "AddFeedActivity";
|
||||
|
||||
private DownloadRequester requester;
|
||||
|
||||
private FeedManager manager;
|
||||
|
||||
private EditText etxtFeedurl;
|
||||
private Button butConfirm;
|
||||
private Button butCancel;
|
||||
private long downloadId;
|
||||
|
||||
|
||||
private DownloadObserver observer;
|
||||
|
||||
private ProgressDialog progDialog;
|
||||
|
||||
private boolean hasImage;
|
||||
private boolean isWaitingForImage = false;
|
||||
private long imageDownloadId;
|
||||
|
||||
private ProgressDialog progDialog;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.addfeed);
|
||||
|
||||
requester = DownloadRequester.getInstance();
|
||||
manager = FeedManager.getInstance();
|
||||
|
||||
createObserver();
|
||||
progDialog = new ProgressDialog(this) {
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
requester.cancelDownload(getContext(), downloadId);
|
||||
observer.cancel(true);
|
||||
createObserver();
|
||||
unregisterReceiver(downloadCompleted);
|
||||
dismiss();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl);
|
||||
butConfirm = (Button) findViewById(R.id.butConfirm);
|
||||
butCancel = (Button) findViewById(R.id.butCancel);
|
||||
@ -63,7 +70,7 @@ public class AddFeedActivity extends SherlockActivity {
|
||||
butConfirm.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
addNewFeed();
|
||||
addNewFeed();
|
||||
}
|
||||
});
|
||||
|
||||
@ -75,36 +82,18 @@ 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
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
Log.d(TAG, "Stopping Activity");
|
||||
observer.cancel(true);
|
||||
}
|
||||
|
||||
private void addNewFeed() {
|
||||
String url = etxtFeedurl.getText().toString();
|
||||
String url = etxtFeedurl.getText().toString();
|
||||
url = URLChecker.prepareURL(url);
|
||||
|
||||
if(url != null) {
|
||||
if (url != null) {
|
||||
Feed feed = new Feed(url, new Date());
|
||||
downloadId = requester.downloadFeed(this, feed);
|
||||
observeDownload(feed);
|
||||
@ -113,7 +102,65 @@ public class AddFeedActivity extends SherlockActivity {
|
||||
|
||||
private void observeDownload(Feed feed) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ public class FeedManager {
|
||||
new Date()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public long addDownloadStatus(Context context, DownloadStatus status) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
downloadLog.add(status);
|
||||
@ -263,6 +263,15 @@ public class FeedManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
public DownloadStatus getDownloadStatus(long statusId) {
|
||||
for (DownloadStatus status : downloadLog) {
|
||||
if (status.getId() == statusId) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Reads the database */
|
||||
public void loadDBData(Context context) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
@ -375,10 +384,14 @@ public class FeedManager {
|
||||
feedfile = getFeedMedia(feedfileId);
|
||||
}
|
||||
if (feedfile != null) { // otherwise ignore status
|
||||
boolean successful = logCursor.getInt(logCursor.getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL)) > 0;
|
||||
int reason = logCursor.getInt(logCursor.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));
|
||||
boolean successful = logCursor.getInt(logCursor
|
||||
.getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL)) > 0;
|
||||
int reason = logCursor.getInt(logCursor
|
||||
.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());
|
||||
|
@ -43,6 +43,15 @@ public class DownloadService extends Service {
|
||||
|
||||
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_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 DownloadRequester requester;
|
||||
@ -179,14 +188,29 @@ public class DownloadService extends Service {
|
||||
.getColumnIndex(DownloadManager.COLUMN_REASON));
|
||||
Log.d(TAG, "reason code is " + reason);
|
||||
successful = false;
|
||||
long statusId = manager.addDownloadStatus(context, new DownloadStatus(download,
|
||||
reason, successful));
|
||||
sendDownloadHandledIntent(download.getDownloadId(), statusId, false, 0);
|
||||
download.setDownloadId(0);
|
||||
|
||||
}
|
||||
manager.addDownloadStatus(context, new DownloadStatus(download,
|
||||
reason, successful));
|
||||
|
||||
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 */
|
||||
public synchronized void queryDownloads() {
|
||||
@ -239,6 +263,9 @@ public class DownloadService extends Service {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
long imageId = 0;
|
||||
boolean hasImage = false;
|
||||
|
||||
FeedManager manager = FeedManager.getInstance();
|
||||
FeedHandler handler = new FeedHandler();
|
||||
feed.setDownloaded(true);
|
||||
@ -247,12 +274,15 @@ public class DownloadService extends Service {
|
||||
// Download Feed Image if provided
|
||||
if (feed.getImage() != null) {
|
||||
Log.d(TAG, "Feed has image; Downloading....");
|
||||
requester.downloadImage(service, feed.getImage());
|
||||
imageId = requester.downloadImage(service, feed.getImage());
|
||||
hasImage = true;
|
||||
}
|
||||
requester.removeDownload(feed);
|
||||
|
||||
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
|
||||
manager.updateFeed(service, feed);
|
||||
queryDownloads();
|
||||
@ -283,6 +313,11 @@ public class DownloadService extends Service {
|
||||
public void run() {
|
||||
image.setDownloaded(true);
|
||||
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);
|
||||
queryDownloads();
|
||||
}
|
||||
@ -313,6 +348,9 @@ public class DownloadService extends Service {
|
||||
media.setDuration(mediaplayer.getDuration());
|
||||
Log.d(TAG, "Duration of file is " + media.getDuration());
|
||||
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);
|
||||
queryDownloads();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user