Skip to content

Commit

Permalink
ShizukuSystemApis use rikka.hidden.compat classes
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Mar 10, 2024
1 parent 5299ece commit 909eb43
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 55 deletions.
1 change: 1 addition & 0 deletions manager/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ dependencies {
annotationProcessor libs.refine.annotation.processor
implementation libs.refine.annotation

implementation libs.hidden.compat
compileOnly libs.hidden.stub

implementation 'androidx.browser:browser:1.5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,5 @@ class AdbPairingTutorialActivity : AppBarActivity() {
startService(intent)
}
}

val mode = getSystemService(AppOpsManager::class.java)
.noteOpNoThrow("android:start_foreground", android.os.Process.myUid(), packageName, null, null)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object AuthorizationManager {
val allPackages: MutableList<PackageInfo> = ArrayList()
for (user in ShizukuSystemApis.getUsers(useCache = false)) {
try {
allPackages.addAll(ShizukuSystemApis.getInstalledPackages(PackageManager.GET_META_DATA or PackageManager.GET_PERMISSIONS, user.id))
allPackages.addAll(ShizukuSystemApis.getInstalledPackages((PackageManager.GET_META_DATA or PackageManager.GET_PERMISSIONS).toLong(), user.id))
} catch (e: Throwable) {
LOGGER.w(e, "getInstalledPackages")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package moe.shizuku.manager.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.*
import android.os.Build
import android.os.IUserManager
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.ParceledListSlice
import android.os.RemoteException
import android.permission.IPermissionManager
import rikka.core.ktx.unsafeLazy
import rikka.hidden.compat.PackageManagerApis
import rikka.hidden.compat.PermissionManagerApis
import rikka.hidden.compat.UserManagerApis
import rikka.hidden.compat.util.SystemServiceBinder
import rikka.shizuku.Shizuku
import rikka.shizuku.ShizukuBinderWrapper
import rikka.shizuku.SystemServiceHelper
import java.util.*

object ShizukuSystemApis {

private val packageManager by unsafeLazy {
IPackageManager.Stub.asInterface(ShizukuBinderWrapper(SystemServiceHelper.getSystemService("package")))
}

@delegate:SuppressLint("NewApi")
private val permissionManager by unsafeLazy {
IPermissionManager.Stub.asInterface(ShizukuBinderWrapper(SystemServiceHelper.getSystemService("permissionmgr")))
}

private val userManager by unsafeLazy {
IUserManager.Stub.asInterface(ShizukuBinderWrapper(SystemServiceHelper.getSystemService(Context.USER_SERVICE)))
init {
SystemServiceBinder.setOnGetBinderListener {
return@setOnGetBinderListener ShizukuBinderWrapper(it)
}
}

private val users = arrayListOf<UserInfoCompat>()
Expand All @@ -34,11 +25,7 @@ object ShizukuSystemApis {
return if (!Shizuku.pingBinder()) {
arrayListOf(UserInfoCompat(UserHandleCompat.myUserId(), "Owner"))
} else try {
val list = try {
userManager.getUsers(true, true, true)
} catch (e: NoSuchMethodError) {
userManager.getUsers(true)
}
val list = UserManagerApis.getUsers(true, true, true)
val users: MutableList<UserInfoCompat> = ArrayList<UserInfoCompat>()
for (ui in list) {
users.add(UserInfoCompat(ui.id, ui.name))
Expand All @@ -60,14 +47,21 @@ object ShizukuSystemApis {
}

fun getUserInfo(userId: Int): UserInfoCompat {
return getUsers(useCache = true).firstOrNull { it.id == userId } ?: UserInfoCompat(UserHandleCompat.myUserId(), "Unknown")
return getUsers(useCache = true).firstOrNull { it.id == userId } ?: UserInfoCompat(
UserHandleCompat.myUserId(),
"Unknown"
)
}

fun getInstalledPackages(flags: Int, userId: Int): List<PackageInfo> {
fun getInstalledPackages(flags: Long, userId: Int): List<PackageInfo> {
return if (!Shizuku.pingBinder()) {
ArrayList()
} else try {
val listSlice: ParceledListSlice<PackageInfo>? = packageManager.getInstalledPackages(flags, userId) as ParceledListSlice<PackageInfo>?
val listSlice: ParceledListSlice<PackageInfo>? =
PackageManagerApis.getInstalledPackages(
flags,
userId
)
return if (listSlice != null) {
listSlice.list
} else ArrayList()
Expand All @@ -80,13 +74,7 @@ object ShizukuSystemApis {
return if (!Shizuku.pingBinder()) {
PackageManager.PERMISSION_DENIED
} else try {
if (Build.VERSION.SDK_INT >= 31 || Build.VERSION.SDK_INT == 30 && Build.VERSION.PREVIEW_SDK_INT > 0) {
packageManager.checkPermission(permName, pkgName, userId)
} else if (Build.VERSION.SDK_INT >= 30) {
permissionManager.checkPermission(permName, pkgName, userId)
} else {
packageManager.checkPermission(permName, pkgName, userId)
}
PermissionManagerApis.checkPermission(permName, pkgName, userId)
} catch (tr: RemoteException) {
throw RuntimeException(tr.message, tr)
}
Expand All @@ -97,11 +85,7 @@ object ShizukuSystemApis {
return
}
try {
if (Build.VERSION.SDK_INT >= 30) {
permissionManager.grantRuntimePermission(packageName, permissionName, userId)
} else {
packageManager.grantRuntimePermission( packageName, permissionName, userId)
}
PermissionManagerApis.grantRuntimePermission(packageName, permissionName, userId)
} catch (tr: RemoteException) {
throw RuntimeException(tr.message, tr)
}
Expand All @@ -112,11 +96,7 @@ object ShizukuSystemApis {
return
}
try {
if (Build.VERSION.SDK_INT >= 30) {
permissionManager.revokeRuntimePermission(packageName, permissionName, userId, null)
} else {
packageManager.revokeRuntimePermission(packageName, permissionName, userId)
}
PermissionManagerApis.revokeRuntimePermission(packageName, permissionName, userId)
} catch (tr: RemoteException) {
throw RuntimeException(tr.message, tr)
}
Expand Down
10 changes: 5 additions & 5 deletions server/src/main/java/rikka/shizuku/server/ShizukuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,15 @@ public void dispatchPermissionConfirmationResult(int requestUid, int requestPid,

int deviceId = 0;//Context.DEVICE_ID_DEFAULT
if (allowed) {
PermissionManagerApis.grantRuntimePermission(packageName, PERMISSION, deviceId, userId);
PermissionManagerApis.grantRuntimePermission(packageName, PERMISSION, userId);
} else {
PermissionManagerApis.revokeRuntimePermission(packageName, PERMISSION, deviceId, userId);
PermissionManagerApis.revokeRuntimePermission(packageName, PERMISSION, userId);
}
}
}
}

private int getFlagsForUidInternal(int uid, int mask, boolean allowRuntimePermission) {
private int getFlagsForUidInternal(int uid, int mask, boolean allowRuntimePermission) {
ShizukuConfig.PackageEntry entry = configManager.find(uid);
if (entry != null) {
return entry.flags & mask;
Expand Down Expand Up @@ -393,9 +393,9 @@ public void updateFlagsForUid(int uid, int mask, int value) throws RemoteExcepti

int deviceId = 0;//Context.DEVICE_ID_DEFAULT
if (allowed) {
PermissionManagerApis.grantRuntimePermission(packageName, PERMISSION, deviceId, userId);
PermissionManagerApis.grantRuntimePermission(packageName, PERMISSION, userId);
} else {
PermissionManagerApis.revokeRuntimePermission(packageName, PERMISSION, deviceId, userId);
PermissionManagerApis.revokeRuntimePermission(packageName, PERMISSION, userId);
}

// TODO kill user service using
Expand Down

0 comments on commit 909eb43

Please sign in to comment.