From fa1b53a81cfb9f2e4afb6de06572c1382bba1af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 28 Sep 2013 19:34:48 +0200 Subject: [PATCH] Fix repo update notifications * Get rid of getAppsBasic(boolean) * Use FDroidApp to read apps from UpdateService - Don't read the SQL tables twice if updating manually - Use two app lists, not three * Notify regardless of the previous updates count --- src/org/fdroid/fdroid/DB.java | 114 +---------------------- src/org/fdroid/fdroid/UpdateService.java | 38 ++++---- 2 files changed, 23 insertions(+), 129 deletions(-) diff --git a/src/org/fdroid/fdroid/DB.java b/src/org/fdroid/fdroid/DB.java index 3aa3283f7..5d7f7e03e 100644 --- a/src/org/fdroid/fdroid/DB.java +++ b/src/org/fdroid/fdroid/DB.java @@ -609,8 +609,7 @@ public class DB { // Get the number of apps that have updates available. This can be a // time consuming operation. - public int getNumUpdates() { - List apps = getAppsBasic(true); + public int getNumUpdates(List apps) { int count = 0; for (App app : apps) { if (!app.ignoreUpdates && app.hasUpdates) @@ -924,100 +923,6 @@ public class DB { return apps; } - private List getAppsBasic(boolean getinstalledinfo) { - - // If we're going to need it, get info in what's currently installed - Map systemApks = null; - if (getinstalledinfo) { - Log.d("FDroid", "Reading installed packages"); - systemApks = new HashMap(); - List installedPackages = mContext.getPackageManager() - .getInstalledPackages(0); - for (PackageInfo appInfo : installedPackages) { - systemApks.put(appInfo.packageName, appInfo); - } - } - - Map apps = new HashMap(); - Cursor c = null; - long startTime = System.currentTimeMillis(); - try { - - String cols[] = new String[] { "id", "curVercode" }; - c = db.query(TABLE_APP, cols, null, null, null, null, null); - c.moveToFirst(); - while (!c.isAfterLast()) { - - App app = new App(); - app.id = c.getString(0); - app.curVercode = c.getInt(1); - app.hasUpdates = false; - - if (getinstalledinfo && systemApks.containsKey(app.id)) { - PackageInfo sysapk = systemApks.get(app.id); - app.installedVerCode = sysapk.versionCode; - } else { - app.installedVerCode = 0; - } - - apps.put(app.id, app); - c.moveToNext(); - } - c.close(); - c = null; - - Log.d("FDroid", "Read basic app data from database " + " (took " - + (System.currentTimeMillis() - startTime) + " ms)"); - - cols = new String[] { "id", "vercode", "repo" }; - c = db.query(TABLE_APK, cols, null, null, null, null, - "vercode desc"); - c.moveToFirst(); - while (!c.isAfterLast()) { - Apk apk = new Apk(); - apk.id = c.getString(0); - apk.vercode = c.getInt(1); - apk.repo = c.getInt(2); - apps.get(apk.id).apks.add(apk); - c.moveToNext(); - } - c.close(); - - } catch (Exception e) { - Log.e("FDroid", "Exception during database reading:\n" - + Log.getStackTraceString(e)); - } finally { - if (c != null) { - c.close(); - } - - Log.d("FDroid", "Read basic app and apk data from database " + - " (took " + (System.currentTimeMillis() - startTime) + - " ms)"); - } - - List result = new ArrayList(apps.values()); - Collections.sort(result); - - // Fill in the hasUpdates fields if we have the necessary information... - if (getinstalledinfo) { - - // We'll say an application has updates if it's installed AND the - // version is older than the current one - for (App app : result) { - Apk curver = app.getCurrentVersion(); - if (curver != null - && app.installedVerCode > 0 - && app.installedVerCode < curver.vercode) { - app.hasUpdates = true; - app.updateVersion = curver.version; - } - } - } - - return result; - } - public List doSearch(String query) { List ids = new ArrayList(); @@ -1070,27 +975,18 @@ public class DB { private List updateApps = null; - // Called before a repo update starts. Returns the number of updates - // available beforehand. - public int beginUpdate(List apps) { + // Called before a repo update starts. + public void beginUpdate(List apps) { // Get a list of all apps. All the apps and apks in this list will // have 'updated' set to false at this point, and we will only set // it to true when we see the app/apk in a repository. Thus, at the // end, any that are still false can be removed. updateApps = apps; - Log.d("FDroid", "AppUpdate: " + updateApps.size() - + " apps before starting."); + Log.d("FDroid", "AppUpdate: " + updateApps.size() + " apps before starting."); // Wrap the whole update in a transaction. Make sure to call // either endUpdate or cancelUpdate to commit or discard it, // respectively. db.beginTransaction(); - - int count = 0; - for (App app : updateApps) { - if (!app.ignoreUpdates && app.hasUpdates) - count++; - } - return count; } // Called when a repo update ends. Any applications that have not been @@ -1408,7 +1304,7 @@ public class DB { db.delete(TABLE_REPO, "address = ?", new String[] { address }); } - List apps = getAppsBasic(true); + List apps = getApps(false); for (App app : apps) { if (app.apks.isEmpty()) { db.delete(TABLE_APP, "id = ?", new String[] { app.id }); diff --git a/src/org/fdroid/fdroid/UpdateService.java b/src/org/fdroid/fdroid/UpdateService.java index a646be4f5..c564f6091 100644 --- a/src/org/fdroid/fdroid/UpdateService.java +++ b/src/org/fdroid/fdroid/UpdateService.java @@ -134,8 +134,7 @@ public class UpdateService extends IntentService implements ProgressListener { // Grab some preliminary information, then we can release the // database while we do all the downloading, etc... - int prevUpdates = 0; - int newUpdates = 0; + int updates = 0; List repos; try { DB db = DB.getDB(); @@ -145,7 +144,7 @@ public class UpdateService extends IntentService implements ProgressListener { } // Process each repo... - List apps = new ArrayList(); + List updatingApps = new ArrayList(); List keeprepos = new ArrayList(); boolean success = true; boolean changes = false; @@ -159,7 +158,7 @@ public class UpdateService extends IntentService implements ProgressListener { StringBuilder newetag = new StringBuilder(); String err = RepoXMLHandler.doUpdate(getBaseContext(), - repo, apps, newetag, keeprepos, this); + repo, updatingApps, newetag, keeprepos, this); if (err == null) { String nt = newetag.toString(); if (!nt.equals(repo.lastetag)) { @@ -178,7 +177,6 @@ public class UpdateService extends IntentService implements ProgressListener { } } - List acceptedapps = new ArrayList(); if (!changes && success) { Log.d("FDroid", "Not checking app details or compatibility, because all repos were up to date."); @@ -186,7 +184,7 @@ public class UpdateService extends IntentService implements ProgressListener { sendStatus(STATUS_INFO, getString(R.string.status_checking_compatibility)); - List prevapps = ((FDroidApp) getApplication()).getApps(); + List apps = ((FDroidApp) getApplication()).getApps(); DB db = DB.getDB(); try { @@ -195,7 +193,7 @@ public class UpdateService extends IntentService implements ProgressListener { // no data about during the update. (i.e. stuff from a repo // that we know is unchanged due to the etag) for (int keep : keeprepos) { - for (DB.App app : prevapps) { + for (DB.App app : apps) { boolean keepapp = false; for (DB.Apk apk : app.apks) { if (apk.repo == keep) { @@ -205,14 +203,14 @@ public class UpdateService extends IntentService implements ProgressListener { } if (keepapp) { DB.App app_k = null; - for (DB.App app2 : apps) { + for (DB.App app2 : updatingApps) { if (app2.id.equals(app.id)) { app_k = app2; break; } } if (app_k == null) { - apps.add(app); + updatingApps.add(app); app_k = app; } app_k.updated = true; @@ -224,14 +222,15 @@ public class UpdateService extends IntentService implements ProgressListener { } } - prevUpdates = db.beginUpdate(prevapps); - for (DB.App app : apps) { - if (db.updateApplication(app)) - acceptedapps.add(app); + db.beginUpdate(apps); + for (DB.App app : updatingApps) { + db.updateApplication(app); } db.endUpdate(); - if (notify) - newUpdates = db.getNumUpdates(); + if (notify) { + apps = ((FDroidApp) getApplication()).getApps(); + updates = db.getNumUpdates(apps); + } for (DB.Repo repo : repos) db.writeLastEtag(repo); } catch (Exception ex) { @@ -249,9 +248,8 @@ public class UpdateService extends IntentService implements ProgressListener { if (success && changes) ((FDroidApp) getApplication()).invalidateAllApps(); - if (success && changes && notify && (newUpdates > prevUpdates)) { - Log.d("FDroid", "Notifying updates. Apps before:" + prevUpdates - + ", apps after: " + newUpdates); + if (success && changes && notify && updates > 0) { + Log.d("FDroid", "Notifying "+updates+" updates."); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this) .setSmallIcon(R.drawable.icon) @@ -263,9 +261,9 @@ public class UpdateService extends IntentService implements ProgressListener { getString(R.string.fdroid_updates_available)); Intent notifyIntent = new Intent(this, FDroid.class) .putExtra(FDroid.EXTRA_TAB_UPDATE, true); - if (newUpdates > 1) { + if (updates > 1) { mBuilder.setContentText(getString( - R.string.many_updates_available, newUpdates)); + R.string.many_updates_available, updates)); } else { mBuilder.setContentText(getString(R.string.one_update_available));