Stop progress updates when in background

This commit is contained in:
Henrik Tunedal 2011-03-22 22:26:53 +01:00
parent 7c43b91502
commit 767f1a6866
2 changed files with 30 additions and 4 deletions

View File

@ -210,9 +210,20 @@ public class AppDetails extends ListActivity {
reset(); reset();
viewResetRequired = false; viewResetRequired = false;
} }
if (download != null && downloadHandler != null) {
downloadHandler.startUpdates();
}
super.onResume(); super.onResume();
} }
@Override
protected void onPause() {
if (downloadHandler != null) {
downloadHandler.stopUpdates();
}
super.onPause();
}
@Override @Override
protected void onStop() { protected void onStop() {
db.close(); db.close();
@ -243,7 +254,7 @@ public class AppDetails extends ListActivity {
// there. // there.
private void copyState(AppDetails old) { private void copyState(AppDetails old) {
download = old.download; download = old.download;
if (download != null && download.isAlive()) { if (download != null) {
downloadHandler = new DownloadHandler(); downloadHandler = new DownloadHandler();
} }
ApkListAdapter oldAdapter = (ApkListAdapter)old.getListAdapter(); ApkListAdapter oldAdapter = (ApkListAdapter)old.getListAdapter();
@ -435,8 +446,8 @@ public class AppDetails extends ListActivity {
alert.show(); alert.show();
return; return;
} }
downloadHandler = new DownloadHandler();
download = new Downloader(curapk); download = new Downloader(curapk);
downloadHandler = new DownloadHandler();
download.start(); download.start();
} }
@ -490,9 +501,10 @@ public class AppDetails extends ListActivity {
// Handler used to update the progress dialog while downloading. // Handler used to update the progress dialog while downloading.
private class DownloadHandler extends Handler { private class DownloadHandler extends Handler {
private ProgressDialog pd; private ProgressDialog pd;
boolean updating;
public DownloadHandler() { public DownloadHandler() {
sendEmptyMessage(0); // Start updating progress startUpdates();
} }
public boolean updateProgress() { public boolean updateProgress() {
@ -532,6 +544,18 @@ public class AppDetails extends ListActivity {
return finished; return finished;
} }
public void startUpdates() {
if (!updating) {
updating = true;
sendEmptyMessage(0);
}
}
public void stopUpdates() {
updating = false;
removeMessages(0);
}
public void destroy() { public void destroy() {
// The dialog can't be dismissed when it's not displayed, // The dialog can't be dismissed when it's not displayed,
// so do it when the activity is being destroyed. // so do it when the activity is being destroyed.
@ -541,7 +565,7 @@ public class AppDetails extends ListActivity {
} }
// Cancel any scheduled updates so that we don't // Cancel any scheduled updates so that we don't
// accidentally recreate the progress dialog. // accidentally recreate the progress dialog.
removeMessages(0); stopUpdates();
} }
// Repeatedly run updateProgress() until it's finished. // Repeatedly run updateProgress() until it's finished.
@ -566,6 +590,7 @@ public class AppDetails extends ListActivity {
File file = new File(download.localFile()); File file = new File(download.localFile());
Log.d("FDroid", "Cleaning up: " + file); Log.d("FDroid", "Cleaning up: " + file);
file.delete(); file.delete();
download = null;
} }
viewResetRequired = true; viewResetRequired = true;
break; break;

View File

@ -205,6 +205,7 @@ public class Downloader extends Thread {
synchronized (this) { synchronized (this) {
status = Status.DONE; status = Status.DONE;
} }
Log.d("FDroid", "Download finished: " + apk_file);
} }
} }
} }