From 9b20142fd9b1199deda1faedd545b83c32e4bd58 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Wed, 5 Jul 2017 17:28:46 +1000 Subject: [PATCH] Extract getInstalledApk() from AppDetails2 to App --- .../java/org/fdroid/fdroid/AppDetails2.java | 44 ++----------------- .../main/java/org/fdroid/fdroid/data/App.java | 31 +++++++++++++ 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/AppDetails2.java b/app/src/main/java/org/fdroid/fdroid/AppDetails2.java index 1f993b4c8..2a4f6b2f7 100644 --- a/app/src/main/java/org/fdroid/fdroid/AppDetails2.java +++ b/app/src/main/java/org/fdroid/fdroid/AppDetails2.java @@ -29,13 +29,10 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; import android.database.ContentObserver; import android.net.Uri; import android.os.Bundle; import android.os.Handler; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.AppBarLayout; import android.support.design.widget.CoordinatorLayout; @@ -60,8 +57,6 @@ import org.fdroid.fdroid.data.ApkProvider; import org.fdroid.fdroid.data.App; import org.fdroid.fdroid.data.AppPrefsProvider; import org.fdroid.fdroid.data.AppProvider; -import org.fdroid.fdroid.data.InstalledApp; -import org.fdroid.fdroid.data.InstalledAppProvider; import org.fdroid.fdroid.data.Schema; import org.fdroid.fdroid.installer.InstallManagerService; import org.fdroid.fdroid.installer.Installer; @@ -740,40 +735,6 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog installApk(apkToInstall); } - /** - * Attempts to find the installed {@link Apk} from the database. If not found, will lookup the - * {@link InstalledAppProvider} to find the details of the installed app and use that to - * instantiate an {@link Apk} to be returned. - * - * Cases where an {@link Apk} will not be found in the database and for which we fall back to - * the {@link InstalledAppProvider} include: - * + System apps which are provided by a repository, but for which the version code bundled - * with the system is not included in the repository. - * + Regular apps from a repository, where the installed version is old enough that it is no - * longer available in the repository. - * - * @throws IllegalStateException If neither the {@link PackageManager} or the - * {@link InstalledAppProvider} can't find a reference to the installed apk. - */ - @NonNull - private Apk getInstalledApk() { - try { - PackageInfo pi = getPackageManager().getPackageInfo(app.packageName, 0); - Apk apk = ApkProvider.Helper.findApkFromAnyRepo(this, pi.packageName, pi.versionCode); - if (apk == null) { - InstalledApp installedApp = InstalledAppProvider.Helper.findByPackageName(this, pi.packageName); - if (installedApp == null) { - throw new IllegalStateException("No installed app found when trying to uninstall"); - } - apk = new Apk(installedApp); - } - return apk; - } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); - throw new IllegalStateException("Couldn't find installed apk for " + app.packageName, e); - } - } - @Override public void uninstallApk() { Apk apk = app.installedApk; @@ -783,7 +744,10 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog apk = app.getMediaApkifInstalled(getApplicationContext()); if (apk == null) { // When the app isn't a media file - the above workaround refers to this. - apk = getInstalledApk(); + apk = app.getInstalledApk(this); + if (apk == null) { + throw new IllegalStateException("Couldn't find installed apk for " + app.packageName); + } } app.installedApk = apk; } diff --git a/app/src/main/java/org/fdroid/fdroid/data/App.java b/app/src/main/java/org/fdroid/fdroid/data/App.java index ed3797860..5a7109683 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/App.java +++ b/app/src/main/java/org/fdroid/fdroid/data/App.java @@ -796,6 +796,37 @@ public class App extends ValueObject implements Comparable, Parcelable { apk.sig = Utils.hashBytes(fdroidSig, "md5"); } + /** + * Attempts to find the installed {@link Apk} from the database. If not found, will lookup the + * {@link InstalledAppProvider} to find the details of the installed app and use that to + * instantiate an {@link Apk} to be returned. + * + * Cases where an {@link Apk} will not be found in the database and for which we fall back to + * the {@link InstalledAppProvider} include: + * + System apps which are provided by a repository, but for which the version code bundled + * with the system is not included in the repository. + * + Regular apps from a repository, where the installed version is old enough that it is no + * longer available in the repository. + * + */ + @Nullable + public Apk getInstalledApk(Context context) { + try { + PackageInfo pi = context.getPackageManager().getPackageInfo(this.packageName, 0); + Apk apk = ApkProvider.Helper.findApkFromAnyRepo(context, pi.packageName, pi.versionCode); + if (apk == null) { + InstalledApp installedApp = InstalledAppProvider.Helper.findByPackageName(context, pi.packageName); + if (installedApp == null) { + throw new IllegalStateException("No installed app found when trying to uninstall"); + } + apk = new Apk(installedApp); + } + return apk; + } catch (PackageManager.NameNotFoundException e) { + return null; + } + } + public boolean isValid() { if (TextUtils.isEmpty(this.name) || TextUtils.isEmpty(this.packageName)) {