Eclipse transfer, Made sure that DownloadObserver cancels when Activity
stops
This commit is contained in:
parent
8f9db70291
commit
72e608a049
@ -14,7 +14,7 @@
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.Sherlock.Light"
|
||||
android:name=".PodcastApp">
|
||||
android:name="de.podfetcher.PodcastApp">
|
||||
<activity
|
||||
android:label="@string/app_name"
|
||||
android:name="de.podfetcher.activity.PodfetcherActivity" >
|
||||
|
@ -21,16 +21,37 @@ import java.util.concurrent.Callable;
|
||||
public class AddFeedActivity extends SherlockActivity {
|
||||
private static final String TAG = "AddFeedActivity";
|
||||
|
||||
private DownloadRequester requester;
|
||||
|
||||
private EditText etxtFeedurl;
|
||||
private Button butConfirm;
|
||||
private Button butCancel;
|
||||
private long downloadId;
|
||||
|
||||
|
||||
|
||||
private DownloadObserver observer;
|
||||
|
||||
private ProgressDialog progDialog;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.addfeed);
|
||||
|
||||
requester = DownloadRequester.getInstance();
|
||||
|
||||
createObserver();
|
||||
progDialog = new ProgressDialog(this) {
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
requester.cancelDownload(getContext(), downloadId);
|
||||
observer.cancel(true);
|
||||
createObserver();
|
||||
dismiss();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl);
|
||||
butConfirm = (Button) findViewById(R.id.butConfirm);
|
||||
butCancel = (Button) findViewById(R.id.butCancel);
|
||||
@ -49,9 +70,30 @@ public class AddFeedActivity extends SherlockActivity {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createObserver() {
|
||||
observer = new DownloadObserver(this) {
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
progDialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(DownloadObserver.DownloadStatus... values) {
|
||||
DownloadObserver.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() {
|
||||
@ -60,29 +102,13 @@ public class AddFeedActivity extends SherlockActivity {
|
||||
|
||||
if(url != null) {
|
||||
Feed feed = new Feed(url);
|
||||
DownloadRequester req = DownloadRequester.getInstance();
|
||||
req.downloadFeed(this, feed);
|
||||
downloadId = requester.downloadFeed(this, feed);
|
||||
observeDownload(feed);
|
||||
}
|
||||
}
|
||||
|
||||
private void observeDownload(Feed feed) {
|
||||
final ProgressDialog dialog = new ProgressDialog(this);
|
||||
final DownloadObserver observer = new DownloadObserver(this) {
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(DownloadObserver.DownloadStatus... values) {
|
||||
DownloadObserver.DownloadStatus progr = values[0];
|
||||
dialog.setMessage(getContext().getString(progr.getStatusMsg())
|
||||
+ " (" + progr.getProgressPercent() + "%)");
|
||||
}
|
||||
};
|
||||
dialog.show();
|
||||
progDialog.show();
|
||||
observer.execute(feed);
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import de.podfetcher.feed.FeedFile;
|
||||
import com.actionbarsherlock.app.SherlockListActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
public class DownloadActivity extends SherlockListActivity {
|
||||
private static final String TAG = "DownloadActivity";
|
||||
@ -25,6 +26,13 @@ public class DownloadActivity extends SherlockListActivity {
|
||||
new FeedFile[requester.getMediaDownloads().size()]));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
Log.d(TAG, "Stopping Activity");
|
||||
observer.cancel(true);
|
||||
}
|
||||
|
||||
private final DownloadObserver observer = new DownloadObserver(this) {
|
||||
@Override
|
||||
|
@ -64,6 +64,13 @@ public class ItemviewActivity extends SherlockActivity {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
Log.d(TAG, "Stopping Activity");
|
||||
downloadObserver.cancel(true);
|
||||
}
|
||||
|
||||
/** Extracts FeedItem object the activity is supposed to display */
|
||||
private void extractFeeditem() {
|
||||
|
@ -61,7 +61,7 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadObserver.Downl
|
||||
}
|
||||
|
||||
|
||||
while(downloadsLeft()) {
|
||||
while(downloadsLeft() && !isCancelled()) {
|
||||
for (DownloadStatus status : statusList) {
|
||||
if (status.done == false) {
|
||||
Cursor cursor = getDownloadCursor(status.feedfile);
|
||||
|
@ -60,7 +60,7 @@ public class DownloadRequester {
|
||||
return downloader;
|
||||
}
|
||||
|
||||
private void download(Context context, ArrayList<FeedFile> type, FeedFile item, File dest, boolean visibleInUI) {
|
||||
private long download(Context context, ArrayList<FeedFile> type, FeedFile item, File dest, boolean visibleInUI) {
|
||||
Log.d(TAG, "Requesting download of url "+ item.getDownload_url());
|
||||
type.add(item);
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(item.getDownload_url()));
|
||||
@ -71,27 +71,33 @@ public class DownloadRequester {
|
||||
// TODO Set Allowed Network Types
|
||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
context.startService(new Intent(context, DownloadService.class));
|
||||
item.setDownloadId(manager.enqueue(request));
|
||||
item.setFile_url(dest.toString());
|
||||
long downloadId = manager.enqueue(request);
|
||||
item.setDownloadId(downloadId);
|
||||
item.setFile_url(dest.toString());
|
||||
return downloadId;
|
||||
}
|
||||
public void downloadFeed(Context context, Feed feed) {
|
||||
download(context, feeds, feed,
|
||||
public long downloadFeed(Context context, Feed feed) {
|
||||
return download(context, feeds, feed,
|
||||
new File(getFeedfilePath(context), getFeedfileName(feed)),
|
||||
true);
|
||||
}
|
||||
|
||||
public void downloadImage(Context context, FeedImage image) {
|
||||
download(context, images, image,
|
||||
public long downloadImage(Context context, FeedImage image) {
|
||||
return download(context, images, image,
|
||||
new File(getImagefilePath(context), getImagefileName(image)),
|
||||
true);
|
||||
}
|
||||
|
||||
public void downloadMedia(Context context, FeedMedia feedmedia) {
|
||||
download(context, media, feedmedia,
|
||||
public long downloadMedia(Context context, FeedMedia feedmedia) {
|
||||
return download(context, media, feedmedia,
|
||||
new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)),
|
||||
true);
|
||||
}
|
||||
|
||||
|
||||
/** Cancels a running download.
|
||||
* @param context A context needed to get the DownloadManager service
|
||||
* @param id ID of the download to cancel
|
||||
* */
|
||||
public void cancelDownload(final Context context, final long id) {
|
||||
Log.d(TAG, "Cancelling download with id " + id);
|
||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user