From c9c5f549be6cff9b4676325e8f2feede438866d3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 4 Dec 2016 17:52:10 +0100 Subject: [PATCH] some updates to the way we get media from mediastore --- .../gallery/activities/ViewPagerActivity.kt | 65 ++++++++++--------- .../asynctasks/GetDirectoriesAsynctask.kt | 18 ++--- .../gallery/asynctasks/GetMediaAsynctask.kt | 21 +++--- .../gallery/extensions/cursor.kt | 7 ++ 4 files changed, 64 insertions(+), 47 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/extensions/cursor.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 0d4e6263a..9db0eb762 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -28,7 +28,6 @@ import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.activity_medium.* import java.io.File import java.util.* -import java.util.regex.Pattern class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPagerFragment.FragmentClickListener { private var mMedia: MutableList? = null @@ -86,13 +85,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View if (isDirEmpty()) return - val pagerAdapter = MyPagerAdapter(this, supportFragmentManager, mMedia!!) - view_pager.apply { - adapter = pagerAdapter - currentItem = mPos - addOnPageChangeListener(this@ViewPagerActivity) - } - + updatePagerItems() window.decorView.setOnSystemUiVisibilityChangeListener(this) updateActionbarTitle() undo_delete.setOnClickListener { undoDeletion() } @@ -165,6 +158,15 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View adapter.updateItems(mPos) } + private fun updatePagerItems() { + val pagerAdapter = MyPagerAdapter(this, supportFragmentManager, mMedia!!) + view_pager.apply { + adapter = pagerAdapter + currentItem = mPos + addOnPageChangeListener(this@ViewPagerActivity) + } + } + private fun displayCopyDialog() { val files = ArrayList() files.add(getCurrentFile()) @@ -311,32 +313,35 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val where = "${MediaStore.Images.Media.DATA} LIKE ? " val args = arrayOf("$mDirectory%") - val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE) - val cursor = contentResolver.query(uri, columns, where, args, null) - val pattern = "${Pattern.quote(mDirectory)}/[^/]*" + val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE) + var cursor: Cursor? = null - if (cursor?.moveToFirst() == true) { - val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) - do { - val curPath = cursor.getString(pathIndex) ?: continue + try { + cursor = contentResolver.query(uri, columns, where, args, null) - val file = File(curPath) - if (!file.exists()) { - invalidFiles.add(file) - continue - } + if (cursor?.moveToFirst() == true) { + val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) + do { + val curPath = cursor.getString(pathIndex) ?: continue + if (curPath != mToBeDeleted && curPath != mBeingDeleted) { + val file = File(curPath) + if (file.exists()) { + if (file.parent != mDirectory) + continue - if (curPath.matches(pattern.toRegex()) && curPath != mToBeDeleted && curPath != mBeingDeleted) { - val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED) - val timestamp = cursor.getLong(dateIndex) - - val sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE) - val size = cursor.getLong(sizeIndex) - media.add(Medium(file.name, curPath, i == 1, timestamp, size)) - } - } while (cursor.moveToNext()) + val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) + val timestamp = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) + val size = cursor.getLongValue(MediaStore.Images.Media.SIZE) + media.add(Medium(name, curPath, i == 1, timestamp, size)) + } else { + invalidFiles.add(file) + } + } + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() } - cursor?.close() } scanFiles(invalidFiles) {} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 737813b4d..e2c7664bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -5,11 +5,12 @@ import android.database.Cursor import android.os.AsyncTask import android.provider.MediaStore import com.simplemobiletools.filepicker.extensions.scanFiles -import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.extensions.getHumanizedFilename +import com.simplemobiletools.gallery.extensions.getLongValue +import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.SORT_BY_NAME import com.simplemobiletools.gallery.helpers.SORT_DESCENDING -import com.simplemobiletools.gallery.extensions.getHumanizedFilename import com.simplemobiletools.gallery.models.Directory import java.io.File import java.util.* @@ -37,16 +38,17 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI } - val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED) + val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE) val order = getSortOrder() var cursor: Cursor? = null try { cursor = context.contentResolver.query(uri, columns, null, null, order) - if (cursor != null && cursor.moveToFirst()) { + + if (cursor?.moveToFirst() == true) { val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) do { - val fullPath: String = cursor.getString(pathIndex) ?: continue + val fullPath = cursor.getString(pathIndex) ?: continue val file = File(fullPath) val parentDir = file.parent @@ -55,8 +57,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va continue } - val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED) - val timestamp = cursor.getLong(dateIndex) if (directories.containsKey(parentDir)) { val directory: Directory = directories[parentDir]!! val newImageCnt = directory.mediaCnt + 1 @@ -68,7 +68,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va dirName += " ${context.resources.getString(R.string.hidden)}" } - directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, timestamp, file.length())) + val timestamp = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) + val size = cursor.getLongValue(MediaStore.Images.Media.SIZE) + directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, timestamp, size)) } } while (cursor.moveToNext()) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index a10903c7c..98d8cbf2e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -5,11 +5,12 @@ import android.database.Cursor import android.os.AsyncTask import android.provider.MediaStore import com.simplemobiletools.filepicker.extensions.scanFiles +import com.simplemobiletools.gallery.extensions.getLongValue +import com.simplemobiletools.gallery.extensions.getStringValue import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.models.Medium import java.io.File import java.util.* -import java.util.regex.Pattern class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean, val isPickImage: Boolean, val mToBeDeleted: List, val callback: (media: ArrayList) -> Unit) : AsyncTask>() { @@ -36,24 +37,26 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo } val where = "${MediaStore.Images.Media.DATA} LIKE ?" val args = arrayOf("$mPath%") - val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED) - val pattern = "${Pattern.quote(mPath)}/[^/]*" + val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE) var cursor: Cursor? = null try { cursor = context.contentResolver.query(uri, columns, where, args, null) - if (cursor != null && cursor.moveToFirst()) { + if (cursor?.moveToFirst() == true) { val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) do { val curPath = cursor.getString(pathIndex) ?: continue - - if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath)) { + if (!mToBeDeleted.contains(curPath)) { val file = File(curPath) if (file.exists()) { - val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED) - val timestamp = cursor.getLong(dateIndex) - media.add(Medium(file.name, curPath, i == 1, timestamp, file.length())) + if (file.parent != mPath) + continue + + val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) + val timestamp = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) + val size = cursor.getLongValue(MediaStore.Images.Media.SIZE) + media.add(Medium(name, curPath, i == 1, timestamp, size)) } else { invalidFiles.add(file) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/cursor.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/cursor.kt new file mode 100644 index 000000000..125de9983 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/cursor.kt @@ -0,0 +1,7 @@ +package com.simplemobiletools.gallery.extensions + +import android.database.Cursor + +fun Cursor.getStringValue(key: String) = getString(getColumnIndex(key)) + +fun Cursor.getLongValue(key: String) = getLong(getColumnIndex(key))