Skip to content

Commit

Permalink
pref: clear delegated scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamr0s committed Aug 19, 2024
1 parent 647964b commit 06d40d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Context>()

private val packageManager by lazy { context.packageManager }

private val devicePolicyManager by lazy {
context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}

override fun flowAll(): Flow<List<AppEntity>> = dao.flowAll()

override fun flowFind(id: Long): Flow<AppEntity?> = dao.flowFind(id)
Expand All @@ -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)
}
Expand All @@ -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())
}
}
}
10 changes: 9 additions & 1 deletion app/src/main/java/com/rosan/dhizuku/server/DhizukuState.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Context>()

override fun onCreateService(client: IDhizukuClient): DhizukuService {
val component = ComponentName(context, DhizukuDAReceiver::class.java)
return MyDhizukuService(context, component, client)
return MyDhizukuService(context, DhizukuState.component, client)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 06d40d7

Please sign in to comment.