use standard method for basic installed app queries

Makes easy to track where these lookups are happening, and hopefully
simplifies the code a bit.
This commit is contained in:
Hans-Christoph Steiner 2018-04-11 11:34:09 +02:00
parent 1e6fb13ebc
commit 1413c35342
4 changed files with 27 additions and 27 deletions

View File

@ -56,7 +56,6 @@ public class AppUpdateStatusService extends IntentService {
if (cacheDirList == null) { if (cacheDirList == null) {
return; return;
} }
PackageManager packageManager = getPackageManager();
List<Apk> apksReadyToInstall = new ArrayList<>(); List<Apk> apksReadyToInstall = new ArrayList<>();
for (String repoDirName : cacheDirList) { for (String repoDirName : cacheDirList) {
File repoDir = new File(cacheDir, repoDirName); File repoDir = new File(cacheDir, repoDirName);
@ -67,12 +66,7 @@ public class AppUpdateStatusService extends IntentService {
for (String apkFileName : apks) { for (String apkFileName : apks) {
Apk apk = processDownloadedApk(new File(repoDir, apkFileName)); Apk apk = processDownloadedApk(new File(repoDir, apkFileName));
if (apk != null) { if (apk != null) {
PackageInfo packageInfo = null; PackageInfo packageInfo = Utils.getPackageInfo(this, apk.packageName);
try {
packageInfo = packageManager.getPackageInfo(apk.packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
// ignored
}
if (packageInfo == null || packageInfo.versionCode != apk.versionCode) { if (packageInfo == null || packageInfo.versionCode != apk.versionCode) {
Utils.debugLog(TAG, "Marking downloaded apk " + apk.apkName + " as ReadyToInstall"); Utils.debugLog(TAG, "Marking downloaded apk " + apk.apkName + " as ReadyToInstall");
apksReadyToInstall.add(apk); apksReadyToInstall.add(apk);
@ -134,8 +128,8 @@ public class AppUpdateStatusService extends IntentService {
return null; return null;
} }
try { PackageInfo info = Utils.getPackageInfo(this, downloadedApk.packageName);
PackageInfo info = getPackageManager().getPackageInfo(downloadedApk.packageName, 0); if (info != null) {
File pathToInstalled = InstalledAppProviderService.getPathToInstalledApk(info); File pathToInstalled = InstalledAppProviderService.getPathToInstalledApk(info);
if (pathToInstalled != null && pathToInstalled.canRead() && if (pathToInstalled != null && pathToInstalled.canRead() &&
pathToInstalled.length() == downloadedApk.size && // Check size before hash for performance. pathToInstalled.length() == downloadedApk.size && // Check size before hash for performance.
@ -145,7 +139,6 @@ public class AppUpdateStatusService extends IntentService {
AppUpdateStatusManager.getInstance(this).markAsNoLongerPendingInstall(downloadedApk.getUrl()); AppUpdateStatusManager.getInstance(this).markAsNoLongerPendingInstall(downloadedApk.getUrl());
return null; return null;
} }
} catch (PackageManager.NameNotFoundException ignored) {
} }
Utils.debugLog(TAG, downloadedApk.packageName + ':' + downloadedApk.versionCode Utils.debugLog(TAG, downloadedApk.packageName + ':' + downloadedApk.versionCode

View File

@ -26,7 +26,6 @@ import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -450,16 +449,9 @@ public class RepoUpdater {
* per repo basis. * per repo basis.
*/ */
void processRepoPushRequests() { void processRepoPushRequests() {
PackageManager pm = context.getPackageManager();
for (RepoPushRequest repoPushRequest : repoPushRequestList) { for (RepoPushRequest repoPushRequest : repoPushRequestList) {
String packageName = repoPushRequest.packageName; String packageName = repoPushRequest.packageName;
PackageInfo packageInfo = null; PackageInfo packageInfo = Utils.getPackageInfo(context, packageName);
try {
packageInfo = pm.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
// ignored
}
if (RepoPushRequest.INSTALL.equals(repoPushRequest.request)) { if (RepoPushRequest.INSTALL.equals(repoPushRequest.request)) {
ContentResolver cr = context.getContentResolver(); ContentResolver cr = context.getContentResolver();

View File

@ -19,6 +19,7 @@
package org.fdroid.fdroid; package org.fdroid.fdroid;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
@ -668,7 +669,12 @@ public final class Utils {
} }
} }
// Try to get the version name of the client. Return null on failure. /**
* Try to get the {@link PackageInfo#versionName} of the
* client.
*
* @return null on failure
*/
public static String getVersionName(Context context) { public static String getVersionName(Context context) {
String versionName = null; String versionName = null;
try { try {
@ -680,6 +686,20 @@ public final class Utils {
return versionName; return versionName;
} }
/**
* Try to get the {@link PackageInfo} for the {@code packageName} provided.
*
* @return null on failure
*/
public static PackageInfo getPackageInfo(Context context, String packageName) {
try {
return context.getPackageManager().getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
debugLog(TAG, "Could not get PackageInfo: ", e);
}
return null;
}
/** /**
* Useful for debugging during development, so that arbitrary queries can be made, and their * Useful for debugging during development, so that arbitrary queries can be made, and their
* results inspected in the debugger. * results inspected in the debugger.

View File

@ -4,10 +4,8 @@ import android.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
/** /**
@ -38,12 +36,9 @@ public class ObbUrlActivity extends Activity {
String packageName = componentName.getPackageName(); String packageName = componentName.getPackageName();
Apk apk = null; Apk apk = null;
try { PackageInfo packageInfo = Utils.getPackageInfo(this, packageName);
PackageManager pm = getPackageManager(); if (packageInfo != null) {
PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
apk = ApkProvider.Helper.findApkFromAnyRepo(this, packageName, packageInfo.versionCode); apk = ApkProvider.Helper.findApkFromAnyRepo(this, packageName, packageInfo.versionCode);
} catch (PackageManager.NameNotFoundException e) {
Utils.debugLog(TAG, e.getLocalizedMessage());
} }
if (apk == null) { if (apk == null) {