diff --git a/app/src/main/java/com/topjohnwu/magisk/core/magiskdb/PolicyDao.kt b/app/src/main/java/com/topjohnwu/magisk/core/magiskdb/PolicyDao.kt index 26da6e725..a7d56567a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/magiskdb/PolicyDao.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/magiskdb/PolicyDao.kt @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.core.magiskdb -import android.content.pm.PackageManager import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.model.su.toPolicy @@ -54,13 +53,9 @@ class PolicyDao : BaseDao() { private fun Map.toPolicyOrNull(): SuPolicy? { return runCatching { toPolicy(AppContext.packageManager) }.getOrElse { - Timber.e(it) - if (it is PackageManager.NameNotFoundException) { - val uid = getOrElse("uid") { null } ?: return null - GlobalScope.launch { - delete(uid.toInt()) - } - } + Timber.w(it) + val uid = getOrElse("uid") { return null } + GlobalScope.launch { delete(uid.toInt()) } null } } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuLog.kt b/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuLog.kt index eb126344d..82e4d2534 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuLog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuLog.kt @@ -1,10 +1,8 @@ package com.topjohnwu.magisk.core.model.su import androidx.room.Entity -import androidx.room.Ignore import androidx.room.PrimaryKey -import com.topjohnwu.magisk.ktx.timeFormatTime -import com.topjohnwu.magisk.ktx.toTime +import com.topjohnwu.magisk.ktx.now @Entity(tableName = "logs") data class SuLog( @@ -15,8 +13,7 @@ data class SuLog( val appName: String, val command: String, val action: Boolean, - val time: Long = -1 + val time: Long = now ) { @PrimaryKey(autoGenerate = true) var id: Int = 0 - @Ignore val timeString = time.toTime(timeFormatTime) } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt b/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt index 0010712c4..bd59df54f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt @@ -9,7 +9,7 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.INTERACTIVE import com.topjohnwu.magisk.ktx.getLabel data class SuPolicy( - var uid: Int, + val uid: Int, val packageName: String, val appName: String, val icon: Drawable, @@ -27,8 +27,7 @@ data class SuPolicy( fun toLog(toUid: Int, fromPid: Int, command: String) = SuLog( uid, toUid, fromPid, packageName, appName, - command, policy == ALLOW, System.currentTimeMillis() - ) + command, policy == ALLOW) fun toMap() = mapOf( "uid" to uid, diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt index a3f3a70c0..c988b9249 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/SuCallbackHandler.kt @@ -2,7 +2,6 @@ package com.topjohnwu.magisk.core.su import android.content.Context import android.os.Bundle -import android.os.Process import android.widget.Toast import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.R @@ -41,36 +40,37 @@ object SuCallbackHandler { } } - private fun Any?.toInt(): Int? { - return when (this) { - is Number -> this.toInt() - else -> null + // https://android.googlesource.com/platform/frameworks/base/+/547bf5487d52b93c9fe183aa6d56459c170b17a4 + private fun Bundle.getIntComp(key: String, defaultValue: Int): Int { + val value = get(key) ?: return defaultValue + return when (value) { + is Int -> value + is Long -> value.toInt() + else -> defaultValue } } private fun handleLogging(context: Context, data: Bundle) { - val fromUid = data["from.uid"].toInt() ?: return - if (fromUid == Process.myUid()) - return + val fromUid = data.getIntComp("from.uid", -1) + val notify = data.getBoolean("notify", true) + val allow = data.getIntComp("policy", SuPolicy.ALLOW) val pm = context.packageManager - val notify = data.getBoolean("notify", true) - val allow = data["policy"].toInt() ?: return - val policy = runCatching { fromUid.toPolicy(pm, allow) }.getOrElse { + GlobalScope.launch { ServiceLocator.policyDB.delete(fromUid) } fromUid.toUidPolicy(pm, allow) } if (notify) notify(context, policy) - val toUid = data["to.uid"].toInt() ?: return - val pid = data["pid"].toInt() ?: return + val toUid = data.getIntComp("to.uid", -1) + val pid = data.getIntComp("pid", -1) - val command = data.getString("command") ?: return + val command = data.getString("command", "") val log = policy.toLog( toUid = toUid, fromPid = pid, @@ -83,22 +83,23 @@ object SuCallbackHandler { } private fun handleNotify(context: Context, data: Bundle) { - val fromUid = data["from.uid"].toInt() ?: return - if (fromUid == Process.myUid()) - return + val fromUid = data.getIntComp("from.uid", -1) + val allow = data.getIntComp("policy", SuPolicy.ALLOW) val pm = context.packageManager - val allow = data["policy"].toInt() ?: return - runCatching { - val policy = fromUid.toPolicy(pm, allow) - if (policy.policy >= 0) - notify(context, policy) + + val policy = runCatching { + fromUid.toPolicy(pm, allow) + }.getOrElse { + GlobalScope.launch { ServiceLocator.policyDB.delete(fromUid) } + fromUid.toUidPolicy(pm, allow) } + notify(context, policy) } private fun notify(context: Context, policy: SuPolicy) { - if (policy.notification && Config.suNotification == Config.Value.NOTIFICATION_TOAST) { + if (Config.suNotification == Config.Value.NOTIFICATION_TOAST) { val resId = if (policy.policy == SuPolicy.ALLOW) R.string.su_allow_toast else diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt index 13addc6e3..f05af77be 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt @@ -4,11 +4,11 @@ import android.content.Intent import android.content.pm.PackageManager import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.core.Config -import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.magiskdb.PolicyDao import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.model.su.toPolicy import com.topjohnwu.magisk.ktx.now +import com.topjohnwu.superuser.Shell import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -35,8 +35,10 @@ class SuRequestHandler( return false // Never allow com.topjohnwu.magisk (could be malware) - if (policy.packageName == BuildConfig.APPLICATION_ID) + if (policy.packageName == BuildConfig.APPLICATION_ID) { + Shell.su("(pm uninstall ${BuildConfig.APPLICATION_ID})& >/dev/null 2>&1").exec() return false + } when (Config.suAutoResponse) { Config.Value.SU_AUTO_DENY -> { @@ -87,7 +89,6 @@ class SuRequestHandler( policy.policy = action policy.until = until - policy.uid = policy.uid % 100000 + Const.USER_ID * 100000 GlobalScope.launch(Dispatchers.IO) { try { diff --git a/app/src/main/java/com/topjohnwu/magisk/ktx/XTime.kt b/app/src/main/java/com/topjohnwu/magisk/ktx/XTime.kt index ab8c6a8db..da2c21f6d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ktx/XTime.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ktx/XTime.kt @@ -2,31 +2,17 @@ package com.topjohnwu.magisk.ktx import com.topjohnwu.magisk.core.utils.currentLocale import java.text.DateFormat -import java.text.ParseException import java.text.SimpleDateFormat val now get() = System.currentTimeMillis() fun Long.toTime(format: DateFormat) = format.format(this).orEmpty() -fun String.toTime(format: DateFormat) = try { - format.parse(this)?.time ?: -1 -} catch (e: ParseException) { - -1L -} -val timeFormatFull by lazy { SimpleDateFormat("yyyy/MM/dd_HH:mm:ss", +val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", currentLocale ) } -val timeFormatStandard by lazy { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", - currentLocale -) } -val timeFormatMedium by lazy { DateFormat.getDateInstance(DateFormat.MEDIUM, - currentLocale -) } -val timeFormatTime by lazy { SimpleDateFormat("h:mm a", - currentLocale -) } -val timeDateFormat by lazy { + +val timeDateFormat: DateFormat by lazy { DateFormat.getDateTimeInstance( DateFormat.DEFAULT, DateFormat.DEFAULT,