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

Remove trying to resolve activities on API 30+.

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 to list that package/intent
in a `<queries>` block in the Manifest.

But since we used `resolveActivity()` only to avoid an exception
if the Intent cannot be resolved, it's much easier and more robust
to just try and catch that exception if necessary.

This fixes adding contacts (and other things) on API level 30+.
This commit is contained in:
Markus Fisch 2021-03-02 19:12:56 +01:00
parent fed7d561c5
commit 49d32ff95c
4 changed files with 38 additions and 24 deletions

View File

@ -21,7 +21,7 @@ abstract class IntentAction : IAction {
val intent = createIntent(context, data) ?: return context.toast(
errorMsg
)
execShareIntent(context, intent)
context.execShareIntent(intent)
}
abstract suspend fun createIntent(context: Context, data: ByteArray): Intent?
@ -43,6 +43,6 @@ abstract class SchemeAction : IAction {
final override suspend fun execute(context: Context, data: ByteArray) {
val uri = parseAndNormalizeUri(String(data))
execShareIntent(context, Intent(intentAction, uri))
context.execShareIntent(Intent(intentAction, uri))
}
}

View File

@ -19,18 +19,26 @@ object OpenOrSearchAction : IAction {
override suspend fun execute(context: Context, data: ByteArray) {
val intent = openUri(context, String(data)) ?: return
execShareIntent(context, intent)
context.execShareIntent(intent)
}
private suspend fun openUri(context: Context, data: String, search: Boolean = true): Intent? {
private suspend fun openUri(
context: Context,
data: String,
search: Boolean = true
): Intent? {
val uri = parseAndNormalizeUri(data)
val intent = Intent(Intent.ACTION_VIEW, uri)
when {
intent.resolveActivity(context.packageManager) != null -> return intent
search -> return getSearchIntent(context, data)
else -> context.toast(R.string.cannot_resolve_action)
return when {
// It's okay to use `resolveActivity()` at API level 30+ here
// because ACTION_VIEW is defined in `<queries>` in the Manifest.
intent.resolveActivity(context.packageManager) != null -> intent
search -> getSearchIntent(context, data)
else -> {
context.toast(R.string.cannot_resolve_action)
null
}
}
return null
}
private suspend fun getSearchIntent(context: Context, query: String): Intent? {

View File

@ -27,6 +27,7 @@ import com.google.zxing.ResultMetadataType
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.app.*
import de.markusfisch.android.binaryeye.content.copyToClipboard
import de.markusfisch.android.binaryeye.content.execShareIntent
import de.markusfisch.android.binaryeye.database.Scan
import de.markusfisch.android.binaryeye.graphics.Mapping
import de.markusfisch.android.binaryeye.graphics.frameToView
@ -280,11 +281,7 @@ class CameraActivity : AppCompatActivity() {
Intent.ACTION_VIEW,
Uri.parse(getString(R.string.project_url))
)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
} else {
toast(R.string.project_url)
}
execShareIntent(intent)
}
private fun handleSendText(intent: Intent) {

View File

@ -1,5 +1,6 @@
package de.markusfisch.android.binaryeye.content
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
@ -10,11 +11,27 @@ import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.widget.toast
import java.io.File
fun Context.execShareIntent(intent: Intent) {
// 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
// to list that package/intent in a `<queries>` block in the
// Manifest. But since we used `resolveActivity()` only to avoid
// an exception if the Intent cannot be resolved, it's much easier
// and more robust to just try and catch that exception if
// necessary.
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
toast(R.string.cannot_resolve_action)
}
}
fun shareText(context: Context, text: String, type: String = "text/plain") {
val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, text)
intent.type = type
execShareIntent(context, intent)
context.execShareIntent(intent)
}
fun shareUri(context: Context, uri: Uri, type: String) {
@ -22,15 +39,7 @@ fun shareUri(context: Context, uri: Uri, type: String) {
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.type = type
execShareIntent(context, intent)
}
fun execShareIntent(context: Context, intent: Intent) {
if (intent.resolveActivity(context.packageManager) != null) {
context.startActivity(intent)
} else {
context.toast(R.string.cannot_resolve_action)
}
context.execShareIntent(intent)
}
fun shareFile(context: Context, file: File, type: String) {