[Android] Preferences: check ML state for clearDatabase

Duncan McNamara git at videolan.org
Tue Apr 19 08:46:38 UTC 2022


vlc-android | branch: master | Duncan McNamara <dcn.mcnamara at gmail.com> | Thu Apr 14 16:06:25 2022 +0200| [af47673fbfc83b2a35e35d3d802dfb0a9d46baf7] | committer: Nicolas Pomepuy

Preferences: check ML state for clearDatabase

Instead of stopping MediaParsingService before clearing the
medialibrary to ensure that there won't be any operations going, this
just checks if the medialibrary is working before clearing the database

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

 .../ui/preferences/PreferencesAdvanced.kt          | 56 +++++++++++++-------
 .../vlc/gui/preferences/PreferencesAdvanced.kt     | 61 +++++++++++++---------
 2 files changed, 74 insertions(+), 43 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt
index 8a1e3975d..8a19a2f9d 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAdvanced.kt
@@ -136,29 +136,47 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
                 return true
             }
             "clear_media_db" -> {
-                val dialog = ConfirmDeleteDialog.newInstance(title = getString(R.string.clear_media_db), description = getString(R.string.clear_media_db_message), buttonText = getString(R.string.clear))
-                dialog.show((activity as FragmentActivity).supportFragmentManager, RenameDialog::class.simpleName)
-                dialog.setListener {
-                    launch {
-                        val medialibrary = Medialibrary.getInstance()
-                        activity.stopService(Intent(activity, MediaParsingService::class.java))
-                        withContext((Dispatchers.IO)) {
-                            medialibrary.clearDatabase(false)
-                            deleteAllWatchNext(activity)
-                            //delete thumbnails
-                            try {
-                                activity.getExternalFilesDir(null)?. let {
-                                    val files = File(it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME).listFiles()
-                                    files?.forEach { file ->
-                                        if (file.isFile) FileUtils.deleteFile(file)
+                val medialibrary = Medialibrary.getInstance()
+                if (medialibrary.isWorking) {
+                    activity?.let {
+                        Toast.makeText(
+                            it,
+                            R.string.settings_ml_block_scan,
+                            Toast.LENGTH_LONG
+                        ).show()
+                    }
+                } else {
+                    val dialog = ConfirmDeleteDialog.newInstance(
+                        title = getString(R.string.clear_media_db),
+                        description = getString(R.string.clear_media_db_message),
+                        buttonText = getString(R.string.clear)
+                    )
+                    dialog.show(
+                        (activity as FragmentActivity).supportFragmentManager,
+                        RenameDialog::class.simpleName
+                    )
+                    dialog.setListener {
+                        launch {
+                            activity.stopService(Intent(activity, MediaParsingService::class.java))
+                            withContext((Dispatchers.IO)) {
+                                medialibrary.clearDatabase(false)
+                                deleteAllWatchNext(activity)
+                                //delete thumbnails
+                                try {
+                                    activity.getExternalFilesDir(null)?.let {
+                                        val files =
+                                            File(it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME).listFiles()
+                                        files?.forEach { file ->
+                                            if (file.isFile) FileUtils.deleteFile(file)
+                                        }
                                     }
+                                    BitmapCache.clear()
+                                } catch (e: IOException) {
+                                    Log.e(this::class.java.simpleName, e.message, e)
                                 }
-                                BitmapCache.clear()
-                            } catch (e: IOException) {
-                                Log.e(this::class.java.simpleName, e.message, e)
                             }
+                            medialibrary.discover(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)
                         }
-                        medialibrary.discover(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)
                     }
                 }
                 return true
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAdvanced.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAdvanced.kt
index 6f1d51e65..8d4d43c6d 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAdvanced.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAdvanced.kt
@@ -42,15 +42,11 @@ import androidx.preference.EditTextPreference
 import androidx.preference.Preference
 import kotlinx.coroutines.*
 import org.videolan.medialibrary.interfaces.Medialibrary
-import org.videolan.resources.AndroidDevices
-import org.videolan.resources.KEY_AUDIO_LAST_PLAYLIST
-import org.videolan.resources.KEY_MEDIA_LAST_PLAYLIST
-import org.videolan.resources.VLCInstance
+import org.videolan.resources.*
 import org.videolan.tools.BitmapCache
 import org.videolan.tools.Settings
 import org.videolan.tools.putSingle
 import org.videolan.vlc.BuildConfig
-import org.videolan.vlc.MediaParsingService
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.DebugLogActivity
 import org.videolan.vlc.gui.dialogs.ConfirmDeleteDialog
@@ -118,31 +114,48 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
                 return true
             }
             "clear_media_db" -> {
-                val dialog = ConfirmDeleteDialog.newInstance(title = getString(R.string.clear_media_db), description = getString(R.string.clear_media_db_message), buttonText = getString(R.string.clear))
-                dialog.show(requireActivity().supportFragmentManager, RenameDialog::class.simpleName)
-                dialog.setListener {
-                    lifecycleScope.launch {
-                        val medialibrary = Medialibrary.getInstance()
-                        requireActivity().stopService(Intent(requireActivity(), MediaParsingService::class.java))
-                        withContext((Dispatchers.IO)) {
-                            medialibrary.clearDatabase(false)
-                            //delete thumbnails
-                            try {
-                                requireActivity().getExternalFilesDir(null)?. let {
-                                    val files = File(it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME).listFiles()
-                                    files?.forEach { file ->
-                                        if (file.isFile) FileUtils.deleteFile(file)
+                val medialibrary = Medialibrary.getInstance()
+                if (medialibrary.isWorking) {
+                    activity?.let {
+                        Toast.makeText(
+                            it,
+                            R.string.settings_ml_block_scan,
+                            Toast.LENGTH_LONG
+                        ).show()
+                    }
+                } else {
+                    val dialog = ConfirmDeleteDialog.newInstance(
+                        title = getString(R.string.clear_media_db),
+                        description = getString(R.string.clear_media_db_message),
+                        buttonText = getString(R.string.clear)
+                    )
+                    dialog.show(
+                        requireActivity().supportFragmentManager,
+                        RenameDialog::class.simpleName
+                    )
+                    dialog.setListener {
+                        lifecycleScope.launch {
+                            withContext((Dispatchers.IO)) {
+                                medialibrary.clearDatabase(false)
+                                //delete thumbnails
+                                try {
+                                    requireActivity().getExternalFilesDir(null)?.let {
+                                        val files =
+                                            File(it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME).listFiles()
+                                        files?.forEach { file ->
+                                            if (file.isFile) FileUtils.deleteFile(file)
+                                        }
                                     }
+                                    BitmapCache.clear()
+                                } catch (e: IOException) {
+                                    Log.e(this::class.java.simpleName, e.message, e)
                                 }
-                                BitmapCache.clear()
-                            } catch (e: IOException) {
-                                Log.e(this::class.java.simpleName, e.message, e)
                             }
+                            medialibrary.discover(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)
                         }
-                        medialibrary.discover(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY)
                     }
+                    return true
                 }
-                return true
             }
             "clear_app_data" -> {
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {



More information about the Android mailing list