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

View File

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

View File

@ -19,6 +19,7 @@
package org.fdroid.fdroid;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
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) {
String versionName = null;
try {
@ -680,6 +686,20 @@ public final class Utils {
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
* results inspected in the debugger.

View File

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