update commons to 3.13.9

This commit is contained in:
tibbi 2018-02-26 22:59:59 +01:00
parent 0765a67caf
commit 7ae84cf087
4 changed files with 38 additions and 32 deletions

View File

@ -45,7 +45,7 @@ ext {
}
dependencies {
implementation 'com.simplemobiletools:commons:3.11.6'
implementation 'com.simplemobiletools:commons:3.13.9'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"

View File

@ -18,6 +18,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
import com.simplemobiletools.commons.helpers.LICENSE_LEAK_CANARY
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.draw.BuildConfig
import com.simplemobiletools.draw.R
@ -31,7 +32,6 @@ import com.simplemobiletools.draw.models.Svg
import kotlinx.android.synthetic.main.activity_main.*
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
class MainActivity : SimpleActivity(), CanvasListener {
@ -176,7 +176,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
getStoragePermission {
val path = intent.data.path
val path = getRealPathFromURI(intent.data) ?: intent.dataString
openPath(path)
}
}
@ -267,14 +267,14 @@ class MainActivity : SimpleActivity(), CanvasListener {
}
private fun confirmImage() {
val file = File(defaultPath)
if (intentUri?.scheme == "content") {
val outputStream = contentResolver.openOutputStream(intentUri)
saveToOutputStream(outputStream, file.getCompressionFormat())
saveToOutputStream(outputStream, defaultPath.getCompressionFormat())
} else {
handlePermission(PERMISSION_WRITE_STORAGE) {
getFileOutputStream(file) {
saveToOutputStream(it, file.getCompressionFormat())
val fileDirItem = FileDirItem(defaultPath, defaultPath.getFilenameFromPath())
getFileOutputStream(fileDirItem, true) {
saveToOutputStream(it, defaultPath.getCompressionFormat())
}
}
}
@ -310,36 +310,39 @@ class MainActivity : SimpleActivity(), CanvasListener {
}
private fun shareImage() {
val uri = getImageUri(my_canvas.getBitmap())
if (uri != null) {
shareUri(uri, BuildConfig.APPLICATION_ID)
getImagePath(my_canvas.getBitmap()) {
if (it != null) {
sharePathIntent(it, BuildConfig.APPLICATION_ID)
} else {
toast(R.string.unknown_error_occurred)
}
}
}
private fun getImageUri(bitmap: Bitmap): Uri? {
private fun getImagePath(bitmap: Bitmap, callback: (path: String?) -> Unit) {
val bytes = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bytes)
val folder = File(cacheDir, FOLDER_NAME)
if (!folder.exists()) {
if (!folder.mkdir()) {
return null
callback(null)
return
}
}
val file = File(folder, FILE_NAME)
var fileOutputStream: FileOutputStream? = null
val newPath = "$folder/$FILE_NAME"
val fileDirItem = FileDirItem(newPath, FILE_NAME)
getFileOutputStream(fileDirItem, true) {
try {
fileOutputStream = FileOutputStream(file)
fileOutputStream.write(bytes.toByteArray())
it?.write(bytes.toByteArray())
callback(newPath)
} catch (e: Exception) {
} finally {
fileOutputStream?.close()
it?.close()
}
return getFilePublicUri(file, BuildConfig.APPLICATION_ID)
}
callback("")
}
private fun clearCanvas() {

View File

@ -4,6 +4,7 @@ import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.activities.SimpleActivity
import com.simplemobiletools.draw.helpers.JPG
@ -84,22 +85,23 @@ class SaveImageDialog(val activity: SimpleActivity, val defaultExtension: String
when (file.extension) {
SVG -> Svg.saveSvg(activity, file, canvas)
else -> saveImageFile(file)
else -> saveImageFile(file.absolutePath)
}
activity.scanFile(file) {}
return true
}
private fun saveImageFile(file: File) {
activity.getFileOutputStream(file) {
writeToOutputStream(file, it!!)
private fun saveImageFile(path: String) {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
activity.getFileOutputStream(fileDirItem, true) {
writeToOutputStream(path, it!!)
activity.toast(R.string.file_saved)
}
}
private fun writeToOutputStream(file: File, out: OutputStream) {
private fun writeToOutputStream(path: String, out: OutputStream) {
out.use {
canvas.getBitmap().compress(file.getCompressionFormat(), 70, out)
canvas.getBitmap().compress(path.getCompressionFormat(), 70, out)
}
}

View File

@ -6,11 +6,12 @@ import android.net.Uri
import android.sax.RootElement
import android.util.Xml
import com.simplemobiletools.commons.extensions.getFileOutputStream
import com.simplemobiletools.commons.extensions.toFileDirItem
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.draw.views.MyCanvas
import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.activities.MainActivity
import com.simplemobiletools.draw.activities.SimpleActivity
import com.simplemobiletools.draw.views.MyCanvas
import java.io.*
import java.util.*
@ -18,7 +19,7 @@ object Svg {
fun saveSvg(activity: SimpleActivity, file: File, canvas: MyCanvas) {
val backgroundColor = (canvas.background as ColorDrawable).color
activity.getFileOutputStream(file) {
activity.getFileOutputStream(file.toFileDirItem(activity), true) {
val writer = BufferedWriter(OutputStreamWriter(it))
writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height)
writer.close()