unify the storage permission requesting
This commit is contained in:
parent
b29f7f07cc
commit
e805baf6eb
|
@ -122,42 +122,36 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
|
|
||||||
private fun checkIntents() {
|
private fun checkIntents() {
|
||||||
if (intent?.action == Intent.ACTION_SEND && intent.type.startsWith("image/")) {
|
if (intent?.action == Intent.ACTION_SEND && intent.type.startsWith("image/")) {
|
||||||
handlePermission(PERMISSION_WRITE_STORAGE) {
|
getStoragePermission {
|
||||||
if (it) {
|
|
||||||
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
|
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
|
||||||
tryOpenUri(uri)
|
tryOpenUri(uri)
|
||||||
} else {
|
|
||||||
toast(R.string.no_storage_permissions)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intent?.action == Intent.ACTION_SEND_MULTIPLE && intent.type.startsWith("image/")) {
|
if (intent?.action == Intent.ACTION_SEND_MULTIPLE && intent.type.startsWith("image/")) {
|
||||||
handlePermission(PERMISSION_WRITE_STORAGE) {
|
getStoragePermission {
|
||||||
if (it) {
|
|
||||||
val imageUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
|
val imageUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
|
||||||
for (uri in imageUris) {
|
imageUris.any { tryOpenUri(it) }
|
||||||
if (tryOpenUri(uri)) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
toast(R.string.no_storage_permissions)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
|
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
|
||||||
val path = intent.data!!.path
|
getStoragePermission {
|
||||||
|
val path = intent.data.path
|
||||||
|
openPath(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getStoragePermission(callback: () -> Unit) {
|
||||||
handlePermission(PERMISSION_WRITE_STORAGE) {
|
handlePermission(PERMISSION_WRITE_STORAGE) {
|
||||||
if (it) {
|
if (it) {
|
||||||
openPath(path)
|
callback()
|
||||||
} else {
|
} else {
|
||||||
toast(R.string.no_storage_permissions)
|
toast(R.string.no_storage_permissions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun tryOpenUri(uri: Uri) = when {
|
private fun tryOpenUri(uri: Uri) = when {
|
||||||
uri.scheme == "file" -> openPath(uri.path)
|
uri.scheme == "file" -> openPath(uri.path)
|
||||||
|
|
Loading…
Reference in New Issue