From 7ac55068db0c1dce543b68b36824ab3afc57887d Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Fri, 25 Sep 2020 17:32:54 +0800 Subject: [PATCH] Catch ActivityNotFoundException --- .../com/topjohnwu/magisk/core/base/BaseActivity.kt | 10 +++++++++- .../magisk/events/InstallExternalModuleEvent.kt | 10 +++++++++- .../java/com/topjohnwu/magisk/events/ViewEvents.kt | 13 ++++++++++++- .../topjohnwu/magisk/ui/install/InstallViewModel.kt | 3 --- app/src/main/res/values/strings.xml | 1 + 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt b/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt index 1e50a737f..acd6299cb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/base/BaseActivity.kt @@ -1,18 +1,22 @@ package com.topjohnwu.magisk.core.base import android.Manifest +import android.content.ActivityNotFoundException import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.res.Configuration import android.os.Build +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.collection.SparseArrayCompat import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.utils.currentLocale import com.topjohnwu.magisk.core.wrap import com.topjohnwu.magisk.ktx.set +import com.topjohnwu.magisk.utils.Utils import kotlin.random.Random typealias RequestCallback = BaseActivity.(Int, Intent?) -> Unit @@ -85,7 +89,11 @@ abstract class BaseActivity : AppCompatActivity() { fun startActivityForResult(intent: Intent, requestCode: Int, listener: RequestCallback) { resultCallbacks[requestCode] = listener - startActivityForResult(intent, requestCode) + try { + startActivityForResult(intent, requestCode) + } catch (e: ActivityNotFoundException) { + Utils.toast(R.string.app_not_found, Toast.LENGTH_SHORT) + } } override fun recreate() { diff --git a/app/src/main/java/com/topjohnwu/magisk/events/InstallExternalModuleEvent.kt b/app/src/main/java/com/topjohnwu/magisk/events/InstallExternalModuleEvent.kt index df7edafa1..6d15a856b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/InstallExternalModuleEvent.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/InstallExternalModuleEvent.kt @@ -2,14 +2,18 @@ package com.topjohnwu.magisk.events import android.Manifest import android.app.Activity +import android.content.ActivityNotFoundException import android.content.Intent +import android.widget.Toast import androidx.annotation.RequiresPermission import androidx.navigation.NavDirections import com.topjohnwu.magisk.MainDirections +import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.ActivityExecutor import com.topjohnwu.magisk.arch.BaseUIActivity import com.topjohnwu.magisk.arch.ViewEvent import com.topjohnwu.magisk.core.Const +import com.topjohnwu.magisk.utils.Utils class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor { @@ -17,7 +21,11 @@ class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor { override fun invoke(activity: BaseUIActivity<*, *>) { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.type = "application/zip" - activity.startActivityForResult(intent, Const.ID.FETCH_ZIP) + try { + activity.startActivityForResult(intent, Const.ID.FETCH_ZIP) + } catch (e: ActivityNotFoundException) { + Utils.toast(R.string.app_not_found, Toast.LENGTH_SHORT) + } } companion object { diff --git a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt index a54897ecc..217f2d26e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt @@ -1,12 +1,16 @@ package com.topjohnwu.magisk.events import android.app.Activity +import android.content.ActivityNotFoundException import android.content.Context import android.content.Intent +import android.widget.Toast import androidx.navigation.NavDirections +import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.* import com.topjohnwu.magisk.core.base.BaseActivity import com.topjohnwu.magisk.core.model.module.Repo +import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.view.MarkDownWindow import com.topjohnwu.magisk.view.Shortcuts import kotlinx.coroutines.launch @@ -68,7 +72,14 @@ class RequestFileEvent : ViewEvent(), ActivityExecutor { Intent(Intent.ACTION_GET_CONTENT) .setType("*/*") .addCategory(Intent.CATEGORY_OPENABLE) - .also { activity.startActivityForResult(it, REQUEST_CODE) } + .also { + try { + activity.startActivityForResult(it, REQUEST_CODE) + Utils.toast(R.string.patch_file_msg, Toast.LENGTH_LONG) + } catch (e: ActivityNotFoundException) { + Utils.toast(R.string.app_not_found, Toast.LENGTH_SHORT) + } + } } companion object { diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt index d34dc667e..986fdc8e5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/install/InstallViewModel.kt @@ -1,7 +1,6 @@ package com.topjohnwu.magisk.ui.install import android.net.Uri -import android.widget.Toast import androidx.databinding.Bindable import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.BR @@ -14,7 +13,6 @@ import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.data.repository.StringRepository import com.topjohnwu.magisk.events.RequestFileEvent import com.topjohnwu.magisk.events.dialog.SecondSlotWarningDialog -import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.set import com.topjohnwu.superuser.Shell import kotlinx.coroutines.launch @@ -37,7 +35,6 @@ class InstallViewModel( set(value) = set(value, field, { field = it }, BR.method) { when (it) { R.id.method_patch -> { - Utils.toast(R.string.patch_file_msg, Toast.LENGTH_LONG) RequestFileEvent().publish() } R.id.method_inactive_slot -> { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0e9d592d1..e1bbbf203 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -236,5 +236,6 @@ Grant storage permission to enable this functionality Add shortcut to home screen After hiding Magisk Manager, its name and icon might become difficult to recognize. Do you want to add a pretty shortcut to the home screen? + No application found to handle this action