changing the way activity intents are launched, mostly for Android 11+

This commit is contained in:
tibbi 2021-04-03 23:40:14 +02:00
parent 036c96e56c
commit 8084f88f20
2 changed files with 60 additions and 47 deletions

View file

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.app.ActivityManager
import android.app.role.RoleManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
@ -357,13 +358,16 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
funAfterSAFPermission = callback
WritePermissionDialog(this, true) {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
if (resolveActivity(packageManager) == null) {
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_OTG)
return@apply
} catch (e: Exception) {
type = "*/*"
}
if (resolveActivity(packageManager) != null) {
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_OTG)
} else {
} catch (e: Exception) {
toast(R.string.unknown_error_occurred)
}
}
@ -602,10 +606,12 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
}
} else {
Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, packageName).apply {
if (resolveActivity(packageManager) != null) {
try {
startActivityForResult(this, REQUEST_CODE_SET_DEFAULT_DIALER)
} else {
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
} catch (e: Exception) {
showErrorToast(e)
}
}
}

View file

@ -109,14 +109,18 @@ fun BaseSimpleActivity.isShowingSAFDialog(path: String): Boolean {
WritePermissionDialog(this, false) {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
putExtra("android.content.extra.SHOW_ADVANCED", true)
if (resolveActivity(packageManager) == null) {
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE)
checkedDocumentPath = path
return@apply
} catch (e: Exception) {
type = "*/*"
}
if (resolveActivity(packageManager) != null) {
checkedDocumentPath = path
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE)
} else {
checkedDocumentPath = path
} catch (e: Exception) {
toast(R.string.unknown_error_occurred)
}
}
@ -143,14 +147,18 @@ fun BaseSimpleActivity.showOTGPermissionDialog(path: String) {
if (!isDestroyed && !isFinishing) {
WritePermissionDialog(this, true) {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
if (resolveActivity(packageManager) == null) {
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_OTG)
checkedDocumentPath = path
return@apply
} catch (e: Exception) {
type = "*/*"
}
if (resolveActivity(packageManager) != null) {
checkedDocumentPath = path
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_OTG)
} else {
checkedDocumentPath = path
} catch (e: Exception) {
toast(R.string.unknown_error_occurred)
}
}
@ -203,17 +211,17 @@ fun Activity.sharePathIntent(path: String, applicationId: String) {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
if (resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(this, getString(R.string.share_via)))
} else {
toast(R.string.no_app_found)
}
startActivity(Intent.createChooser(this, getString(R.string.share_via)))
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
} catch (e: RuntimeException) {
if (e.cause is TransactionTooLargeException) {
toast(R.string.maximum_share_reached)
} else {
showErrorToast(e)
}
} catch (e: Exception) {
showErrorToast(e)
}
}
}
@ -243,17 +251,17 @@ fun Activity.sharePathsIntent(paths: List<String>, applicationId: String) {
putParcelableArrayListExtra(Intent.EXTRA_STREAM, newUris)
try {
if (resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(this, getString(R.string.share_via)))
} else {
toast(R.string.no_app_found)
}
startActivity(Intent.createChooser(this, getString(R.string.share_via)))
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
} catch (e: RuntimeException) {
if (e.cause is TransactionTooLargeException) {
toast(R.string.maximum_share_reached)
} else {
showErrorToast(e)
}
} catch (e: Exception) {
showErrorToast(e)
}
}
}
@ -268,17 +276,17 @@ fun Activity.shareTextIntent(text: String) {
putExtra(Intent.EXTRA_TEXT, text)
try {
if (resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(this, getString(R.string.share_via)))
} else {
toast(R.string.no_app_found)
}
startActivity(Intent.createChooser(this, getString(R.string.share_via)))
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
} catch (e: RuntimeException) {
if (e.cause is TransactionTooLargeException) {
toast(R.string.maximum_share_reached)
} else {
showErrorToast(e)
}
} catch (e: Exception) {
showErrorToast(e)
}
}
}
@ -293,10 +301,12 @@ fun Activity.setAsIntent(path: String, applicationId: String) {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooser = Intent.createChooser(this, getString(R.string.set_as))
if (resolveActivity(packageManager) != null) {
try {
startActivityForResult(chooser, REQUEST_SET_AS)
} else {
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
} catch (e: Exception) {
showErrorToast(e)
}
}
}
@ -325,15 +335,13 @@ fun Activity.openEditorIntent(path: String, forceChooser: Boolean, applicationId
putExtra(MediaStore.EXTRA_OUTPUT, outputUri)
putExtra(REAL_FILE_PATH, path)
if (resolveActivity(packageManager) != null) {
try {
val chooser = Intent.createChooser(this, getString(R.string.edit_with))
startActivityForResult(if (forceChooser) chooser else this, REQUEST_EDIT_IMAGE)
} catch (e: SecurityException) {
showErrorToast(e)
}
} else {
try {
val chooser = Intent.createChooser(this, getString(R.string.edit_with))
startActivityForResult(if (forceChooser) chooser else this, REQUEST_EDIT_IMAGE)
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
} catch (e: Exception) {
showErrorToast(e)
}
}
}
@ -358,17 +366,15 @@ fun Activity.openPathIntent(path: String, forceChooser: Boolean, applicationId:
putExtra(REAL_FILE_PATH, path)
if (resolveActivity(packageManager) != null) {
try {
val chooser = Intent.createChooser(this, getString(R.string.open_with))
try {
startActivity(if (forceChooser) chooser else this)
} catch (e: NullPointerException) {
showErrorToast(e)
}
} else {
startActivity(if (forceChooser) chooser else this)
} catch (e: ActivityNotFoundException) {
if (!tryGenericMimeType(this, mimeType, newUri)) {
toast(R.string.no_app_found)
}
} catch (e: Exception) {
showErrorToast(e)
}
}
}
@ -435,10 +441,11 @@ fun Activity.tryGenericMimeType(intent: Intent, mimeType: String, uri: Uri): Boo
}
intent.setDataAndType(uri, genericMimeType)
return if (intent.resolveActivity(packageManager) != null) {
return try {
startActivity(intent)
true
} else {
} catch (e: Exception) {
false
}
}