Glimpse: Cache cursor indexes
Change-Id: I5f7f4bbf5fc5ae987e01fb2bbac43bb9951210a4
This commit is contained in:
parent
4bbb71c891
commit
f6b8998442
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
package org.lineageos.glimpse.thumbnail
|
package org.lineageos.glimpse.thumbnail
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
@ -25,6 +26,12 @@ class MediaViewerAdapter(
|
||||||
private val exoPlayer: ExoPlayer,
|
private val exoPlayer: ExoPlayer,
|
||||||
private val currentPositionLiveData: LiveData<Int>,
|
private val currentPositionLiveData: LiveData<Int>,
|
||||||
) : BaseCursorAdapter<MediaViewerAdapter.MediaViewHolder>() {
|
) : BaseCursorAdapter<MediaViewerAdapter.MediaViewHolder>() {
|
||||||
|
// Cursor indexes
|
||||||
|
private var idIndex = -1
|
||||||
|
private var isFavoriteIndex = -1
|
||||||
|
private var mediaTypeIndex = -1
|
||||||
|
private var dateAddedIndex = -1
|
||||||
|
|
||||||
init {
|
init {
|
||||||
setHasStableIds(true)
|
setHasStableIds(true)
|
||||||
}
|
}
|
||||||
|
@ -50,14 +57,20 @@ class MediaViewerAdapter(
|
||||||
holder.onViewDetachedFromWindow()
|
holder.onViewDetachedFromWindow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onChangedCursor(cursor: Cursor?) {
|
||||||
|
super.onChangedCursor(cursor)
|
||||||
|
|
||||||
|
cursor?.let {
|
||||||
|
idIndex = it.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
||||||
|
isFavoriteIndex = it.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
|
||||||
|
mediaTypeIndex = it.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
|
||||||
|
dateAddedIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun getMediaFromMediaStore(position: Int): Media? {
|
fun getMediaFromMediaStore(position: Int): Media? {
|
||||||
val cursor = cursor ?: return null
|
val cursor = cursor ?: return null
|
||||||
|
|
||||||
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
|
||||||
val isFavoriteIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
|
|
||||||
val mediaTypeIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
|
|
||||||
val dateAddedIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
|
|
||||||
|
|
||||||
cursor.moveToPosition(position)
|
cursor.moveToPosition(position)
|
||||||
|
|
||||||
val id = cursor.getLong(idIndex)
|
val id = cursor.getLong(idIndex)
|
||||||
|
@ -75,7 +88,6 @@ class MediaViewerAdapter(
|
||||||
|
|
||||||
private fun getIdFromMediaStore(position: Int): Long {
|
private fun getIdFromMediaStore(position: Int): Long {
|
||||||
val cursor = cursor ?: return 0
|
val cursor = cursor ?: return 0
|
||||||
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
|
||||||
cursor.moveToPosition(position)
|
cursor.moveToPosition(position)
|
||||||
return cursor.getLong(idIndex)
|
return cursor.getLong(idIndex)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,12 @@ class ThumbnailAdapter(
|
||||||
|
|
||||||
private var recyclerView: RecyclerView? = null
|
private var recyclerView: RecyclerView? = null
|
||||||
|
|
||||||
|
// Cursor indexes
|
||||||
|
private var idIndex = -1
|
||||||
|
private var isFavoriteIndex = -1
|
||||||
|
private var mediaTypeIndex = -1
|
||||||
|
private var dateAddedIndex = -1
|
||||||
|
|
||||||
init {
|
init {
|
||||||
setHasStableIds(true)
|
setHasStableIds(true)
|
||||||
}
|
}
|
||||||
|
@ -126,6 +132,13 @@ class ThumbnailAdapter(
|
||||||
super.onChangedCursor(cursor)
|
super.onChangedCursor(cursor)
|
||||||
|
|
||||||
headersPositions.clear()
|
headersPositions.clear()
|
||||||
|
|
||||||
|
cursor?.let {
|
||||||
|
idIndex = it.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
||||||
|
isFavoriteIndex = it.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
|
||||||
|
mediaTypeIndex = it.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
|
||||||
|
dateAddedIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTruePosition(position: Int) =
|
private fun getTruePosition(position: Int) =
|
||||||
|
@ -139,7 +152,6 @@ class ThumbnailAdapter(
|
||||||
|
|
||||||
private fun getIdFromMediaStore(position: Int): Long {
|
private fun getIdFromMediaStore(position: Int): Long {
|
||||||
val cursor = cursor ?: return 0
|
val cursor = cursor ?: return 0
|
||||||
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
|
||||||
cursor.moveToPosition(getTruePosition(position))
|
cursor.moveToPosition(getTruePosition(position))
|
||||||
return cursor.getLong(idIndex)
|
return cursor.getLong(idIndex)
|
||||||
}
|
}
|
||||||
|
@ -147,11 +159,6 @@ class ThumbnailAdapter(
|
||||||
private fun getMediaFromMediaStore(position: Int): Media? {
|
private fun getMediaFromMediaStore(position: Int): Media? {
|
||||||
val cursor = cursor ?: return null
|
val cursor = cursor ?: return null
|
||||||
|
|
||||||
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
|
||||||
val isFavoriteIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
|
|
||||||
val mediaTypeIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
|
|
||||||
val dateAddedIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
|
|
||||||
|
|
||||||
cursor.moveToPosition(position)
|
cursor.moveToPosition(position)
|
||||||
|
|
||||||
val id = cursor.getLong(idIndex)
|
val id = cursor.getLong(idIndex)
|
||||||
|
|
Loading…
Reference in New Issue