From 4f864aaf1093e70b228843515262df9991d22f10 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Mon, 8 Nov 2010 23:49:22 +0000 Subject: [PATCH] Only do background updates when the main activities are not in the foreground --- AndroidManifest.xml | 5 +++-- src/org/fdroid/fdroid/AppDetails.java | 17 ++++++++++++----- src/org/fdroid/fdroid/FDroid.java | 8 ++++++++ src/org/fdroid/fdroid/UpdateService.java | 11 +++++++++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 519f2504e..4e4fd734a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,7 +2,8 @@ - + @@ -29,6 +30,6 @@ - + diff --git a/src/org/fdroid/fdroid/AppDetails.java b/src/org/fdroid/fdroid/AppDetails.java index 940855aff..6397ae02c 100644 --- a/src/org/fdroid/fdroid/AppDetails.java +++ b/src/org/fdroid/fdroid/AppDetails.java @@ -170,13 +170,19 @@ public class AppDetails extends ListActivity { @Override protected void onStart() { super.onStart(); - + ((FDroidApp) getApplication()).inActivity++; // Get the preferences we're going to use in this Activity... SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); pref_cacheDownloaded = prefs.getBoolean("cacheDownloaded", true); } + @Override + protected void onStop() { + ((FDroidApp) getApplication()).inActivity--; + super.onStop(); + } + // Reset the display and list contents. Used when entering the activity, and // also when something has been installed/uninstalled. In the latter case, // 'update' is true to make the installed status get refreshed. @@ -381,7 +387,7 @@ public class AppDetails extends ListActivity { msg.arg2 = 1; msg.obj = new String(localfile); download_handler.sendMessage(msg); - msg=new Message(); + msg = new Message(); msg.arg1 = 1; download_handler.sendMessage(msg); } else { @@ -526,9 +532,10 @@ public class AppDetails extends ListActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if(requestCode==REQUEST_INSTALL) { - // If we're not meant to be caching, delete the apk file we just - // installed (or maybe the user cancelled the install - doesn't matter) + if (requestCode == REQUEST_INSTALL) { + // If we're not meant to be caching, delete the apk file we just + // installed (or maybe the user cancelled the install - doesn't + // matter) // from the SD card... if (!pref_cacheDownloaded) { String apkname = curapk.apkName; diff --git a/src/org/fdroid/fdroid/FDroid.java b/src/org/fdroid/fdroid/FDroid.java index 419fed4f3..b85bab5aa 100644 --- a/src/org/fdroid/fdroid/FDroid.java +++ b/src/org/fdroid/fdroid/FDroid.java @@ -192,9 +192,16 @@ public class FDroid extends TabActivity implements OnItemClickListener { @Override protected void onStart() { super.onStart(); + ((FDroidApp) getApplication()).inActivity++; populateLists(true); } + @Override + protected void onStop() { + ((FDroidApp) getApplication()).inActivity--; + super.onStop(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -300,6 +307,7 @@ public class FDroid extends TabActivity implements OnItemClickListener { // service accordingly. It's cheap, so no need to check if the particular setting has // actually been changed. UpdateService.schedule(getBaseContext()); + populateLists(false); break; } diff --git a/src/org/fdroid/fdroid/UpdateService.java b/src/org/fdroid/fdroid/UpdateService.java index 91686db83..a5330dace 100644 --- a/src/org/fdroid/fdroid/UpdateService.java +++ b/src/org/fdroid/fdroid/UpdateService.java @@ -76,6 +76,12 @@ public class UpdateService extends Service { new Thread() { public void run() { + // If we're in one of our list activities, we don't want + // to run an update because the database will be out of + // sync with the display. + if (((FDroidApp) getApplication()).inActivity != 0) + return; + // See if it's time to actually do anything yet... SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); @@ -99,8 +105,9 @@ public class UpdateService extends Service { try { db = new DB(getBaseContext()); RepoXMLHandler.doUpdates(db); - } catch(Exception e) { - Log.d("FDroid","Exception during handleCommand() - " + e.getMessage()); + } catch (Exception e) { + Log.d("FDroid", "Exception during handleCommand() - " + + e.getMessage()); } finally { if (db != null) db.close();