0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-20 03:52:16 +02:00

Simplify opening URLs in external browser

So we can use `openUrl()` from anywhere now.
This commit is contained in:
Markus Fisch 2021-12-23 17:42:27 +01:00
parent b4fefad54f
commit 680030e89e
3 changed files with 21 additions and 13 deletions

View File

@ -2,8 +2,8 @@ package de.markusfisch.android.binaryeye.actions
import android.content.Context
import android.content.Intent
import de.markusfisch.android.binaryeye.app.parseAndNormalizeUri
import de.markusfisch.android.binaryeye.content.execShareIntent
import de.markusfisch.android.binaryeye.content.openUrl
import de.markusfisch.android.binaryeye.widget.toast
interface IAction {
@ -31,7 +31,6 @@ abstract class IntentAction : IAction {
abstract class SchemeAction : IAction {
abstract val scheme: String
open val intentAction: String = Intent.ACTION_VIEW
open val buildRegex: Boolean = false
final override fun canExecuteOn(data: ByteArray): Boolean {
@ -44,7 +43,6 @@ abstract class SchemeAction : IAction {
}
final override suspend fun execute(context: Context, data: ByteArray) {
val uri = parseAndNormalizeUri(String(data))
context.execShareIntent(Intent(intentAction, uri))
context.openUrl(String(data))
}
}

View File

@ -1,13 +1,11 @@
package de.markusfisch.android.binaryeye.actions.search
import android.content.Context
import android.content.Intent
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.actions.IAction
import de.markusfisch.android.binaryeye.app.alertDialog
import de.markusfisch.android.binaryeye.app.parseAndNormalizeUri
import de.markusfisch.android.binaryeye.app.prefs
import de.markusfisch.android.binaryeye.content.startIntent
import de.markusfisch.android.binaryeye.content.openUrl
import java.net.URLEncoder
object OpenOrSearchAction : IAction {
@ -20,10 +18,13 @@ object OpenOrSearchAction : IAction {
view(context, String(data), true)
}
private suspend fun view(context: Context, s: String, search: Boolean) {
val intent = Intent(Intent.ACTION_VIEW, parseAndNormalizeUri(s))
if (!context.startIntent(intent) && search) {
openSearch(context, s)
private suspend fun view(
context: Context,
url: String,
search: Boolean
) {
if (!context.openUrl(url) && search) {
openSearch(context, url)
}
}

View File

@ -8,16 +8,19 @@ import android.os.Build
import android.support.v4.content.FileProvider
import de.markusfisch.android.binaryeye.BuildConfig
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.app.parseAndNormalizeUri
import de.markusfisch.android.binaryeye.widget.toast
import java.io.File
fun Context.execShareIntent(intent: Intent) {
fun Context.execShareIntent(intent: Intent): Boolean {
if (!startIntent(intent)) {
toast(R.string.cannot_resolve_action)
return false
}
return true
}
fun Context.startIntent(intent: Intent) = try {
fun Context.startIntent(intent: Intent): Boolean = try {
// Avoid using `intent.resolveActivity()` at API level 30+ due
// to the new package visibility restrictions. In order for
// `resolveActivity()` to "see" another package, we would need
@ -32,6 +35,12 @@ fun Context.startIntent(intent: Intent) = try {
false
}
fun Context.openUrl(url: String): Boolean = openUri(parseAndNormalizeUri(url))
fun Context.openUri(uri: Uri): Boolean = execShareIntent(
Intent(Intent.ACTION_VIEW, uri)
)
fun Context.shareText(text: String, mimeType: String = "text/plain") {
execShareIntent(Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_TEXT, text)