From 5269ce0dc2eaa8d7bc4fcb6cd64dfa94c6b6f1c1 Mon Sep 17 00:00:00 2001 From: Tim Schneeberger Date: Thu, 12 Dec 2024 18:44:03 +0100 Subject: [PATCH] fix: crash during Shizuku setup on Android 15 fixed --- .../ShizukuSystemServerApi.java | 29 +++++++++++++++++-- .../permission/IPermissionManager.java | 7 +++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hidden-api-impl/src/main/java/me/timschneeberger/hiddenapi_impl/ShizukuSystemServerApi.java b/hidden-api-impl/src/main/java/me/timschneeberger/hiddenapi_impl/ShizukuSystemServerApi.java index 208a9cf72..e197a99e4 100644 --- a/hidden-api-impl/src/main/java/me/timschneeberger/hiddenapi_impl/ShizukuSystemServerApi.java +++ b/hidden-api-impl/src/main/java/me/timschneeberger/hiddenapi_impl/ShizukuSystemServerApi.java @@ -9,9 +9,12 @@ import com.android.internal.app.IAppOpsService; +import java.io.OutputStream; import java.lang.reflect.Method; +import rikka.shizuku.Shizuku; import rikka.shizuku.ShizukuBinderWrapper; +import rikka.shizuku.ShizukuRemoteProcess; import rikka.shizuku.SystemServiceHelper; public class ShizukuSystemServerApi { @@ -45,10 +48,14 @@ protected IAudioPolicyService create() { public static void PermissionManager_grantRuntimePermission(String packageName, String permissionName, int userId) { try { - if (Build.VERSION.SDK_INT >= 34) { + if (Build.VERSION.SDK_INT >= 35) { + PERMISSION_MANAGER.getOrThrow().grantRuntimePermission(packageName, permissionName, "default:0", userId); + } + else if (Build.VERSION.SDK_INT == 34) { try { PERMISSION_MANAGER.getOrThrow().grantRuntimePermission(packageName, permissionName, 0, userId); - }catch (NoSuchMethodError e) { + } + catch (NoSuchMethodError e) { PERMISSION_MANAGER.getOrThrow().grantRuntimePermission(packageName, permissionName, userId); } } else { @@ -57,9 +64,27 @@ public static void PermissionManager_grantRuntimePermission(String packageName, } catch(Exception ex) { Log.e("ShizukuSystemServerApi", "Failed to call app ops service"); + exec("pm grant " + packageName + " " + permissionName); + } + } + + private static synchronized void exec(String cmd) { + try { + Method newProcess = Shizuku.class.getDeclaredMethod("newProcess", String[].class, String[].class, String.class); + newProcess.setAccessible(true); + ShizukuRemoteProcess process = (ShizukuRemoteProcess) newProcess.invoke(null, new String[]{"sh"}, null, null); + assert process != null; + OutputStream outputStream = process.getOutputStream(); + outputStream.write((cmd + "\nexit\n").getBytes()); + outputStream.flush(); + outputStream.close(); + process.waitFor(); + } catch (Exception e) { + Log.e("ShizukuSystemServerApi", "Failed to call cmd via exec"); } } + public static final String APP_OPS_MODE_ALLOW = "allow"; public static final String APP_OPS_MODE_IGNORE = "ignore"; public static final String APP_OPS_MODE_DENY = "deny"; diff --git a/hidden-api-stubs/src/main/java/android/permission/IPermissionManager.java b/hidden-api-stubs/src/main/java/android/permission/IPermissionManager.java index 681bf87d3..ccf369bde 100644 --- a/hidden-api-stubs/src/main/java/android/permission/IPermissionManager.java +++ b/hidden-api-stubs/src/main/java/android/permission/IPermissionManager.java @@ -5,13 +5,20 @@ import android.os.IInterface; import android.os.RemoteException; +import androidx.annotation.RequiresApi; + public interface IPermissionManager extends IInterface { void grantRuntimePermission(String packageName, String permissionName, int userId) throws RemoteException; + @RequiresApi(34) void grantRuntimePermission(String packageName, String permissionName, int deviceId, int userId) throws RemoteException; + @RequiresApi(35) + void grantRuntimePermission(String packageName, String permissionName, String deviceId, int userId) + throws RemoteException; + void revokeRuntimePermission(String packageName, String permissionName, int userId, String reason) throws RemoteException;