Skip to content

Commit b29f7f0

Browse files
committed
try handling a few more share intents
1 parent 294b9df commit b29f7f0

3 files changed

Lines changed: 65 additions & 25 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
<category android:name="android.intent.category.DEFAULT"/>
4141
<data android:mimeType="image/*"/>
4242
</intent-filter>
43+
44+
<intent-filter>
45+
<action android:name="android.intent.action.SEND_MULTIPLE"/>
46+
<category android:name="android.intent.category.DEFAULT"/>
47+
<data android:mimeType="image/*"/>
48+
</intent-filter>
4349
</activity>
4450

4551
<activity

app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.simplemobiletools.draw
22

33
import android.graphics.Color
44
import android.graphics.drawable.ColorDrawable
5+
import android.net.Uri
56
import android.sax.RootElement
67
import android.util.Xml
78
import com.simplemobiletools.commons.extensions.getFileOutputStream
@@ -51,8 +52,8 @@ object Svg {
5152
}
5253
}
5354

54-
fun loadSvg(activity: MainActivity, file: File, canvas: MyCanvas) {
55-
val svg = parseSvg(file)
55+
fun loadSvg(activity: MainActivity, fileOrUri: Any, canvas: MyCanvas) {
56+
val svg = parseSvg(activity, fileOrUri)
5657

5758
canvas.clearCanvas()
5859
activity.setBackgroundColor(svg.background!!.color)
@@ -66,11 +67,15 @@ object Svg {
6667
}
6768
}
6869

69-
private fun parseSvg(file: File): SSvg {
70+
private fun parseSvg(activity: MainActivity, fileOrUri: Any): SSvg {
7071
var inputStream: InputStream? = null
7172
val svg = SSvg()
7273
try {
73-
inputStream = FileInputStream(file)
74+
inputStream = when (fileOrUri) {
75+
is File -> FileInputStream(fileOrUri)
76+
is Uri -> activity.contentResolver.openInputStream(fileOrUri)
77+
else -> null
78+
}
7479

7580
// Actual parsing (http://stackoverflow.com/a/4828765)
7681
val ns = "http://www.w3.org/2000/svg"

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

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import java.io.ByteArrayOutputStream
3131
import java.io.File
3232
import java.io.FileOutputStream
3333

34-
3534
class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
3635
private val FOLDER_NAME = "images"
3736
private val FILE_NAME = "simple-draw.png"
@@ -126,10 +125,21 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
126125
handlePermission(PERMISSION_WRITE_STORAGE) {
127126
if (it) {
128127
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
129-
if (uri.scheme == "file") {
130-
openPath(uri.path)
131-
} else if (uri.scheme == "content") {
132-
openUri(uri)
128+
tryOpenUri(uri)
129+
} else {
130+
toast(R.string.no_storage_permissions)
131+
}
132+
}
133+
}
134+
135+
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+
}
133143
}
134144
} else {
135145
toast(R.string.no_storage_permissions)
@@ -149,30 +159,49 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
149159
}
150160
}
151161

152-
private fun openPath(path: String) {
153-
when {
154-
path.endsWith(".svg") -> {
155-
my_canvas.mBackgroundBitmap = null
156-
Svg.loadSvg(this, File(path), my_canvas)
157-
suggestedFileExtension = SVG
158-
}
159-
File(path).isImageSlow() -> {
160-
my_canvas.drawBitmap(this, path)
161-
suggestedFileExtension = JPG
162-
}
163-
else -> toast(R.string.invalid_file_format)
162+
private fun tryOpenUri(uri: Uri) = when {
163+
uri.scheme == "file" -> openPath(uri.path)
164+
uri.scheme == "content" -> openUri(uri, intent)
165+
else -> false
166+
}
167+
168+
private fun openPath(path: String) = when {
169+
path.endsWith(".svg") -> {
170+
my_canvas.mBackgroundBitmap = null
171+
Svg.loadSvg(this, File(path), my_canvas)
172+
suggestedFileExtension = SVG
173+
true
174+
}
175+
File(path).isImageSlow() -> {
176+
my_canvas.drawBitmap(this, path)
177+
suggestedFileExtension = JPG
178+
true
179+
}
180+
else -> {
181+
toast(R.string.invalid_file_format)
182+
false
164183
}
165184
}
166185

167-
private fun openUri(uri: Uri) {
186+
private fun openUri(uri: Uri, intent: Intent): Boolean {
168187
val mime = MimeTypeMap.getSingleton()
169-
val type = mime.getExtensionFromMimeType(contentResolver.getType(uri))
170-
when (type) {
188+
val type = mime.getExtensionFromMimeType(contentResolver.getType(uri)) ?: intent.type
189+
return when (type) {
190+
"svg", "image/svg+xml" -> {
191+
my_canvas.mBackgroundBitmap = null
192+
Svg.loadSvg(this, uri, my_canvas)
193+
suggestedFileExtension = SVG
194+
true
195+
}
171196
"jpg", "jpeg", "png" -> {
172197
my_canvas.drawBitmap(this, uri)
173198
suggestedFileExtension = JPG
199+
true
200+
}
201+
else -> {
202+
toast(R.string.invalid_file_format)
203+
false
174204
}
175-
else -> toast(R.string.invalid_file_format)
176205
}
177206
}
178207

0 commit comments

Comments
 (0)