use standard code style and var naming patterns for AppDiff

The standard pattern is to pass a Context in rather than call things like
getPackageManager in.  It should only pass a PackageManager if that is
actually being reused.

This shouldn't change the logic at all.
This commit is contained in:
Hans-Christoph Steiner 2018-04-06 11:26:26 +02:00
parent 98aea01272
commit 7ba1966538
4 changed files with 43 additions and 39 deletions

View File

@ -106,15 +106,23 @@ public abstract class Installer {
return intent;
}
/**
* Return if this installation process has any new permissions that the user
* should be aware of. Starting in {@code android-23}, all new permissions
* are requested when they are used, and the permissions prompt at time of
* install is not used. All permissions in a new install are considered new.
*
* @return the number of new permissions
*/
private int newPermissionCount() {
boolean supportsRuntimePermissions = apk.targetSdkVersion >= 23;
if (supportsRuntimePermissions) {
return 0;
}
AppDiff appDiff = new AppDiff(context.getPackageManager(), apk);
AppSecurityPermissions perms = new AppSecurityPermissions(context, appDiff.pkgInfo);
if (appDiff.installedAppInfo != null) {
AppDiff appDiff = new AppDiff(context, apk);
AppSecurityPermissions perms = new AppSecurityPermissions(context, appDiff.apkPackageInfo);
if (appDiff.installedApplicationInfo != null) {
// update to an existing app
return perms.getPermissionCount(AppSecurityPermissions.WHICH_NEW);
}

View File

@ -18,55 +18,51 @@
package org.fdroid.fdroid.privileged.views;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import org.fdroid.fdroid.data.Apk;
/**
* Represents the permissions difference between the installed APK, and the
* update APK represented by {@link Apk}.
*/
public class AppDiff {
private final PackageManager pm;
public final PackageInfo pkgInfo;
public ApplicationInfo installedAppInfo;
public final PackageInfo apkPackageInfo;
public final ApplicationInfo installedApplicationInfo;
/**
* Constructor based on F-Droids Apk object
*/
public AppDiff(PackageManager pm, Apk apk) {
this.pm = pm;
public AppDiff(Context context, Apk apk) {
PackageManager pm = context.getPackageManager();
apkPackageInfo = new PackageInfo();
apkPackageInfo.packageName = apk.packageName;
apkPackageInfo.applicationInfo = new ApplicationInfo();
apkPackageInfo.requestedPermissions = apk.requestedPermissions;
pkgInfo = new PackageInfo();
pkgInfo.packageName = apk.packageName;
pkgInfo.applicationInfo = new ApplicationInfo();
pkgInfo.requestedPermissions = apk.requestedPermissions;
init();
}
private void init() {
String pkgName = pkgInfo.packageName;
String packageName = apkPackageInfo.packageName;
// Check if there is already a package on the device with this name
// but it has been renamed to something else.
final String[] oldName = pm.canonicalToCurrentPackageNames(new String[]{pkgName});
final String[] oldName = pm.canonicalToCurrentPackageNames(new String[]{packageName});
if (oldName != null && oldName.length > 0 && oldName[0] != null) {
pkgName = oldName[0];
pkgInfo.packageName = pkgName;
pkgInfo.applicationInfo.packageName = pkgName;
packageName = oldName[0];
apkPackageInfo.packageName = packageName;
apkPackageInfo.applicationInfo.packageName = packageName;
}
// Check if package is already installed
ApplicationInfo applicationInfo;
try {
// This is a little convoluted because we want to get all uninstalled
// apps, but this may include apps with just data, and if it is just
// data we still want to count it as "installed".
//noinspection WrongConstant (lint is actually wrong here!)
installedAppInfo = pm.getApplicationInfo(pkgName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if ((installedAppInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
installedAppInfo = null;
applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
if ((applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
applicationInfo = null;
}
} catch (PackageManager.NameNotFoundException e) {
installedAppInfo = null;
applicationInfo = null;
}
installedApplicationInfo = applicationInfo;
}
}

View File

@ -92,9 +92,9 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel
scrollView = null;
okCanInstall = false;
int msg = 0;
AppSecurityPermissions perms = new AppSecurityPermissions(this, appDiff.pkgInfo);
if (appDiff.installedAppInfo != null) {
msg = (appDiff.installedAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
AppSecurityPermissions perms = new AppSecurityPermissions(this, appDiff.apkPackageInfo);
if (appDiff.installedApplicationInfo != null) {
msg = (appDiff.installedApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
? R.string.install_confirm_update_system
: R.string.install_confirm_update;
scrollView = new CaffeinatedScrollView(this);
@ -131,10 +131,10 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel
}
if (!permVisible) {
if (appDiff.installedAppInfo != null) {
if (appDiff.installedApplicationInfo != null) {
// This is an update to an application, but there are no
// permissions at all.
msg = (appDiff.installedAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
msg = (appDiff.installedApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
? R.string.install_confirm_update_system_no_perms
: R.string.install_confirm_update_no_perms;
} else {
@ -182,7 +182,7 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel
app = AppProvider.Helper.findSpecificApp(getContentResolver(),
apk.packageName, apk.repoId, Schema.AppMetadataTable.Cols.ALL);
appDiff = new AppDiff(getPackageManager(), apk);
appDiff = new AppDiff(this, apk);
setContentView(R.layout.install_start);

View File

@ -789,8 +789,8 @@ public class AppDetailsRecyclerViewAdapter
headerView.setText(R.string.permissions);
updateExpandableItem(false);
contentView.removeAllViews();
AppDiff appDiff = new AppDiff(context.getPackageManager(), versions.get(0));
AppSecurityPermissions perms = new AppSecurityPermissions(context, appDiff.pkgInfo);
AppDiff appDiff = new AppDiff(context, versions.get(0));
AppSecurityPermissions perms = new AppSecurityPermissions(context, appDiff.apkPackageInfo);
contentView.addView(perms.getPermissionsView(AppSecurityPermissions.WHICH_ALL));
}