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:icon="@drawable/ic_launcher"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:theme="@style/Theme.Sherlock.Light"
|
android:theme="@style/Theme.Sherlock.Light"
|
||||||
android:name=".PodcastApp">
|
android:name="de.podfetcher.PodcastApp">
|
||||||
<activity
|
<activity
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:name="de.podfetcher.activity.PodfetcherActivity" >
|
android:name="de.podfetcher.activity.PodfetcherActivity" >
|
||||||
|
@ -21,16 +21,37 @@ import java.util.concurrent.Callable;
|
|||||||
public class AddFeedActivity extends SherlockActivity {
|
public class AddFeedActivity extends SherlockActivity {
|
||||||
private static final String TAG = "AddFeedActivity";
|
private static final String TAG = "AddFeedActivity";
|
||||||
|
|
||||||
|
private DownloadRequester requester;
|
||||||
|
|
||||||
private EditText etxtFeedurl;
|
private EditText etxtFeedurl;
|
||||||
private Button butConfirm;
|
private Button butConfirm;
|
||||||
private Button butCancel;
|
private Button butCancel;
|
||||||
|
private long downloadId;
|
||||||
|
|
||||||
|
|
||||||
|
private DownloadObserver observer;
|
||||||
|
|
||||||
|
private ProgressDialog progDialog;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.addfeed);
|
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);
|
etxtFeedurl = (EditText) findViewById(R.id.etxtFeedurl);
|
||||||
butConfirm = (Button) findViewById(R.id.butConfirm);
|
butConfirm = (Button) findViewById(R.id.butConfirm);
|
||||||
butCancel = (Button) findViewById(R.id.butCancel);
|
butCancel = (Button) findViewById(R.id.butCancel);
|
||||||
@ -49,9 +70,30 @@ public class AddFeedActivity extends SherlockActivity {
|
|||||||
finish();
|
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() {
|
private void addNewFeed() {
|
||||||
@ -60,29 +102,13 @@ public class AddFeedActivity extends SherlockActivity {
|
|||||||
|
|
||||||
if(url != null) {
|
if(url != null) {
|
||||||
Feed feed = new Feed(url);
|
Feed feed = new Feed(url);
|
||||||
DownloadRequester req = DownloadRequester.getInstance();
|
downloadId = requester.downloadFeed(this, feed);
|
||||||
req.downloadFeed(this, feed);
|
|
||||||
observeDownload(feed);
|
observeDownload(feed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void observeDownload(Feed feed) {
|
private void observeDownload(Feed feed) {
|
||||||
final ProgressDialog dialog = new ProgressDialog(this);
|
progDialog.show();
|
||||||
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();
|
|
||||||
observer.execute(feed);
|
observer.execute(feed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import de.podfetcher.feed.FeedFile;
|
|||||||
import com.actionbarsherlock.app.SherlockListActivity;
|
import com.actionbarsherlock.app.SherlockListActivity;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
public class DownloadActivity extends SherlockListActivity {
|
public class DownloadActivity extends SherlockListActivity {
|
||||||
private static final String TAG = "DownloadActivity";
|
private static final String TAG = "DownloadActivity";
|
||||||
@ -26,6 +27,13 @@ public class DownloadActivity extends SherlockListActivity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
Log.d(TAG, "Stopping Activity");
|
||||||
|
observer.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
private final DownloadObserver observer = new DownloadObserver(this) {
|
private final DownloadObserver observer = new DownloadObserver(this) {
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(DownloadObserver.DownloadStatus... values) {
|
protected void onProgressUpdate(DownloadObserver.DownloadStatus... values) {
|
||||||
|
@ -65,6 +65,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 */
|
/** Extracts FeedItem object the activity is supposed to display */
|
||||||
private void extractFeeditem() {
|
private void extractFeeditem() {
|
||||||
long itemId = getIntent().getLongExtra(FeedItemlistActivity.EXTRA_SELECTED_FEEDITEM, -1);
|
long itemId = getIntent().getLongExtra(FeedItemlistActivity.EXTRA_SELECTED_FEEDITEM, -1);
|
||||||
|
@ -61,7 +61,7 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadObserver.Downl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
while(downloadsLeft()) {
|
while(downloadsLeft() && !isCancelled()) {
|
||||||
for (DownloadStatus status : statusList) {
|
for (DownloadStatus status : statusList) {
|
||||||
if (status.done == false) {
|
if (status.done == false) {
|
||||||
Cursor cursor = getDownloadCursor(status.feedfile);
|
Cursor cursor = getDownloadCursor(status.feedfile);
|
||||||
|
@ -60,7 +60,7 @@ public class DownloadRequester {
|
|||||||
return downloader;
|
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());
|
Log.d(TAG, "Requesting download of url "+ item.getDownload_url());
|
||||||
type.add(item);
|
type.add(item);
|
||||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(item.getDownload_url()));
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(item.getDownload_url()));
|
||||||
@ -71,27 +71,33 @@ public class DownloadRequester {
|
|||||||
// TODO Set Allowed Network Types
|
// TODO Set Allowed Network Types
|
||||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||||
context.startService(new Intent(context, DownloadService.class));
|
context.startService(new Intent(context, DownloadService.class));
|
||||||
item.setDownloadId(manager.enqueue(request));
|
long downloadId = manager.enqueue(request);
|
||||||
|
item.setDownloadId(downloadId);
|
||||||
item.setFile_url(dest.toString());
|
item.setFile_url(dest.toString());
|
||||||
|
return downloadId;
|
||||||
}
|
}
|
||||||
public void downloadFeed(Context context, Feed feed) {
|
public long downloadFeed(Context context, Feed feed) {
|
||||||
download(context, feeds, feed,
|
return download(context, feeds, feed,
|
||||||
new File(getFeedfilePath(context), getFeedfileName(feed)),
|
new File(getFeedfilePath(context), getFeedfileName(feed)),
|
||||||
true);
|
true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void downloadImage(Context context, FeedImage image) {
|
public long downloadImage(Context context, FeedImage image) {
|
||||||
download(context, images, image,
|
return download(context, images, image,
|
||||||
new File(getImagefilePath(context), getImagefileName(image)),
|
new File(getImagefilePath(context), getImagefileName(image)),
|
||||||
true);
|
true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void downloadMedia(Context context, FeedMedia feedmedia) {
|
public long downloadMedia(Context context, FeedMedia feedmedia) {
|
||||||
download(context, media, feedmedia,
|
return download(context, media, feedmedia,
|
||||||
new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)),
|
new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)),
|
||||||
true);
|
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) {
|
public void cancelDownload(final Context context, final long id) {
|
||||||
Log.d(TAG, "Cancelling download with id " + id);
|
Log.d(TAG, "Cancelling download with id " + id);
|
||||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user