[Android] Add a migration to invalidate the video thumbs on upgrade

Nicolas Pomepuy git at videolan.org
Thu Jun 10 11:15:41 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri May 21 07:42:18 2021 +0200| [fb33ec3fb2cc23a4c9a81d847afeb6590f79cb38] | committer: Nicolas Pomepuy

Add a migration to invalidate the video thumbs on upgrade

Fixes #2024

> https://code.videolan.org/videolan/vlc-android/commit/fb33ec3fb2cc23a4c9a81d847afeb6590f79cb38
---

 .../org/videolan/mobile/app/AppSetupDelegate.kt    |  4 +--
 .../org/videolan/vlc/util/ThumbnailsProvider.kt    |  2 +-
 .../{SettingsMigration.kt => VersionMigration.kt}  | 38 +++++++++++++++++++---
 3 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
index 45da6efc6..b0d10ab23 100644
--- a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
+++ b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
@@ -46,7 +46,7 @@ import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.gui.SendCrashActivity
 import org.videolan.vlc.gui.helpers.NotificationHelper
 import org.videolan.vlc.util.DialogDelegate
-import org.videolan.vlc.util.SettingsMigration
+import org.videolan.vlc.util.VersionMigration
 
 interface AppDelegate {
     val appContextProvider : AppContextProvider
@@ -94,7 +94,7 @@ class AppSetupDelegate : AppDelegate,
         }
         packageManager.setComponentEnabledSetting(ComponentName(this at backgroundInit, SendCrashActivity::class.java),
                 if (BuildConfig.BETA) PackageManager.COMPONENT_ENABLED_STATE_ENABLED else PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
-        SettingsMigration.migrateSettings(this at backgroundInit)
+        VersionMigration.migrateVersion(this at backgroundInit)
 
     }
 }
diff --git a/application/vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.kt b/application/vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.kt
index 578f19752..c585436fb 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/ThumbnailsProvider.kt
@@ -69,7 +69,7 @@ object ThumbnailsProvider {
             if (appDir == null) appDir = AppContextProvider.appContext.getExternalFilesDir(null)
             val hasCache = appDir != null && appDir!!.exists()
             if (hasCache && cacheDir == null) cacheDir = appDir!!.absolutePath + MEDIALIB_FOLDER_NAME
-            return if (hasCache) StringBuilder(cacheDir!!).append('/').append(item.fileName).append(".jpg").toString() else null
+            return if (hasCache) StringBuilder(cacheDir!!).append('/').append(item.id).append(".jpg").toString() else null
         }
         return item.artworkMrl
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/util/SettingsMigration.kt b/application/vlc-android/src/org/videolan/vlc/util/VersionMigration.kt
similarity index 66%
rename from application/vlc-android/src/org/videolan/vlc/util/SettingsMigration.kt
rename to application/vlc-android/src/org/videolan/vlc/util/VersionMigration.kt
index c961034dd..5ebb139c0 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/SettingsMigration.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/VersionMigration.kt
@@ -1,6 +1,6 @@
 /*
  * ************************************************************************
- *  SettingsMigration.kt
+ *  VersionMigration.kt
  * *************************************************************************
  * Copyright © 2019 VLC authors and VideoLAN
  * Author: Nicolas POMEPUY
@@ -29,18 +29,27 @@ import android.content.SharedPreferences
 import android.util.Log
 import androidx.appcompat.app.AppCompatDelegate
 import androidx.core.content.edit
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import org.videolan.medialibrary.interfaces.Medialibrary
+import org.videolan.resources.util.getFromMl
 import org.videolan.tools.*
+import java.io.File
+import java.io.IOException
 
-private const val CURRENT_VERSION = 1
+private const val CURRENT_VERSION = 2
 
-object SettingsMigration {
+object VersionMigration {
 
-    fun migrateSettings(context: Context) {
+    suspend fun migrateVersion(context: Context) {
         val settings = Settings.getInstance(context)
         val lastVersion = settings.getInt(KEY_CURRENT_SETTINGS_VERSION, 0)
         if (lastVersion < 1) {
             migrateToVersion1(settings)
         }
+        if (lastVersion < 2) {
+            migrateToVersion2(context)
+        }
         settings.putSingle(KEY_CURRENT_SETTINGS_VERSION, CURRENT_VERSION)
     }
 
@@ -64,4 +73,25 @@ object SettingsMigration {
             remove("enable_black_theme")
         }
     }
+
+    /**
+     * Deletes all the video thumbnails as we change the way to name them.
+     */
+    private suspend fun migrateToVersion2(context: Context) {
+        Log.i(this::class.java.simpleName, "Migrating version to Version 2: flush all the video thumbnails")
+        withContext(Dispatchers.IO) {
+            try {
+                context.getExternalFilesDir(null)?. let {
+                    val cacheDir = it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME
+                    val files = File(cacheDir).listFiles()
+                    files?.forEach { file ->
+                        if (file.isFile) FileUtils.deleteFile(file)
+                    }
+                }
+            } catch (e: IOException) {
+                Log.e(this::class.java.simpleName, e.message, e)
+            }
+        }
+        context.getFromMl { flushUserProvidedThumbnails() }
+    }
 }
\ No newline at end of file



More information about the Android mailing list