move the media getter in a context extension

This commit is contained in:
tibbi 2017-05-20 21:33:21 +02:00
parent 946f143409
commit 4c7efca86d
2 changed files with 101 additions and 110 deletions

View file

@ -2,122 +2,21 @@ package com.simplemobiletools.gallery.asynctasks
import android.content.Context
import android.os.AsyncTask
import android.provider.MediaStore
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.VIDEOS
import com.simplemobiletools.gallery.extensions.getFilesFrom
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean = false, val isPickImage: Boolean = false,
val showAll: Boolean, val callback: (media: ArrayList<Medium>) -> Unit) :
AsyncTask<Void, Void, Unit>() {
var media = ArrayList<Medium>()
AsyncTask<Void, Void, ArrayList<Medium>>() {
override fun doInBackground(vararg params: Void): Unit {
if (showAll) {
media = getFilesFrom("")
} else {
media = getFilesFrom(mPath)
}
return Unit
override fun doInBackground(vararg params: Void): ArrayList<Medium> {
val path = if (showAll) "" else mPath
return context.getFilesFrom(path, isPickImage, isPickVideo)
}
private fun getFilesFrom(curPath: String): ArrayList<Medium> {
val curMedia = ArrayList<Medium>()
val showMedia = context.config.showMedia
val showHidden = context.config.shouldShowHidden
val projection = arrayOf(MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_MODIFIED,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.SIZE)
val uri = MediaStore.Files.getContentUri("external")
val selection = if (curPath.isEmpty()) null else "(${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?)"
val selectionArgs = if (curPath.isEmpty()) null else arrayOf("$curPath/%", "$curPath/%/%")
val cur = context.contentResolver.query(uri, projection, selection, selectionArgs, getSorting())
if (cur.moveToFirst()) {
var filename: String
var path: String
var dateTaken: Long
var dateModified: Long
var size: Long
var isImage: Boolean
var isVideo: Boolean
do {
path = cur.getStringValue(MediaStore.Images.Media.DATA)
size = cur.getLongValue(MediaStore.Images.Media.SIZE)
if (size == 0L) {
size = File(path).length()
}
if (size <= 0L) {
continue
}
filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
if (filename.isEmpty())
filename = path.getFilenameFromPath()
isImage = filename.isImageFast() || filename.isGif()
isVideo = if (isImage) false else filename.isVideoFast()
if (!isImage && !isVideo)
continue
if (isVideo && (isPickImage || showMedia == IMAGES))
continue
if (isImage && (isPickVideo || showMedia == VIDEOS))
continue
if (!showHidden && filename.startsWith('.'))
continue
dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L
val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size)
curMedia.add(medium)
} while (cur.moveToNext())
}
cur.close()
Medium.sorting = context.config.getFileSorting(mPath)
curMedia.sort()
return curMedia
}
private fun getSorting(): String {
val sorting = context.config.getFileSorting(mPath)
val sortValue = if (sorting and SORT_BY_NAME > 0)
MediaStore.Images.Media.DISPLAY_NAME
else if (sorting and SORT_BY_SIZE > 0)
MediaStore.Images.Media.SIZE
else if (sorting and SORT_BY_DATE_MODIFIED > 0)
MediaStore.Images.Media.DATE_MODIFIED
else
MediaStore.Images.Media.DATE_TAKEN
return if (sorting and SORT_DESCENDING > 0)
"$sortValue DESC"
else
"$sortValue ASC"
}
override fun onPostExecute(result: Unit?) {
super.onPostExecute(result)
override fun onPostExecute(media: ArrayList<Medium>) {
super.onPostExecute(media)
callback.invoke(media)
}
}

View file

@ -5,11 +5,17 @@ import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.gallery.activities.SettingsActivity
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.helpers.VIDEOS
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
@ -117,3 +123,89 @@ fun Context.getNoMediaFolders(): ArrayList<String> {
}
val Context.config: Config get() = Config.newInstance(this)
fun Context.getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList<Medium> {
val curMedia = ArrayList<Medium>()
val showMedia = config.showMedia
val showHidden = config.shouldShowHidden
val projection = arrayOf(MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_MODIFIED,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.SIZE)
val uri = MediaStore.Files.getContentUri("external")
val selection = if (curPath.isEmpty()) null else "(${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?)"
val selectionArgs = if (curPath.isEmpty()) null else arrayOf("$curPath/%", "$curPath/%/%")
val cur = contentResolver.query(uri, projection, selection, selectionArgs, getSortingForFolder(curPath))
if (cur.moveToFirst()) {
var filename: String
var path: String
var dateTaken: Long
var dateModified: Long
var size: Long
var isImage: Boolean
var isVideo: Boolean
do {
path = cur.getStringValue(MediaStore.Images.Media.DATA)
size = cur.getLongValue(MediaStore.Images.Media.SIZE)
if (size == 0L) {
size = File(path).length()
}
if (size <= 0L) {
continue
}
filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
if (filename.isEmpty())
filename = path.getFilenameFromPath()
isImage = filename.isImageFast() || filename.isGif()
isVideo = if (isImage) false else filename.isVideoFast()
if (!isImage && !isVideo)
continue
if (isVideo && (isPickImage || showMedia == IMAGES))
continue
if (isImage && (isPickVideo || showMedia == VIDEOS))
continue
if (!showHidden && filename.startsWith('.'))
continue
dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L
val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size)
curMedia.add(medium)
} while (cur.moveToNext())
}
cur.close()
Medium.sorting = config.getFileSorting(curPath)
curMedia.sort()
return curMedia
}
fun Context.getSortingForFolder(path: String): String {
val sorting = config.getFileSorting(path)
val sortValue = if (sorting and SORT_BY_NAME > 0)
MediaStore.Images.Media.DISPLAY_NAME
else if (sorting and SORT_BY_SIZE > 0)
MediaStore.Images.Media.SIZE
else if (sorting and SORT_BY_DATE_MODIFIED > 0)
MediaStore.Images.Media.DATE_MODIFIED
else
MediaStore.Images.Media.DATE_TAKEN
return if (sorting and SORT_DESCENDING > 0)
"$sortValue DESC"
else
"$sortValue ASC"
}