Skip to content

Commit

Permalink
[Refactor] Fix retrieving and updating permission flags in Android 14…
Browse files Browse the repository at this point in the history
… r29 onwards

Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
  • Loading branch information
MuntashirAkon committed Mar 19, 2024
1 parent 884cc10 commit c7b93eb
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
import java.util.List;

import dev.rikka.tools.refine.Refine;
import io.github.muntashirakon.AppManager.BuildConfig;
import io.github.muntashirakon.AppManager.ipc.ProxyBinder;
import io.github.muntashirakon.AppManager.logs.Log;
import io.github.muntashirakon.AppManager.settings.Ops;
import io.github.muntashirakon.AppManager.utils.ExUtils;

public final class PermissionCompat {
public static final int FLAG_PERMISSION_NONE = 0;
Expand Down Expand Up @@ -279,7 +275,13 @@ public static int getPermissionFlags(@NonNull String permissionName,
@NonNull String packageName,
@UserIdInt int userId) throws SecurityException {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (Build.VERSION.SDK_INT >= 34) {
try {
return getPermissionManager().getPermissionFlags(packageName, permissionName, userId);
} catch (NoSuchMethodError e) {
return getPermissionManager().getPermissionFlags(packageName, permissionName, 0, userId);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return getPermissionManager().getPermissionFlags(packageName, permissionName, userId);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
return getPermissionManager().getPermissionFlags(permissionName, packageName, userId);
Expand Down Expand Up @@ -313,7 +315,15 @@ public static void updatePermissionFlags(@NonNull String permissionName,
boolean checkAdjustPolicyFlagPermission,
@UserIdInt int userId) throws RemoteException {
IPackageManager pm = PackageManagerCompat.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (Build.VERSION.SDK_INT >= 34) {
try {
getPermissionManager().updatePermissionFlags(packageName, permissionName, flagMask, flagValues,
checkAdjustPolicyFlagPermission, userId);
} catch (NoSuchMethodError e) {
getPermissionManager().updatePermissionFlags(packageName, permissionName, flagMask, flagValues,
checkAdjustPolicyFlagPermission, 0, userId);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
getPermissionManager().updatePermissionFlags(packageName, permissionName, flagMask, flagValues,
checkAdjustPolicyFlagPermission, userId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Expand All @@ -337,9 +347,14 @@ public static void grantPermission(@NonNull String packageName,
@UserIdInt int userId)
throws RemoteException {
IPackageManager pm = PackageManagerCompat.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
IPermissionManager permissionManager = getPermissionManager();
permissionManager.grantRuntimePermission(packageName, permissionName, userId);
if (Build.VERSION.SDK_INT >= 34) {
try {
getPermissionManager().grantRuntimePermission(packageName, permissionName, userId);
} catch (NoSuchMethodError e) {
getPermissionManager().grantRuntimePermission(packageName, permissionName, 0, userId);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getPermissionManager().grantRuntimePermission(packageName, permissionName, userId);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pm.grantRuntimePermission(packageName, permissionName, userId);
} else {
Expand Down Expand Up @@ -367,9 +382,14 @@ public static void revokePermission(@NonNull String packageName,
@UserIdInt int userId,
@Nullable String reason) throws RemoteException {
IPackageManager pm = PackageManagerCompat.getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
IPermissionManager permissionManager = getPermissionManager();
permissionManager.revokeRuntimePermission(packageName, permissionName, userId, reason);
if (Build.VERSION.SDK_INT >= 34) {
try {
getPermissionManager().revokeRuntimePermission(packageName, permissionName, userId, reason);
} catch (NoSuchMethodError e) {
getPermissionManager().revokeRuntimePermission(packageName, permissionName, 0, userId, reason);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getPermissionManager().revokeRuntimePermission(packageName, permissionName, userId, reason);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pm.revokeRuntimePermission(packageName, permissionName, userId);
} else {
Expand Down
72 changes: 72 additions & 0 deletions hiddenapi/src/main/java/android/permission/IPermissionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,34 @@ public interface IPermissionManager extends IInterface {

/**
* First two parameters are permuted since Android 12 (S)
*
* @deprecated Replaced in Android 14 r29 (Upside Down Cake) by {@link #getPermissionFlags(String, String, int, int)}
*/
@Deprecated
int getPermissionFlags(String permName, String packageName, int userId) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
int getPermissionFlags(String packageName, String permName, int deviceId, int userId) throws RemoteException;

/**
* First two parameters are permuted since Android 12 (S)
*
* @deprecated Replaced in Android 14 r29 (Upside Down Cake) by {@link #updatePermissionFlags(String, String, int, int, boolean, int, int)}
*/
@Deprecated
void updatePermissionFlags(String permName, String packageName, int flagMask,
int flagValues, boolean checkAdjustPolicyFlagPermission, int userId) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
void updatePermissionFlags(String packageName, String permName, int flagMask,
int flagValues, boolean checkAdjustPolicyFlagPermission, int deviceId, int userId) throws RemoteException;

void updatePermissionFlagsForAllApps(int flagMask, int flagValues, int userId) throws RemoteException;

/**
Expand Down Expand Up @@ -107,10 +126,31 @@ boolean addAllowlistedRestrictedPermission(String packageName, String permission
boolean removeAllowlistedRestrictedPermission(String packageName, String permissionName, int flags, int userId)
throws RemoteException;

/**
* @deprecated Replaced in Android 14 r29 (Upside Down Cake) by {@link #grantRuntimePermission(String, String, int, int)}
*/
@Deprecated
void grantRuntimePermission(String packageName, String permName, int userId) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
void grantRuntimePermission(String packageName, String permName, int deviceId, int userId) throws RemoteException;

/**
* @deprecated Replaced in Android 14 r29 (Upside Down Cake) by {@link #revokeRuntimePermission(String, String, int, int, String)}
*/
@Deprecated
void revokeRuntimePermission(String packageName, String permName, int userId, String reason) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
void revokeRuntimePermission(String packageName, String permName, int deviceId,
int userId, String reason) throws RemoteException;

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
void revokePostNotificationPermissionWithoutKillForTest(String packageName, int userId) throws RemoteException;

Expand Down Expand Up @@ -182,15 +222,35 @@ boolean removeAllowlistedRestrictedPermission(String packageName, String permiss

/**
* First two parameters are permuted since Android 12 (S)
*
* @deprecated Replaced in Android 14 r29 (Upside Down Cake) by {@link #shouldShowRequestPermissionRationale(String, String, int, int)}
*/
@Deprecated
boolean shouldShowRequestPermissionRationale(String permName, String packageName, int userId)
throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
boolean shouldShowRequestPermissionRationale(String packageName, String permName, int deviceId, int userId)
throws RemoteException;

/**
* First two parameters are permuted since Android 12 (S)
*
* @deprecated Replaced in Android 14 r29 (Upside Down Cake) by {@link #isPermissionRevokedByPolicy(String, String, int, int)}
*/
@Deprecated
boolean isPermissionRevokedByPolicy(String permName, String packageName, int userId) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
boolean isPermissionRevokedByPolicy(String packageName, String permName, int deviceId,
int userId) throws RemoteException;

List<SplitPermissionInfoParcelable> getSplitPermissions() throws RemoteException;

/**
Expand Down Expand Up @@ -237,6 +297,18 @@ void startOneTimePermissionSession(String packageName, int userId, long timeout,
@RequiresApi(Build.VERSION_CODES.S)
boolean isAutoRevokeExempted(String packageName, int userId) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
int checkPermission(String packageName, String permissionName, int deviceId, int userId) throws RemoteException;

/**
* Introduced in Android 14.0.0_r29
*/
@RequiresApi(34)
int checkUidPermission(int uid, String permissionName, int deviceId) throws RemoteException;

abstract class Stub extends Binder implements IPermissionManager {
public static IPermissionManager asInterface(IBinder binder) {
throw new UnsupportedOperationException();
Expand Down

0 comments on commit c7b93eb

Please sign in to comment.