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 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() { private int newPermissionCount() {
boolean supportsRuntimePermissions = apk.targetSdkVersion >= 23; boolean supportsRuntimePermissions = apk.targetSdkVersion >= 23;
if (supportsRuntimePermissions) { if (supportsRuntimePermissions) {
return 0; return 0;
} }
AppDiff appDiff = new AppDiff(context.getPackageManager(), apk); AppDiff appDiff = new AppDiff(context, apk);
AppSecurityPermissions perms = new AppSecurityPermissions(context, appDiff.pkgInfo); AppSecurityPermissions perms = new AppSecurityPermissions(context, appDiff.apkPackageInfo);
if (appDiff.installedAppInfo != null) { if (appDiff.installedApplicationInfo != null) {
// update to an existing app // update to an existing app
return perms.getPermissionCount(AppSecurityPermissions.WHICH_NEW); return perms.getPermissionCount(AppSecurityPermissions.WHICH_NEW);
} }

View File

@ -18,55 +18,51 @@
package org.fdroid.fdroid.privileged.views; package org.fdroid.fdroid.privileged.views;
import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import org.fdroid.fdroid.data.Apk; 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 { public class AppDiff {
private final PackageManager pm; public final PackageInfo apkPackageInfo;
public final PackageInfo pkgInfo; public final ApplicationInfo installedApplicationInfo;
public ApplicationInfo installedAppInfo;
/** public AppDiff(Context context, Apk apk) {
* Constructor based on F-Droids Apk object PackageManager pm = context.getPackageManager();
*/ apkPackageInfo = new PackageInfo();
public AppDiff(PackageManager pm, Apk apk) { apkPackageInfo.packageName = apk.packageName;
this.pm = pm; apkPackageInfo.applicationInfo = new ApplicationInfo();
apkPackageInfo.requestedPermissions = apk.requestedPermissions;
pkgInfo = new PackageInfo(); String packageName = apkPackageInfo.packageName;
pkgInfo.packageName = apk.packageName;
pkgInfo.applicationInfo = new ApplicationInfo();
pkgInfo.requestedPermissions = apk.requestedPermissions;
init();
}
private void init() {
String pkgName = pkgInfo.packageName;
// Check if there is already a package on the device with this name // Check if there is already a package on the device with this name
// but it has been renamed to something else. // 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) { if (oldName != null && oldName.length > 0 && oldName[0] != null) {
pkgName = oldName[0]; packageName = oldName[0];
pkgInfo.packageName = pkgName; apkPackageInfo.packageName = packageName;
pkgInfo.applicationInfo.packageName = pkgName; apkPackageInfo.applicationInfo.packageName = packageName;
} }
// Check if package is already installed // Check if package is already installed
ApplicationInfo applicationInfo;
try { try {
// This is a little convoluted because we want to get all uninstalled // 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 // apps, but this may include apps with just data, and if it is just
// data we still want to count it as "installed". // data we still want to count it as "installed".
//noinspection WrongConstant (lint is actually wrong here!) //noinspection WrongConstant (lint is actually wrong here!)
installedAppInfo = pm.getApplicationInfo(pkgName, applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
PackageManager.GET_UNINSTALLED_PACKAGES); if ((applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
if ((installedAppInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { applicationInfo = null;
installedAppInfo = null;
} }
} catch (PackageManager.NameNotFoundException e) { } 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; scrollView = null;
okCanInstall = false; okCanInstall = false;
int msg = 0; int msg = 0;
AppSecurityPermissions perms = new AppSecurityPermissions(this, appDiff.pkgInfo); AppSecurityPermissions perms = new AppSecurityPermissions(this, appDiff.apkPackageInfo);
if (appDiff.installedAppInfo != null) { if (appDiff.installedApplicationInfo != null) {
msg = (appDiff.installedAppInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 msg = (appDiff.installedApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
? R.string.install_confirm_update_system ? R.string.install_confirm_update_system
: R.string.install_confirm_update; : R.string.install_confirm_update;
scrollView = new CaffeinatedScrollView(this); scrollView = new CaffeinatedScrollView(this);
@ -131,10 +131,10 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel
} }
if (!permVisible) { if (!permVisible) {
if (appDiff.installedAppInfo != null) { if (appDiff.installedApplicationInfo != null) {
// This is an update to an application, but there are no // This is an update to an application, but there are no
// permissions at all. // 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_system_no_perms
: R.string.install_confirm_update_no_perms; : R.string.install_confirm_update_no_perms;
} else { } else {
@ -182,7 +182,7 @@ public class InstallConfirmActivity extends FragmentActivity implements OnCancel
app = AppProvider.Helper.findSpecificApp(getContentResolver(), app = AppProvider.Helper.findSpecificApp(getContentResolver(),
apk.packageName, apk.repoId, Schema.AppMetadataTable.Cols.ALL); apk.packageName, apk.repoId, Schema.AppMetadataTable.Cols.ALL);
appDiff = new AppDiff(getPackageManager(), apk); appDiff = new AppDiff(this, apk);
setContentView(R.layout.install_start); setContentView(R.layout.install_start);

View File

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