From 2954eb4bdc85eb282c8869f39d18cc870782fbb8 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Mon, 4 Oct 2021 22:16:16 +0800 Subject: [PATCH] Remove CustomTab --- .../magisk/events/OpenInappLinkEvent.kt | 33 ------------------- .../topjohnwu/magisk/ui/home/HomeViewModel.kt | 13 ++++---- .../java/com/topjohnwu/magisk/utils/Utils.kt | 10 +++--- 3 files changed, 11 insertions(+), 45 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/events/OpenInappLinkEvent.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/events/OpenInappLinkEvent.kt b/app/src/main/java/com/topjohnwu/magisk/events/OpenInappLinkEvent.kt deleted file mode 100644 index ae9be3669..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/events/OpenInappLinkEvent.kt +++ /dev/null @@ -1,33 +0,0 @@ -package com.topjohnwu.magisk.events - -import android.content.Context -import android.content.res.Resources -import android.util.TypedValue -import androidx.annotation.AttrRes -import androidx.browser.customtabs.CustomTabsIntent -import androidx.core.net.toUri -import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.arch.ContextExecutor -import com.topjohnwu.magisk.arch.ViewEvent - -data class OpenInappLinkEvent( - private val link: String -) : ViewEvent(), ContextExecutor { - - // todo find app that can open the link and as a fallback open custom tabs! it shouldn't be the default - override fun invoke(context: Context) = CustomTabsIntent.Builder() - .setShowTitle(true) - .setToolbarColor(context.themedColor(R.attr.colorSurface)) - .enableUrlBarHiding() - .build() - .launchUrl(context, link.toUri()) - - private fun Context.themedColor(@AttrRes attribute: Int) = theme - .resolveAttribute(attribute).data - - private fun Resources.Theme.resolveAttribute( - @AttrRes attribute: Int, - resolveRefs: Boolean = true - ) = TypedValue().also { resolveAttribute(attribute, it, resolveRefs) } - -} diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt index a78cb4134..70fa9a71b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt @@ -1,13 +1,12 @@ package com.topjohnwu.magisk.ui.home +import android.content.Context +import androidx.core.net.toUri import androidx.databinding.Bindable import androidx.lifecycle.viewModelScope import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.arch.ActivityExecutor -import com.topjohnwu.magisk.arch.BaseUIActivity -import com.topjohnwu.magisk.arch.BaseViewModel -import com.topjohnwu.magisk.arch.ViewEvent +import com.topjohnwu.magisk.arch.* import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.download.Subject @@ -15,12 +14,12 @@ import com.topjohnwu.magisk.core.download.Subject.Manager import com.topjohnwu.magisk.data.repository.NetworkService import com.topjohnwu.magisk.databinding.itemBindingOf import com.topjohnwu.magisk.databinding.set -import com.topjohnwu.magisk.events.OpenInappLinkEvent import com.topjohnwu.magisk.events.SnackbarEvent import com.topjohnwu.magisk.events.dialog.EnvFixDialog import com.topjohnwu.magisk.events.dialog.ManagerInstallDialog import com.topjohnwu.magisk.events.dialog.UninstallDialog import com.topjohnwu.magisk.ktx.await +import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.asText import com.topjohnwu.superuser.Shell import kotlinx.coroutines.launch @@ -116,7 +115,9 @@ class HomeViewModel( stateManagerProgress = progress.times(100f).roundToInt() } - fun onLinkPressed(link: String) = OpenInappLinkEvent(link).publish() + fun onLinkPressed(link: String) = object : ViewEvent(), ContextExecutor { + override fun invoke(context: Context) = Utils.openLink(context, link.toUri()) + }.publish() fun onDeletePressed() = UninstallDialog().publish() diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt index 0041f99e4..9dffa979f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.kt @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.utils +import android.content.ActivityNotFoundException import android.content.Context import android.content.Intent import android.net.Uri @@ -29,13 +30,10 @@ object Utils { fun openLink(context: Context, link: Uri) { val intent = Intent(Intent.ACTION_VIEW, link) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - if (intent.resolveActivity(context.packageManager) != null) { + try { context.startActivity(intent) - } else { - toast( - R.string.open_link_failed_toast, - Toast.LENGTH_SHORT - ) + } catch (e: ActivityNotFoundException) { + toast(R.string.open_link_failed_toast, Toast.LENGTH_SHORT) } } }