Extract getInstalledApk() from AppDetails2 to App

This commit is contained in:
Peter Serwylo 2017-07-05 17:28:46 +10:00
parent 073cb199fc
commit 9b20142fd9
2 changed files with 35 additions and 40 deletions

View File

@ -29,13 +29,10 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout; import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout; 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.App;
import org.fdroid.fdroid.data.AppPrefsProvider; import org.fdroid.fdroid.data.AppPrefsProvider;
import org.fdroid.fdroid.data.AppProvider; 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.data.Schema;
import org.fdroid.fdroid.installer.InstallManagerService; import org.fdroid.fdroid.installer.InstallManagerService;
import org.fdroid.fdroid.installer.Installer; import org.fdroid.fdroid.installer.Installer;
@ -740,40 +735,6 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
installApk(apkToInstall); 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 @Override
public void uninstallApk() { public void uninstallApk() {
Apk apk = app.installedApk; Apk apk = app.installedApk;
@ -783,7 +744,10 @@ public class AppDetails2 extends AppCompatActivity implements ShareChooserDialog
apk = app.getMediaApkifInstalled(getApplicationContext()); apk = app.getMediaApkifInstalled(getApplicationContext());
if (apk == null) { if (apk == null) {
// When the app isn't a media file - the above workaround refers to this. // 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; app.installedApk = apk;
} }

View File

@ -796,6 +796,37 @@ public class App extends ValueObject implements Comparable<App>, Parcelable {
apk.sig = Utils.hashBytes(fdroidSig, "md5"); 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() { public boolean isValid() {
if (TextUtils.isEmpty(this.name) if (TextUtils.isEmpty(this.name)
|| TextUtils.isEmpty(this.packageName)) { || TextUtils.isEmpty(this.packageName)) {