Skip to content

Commit e805baf

Browse files
committed
unify the storage permission requesting
1 parent b29f7f0 commit e805baf

1 file changed

Lines changed: 19 additions & 25 deletions

File tree

app/src/main/kotlin/com/simplemobiletools/draw/activities/MainActivity.kt

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,33 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
122122

123123
private fun checkIntents() {
124124
if (intent?.action == Intent.ACTION_SEND && intent.type.startsWith("image/")) {
125-
handlePermission(PERMISSION_WRITE_STORAGE) {
126-
if (it) {
127-
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
128-
tryOpenUri(uri)
129-
} else {
130-
toast(R.string.no_storage_permissions)
131-
}
125+
getStoragePermission {
126+
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
127+
tryOpenUri(uri)
132128
}
133129
}
134130

135131
if (intent?.action == Intent.ACTION_SEND_MULTIPLE && intent.type.startsWith("image/")) {
136-
handlePermission(PERMISSION_WRITE_STORAGE) {
137-
if (it) {
138-
val imageUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
139-
for (uri in imageUris) {
140-
if (tryOpenUri(uri)) {
141-
break
142-
}
143-
}
144-
} else {
145-
toast(R.string.no_storage_permissions)
146-
}
132+
getStoragePermission {
133+
val imageUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
134+
imageUris.any { tryOpenUri(it) }
147135
}
148136
}
149137

150138
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
151-
val path = intent.data!!.path
152-
handlePermission(PERMISSION_WRITE_STORAGE) {
153-
if (it) {
154-
openPath(path)
155-
} else {
156-
toast(R.string.no_storage_permissions)
157-
}
139+
getStoragePermission {
140+
val path = intent.data.path
141+
openPath(path)
142+
}
143+
}
144+
}
145+
146+
private fun getStoragePermission(callback: () -> Unit) {
147+
handlePermission(PERMISSION_WRITE_STORAGE) {
148+
if (it) {
149+
callback()
150+
} else {
151+
toast(R.string.no_storage_permissions)
158152
}
159153
}
160154
}

0 commit comments

Comments
 (0)