shortening some code by importing more specific mediastore classes

This commit is contained in:
tibbi 2020-04-14 16:25:27 +02:00
parent c17fb071fc
commit ed501572d1
8 changed files with 74 additions and 63 deletions

View File

@ -9,6 +9,8 @@ import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.provider.MediaStore.Images
import android.provider.MediaStore.Video
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
@ -736,24 +738,24 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
private fun isGetContentIntent(intent: Intent) = intent.action == Intent.ACTION_GET_CONTENT && intent.type != null
private fun isGetImageContentIntent(intent: Intent) = isGetContentIntent(intent) &&
(intent.type!!.startsWith("image/") || intent.type == MediaStore.Images.Media.CONTENT_TYPE)
(intent.type!!.startsWith("image/") || intent.type == Images.Media.CONTENT_TYPE)
private fun isGetVideoContentIntent(intent: Intent) = isGetContentIntent(intent) &&
(intent.type!!.startsWith("video/") || intent.type == MediaStore.Video.Media.CONTENT_TYPE)
(intent.type!!.startsWith("video/") || intent.type == Video.Media.CONTENT_TYPE)
private fun isGetAnyContentIntent(intent: Intent) = isGetContentIntent(intent) && intent.type == "*/*"
private fun isSetWallpaperIntent(intent: Intent?) = intent?.action == Intent.ACTION_SET_WALLPAPER
private fun hasImageContentData(intent: Intent) = (intent.data == MediaStore.Images.Media.EXTERNAL_CONTENT_URI ||
intent.data == MediaStore.Images.Media.INTERNAL_CONTENT_URI)
private fun hasImageContentData(intent: Intent) = (intent.data == Images.Media.EXTERNAL_CONTENT_URI ||
intent.data == Images.Media.INTERNAL_CONTENT_URI)
private fun hasVideoContentData(intent: Intent) = (intent.data == MediaStore.Video.Media.EXTERNAL_CONTENT_URI ||
intent.data == MediaStore.Video.Media.INTERNAL_CONTENT_URI)
private fun hasVideoContentData(intent: Intent) = (intent.data == Video.Media.EXTERNAL_CONTENT_URI ||
intent.data == Video.Media.INTERNAL_CONTENT_URI)
private fun isImageType(intent: Intent) = (intent.type?.startsWith("image/") == true || intent.type == MediaStore.Images.Media.CONTENT_TYPE)
private fun isImageType(intent: Intent) = (intent.type?.startsWith("image/") == true || intent.type == Images.Media.CONTENT_TYPE)
private fun isVideoType(intent: Intent) = (intent.type?.startsWith("video/") == true || intent.type == MediaStore.Video.Media.CONTENT_TYPE)
private fun isVideoType(intent: Intent) = (intent.type?.startsWith("video/") == true || intent.type == Video.Media.CONTENT_TYPE)
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
if (resultCode == Activity.RESULT_OK) {

View File

@ -3,7 +3,8 @@ package com.simplemobiletools.gallery.pro.activities
import android.annotation.SuppressLint
import android.database.ContentObserver
import android.net.Uri
import android.provider.MediaStore
import android.provider.MediaStore.Images
import android.provider.MediaStore.Video
import android.view.WindowManager
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.FilePickerDialog
@ -70,8 +71,8 @@ open class SimpleActivity : BaseSimpleActivity() {
protected fun registerFileUpdateListener() {
try {
contentResolver.registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, observer)
contentResolver.registerContentObserver(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true, observer)
contentResolver.registerContentObserver(Images.Media.EXTERNAL_CONTENT_URI, true, observer)
contentResolver.registerContentObserver(Video.Media.EXTERNAL_CONTENT_URI, true, observer)
} catch (ignored: Exception) {
}
}

View File

@ -20,7 +20,7 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.provider.MediaStore.Images
import android.text.Html
import android.view.Menu
import android.view.MenuItem
@ -261,10 +261,10 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
if (uri != null) {
var cursor: Cursor? = null
try {
val proj = arrayOf(MediaStore.Images.Media.DATA)
val proj = arrayOf(Images.Media.DATA)
cursor = contentResolver.query(uri, proj, null, null, null)
if (cursor?.moveToFirst() == true) {
mPath = cursor.getStringValue(MediaStore.Images.Media.DATA)
mPath = cursor.getStringValue(Images.Media.DATA)
}
} finally {
cursor?.close()

View File

@ -13,6 +13,7 @@ import android.media.ExifInterface
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.provider.MediaStore.Images
import android.util.DisplayMetrics
import android.view.View
import androidx.appcompat.app.AppCompatActivity
@ -428,10 +429,10 @@ fun Activity.fixDateTaken(paths: ArrayList<String>, showToasts: Boolean, hasResc
val uri = getFileUri(path)
ContentProviderOperation.newUpdate(uri).apply {
val selection = "${MediaStore.Images.Media.DATA} = ?"
val selection = "${Images.Media.DATA} = ?"
val selectionArgs = arrayOf(path)
withSelection(selection, selectionArgs)
withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp)
withValue(Images.Media.DATE_TAKEN, timestamp)
operations.add(build())
}

View File

@ -7,7 +7,8 @@ import android.content.Intent
import android.database.Cursor
import android.graphics.drawable.PictureDrawable
import android.media.AudioManager
import android.provider.MediaStore
import android.provider.MediaStore.Files
import android.provider.MediaStore.Images
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
@ -303,11 +304,11 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
ensureBackgroundThread {
val folders = ArrayList<String>()
val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf(MediaStore.Files.FileColumns.DATA)
val selection = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? AND ${MediaStore.Files.FileColumns.TITLE} LIKE ?"
val selectionArgs = arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_NONE.toString(), "%$NOMEDIA%")
val sortOrder = "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
val uri = Files.getContentUri("external")
val projection = arrayOf(Files.FileColumns.DATA)
val selection = "${Files.FileColumns.MEDIA_TYPE} = ? AND ${Files.FileColumns.TITLE} LIKE ?"
val selectionArgs = arrayOf(Files.FileColumns.MEDIA_TYPE_NONE.toString(), "%$NOMEDIA%")
val sortOrder = "${Files.FileColumns.DATE_MODIFIED} DESC"
val OTGPath = config.OTGPath
var cursor: Cursor? = null
@ -315,7 +316,7 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
if (cursor?.moveToFirst() == true) {
do {
val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA) ?: continue
val path = cursor.getStringValue(Files.FileColumns.DATA) ?: continue
val noMediaFile = File(path)
if (getDoesFilePathExist(noMediaFile.absolutePath, OTGPath) && noMediaFile.name == NOMEDIA) {
folders.add("${noMediaFile.parent}/")
@ -886,11 +887,11 @@ fun Context.updateDirectoryPath(path: String) {
fun Context.getFileDateTaken(path: String): Long {
val projection = arrayOf(
MediaStore.Images.Media.DATE_TAKEN
Images.Media.DATE_TAKEN
)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.Images.Media.DATA} = ?"
val uri = Files.getContentUri("external")
val selection = "${Images.Media.DATA} = ?"
val selectionArgs = arrayOf(path)
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
@ -898,7 +899,7 @@ fun Context.getFileDateTaken(path: String): Long {
if (cursor.moveToFirst()) {
do {
try {
return cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
return cursor.getLongValue(Images.Media.DATE_TAKEN)
} catch (ignored: Exception) {
}
} while (cursor.moveToNext())

View File

@ -2,6 +2,8 @@ package com.simplemobiletools.gallery.pro.fragments
import android.media.ExifInterface
import android.provider.MediaStore
import android.provider.MediaStore.Files
import android.provider.MediaStore.Images
import android.view.MotionEvent
import androidx.fragment.app.Fragment
import com.simplemobiletools.commons.extensions.*
@ -91,14 +93,14 @@ abstract class ViewPagerFragment : Fragment() {
fun getPathToLoad(medium: Medium) = if (context?.isPathOnOTG(medium.path) == true) medium.path.getOTGPublicPath(context!!) else medium.path
private fun getFileLastModified(file: File): String {
val projection = arrayOf(MediaStore.Images.Media.DATE_MODIFIED)
val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf(Images.Media.DATE_MODIFIED)
val uri = Files.getContentUri("external")
val selection = "${MediaStore.MediaColumns.DATA} = ?"
val selectionArgs = arrayOf(file.absolutePath)
val cursor = context!!.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
return if (cursor.moveToFirst()) {
val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L
val dateModified = cursor.getLongValue(Images.Media.DATE_MODIFIED) * 1000L
dateModified.formatDate(context!!)
} else {
file.lastModified().formatDate(context!!)

View File

@ -5,7 +5,8 @@ import android.database.Cursor
import android.net.Uri
import android.os.Environment
import android.provider.BaseColumns
import android.provider.MediaStore
import android.provider.MediaStore.Files
import android.provider.MediaStore.Images
import android.text.format.DateFormat
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
@ -54,8 +55,8 @@ class MediaFetcher(val context: Context) {
).filter { context.getDoesFilePathExist(it, OTGPath) })
val filterMedia = context.config.filterMedia
val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf(MediaStore.Images.Media.DATA)
val uri = Files.getContentUri("external")
val projection = arrayOf(Images.Media.DATA)
val selection = getSelectionQuery(filterMedia)
val selectionArgs = getSelectionArgsQuery(filterMedia).toTypedArray()
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
@ -72,8 +73,8 @@ class MediaFetcher(val context: Context) {
}
private fun getLatestFileFolders(): LinkedHashSet<String> {
val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf(MediaStore.Images.ImageColumns.DATA)
val uri = Files.getContentUri("external")
val projection = arrayOf(Images.ImageColumns.DATA)
val parents = LinkedHashSet<String>()
val sorting = "${BaseColumns._ID} DESC LIMIT 50"
var cursor: Cursor? = null
@ -81,7 +82,7 @@ class MediaFetcher(val context: Context) {
cursor = context.contentResolver.query(uri, projection, null, null, sorting)
if (cursor?.moveToFirst() == true) {
do {
val path = cursor.getStringValue(MediaStore.Images.ImageColumns.DATA) ?: continue
val path = cursor.getStringValue(Images.ImageColumns.DATA) ?: continue
parents.add(path.getParentPath())
} while (cursor.moveToNext())
}
@ -98,33 +99,33 @@ class MediaFetcher(val context: Context) {
val query = StringBuilder()
if (filterMedia and TYPE_IMAGES != 0) {
photoExtensions.forEach {
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
}
}
if (filterMedia and TYPE_PORTRAITS != 0) {
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
}
if (filterMedia and TYPE_VIDEOS != 0) {
videoExtensions.forEach {
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
}
}
if (filterMedia and TYPE_GIFS != 0) {
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
}
if (filterMedia and TYPE_RAWS != 0) {
rawExtensions.forEach {
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
}
}
if (filterMedia and TYPE_SVGS != 0) {
query.append("${MediaStore.Images.Media.DATA} LIKE ? OR ")
query.append("${Images.Media.DATA} LIKE ? OR ")
}
return query.toString().trim().removeSuffix("OR")
@ -176,7 +177,7 @@ class MediaFetcher(val context: Context) {
cursor.use {
if (cursor.moveToFirst()) {
do {
val path = cursor.getStringValue(MediaStore.Images.Media.DATA)
val path = cursor.getStringValue(Images.Media.DATA)
val parentPath = File(path).parent ?: continue
if (!includedFolders.contains(parentPath) && !foldersToIgnore.contains(parentPath)) {
foldersToScan.add(parentPath)
@ -395,12 +396,12 @@ class MediaFetcher(val context: Context) {
val dateTakens = HashMap<String, Long>()
if (folder != FAVORITES) {
val projection = arrayOf(
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
Images.Media.DISPLAY_NAME,
Images.Media.DATE_TAKEN
)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?"
val uri = Files.getContentUri("external")
val selection = "${Images.Media.DATA} LIKE ? AND ${Images.Media.DATA} NOT LIKE ?"
val selectionArgs = arrayOf("$folder/%", "$folder/%/%")
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
@ -408,9 +409,9 @@ class MediaFetcher(val context: Context) {
if (cursor.moveToFirst()) {
do {
try {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
val dateTaken = cursor.getLongValue(Images.Media.DATE_TAKEN)
if (dateTaken != 0L) {
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME)
val name = cursor.getStringValue(Images.Media.DISPLAY_NAME)
dateTakens["$folder/$name"] = dateTaken
}
} catch (e: Exception) {

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.pro.jobs
import android.annotation.TargetApi
import android.app.job.JobInfo
import android.app.job.JobInfo.TriggerContentUri
import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.app.job.JobService
@ -12,6 +13,8 @@ import android.net.Uri
import android.os.Build
import android.os.Handler
import android.provider.MediaStore
import android.provider.MediaStore.Images
import android.provider.MediaStore.Video
import com.simplemobiletools.commons.extensions.getParentPath
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
@ -24,8 +27,8 @@ class NewPhotoFetcher : JobService() {
companion object {
const val PHOTO_VIDEO_CONTENT_JOB = 1
private val MEDIA_URI = Uri.parse("content://${MediaStore.AUTHORITY}/")
private val PHOTO_PATH_SEGMENTS = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.pathSegments
private val VIDEO_PATH_SEGMENTS = MediaStore.Video.Media.EXTERNAL_CONTENT_URI.pathSegments
private val PHOTO_PATH_SEGMENTS = Images.Media.EXTERNAL_CONTENT_URI.pathSegments
private val VIDEO_PATH_SEGMENTS = Video.Media.EXTERNAL_CONTENT_URI.pathSegments
}
private val mHandler = Handler()
@ -38,15 +41,15 @@ class NewPhotoFetcher : JobService() {
fun scheduleJob(context: Context) {
val componentName = ComponentName(context, NewPhotoFetcher::class.java)
val photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
val photoUri = Images.Media.EXTERNAL_CONTENT_URI
val videoUri = Video.Media.EXTERNAL_CONTENT_URI
JobInfo.Builder(PHOTO_VIDEO_CONTENT_JOB, componentName).apply {
addTriggerContentUri(JobInfo.TriggerContentUri(photoUri, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
addTriggerContentUri(JobInfo.TriggerContentUri(videoUri, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
addTriggerContentUri(JobInfo.TriggerContentUri(MEDIA_URI, 0))
addTriggerContentUri(TriggerContentUri(photoUri, TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
addTriggerContentUri(TriggerContentUri(videoUri, TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
addTriggerContentUri(TriggerContentUri(MEDIA_URI, 0))
try {
context.getSystemService(JobScheduler::class.java).schedule(build())
context.getSystemService(JobScheduler::class.java)?.schedule(build())
} catch (ignored: Exception) {
}
}
@ -54,7 +57,7 @@ class NewPhotoFetcher : JobService() {
fun isScheduled(context: Context): Boolean {
val jobScheduler = context.getSystemService(JobScheduler::class.java)
val jobs = jobScheduler.allPendingJobs ?: return false
val jobs = jobScheduler.allPendingJobs
return jobs.any { it.id == PHOTO_VIDEO_CONTENT_JOB }
}
@ -77,17 +80,17 @@ class NewPhotoFetcher : JobService() {
if (selection.isNotEmpty()) {
selection.append(" OR ")
}
selection.append("${MediaStore.Images.ImageColumns._ID} = '$id'")
selection.append("${Images.ImageColumns._ID} = '$id'")
}
var cursor: Cursor? = null
try {
val projection = arrayOf(MediaStore.Images.ImageColumns.DATA)
val uris = arrayListOf(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
val projection = arrayOf(Images.ImageColumns.DATA)
val uris = arrayListOf(Images.Media.EXTERNAL_CONTENT_URI, Video.Media.EXTERNAL_CONTENT_URI)
uris.forEach {
cursor = contentResolver.query(it, projection, selection.toString(), null, null)
while (cursor!!.moveToNext()) {
val path = cursor!!.getStringValue(MediaStore.Images.ImageColumns.DATA)
val path = cursor!!.getStringValue(Images.ImageColumns.DATA)
affectedFolderPaths.add(path.getParentPath())
addPathToDB(path)
}