From ca156befbd64aea1b5d24ef11e4652207e5ac7c8 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sun, 16 Jun 2019 15:18:39 +0200 Subject: [PATCH] Fixed mapping generic pairs to policy crashing when no policy is found The policy (app) is now deleted when found invalid (uninstalled) --- .../magisk/data/database/PolicyDao.kt | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt b/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt index 0ae902bfb..24ce2e664 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/database/PolicyDao.kt @@ -8,6 +8,7 @@ import com.topjohnwu.magisk.model.entity.MagiskPolicy import com.topjohnwu.magisk.model.entity.toMap import com.topjohnwu.magisk.model.entity.toPolicy import com.topjohnwu.magisk.utils.now +import timber.log.Timber import java.util.concurrent.TimeUnit @@ -47,12 +48,7 @@ class PolicyDao( condition { equals("uid", uid) } - }.map { it.firstOrNull()?.toPolicy(context.packageManager) } - .doOnError { - if (it is PackageManager.NameNotFoundException) { - delete(uid).subscribe() - } - } + }.map { it.first().toPolicySafe() } fun update(policy: MagiskPolicy) = query { values(policy.toMap()) @@ -62,8 +58,24 @@ class PolicyDao( condition { equals("uid/100000", Const.USER_ID) } - }.flattenAsFlowable { it } - .map { it.toPolicy(context.packageManager) } - .toList() + }.map { it.mapNotNull { it.toPolicySafe() } } + + + private fun Map.toPolicySafe(): MagiskPolicy? { + val taskResult = runCatching { toPolicy(context.packageManager) } + val result = taskResult.getOrNull() + val exception = taskResult.exceptionOrNull() + + Timber.e(exception) + + when (exception) { + is PackageManager.NameNotFoundException -> { + val uid = getOrElse("uid") { null } ?: return null + delete(uid).subscribe() + } + } + + return result + } } \ No newline at end of file