try handling a few more share intents

This commit is contained in:
tibbi 2017-10-22 11:30:24 +02:00
parent 294b9df67d
commit b29f7f07cc
3 changed files with 65 additions and 25 deletions

View File

@ -40,6 +40,12 @@
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/> <data android:mimeType="image/*"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity> </activity>
<activity <activity

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.draw
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.sax.RootElement import android.sax.RootElement
import android.util.Xml import android.util.Xml
import com.simplemobiletools.commons.extensions.getFileOutputStream import com.simplemobiletools.commons.extensions.getFileOutputStream
@ -51,8 +52,8 @@ object Svg {
} }
} }
fun loadSvg(activity: MainActivity, file: File, canvas: MyCanvas) { fun loadSvg(activity: MainActivity, fileOrUri: Any, canvas: MyCanvas) {
val svg = parseSvg(file) val svg = parseSvg(activity, fileOrUri)
canvas.clearCanvas() canvas.clearCanvas()
activity.setBackgroundColor(svg.background!!.color) activity.setBackgroundColor(svg.background!!.color)
@ -66,11 +67,15 @@ object Svg {
} }
} }
private fun parseSvg(file: File): SSvg { private fun parseSvg(activity: MainActivity, fileOrUri: Any): SSvg {
var inputStream: InputStream? = null var inputStream: InputStream? = null
val svg = SSvg() val svg = SSvg()
try { try {
inputStream = FileInputStream(file) inputStream = when (fileOrUri) {
is File -> FileInputStream(fileOrUri)
is Uri -> activity.contentResolver.openInputStream(fileOrUri)
else -> null
}
// Actual parsing (http://stackoverflow.com/a/4828765) // Actual parsing (http://stackoverflow.com/a/4828765)
val ns = "http://www.w3.org/2000/svg" val ns = "http://www.w3.org/2000/svg"

View File

@ -31,7 +31,6 @@ import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener { class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
private val FOLDER_NAME = "images" private val FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png" private val FILE_NAME = "simple-draw.png"
@ -126,10 +125,21 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) { if (it) {
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM) val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
if (uri.scheme == "file") { tryOpenUri(uri)
openPath(uri.path) } else {
} else if (uri.scheme == "content") { toast(R.string.no_storage_permissions)
openUri(uri) }
}
}
if (intent?.action == Intent.ACTION_SEND_MULTIPLE && intent.type.startsWith("image/")) {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
val imageUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
for (uri in imageUris) {
if (tryOpenUri(uri)) {
break
}
} }
} else { } else {
toast(R.string.no_storage_permissions) toast(R.string.no_storage_permissions)
@ -149,30 +159,49 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
} }
} }
private fun openPath(path: String) { private fun tryOpenUri(uri: Uri) = when {
when { uri.scheme == "file" -> openPath(uri.path)
path.endsWith(".svg") -> { uri.scheme == "content" -> openUri(uri, intent)
my_canvas.mBackgroundBitmap = null else -> false
Svg.loadSvg(this, File(path), my_canvas) }
suggestedFileExtension = SVG
} private fun openPath(path: String) = when {
File(path).isImageSlow() -> { path.endsWith(".svg") -> {
my_canvas.drawBitmap(this, path) my_canvas.mBackgroundBitmap = null
suggestedFileExtension = JPG Svg.loadSvg(this, File(path), my_canvas)
} suggestedFileExtension = SVG
else -> toast(R.string.invalid_file_format) true
}
File(path).isImageSlow() -> {
my_canvas.drawBitmap(this, path)
suggestedFileExtension = JPG
true
}
else -> {
toast(R.string.invalid_file_format)
false
} }
} }
private fun openUri(uri: Uri) { private fun openUri(uri: Uri, intent: Intent): Boolean {
val mime = MimeTypeMap.getSingleton() val mime = MimeTypeMap.getSingleton()
val type = mime.getExtensionFromMimeType(contentResolver.getType(uri)) val type = mime.getExtensionFromMimeType(contentResolver.getType(uri)) ?: intent.type
when (type) { return when (type) {
"svg", "image/svg+xml" -> {
my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, uri, my_canvas)
suggestedFileExtension = SVG
true
}
"jpg", "jpeg", "png" -> { "jpg", "jpeg", "png" -> {
my_canvas.drawBitmap(this, uri) my_canvas.drawBitmap(this, uri)
suggestedFileExtension = JPG suggestedFileExtension = JPG
true
}
else -> {
toast(R.string.invalid_file_format)
false
} }
else -> toast(R.string.invalid_file_format)
} }
} }