From 5c9c0d4ac9e95f49c6930db6fb0f4b03446b322d Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 8 Dec 2016 21:02:40 +0100 Subject: [PATCH] replace some functions with filepicker extensions --- .../gallery/activities/MainActivity.kt | 3 +-- .../gallery/activities/SimpleActivity.kt | 9 --------- .../gallery/dialogs/RenameFileDialog.kt | 10 +--------- .../simplemobiletools/gallery/extensions/activity.kt | 5 +++-- .../com/simplemobiletools/gallery/extensions/uri.kt | 12 ------------ 5 files changed, 5 insertions(+), 34 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index bc77f6923..f89422816 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -18,7 +18,6 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog -import com.simplemobiletools.gallery.extensions.getImageMimeType import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.activity_main.* @@ -265,7 +264,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val path = resultData.data.path val uri = Uri.fromFile(File(path)) if (mIsGetImageContentIntent || mIsGetVideoContentIntent || mIsGetAnyContentIntent) { - val type = uri.getImageMimeType() + val type = File(path).getMimeType("image/jpeg") setDataAndTypeAndNormalize(uri, type) flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION } else if (mIsPickImageIntent || mIsPickVideoIntent) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt index 9aa3b6d49..c7585dd98 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt @@ -2,7 +2,6 @@ package com.simplemobiletools.gallery.activities import android.annotation.TargetApi import android.app.Activity -import android.content.ContentValues import android.content.Intent import android.os.Build import android.os.Bundle @@ -10,7 +9,6 @@ import android.provider.MediaStore import android.support.v7.app.AppCompatActivity import android.view.MenuItem import android.view.View -import com.simplemobiletools.filepicker.extensions.isImageSlow import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.helpers.Config @@ -70,13 +68,6 @@ open class SimpleActivity : AppCompatActivity() { fun isShowingPermDialog(file: File) = isShowingWritePermissions(file, mConfig.treeUri, OPEN_DOCUMENT_TREE) - fun deleteFromMediaStore(file: File): Boolean { - val values = ContentValues() - values.put(MediaStore.MediaColumns.DATA, file.absolutePath) - val uri = if (file.isImageSlow()) MediaStore.Images.Media.EXTERNAL_CONTENT_URI else MediaStore.Video.Media.EXTERNAL_CONTENT_URI - return contentResolver.delete(uri, "${MediaStore.MediaColumns.DATA} = '${file.absolutePath}'", null) == 1 - } - fun showSystemUI() { supportActionBar?.show() window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt index ea7aef61c..f08f29cd1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt @@ -1,8 +1,6 @@ package com.simplemobiletools.gallery.dialogs import android.app.AlertDialog -import android.content.ContentValues -import android.provider.MediaStore import android.view.LayoutInflater import android.view.WindowManager import com.simplemobiletools.filepicker.extensions.* @@ -78,13 +76,7 @@ class RenameFileDialog(val activity: SimpleActivity, val file: File, val callbac } private fun sendSuccess(oldFile: File, newFile: File) { - val values = ContentValues() - values.put(MediaStore.MediaColumns.DISPLAY_NAME, newFile.name) - values.put(MediaStore.MediaColumns.TITLE, newFile.name) - val uri = if (oldFile.isImageSlow()) MediaStore.Images.Media.EXTERNAL_CONTENT_URI else MediaStore.Video.Media.EXTERNAL_CONTENT_URI - val updated = activity.contentResolver.update(uri, values, "${MediaStore.MediaColumns.DATA} = '${oldFile.absolutePath}'", null) == 1 - - if (updated) { + if (activity.updateInMediaStore(oldFile, newFile)) { callback.invoke(newFile) } else { val changedFiles = arrayListOf(oldFile, newFile) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index 835680fd9..f502bec37 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -8,6 +8,7 @@ import android.util.DisplayMetrics import android.view.KeyCharacterMap import android.view.KeyEvent import android.view.ViewConfiguration +import com.simplemobiletools.filepicker.extensions.getMimeType import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.helpers.REQUEST_EDIT_IMAGE @@ -45,7 +46,7 @@ fun Activity.shareMedia(media: List) { fun Activity.setAsWallpaper(file: File) { val intent = Intent(Intent.ACTION_ATTACH_DATA) val uri = Uri.fromFile(file) - intent.setDataAndType(uri, uri.getImageMimeType()) + intent.setDataAndType(uri, file.getMimeType("image/jpeg")) val chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with)) if (intent.resolveActivity(packageManager) != null) { @@ -58,7 +59,7 @@ fun Activity.setAsWallpaper(file: File) { fun Activity.openWith(file: File) { val intent = Intent(Intent.ACTION_VIEW) val uri = Uri.fromFile(file) - intent.setDataAndType(uri, uri.getImageMimeType()) + intent.setDataAndType(uri, file.getMimeType("image/jpeg")) val chooser = Intent.createChooser(intent, getString(R.string.open_with)) if (intent.resolveActivity(packageManager) != null) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt deleted file mode 100644 index ec86cb6ff..000000000 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/uri.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.simplemobiletools.gallery.extensions - -import android.net.Uri -import com.simplemobiletools.filepicker.extensions.getMimeType - -fun Uri.getImageMimeType(): String { - val mimeType = getMimeType(toString()) - return if (mimeType.isNotEmpty()) - mimeType - else - "image/jpeg" -}