Skip to content

Commit

Permalink
feat: add auth ACCESS_RESTRICTED_SETTINGS
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 27, 2024
1 parent 78bb83f commit 8f0abb0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
40 changes: 25 additions & 15 deletions app/src/main/kotlin/li/songe/gkd/shizuku/ShizukuApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.app.ActivityManager
import android.app.IActivityTaskManager
import android.content.ComponentName
import android.content.ServiceConnection
import android.content.pm.IPackageManager
import android.content.pm.PackageManager
import android.os.IBinder
import android.view.Display
Expand Down Expand Up @@ -38,6 +37,7 @@ fun shizukuCheckGranted(): Boolean {
val granted = try {
Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED
} catch (e: Exception) {
e.printStackTrace()
false
}
if (!granted) return false
Expand All @@ -51,6 +51,7 @@ fun shizukuCheckActivity(): Boolean {
return (try {
newActivityTaskManager()?.safeGetTasks(log = false)?.isNotEmpty() == true
} catch (e: Exception) {
e.printStackTrace()
false
})
}
Expand Down Expand Up @@ -138,18 +139,19 @@ fun safeGetTopActivity(): TopActivity? {
val top = taskManager.safeGetTasks()?.lastOrNull()?.topActivity ?: return null
return TopActivity(appId = top.packageName, activityId = top.className)
} catch (e: Exception) {
e.printStackTrace()
return null
}
}

fun newPackageManager(): IPackageManager? {
val service = SystemServiceHelper.getSystemService("package")
if (service == null) {
LogUtils.d("shizuku 无法获取 package")
return null
}
return service.let(::ShizukuBinderWrapper).let(IPackageManager.Stub::asInterface)
}
//fun newPackageManager(): IPackageManager? {
// val service = SystemServiceHelper.getSystemService("package")
// if (service == null) {
// LogUtils.d("shizuku 无法获取 package")
// return null
// }
// return service.let(::ShizukuBinderWrapper).let(IPackageManager.Stub::asInterface)
//}

data class UserServiceWrapper(
val userService: IUserService,
Expand Down Expand Up @@ -202,15 +204,23 @@ private suspend fun serviceWrapper(): UserServiceWrapper = suspendCoroutine { co
Shizuku.bindUserService(serviceArgs, connection)
}

suspend fun execCommandForResult(command: String): Boolean {
serviceWrapperFlow.value?.userService?.let {
return it.execCommandForResult(command) == true
}
val wrapper = serviceWrapper()
return try {
wrapper.userService.execCommandForResult(command) == true
} finally {
wrapper.destroy()
}
}

suspend fun shizukuCheckUserService(): Boolean {
return safeTap(0f, 0f) == true || try {
val wrapper = serviceWrapper()
try {
wrapper.userService.execCommandForResult("input tap 0 0") == true
} finally {
wrapper.destroy()
}
execCommandForResult("input tap 0 0")
} catch (e: Exception) {
e.printStackTrace()
false
}
}
Expand Down
35 changes: 17 additions & 18 deletions app/src/main/kotlin/li/songe/gkd/ui/AuthA11yPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ import com.blankj.utilcode.util.LogUtils
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.update
import li.songe.gkd.META
import li.songe.gkd.MainActivity
import li.songe.gkd.permission.shizukuOkState
import li.songe.gkd.permission.writeSecureSettingsState
import li.songe.gkd.service.A11yService
import li.songe.gkd.service.fixRestartService
import li.songe.gkd.shizuku.newPackageManager
import li.songe.gkd.shizuku.execCommandForResult
import li.songe.gkd.ui.component.updateDialogOptions
import li.songe.gkd.ui.style.itemHorizontalPadding
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.launchAsFn
import li.songe.gkd.util.openA11ySettings
import li.songe.gkd.util.openUri
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.throttle
import li.songe.gkd.util.toast
import rikka.shizuku.Shizuku
Expand Down Expand Up @@ -269,24 +270,22 @@ fun AuthA11yPage() {
}
}

private val commandText by lazy { "adb shell pm grant ${META.appId} android.permission.WRITE_SECURE_SETTINGS" }
private val innerCommandText by lazy { "pm grant ${META.appId} android.permission.WRITE_SECURE_SETTINGS; appops set ${META.appId} ACCESS_RESTRICTED_SETTINGS allow" }
private val commandText by lazy { "adb shell \"${innerCommandText}\"" }

private fun successAuthExec() {
if (writeSecureSettingsState.updateAndGet()) {
toast("授权成功")
storeFlow.update { it.copy(enableService = true) }
fixRestartService()
}
}

private suspend fun MainActivity.grantPermissionByShizuku() {
if (shizukuOkState.stateFlow.value) {
try {
val manager = newPackageManager()
if (manager != null) {
manager.grantRuntimePermission(
META.appId,
"android.permission.WRITE_SECURE_SETTINGS",
0, // maybe others
)
delay(500)
if (writeSecureSettingsState.updateAndGet()) {
toast("授权成功")
fixRestartService()
}
}
execCommandForResult(innerCommandText)
successAuthExec()
} catch (e: Exception) {
toast("授权失败:${e.message}")
LogUtils.d(e)
Expand All @@ -308,12 +307,12 @@ private fun grantPermissionByRoot() {
try {
p = Runtime.getRuntime().exec("su")
val o = DataOutputStream(p.outputStream)
o.writeBytes("pm grant ${META.appId} android.permission.WRITE_SECURE_SETTINGS\nexit\n")
o.writeBytes("${innerCommandText}\nexit\n")
o.flush()
o.close()
p.waitFor()
if (p.exitValue() == 0) {
toast("授权成功")
successAuthExec()
}
} catch (e: Exception) {
toast("授权失败:${e.message}")
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/AuthA11yVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class AuthA11yVm : ViewModel() {
init {
viewModelScope.launch {
while (isActive) {
if (writeSecureSettingsState.updateAndGet()) {
break
}
writeSecureSettingsState.updateAndGet()
delay(1000)
}
}
Expand Down

0 comments on commit 8f0abb0

Please sign in to comment.