From d4bf065cb5e17cdd88d0876f0bca936fa199c728 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 5 Dec 2021 21:18:57 +0100 Subject: [PATCH] adding a new MediaStoreId field to Medium model --- .../gallery/pro/activities/PhotoVideoActivity.kt | 2 +- .../gallery/pro/activities/ViewPagerActivity.kt | 2 +- .../gallery/pro/databases/GalleryDatabase.kt | 9 ++++++++- .../simplemobiletools/gallery/pro/extensions/Context.kt | 4 ++-- .../gallery/pro/helpers/MediaFetcher.kt | 9 ++++++--- .../gallery/pro/interfaces/MediumDao.kt | 6 +++--- .../com/simplemobiletools/gallery/pro/models/Medium.kt | 3 ++- 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PhotoVideoActivity.kt index 3d59b23b6..e7151949e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PhotoVideoActivity.kt @@ -149,7 +149,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList } mIsVideo = type == TYPE_VIDEOS - mMedium = Medium(null, filename, mUri.toString(), mUri!!.path!!.getParentPath(), 0, 0, file.length(), type, 0, false, 0L) + mMedium = Medium(null, filename, mUri.toString(), mUri!!.path!!.getParentPath(), 0, 0, file.length(), type, 0, false, 0L, 0) supportActionBar?.title = mMedium!!.name bundle.putSerializable(MEDIUM, mMedium) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 36d9af350..8a4bf4e63 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -407,7 +407,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val duration = if (type == TYPE_VIDEOS) getDuration(mPath) ?: 0 else 0 val ts = System.currentTimeMillis() val medium = - Medium(null, mPath.getFilenameFromPath(), mPath, mPath.getParentPath(), ts, ts, File(mPath).length(), type, duration, isFavorite, 0) + Medium(null, mPath.getFilenameFromPath(), mPath, mPath.getParentPath(), ts, ts, File(mPath).length(), type, duration, isFavorite, 0, 0L) mediaDB.insert(medium) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt index 2436e87bd..18a7d7a7d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt @@ -9,7 +9,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase import com.simplemobiletools.gallery.pro.interfaces.* import com.simplemobiletools.gallery.pro.models.* -@Database(entities = [Directory::class, Medium::class, Widget::class, DateTaken::class, Favorite::class], version = 9) +@Database(entities = [Directory::class, Medium::class, Widget::class, DateTaken::class, Favorite::class], version = 10) abstract class GalleryDatabase : RoomDatabase() { abstract fun DirectoryDao(): DirectoryDao @@ -36,6 +36,7 @@ abstract class GalleryDatabase : RoomDatabase() { .addMigrations(MIGRATION_6_7) .addMigrations(MIGRATION_7_8) .addMigrations(MIGRATION_8_9) + .addMigrations(MIGRATION_9_10) .build() } } @@ -84,5 +85,11 @@ abstract class GalleryDatabase : RoomDatabase() { database.execSQL("ALTER TABLE date_takens ADD COLUMN last_modified INTEGER default 0 NOT NULL") } } + + private val MIGRATION_9_10 = object : Migration(9, 10) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE media ADD COLUMN media_store_id INTEGER default 0 NOT NULL") + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index e808b6ac2..bb451025a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -969,7 +969,7 @@ fun Context.addPathToDB(path: String) { val videoDuration = if (type == TYPE_VIDEOS) getDuration(path) ?: 0 else 0 val medium = Medium( null, path.getFilenameFromPath(), path, path.getParentPath(), System.currentTimeMillis(), System.currentTimeMillis(), - File(path).length(), type, videoDuration, isFavorite, 0L + File(path).length(), type, videoDuration, isFavorite, 0L, 0L ) mediaDB.insert(medium) @@ -1002,7 +1002,7 @@ fun Context.createDirectoryFromMedia( } val isSortingAscending = config.directorySorting.isSortingAscending() - val defaultMedium = Medium(0, "", "", "", 0L, 0L, 0L, 0, 0, false, 0L) + val defaultMedium = Medium(0, "", "", "", 0L, 0L, 0L, 0, 0, false, 0L, 0L) val firstItem = curMedia.firstOrNull() ?: defaultMedium val lastItem = curMedia.lastOrNull() ?: defaultMedium val dirName = checkAppendingHidden(path, hiddenString, includedFolders, noMediaFolders) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 8de8a3ef9..a1cc62167 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -400,7 +400,7 @@ class MediaFetcher(val context: Context) { } val isFavorite = favoritePaths.contains(path) - val medium = Medium(null, filename, path, file.parent, lastModified, dateTaken, size, type, videoDuration, isFavorite, 0L) + val medium = Medium(null, filename, path, file.parent, lastModified, dateTaken, size, type, videoDuration, isFavorite, 0L, 0L) media.add(medium) } } @@ -421,6 +421,7 @@ class MediaFetcher(val context: Context) { val showHidden = context.config.shouldShowHidden val projection = arrayOf( + Images.Media._ID, Images.Media.DISPLAY_NAME, Images.Media.DATA, Images.Media.DATE_MODIFIED, @@ -437,6 +438,7 @@ class MediaFetcher(val context: Context) { } try { + val mediaStoreId = cursor.getLongValue(Images.Media._ID) val filename = cursor.getStringValue(Images.Media.DISPLAY_NAME) val path = cursor.getStringValue(Images.Media.DATA) val lastModified = cursor.getLongValue(Images.Media.DATE_MODIFIED) * 1000 @@ -487,7 +489,8 @@ class MediaFetcher(val context: Context) { } val isFavorite = favoritePaths.contains(path) - val medium = Medium(null, filename, path, path.getParentPath(), lastModified, dateTaken, size, type, videoDuration, isFavorite, 0L) + val medium = + Medium(null, filename, path, path.getParentPath(), lastModified, dateTaken, size, type, videoDuration, isFavorite, 0L, mediaStoreId) val parent = medium.parentPath.lowercase(Locale.getDefault()) val currentFolderMedia = media[parent] if (currentFolderMedia == null) { @@ -565,7 +568,7 @@ class MediaFetcher(val context: Context) { ) val videoDuration = if (getVideoDurations) context.getDuration(path) ?: 0 else 0 val isFavorite = favoritePaths.contains(path) - val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, videoDuration, isFavorite, 0L) + val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, videoDuration, isFavorite, 0L, 0L) media.add(medium) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediumDao.kt index 8d0e05d6f..6a2d5161d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/MediumDao.kt @@ -9,16 +9,16 @@ import com.simplemobiletools.gallery.pro.models.Medium @Dao interface MediumDao { - @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, video_duration, is_favorite, deleted_ts FROM media WHERE deleted_ts = 0 AND parent_path = :path COLLATE NOCASE") + @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, video_duration, is_favorite, deleted_ts, media_store_id FROM media WHERE deleted_ts = 0 AND parent_path = :path COLLATE NOCASE") fun getMediaFromPath(path: String): List - @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, video_duration, is_favorite, deleted_ts FROM media WHERE deleted_ts = 0 AND is_favorite = 1") + @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, video_duration, is_favorite, deleted_ts, media_store_id FROM media WHERE deleted_ts = 0 AND is_favorite = 1") fun getFavorites(): List @Query("SELECT COUNT(filename) FROM media WHERE deleted_ts = 0 AND is_favorite = 1") fun getFavoritesCount(): Long - @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, video_duration, is_favorite, deleted_ts FROM media WHERE deleted_ts != 0") + @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, video_duration, is_favorite, deleted_ts, media_store_id FROM media WHERE deleted_ts != 0") fun getDeletedMedia(): List @Query("SELECT COUNT(filename) FROM media WHERE deleted_ts != 0") diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt index 680384109..9d6611cc5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt @@ -26,11 +26,12 @@ data class Medium( @ColumnInfo(name = "video_duration") var videoDuration: Int, @ColumnInfo(name = "is_favorite") var isFavorite: Boolean, @ColumnInfo(name = "deleted_ts") var deletedTS: Long, + @ColumnInfo(name = "media_store_id") var mediaStoreId: Long, @Ignore var gridPosition: Int = 0 // used at grid view decoration at Grouping enabled ) : Serializable, ThumbnailItem() { - constructor() : this(null, "", "", "", 0L, 0L, 0L, 0, 0, false, 0L, 0) + constructor() : this(null, "", "", "", 0L, 0L, 0L, 0, 0, false, 0L, 0L, 0) companion object { private const val serialVersionUID = -6553149366975655L