AppDetails2: bring back getInstalledApk() from AppDetails

* Addition: Try to get apk details via InstalledAppProvider too.
 * In certain cases, such as the "UnifiedNlp (no GAPPS)" app on a device
   with actualy GAPPS / GMS installed, apk can be null which leads
   to a crash
 * Ask InstalledAppProvider for the app's details too, like it
   was done in the old UX AppDetails.
 * Also seen when uninstalling app with a signing key different,
   fixes #985
This commit is contained in:
Chirayu Desai 2017-04-18 18:36:39 +05:30
parent 11c42f6a2f
commit ef6c03c45d

View File

@ -34,6 +34,7 @@ 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.design.widget.AppBarLayout; import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
@ -56,6 +57,8 @@ 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;
@ -683,20 +686,48 @@ 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;
if (apk == null) { if (apk == null) {
// TODO ideally, app would be refreshed immediately after install, then this // TODO ideally, app would be refreshed immediately after install, then this
// workaround would be unnecessary // workaround would be unnecessary
try { apk = getInstalledApk();
PackageInfo pi = getPackageManager().getPackageInfo(app.packageName, 0); app.installedApk = apk;
apk = ApkProvider.Helper.findApkFromAnyRepo(this, pi.packageName, pi.versionCode);
app.installedApk = apk;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return; // not installed
}
} }
Installer installer = InstallerFactory.create(this, apk); Installer installer = InstallerFactory.create(this, apk);
Intent intent = installer.getUninstallScreen(); Intent intent = installer.getUninstallScreen();