From d9567fa573e6943366f1e4b6dbad433a6975f97d Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 15 Jan 2022 17:08:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/IntentHelp.kt | 15 +++++++ .../io/legado/app/utils/ContextExtensions.kt | 44 +++++++++++-------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/IntentHelp.kt b/app/src/main/java/io/legado/app/help/IntentHelp.kt index aeb0e9be5..c1f918e8e 100644 --- a/app/src/main/java/io/legado/app/help/IntentHelp.kt +++ b/app/src/main/java/io/legado/app/help/IntentHelp.kt @@ -2,12 +2,27 @@ package io.legado.app.help import android.content.Context import android.content.Intent +import android.net.Uri import io.legado.app.R import io.legado.app.utils.toastOnUi +import splitties.init.appCtx @Suppress("unused") object IntentHelp { + fun getBrowserIntent(url: String): Intent { + return getBrowserIntent(Uri.parse(url)) + } + + fun getBrowserIntent(uri: Uri): Intent { + val intent = Intent(Intent.ACTION_VIEW) + intent.data = uri + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + if (intent.resolveActivity(appCtx.packageManager) == null) { + return Intent.createChooser(intent, "请选择浏览器") + } + return intent + } fun toTTSSetting(context: Context) { //跳转到文字转语音设置界面 diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index f04eba50c..8c0ad814c 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -27,6 +27,7 @@ import androidx.preference.PreferenceManager import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel import io.legado.app.R import io.legado.app.constant.AppConst +import io.legado.app.help.IntentHelp import timber.log.Timber import java.io.File import java.io.FileOutputStream @@ -62,6 +63,20 @@ inline fun Context.servicePendingIntent( return getService(this, 0, intent, flags) } +@SuppressLint("UnspecifiedImmutableFlag") +fun Context.activityPendingIntent( + intent: Intent, + action: String +): PendingIntent? { + intent.action = action + val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + FLAG_UPDATE_CURRENT or FLAG_MUTABLE + } else { + FLAG_UPDATE_CURRENT + } + return getActivity(this, 0, intent, flags) +} + @SuppressLint("UnspecifiedImmutableFlag") inline fun Context.activityPendingIntent( action: String, @@ -145,8 +160,8 @@ fun Context.restart() { intent?.let { intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK - or Intent.FLAG_ACTIVITY_CLEAR_TASK - or Intent.FLAG_ACTIVITY_CLEAR_TOP + or Intent.FLAG_ACTIVITY_CLEAR_TASK + or Intent.FLAG_ACTIVITY_CLEAR_TOP ) startActivity(intent) //杀掉以前进程 @@ -284,25 +299,18 @@ val Context.externalCache: File get() = this.externalCacheDir ?: this.cacheDir fun Context.openUrl(url: String) { - openUrl(Uri.parse(url)) + try { + startActivity(IntentHelp.getBrowserIntent(url)) + } catch (e: Exception) { + toastOnUi(e.localizedMessage ?: "open url error") + } } fun Context.openUrl(uri: Uri) { - val intent = Intent(Intent.ACTION_VIEW) - intent.data = uri - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - if (intent.resolveActivity(packageManager) != null) { - try { - startActivity(intent) - } catch (e: Exception) { - toastOnUi(e.localizedMessage ?: "open url error") - } - } else { - try { - startActivity(Intent.createChooser(intent, "请选择浏览器")) - } catch (e: Exception) { - toastOnUi(e.localizedMessage ?: "open url error") - } + try { + startActivity(IntentHelp.getBrowserIntent(uri)) + } catch (e: Exception) { + toastOnUi(e.localizedMessage ?: "open url error") } }