PackageManagerCompat: Handle exceptions better
* Don't catch all exceptions, only what we expect. * Also re-format comments as javadoc
This commit is contained in:
parent
833ae329e4
commit
204ac3cca9
@ -5,9 +5,24 @@ import android.content.Context;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.fdroid.fdroid.Utils;
|
import org.fdroid.fdroid.Utils;
|
||||||
import org.fdroid.fdroid.installer.PrivilegedInstaller;
|
import org.fdroid.fdroid.installer.PrivilegedInstaller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starting with 7.0 (API 24), we're using PackageInstaller APIs
|
||||||
|
* to install and uninstall apps via the privileged extension.
|
||||||
|
* That enforces the uninstaller being the same as the installer,
|
||||||
|
* so set the package name to privileged extension if it is being used.
|
||||||
|
* <p>
|
||||||
|
* While setting the installer package name, many problems can occur,
|
||||||
|
* such as:
|
||||||
|
* <li> App wasn't installed due to incompatibility
|
||||||
|
* <li> User canceled install
|
||||||
|
* <li> Another app interfered in the process
|
||||||
|
* <li> Another app already set the target's installer package,
|
||||||
|
* which happens in the case where we fell back to {@link org.fdroid.fdroid.installer.DefaultInstaller}
|
||||||
|
*/
|
||||||
public class PackageManagerCompat {
|
public class PackageManagerCompat {
|
||||||
|
|
||||||
private static final String TAG = "PackageManagerCompat";
|
private static final String TAG = "PackageManagerCompat";
|
||||||
@ -16,25 +31,13 @@ public class PackageManagerCompat {
|
|||||||
public static void setInstaller(Context context, PackageManager mPm, String packageName) {
|
public static void setInstaller(Context context, PackageManager mPm, String packageName) {
|
||||||
if (Build.VERSION.SDK_INT < 11) return;
|
if (Build.VERSION.SDK_INT < 11) return;
|
||||||
try {
|
try {
|
||||||
/*
|
|
||||||
* Starting with 7.0 (API 24), we're using PackageInstaller APIs
|
|
||||||
* to install and uninstall apps via the privileged extension.
|
|
||||||
* That enforces the uninstaller being the same as the installer,
|
|
||||||
* so set the package name to that.
|
|
||||||
*/
|
|
||||||
if (Build.VERSION.SDK_INT >= 24 && PrivilegedInstaller.isDefault(context)) {
|
if (Build.VERSION.SDK_INT >= 24 && PrivilegedInstaller.isDefault(context)) {
|
||||||
mPm.setInstallerPackageName(packageName, "org.fdroid.fdroid.privileged");
|
mPm.setInstallerPackageName(packageName, "org.fdroid.fdroid.privileged");
|
||||||
} else {
|
} else {
|
||||||
mPm.setInstallerPackageName(packageName, "org.fdroid.fdroid");
|
mPm.setInstallerPackageName(packageName, "org.fdroid.fdroid");
|
||||||
}
|
}
|
||||||
Utils.debugLog(TAG, "Installer package name for " + packageName + " set successfully");
|
Utils.debugLog(TAG, "Installer package name for " + packageName + " set successfully");
|
||||||
} catch (Exception e) {
|
} catch (SecurityException | IllegalArgumentException e) {
|
||||||
// Many problems can occur:
|
|
||||||
// * App wasn't installed due to incompatibility
|
|
||||||
// * User canceled install
|
|
||||||
// * Another app interfered in the process
|
|
||||||
// * Another app already set the target's installer package
|
|
||||||
// * ...
|
|
||||||
Log.e(TAG, "Could not set installer package name for " +
|
Log.e(TAG, "Could not set installer package name for " +
|
||||||
packageName, e);
|
packageName, e);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user