From 96f358d94c4abf707789be74b623b742da1282b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 21 Jul 2013 17:00:05 +0200 Subject: [PATCH] New getAppsBasic to avoid retrieving unnecessary data --- src/org/fdroid/fdroid/DB.java | 98 ++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/src/org/fdroid/fdroid/DB.java b/src/org/fdroid/fdroid/DB.java index 2cf9e49c1..361c5ac6b 100644 --- a/src/org/fdroid/fdroid/DB.java +++ b/src/org/fdroid/fdroid/DB.java @@ -192,7 +192,7 @@ public class DB { public Apk getCurrentVersion() { // Try and return the real current version first... - if (curVersion != null && curVercode > 0) { + if (curVercode > 0) { for (Apk apk : apks) { if (apk.vercode == curVercode) return apk; @@ -558,7 +558,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 = getApps(true); + List apps = getAppsBasic(true); int count = 0; for (App app : apps) { if (app.hasUpdates) @@ -866,6 +866,100 @@ public class DB { return apps; } + public 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.installedVersion != null + && app.installedVerCode < curver.vercode) { + app.hasUpdates = true; + app.updateVersion = curver.version; + } + } + } + + return result; + } + public List doSearch(String query) { List ids = new ArrayList();