diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt index 9d6c8a757..ebf7a887c 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt @@ -3,6 +3,7 @@ package com.topjohnwu.magisk.core.su import android.content.Context import android.os.Bundle import android.widget.Toast +import com.topjohnwu.magisk.core.AppContext import com.topjohnwu.magisk.core.BuildConfig import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.R @@ -93,6 +94,18 @@ object SuCallbackHandler { SuEvents.notifyPolicyChanged() } + fun notify(granted: Boolean, appName: String) { + when (Config.suNotification) { + Config.Value.NOTIFICATION_TOAST -> { + val resId = if (granted) R.string.su_allow_toast else R.string.su_deny_toast + AppContext.toast(AppContext.getString(resId, appName), Toast.LENGTH_SHORT) + } + Config.Value.NOTIFICATION_STATUS_BAR -> { + Notifications.suNotification(granted, appName) + } + } + } + private fun notify(context: Context, granted: Boolean, appName: String) { when (Config.suNotification) { Config.Value.NOTIFICATION_TOAST -> { diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt index 3cf96cd22..37cebabd9 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt @@ -6,8 +6,12 @@ import android.content.pm.PackageManager import com.topjohnwu.magisk.core.BuildConfig import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.data.magiskdb.PolicyDao +import com.topjohnwu.magisk.core.di.ServiceLocator +import com.topjohnwu.magisk.core.ktx.getLabel import com.topjohnwu.magisk.core.ktx.getPackageInfo +import com.topjohnwu.magisk.core.model.su.SuLog import com.topjohnwu.magisk.core.model.su.SuPolicy +import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.superuser.Shell import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -25,6 +29,7 @@ class SuRequestHandler( private lateinit var output: File private lateinit var policy: SuPolicy + private var pid: Int = -1 lateinit var pkgInfo: PackageInfo private set @@ -55,7 +60,7 @@ class SuRequestHandler( private suspend fun init(intent: Intent): Boolean { val uid = intent.getIntExtra("uid", -1) - val pid = intent.getIntExtra("pid", -1) + pid = intent.getIntExtra("pid", -1) val fifo = intent.getStringExtra("fifo") if (uid <= 0 || pid <= 0 || fifo == null) { Timber.e("Unexpected extras: uid=[${uid}], pid=[${pid}], fifo=[${fifo}]") @@ -104,7 +109,32 @@ class SuRequestHandler( } if (time >= 0) { policyDB.update(policy) + + val appInfo = pkgInfo.applicationInfo + val appName = appInfo?.getLabel(pm) + ?: pkgInfo.sharedUserId ?: "[UID] ${policy.uid}" + val packageName = appInfo?.let { pm.getNameForUid(it.uid) } + ?: pkgInfo.sharedUserId ?: "[UID] ${policy.uid}" + + val log = SuLog( + fromUid = policy.uid, + toUid = 0, + fromPid = pid, + packageName = packageName, + appName = appName, + command = "", + action = policy.policy, + target = -1, + context = "", + gids = "", + ) + ServiceLocator.logRepo.insert(log) + + val granted = policy.policy >= SuPolicy.ALLOW + SuCallbackHandler.notify(granted, appName) + SuEvents.notifyPolicyChanged() + SuEvents.notifyLogUpdated() } } }