From 1d319009c45b96464dac5dbf355d514e92e1734d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=AD?= <mvdan@mvdan.cc>
Date: Fri, 11 Oct 2013 16:20:07 +0200
Subject: [PATCH] Only calculate the current Apk (recommended version) once

---
 src/org/fdroid/fdroid/AppDetails.java         | 30 ++++++++-----------
 src/org/fdroid/fdroid/DB.java                 | 20 +++++--------
 .../fdroid/fdroid/views/AppListAdapter.java   |  2 +-
 3 files changed, 22 insertions(+), 30 deletions(-)

diff --git a/src/org/fdroid/fdroid/AppDetails.java b/src/org/fdroid/fdroid/AppDetails.java
index f14041542..42374ff80 100644
--- a/src/org/fdroid/fdroid/AppDetails.java
+++ b/src/org/fdroid/fdroid/AppDetails.java
@@ -179,7 +179,6 @@ public class AppDetails extends ListActivity {
 
     private DB.App app;
     private int app_currentvercode;
-    private DB.Apk curapk;
     private String appid;
     private PackageManager mPm;
     private DownloadHandler downloadHandler;
@@ -373,8 +372,7 @@ public class AppDetails extends ListActivity {
             DB.releaseDB();
         }
 
-        DB.Apk curver = app.getCurrentVersion();
-        app_currentvercode = curver == null ? 0 : curver.vercode;
+        app_currentvercode = app.curApk == null ? 0 : app.curApk.vercode;
 
         // Get the signature of the installed package...
         mInstalledSignature = null;
@@ -599,10 +597,10 @@ public class AppDetails extends ListActivity {
 
     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
-        curapk = app.apks.get(position - l.getHeaderViewsCount());
-        if (app.installedVerCode == curapk.vercode)
+        app.curApk = app.apks.get(position - l.getHeaderViewsCount());
+        if (app.installedVerCode == app.curApk.vercode)
             removeApk(app.id);
-        else if (app.installedVerCode > curapk.vercode) {
+        else if (app.installedVerCode > app.curApk.vercode) {
             AlertDialog.Builder ask_alrt = new AlertDialog.Builder(this);
             ask_alrt.setMessage(getString(R.string.installDowngrade));
             ask_alrt.setPositiveButton(getString(R.string.yes),
@@ -632,7 +630,6 @@ public class AppDetails extends ListActivity {
         menu.clear();
         if (app == null)
             return true;
-        DB.Apk curver = app.getCurrentVersion();
         if (app.toUpdate) {
             MenuItemCompat.setShowAsAction(menu.add(
                         Menu.NONE, INSTALL, 0, R.string.menu_update)
@@ -640,7 +637,7 @@ public class AppDetails extends ListActivity {
                     MenuItemCompat.SHOW_AS_ACTION_ALWAYS |
                     MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
         }
-        if (app.installedVersion == null && curver != null) {
+        if (app.installedVersion == null && app.curApk != null) {
             MenuItemCompat.setShowAsAction(menu.add(
                         Menu.NONE, INSTALL, 1, R.string.menu_install)
                         .setIcon(android.R.drawable.ic_menu_add),
@@ -740,8 +737,7 @@ public class AppDetails extends ListActivity {
 
         case INSTALL:
             // Note that this handles updating as well as installing.
-            curapk = app.getCurrentVersion();
-            if (curapk != null)
+            if (app.curApk != null)
                 install();
             return true;
 
@@ -793,13 +789,13 @@ public class AppDetails extends ListActivity {
         return super.onOptionsItemSelected(item);
     }
 
-    // Install the version of this app denoted by 'curapk'.
+    // Install the version of this app denoted by 'app.curApk'.
     private void install() {
 
         String ra = null;
         try {
             DB db = DB.getDB();
-            DB.Repo repo = db.getRepo(curapk.repo);
+            DB.Repo repo = db.getRepo(app.curApk.repo);
             if (repo != null)
                 ra = repo.address;
         } catch (Exception ex) {
@@ -811,14 +807,14 @@ public class AppDetails extends ListActivity {
             return;
         final String repoaddress = ra;
 
-        if (!curapk.compatible) {
+        if (!app.curApk.compatible) {
             AlertDialog.Builder ask_alrt = new AlertDialog.Builder(this);
             ask_alrt.setMessage(getString(R.string.installIncompatible));
             ask_alrt.setPositiveButton(getString(R.string.yes),
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                 int whichButton) {
-                            downloadHandler = new DownloadHandler(curapk,
+                            downloadHandler = new DownloadHandler(app.curApk,
                                     repoaddress, DB
                                             .getDataPath(getBaseContext()));
                         }
@@ -834,8 +830,8 @@ public class AppDetails extends ListActivity {
             alert.show();
             return;
         }
-        if (mInstalledSigID != null && curapk.sig != null
-                && !curapk.sig.equals(mInstalledSigID)) {
+        if (mInstalledSigID != null && app.curApk.sig != null
+                && !app.curApk.sig.equals(mInstalledSigID)) {
             AlertDialog.Builder builder = new AlertDialog.Builder(this);
             builder.setMessage(R.string.SignatureMismatch).setPositiveButton(
                     getString(R.string.ok),
@@ -848,7 +844,7 @@ public class AppDetails extends ListActivity {
             alert.show();
             return;
         }
-        downloadHandler = new DownloadHandler(curapk, repoaddress,
+        downloadHandler = new DownloadHandler(app.curApk, repoaddress,
                 DB.getDataPath(this));
     }
 
diff --git a/src/org/fdroid/fdroid/DB.java b/src/org/fdroid/fdroid/DB.java
index b931a8c57..39a8c2b54 100644
--- a/src/org/fdroid/fdroid/DB.java
+++ b/src/org/fdroid/fdroid/DB.java
@@ -177,6 +177,7 @@ public class DB {
 
         public String curVersion;
         public int curVercode;
+        public Apk curApk;
         public Date added;
         public Date lastUpdated;
 
@@ -213,9 +214,6 @@ public class DB {
         // True if the current update for this app is to be ignored
         public boolean ignoreThisUpdate;
 
-        // The name of the version that would be updated to.
-        public String updateVersion;
-
         // Used internally for tracking during repo updates.
         public boolean updated;
 
@@ -864,12 +862,11 @@ public class DB {
             // 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.curApk = app.getCurrentVersion();
+                if (app.curApk != null
                         && app.installedVerCode > 0
-                        && app.installedVerCode < curver.vercode) {
+                        && app.installedVerCode < app.curApk.vercode) {
                     app.hasUpdates = true;
-                    app.updateVersion = curver.version;
                 }
             }
         }
@@ -921,12 +918,11 @@ public class DB {
             }
 
             app.hasUpdates = false;
-            Apk curver = app.getCurrentVersion();
-            if (curver != null
+            app.curApk = app.getCurrentVersion();
+            if (app.curApk != null
                     && app.installedVersion != null
-                    && app.installedVerCode < curver.vercode) {
+                    && app.installedVerCode < app.curApk.vercode) {
                 app.hasUpdates = true;
-                app.updateVersion = curver.version;
             }
 
             apps.set(index, app);
@@ -1157,7 +1153,7 @@ public class DB {
             values.put("ignoreThisUpdate", upapp.ignoreThisUpdate ? 1 : 0);
         } else {
             values.put("ignoreAllUpdates", oldapp.ignoreAllUpdates ? 1 : 0);
-            if (upapp.curVercode > oldapp.curVercode)
+            if (upapp.curApk.vercode > oldapp.curApk.vercode)
                 values.put("ignoreThisUpdate", upapp.ignoreThisUpdate ? 1 : 0);
             else
                 values.put("ignoreThisUpdate", oldapp.ignoreThisUpdate ? 1 : 0);
diff --git a/src/org/fdroid/fdroid/views/AppListAdapter.java b/src/org/fdroid/fdroid/views/AppListAdapter.java
index a508bcc95..0d9d90535 100644
--- a/src/org/fdroid/fdroid/views/AppListAdapter.java
+++ b/src/org/fdroid/fdroid/views/AppListAdapter.java
@@ -154,7 +154,7 @@ abstract public class AppListAdapter extends BaseAdapter {
             version.append(app.installedVersion);
             if (app.toUpdate) {
                 version.append(" -> ");
-                version.append(app.updateVersion);
+                version.append(app.curApk.version);
             }
         } else {
             int numav = app.apks.size();