From 06d40d79b673a4601a9c249c9b43cae5d2e589f6 Mon Sep 17 00:00:00 2001 From: iamr0s <271257581@qq.com> Date: Mon, 19 Aug 2024 20:49:42 +0800 Subject: [PATCH] pref: clear delegated scopes --- .../dhizuku/data/common/util/ContextUtil.kt | 21 +----------- .../settings/model/room/impl/AppRepoImpl.kt | 33 +++++++++++++++++-- .../com/rosan/dhizuku/server/DhizukuState.kt | 10 +++++- .../rosan/dhizuku/server/MyDhizukuProvider.kt | 8 +---- .../dhizuku/ui/page/settings/home/HomePage.kt | 5 +-- 5 files changed, 43 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/rosan/dhizuku/data/common/util/ContextUtil.kt b/app/src/main/java/com/rosan/dhizuku/data/common/util/ContextUtil.kt index 138307f..6e9fbbf 100644 --- a/app/src/main/java/com/rosan/dhizuku/data/common/util/ContextUtil.kt +++ b/app/src/main/java/com/rosan/dhizuku/data/common/util/ContextUtil.kt @@ -1,17 +1,12 @@ package com.rosan.dhizuku.data.common.util -import android.app.admin.DevicePolicyManager -import android.content.ComponentName import android.content.Context import android.content.Intent import android.net.Uri -import android.os.Build import android.os.Handler import android.os.Looper import android.widget.Toast -import androidx.annotation.RequiresApi import androidx.annotation.StringRes -import com.rosan.dhizuku.server.DhizukuDAReceiver fun Context.openUrlInBrowser(url: String) { val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) @@ -27,18 +22,4 @@ fun Context.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) { fun Context.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) { toast(getString(resId), duration) -} - -@RequiresApi(Build.VERSION_CODES.O) -fun Context.clearDelegatedScopes(uid: Int) { - val packageNames = packageManager.getPackagesForUid(uid) ?: emptyArray() - val devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val component = ComponentName(this, DhizukuDAReceiver::class.java) - packageNames.forEach { - devicePolicyManager.setDelegatedScopes( - component, - it, - emptyList() - ) - } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/impl/AppRepoImpl.kt b/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/impl/AppRepoImpl.kt index 670a50c..a4ba31d 100644 --- a/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/impl/AppRepoImpl.kt +++ b/app/src/main/java/com/rosan/dhizuku/data/settings/model/room/impl/AppRepoImpl.kt @@ -1,13 +1,27 @@ package com.rosan.dhizuku.data.settings.model.room.impl +import android.app.admin.DevicePolicyManager +import android.content.Context +import android.os.Build import com.rosan.dhizuku.data.settings.model.room.dao.AppDao import com.rosan.dhizuku.data.settings.model.room.entity.AppEntity import com.rosan.dhizuku.data.settings.repo.AppRepo +import com.rosan.dhizuku.server.DhizukuState import kotlinx.coroutines.flow.Flow +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject class AppRepoImpl( private val dao: AppDao -) : AppRepo { +) : AppRepo, KoinComponent { + private val context by inject() + + private val packageManager by lazy { context.packageManager } + + private val devicePolicyManager by lazy { + context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager + } + override fun flowAll(): Flow> = dao.flowAll() override fun flowFind(id: Long): Flow = dao.flowFind(id) @@ -21,6 +35,9 @@ class AppRepoImpl( override suspend fun findByUID(uid: Int): AppEntity? = dao.findByUID(uid) override suspend fun update(entity: AppEntity) { + if (!entity.allowApi) { + clearDelegatedScopes(entity) + } entity.modifiedAt = System.currentTimeMillis() dao.update(entity) } @@ -31,5 +48,17 @@ class AppRepoImpl( dao.insert(entity) } - override suspend fun delete(entity: AppEntity) = dao.delete(entity) + override suspend fun delete(entity: AppEntity) { + clearDelegatedScopes(entity) + dao.delete(entity) + } + + private fun clearDelegatedScopes(entity: AppEntity) { + if (!DhizukuState.state.owner) return + val packageNames = packageManager.getPackagesForUid(entity.uid) ?: emptyArray() + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return + packageNames.forEach { + devicePolicyManager.setDelegatedScopes(DhizukuState.component, it, emptyList()) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/rosan/dhizuku/server/DhizukuState.kt b/app/src/main/java/com/rosan/dhizuku/server/DhizukuState.kt index 90f5f38..427c214 100644 --- a/app/src/main/java/com/rosan/dhizuku/server/DhizukuState.kt +++ b/app/src/main/java/com/rosan/dhizuku/server/DhizukuState.kt @@ -1,6 +1,7 @@ package com.rosan.dhizuku.server import android.app.admin.DevicePolicyManager +import android.content.ComponentName import android.content.Context import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -18,9 +19,16 @@ data object DhizukuState { var state by mutableStateOf(State()) private set + var component by mutableStateOf( + ComponentName(BuildConfig.APPLICATION_ID, DhizukuDAReceiver::class.java.name) + ) + private set + fun sync(context: Context) { - val devicePolicyManager = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val devicePolicyManager = + context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager val packageName = context.packageName + component = ComponentName(packageName, component.className) state = State( device = devicePolicyManager.isDeviceOwnerApp(packageName), profile = devicePolicyManager.isProfileOwnerApp(packageName) diff --git a/app/src/main/java/com/rosan/dhizuku/server/MyDhizukuProvider.kt b/app/src/main/java/com/rosan/dhizuku/server/MyDhizukuProvider.kt index 418a74f..a13972c 100644 --- a/app/src/main/java/com/rosan/dhizuku/server/MyDhizukuProvider.kt +++ b/app/src/main/java/com/rosan/dhizuku/server/MyDhizukuProvider.kt @@ -1,18 +1,12 @@ package com.rosan.dhizuku.server -import android.content.ComponentName -import android.content.Context import com.rosan.dhizuku.aidl.IDhizukuClient import com.rosan.dhizuku.server_api.DhizukuProvider import com.rosan.dhizuku.server_api.DhizukuService import org.koin.core.component.KoinComponent -import org.koin.core.component.inject class MyDhizukuProvider : DhizukuProvider(), KoinComponent { - private val context by inject() - override fun onCreateService(client: IDhizukuClient): DhizukuService { - val component = ComponentName(context, DhizukuDAReceiver::class.java) - return MyDhizukuService(context, component, client) + return MyDhizukuService(context, DhizukuState.component, client) } } \ No newline at end of file diff --git a/app/src/main/java/com/rosan/dhizuku/ui/page/settings/home/HomePage.kt b/app/src/main/java/com/rosan/dhizuku/ui/page/settings/home/HomePage.kt index 5c52b6d..1e56b68 100644 --- a/app/src/main/java/com/rosan/dhizuku/ui/page/settings/home/HomePage.kt +++ b/app/src/main/java/com/rosan/dhizuku/ui/page/settings/home/HomePage.kt @@ -1,7 +1,6 @@ package com.rosan.dhizuku.ui.page.settings.home import android.app.admin.DevicePolicyManager -import android.content.ComponentName import android.content.Context import androidx.compose.animation.AnimatedContent import androidx.compose.animation.animateColorAsState @@ -74,7 +73,6 @@ import androidx.compose.ui.unit.dp import androidx.navigation.NavController import com.rosan.dhizuku.R import com.rosan.dhizuku.data.common.util.openUrlInBrowser -import com.rosan.dhizuku.server.DhizukuDAReceiver import com.rosan.dhizuku.server.DhizukuState import com.rosan.dhizuku.ui.page.settings.SettingsRoute import com.rosan.dhizuku.ui.theme.exclude @@ -263,8 +261,7 @@ private fun LazyItemScope.ShizukuWidget(navController: NavController) { @Composable private fun LazyItemScope.AdbWidget() { - val component = ComponentName(LocalContext.current, DhizukuDAReceiver::class.java) - val command = "adb shell dpm set-device-owner ${component.flattenToShortString()}" + val command = "adb shell dpm set-device-owner ${DhizukuState.component.flattenToShortString()}" var state by remember { mutableStateOf(false) }