[Android] Fix the PreferencesActivity direct access to a preference
Nicolas Pomepuy
git at videolan.org
Wed Sep 1 11:39:51 UTC 2021
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Aug 31 12:00:32 2021 +0200| [24a38e235c1bb9cf43e005c38d7204a4cba838f5] | committer: Nicolas Pomepuy
Fix the PreferencesActivity direct access to a preference
> https://code.videolan.org/videolan/vlc-android/commit/24a38e235c1bb9cf43e005c38d7204a4cba838f5
---
.../resources/src/main/res/values/strings.xml | 2 +-
.../src/org/videolan/vlc/gui/helpers/UiTools.kt | 15 ++++++---------
.../vlc/gui/preferences/PreferencesActivity.kt | 21 +++++++++++++++++++++
.../vlc/viewmodels/mobile/VideosViewModel.kt | 2 +-
4 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index c471dce37..d095dee9c 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -832,7 +832,7 @@
<string name="soundfont_summary">Pick a SoundFont file to play midi audio tracks</string>
<string name="missing_media_snack">Media is missing. Hide all missing media?</string>
<string name="browser_show_missing_media">Show missing medias</string>
- <string name="browser_show_missing_media_summary">Show distant medias event if they are not present</string>
+ <string name="browser_show_missing_media_summary">Show distant medias even if they are not present</string>
<string name="search_prefs">Search settings</string>
<string name="preferred_resolution">Preferred video resolution</string>
<string name="preferred_resolution_summary">Maximum video quality for streams, when applicable, will be: %s</string>
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
index a7db40768..3a50f4632 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
@@ -57,13 +57,11 @@ import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat
import androidx.databinding.BindingAdapter
import androidx.fragment.app.FragmentActivity
+import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.ObsoleteCoroutinesApi
-import kotlinx.coroutines.launch
+import kotlinx.coroutines.*
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.Medialibrary
@@ -83,7 +81,6 @@ import org.videolan.vlc.gui.dialogs.AddToGroupDialog
import org.videolan.vlc.gui.dialogs.SavePlaylistDialog
import org.videolan.vlc.gui.dialogs.VLCBillingDialog
import org.videolan.vlc.gui.dialogs.VideoTracksDialog
-import org.videolan.vlc.gui.preferences.EXTRA_PREF_END_POINT
import org.videolan.vlc.gui.preferences.PreferencesActivity
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
@@ -293,13 +290,13 @@ object UiTools {
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
- fun snackerMissing(activity: Activity) {
+ fun snackerMissing(activity: FragmentActivity) {
val view = getSnackAnchorView(activity) ?: return
val snack = Snackbar.make(view, activity.getString(R.string.missing_media_snack), Snackbar.LENGTH_LONG)
.setAction(R.string.ok) {
- val intent = Intent(activity, PreferencesActivity::class.java)
- intent.putExtra(EXTRA_PREF_END_POINT, "include_missing")
- activity.startActivityForResult(intent, ACTIVITY_RESULT_PREFERENCES)
+ activity.lifecycleScope.launch {
+ PreferencesActivity.launchWithPref(activity, "include_missing")
+ }
}
if (AndroidUtil.isLolliPopOrLater)
snack.view.elevation = view.resources.getDimensionPixelSize(R.dimen.audio_player_elevation).toFloat()
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt
index 3fd945557..68539631c 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt
@@ -28,9 +28,13 @@ import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
+import androidx.fragment.app.FragmentActivity
import com.google.android.material.appbar.AppBarLayout
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
+import kotlinx.coroutines.withContext
+import org.videolan.resources.ACTIVITY_RESULT_PREFERENCES
import org.videolan.tools.RESULT_RESTART
import org.videolan.tools.RESULT_RESTART_APP
import org.videolan.tools.RESULT_UPDATE_ARTISTS
@@ -127,4 +131,21 @@ class PreferencesActivity : BaseActivity() {
val le = PlaybackService.headSetDetection
if (le.hasObservers()) le.value = detect
}
+
+ companion object {
+ /**
+ * Launch the preferences and redirect to a given preference
+ * @param activity The calling activity
+ * @param prefKey The preference key to redirect to
+ * @throws NoSuchElementException if the key is not found
+ */
+ suspend fun launchWithPref(activity: FragmentActivity, prefKey:String) {
+ val pref = withContext(Dispatchers.IO) {
+ PreferenceParser.parsePreferences(activity)
+ }.first { it.key == prefKey }
+ val intent = Intent(activity, PreferencesActivity::class.java)
+ intent.putExtra(EXTRA_PREF_END_POINT, pref)
+ activity.startActivityForResult(intent, ACTIVITY_RESULT_PREFERENCES)
+ }
+ }
}
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index 8bf1093c3..d1aed411d 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -127,7 +127,7 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
MediaUtils.appendMedia(context, list)
}
- internal fun playVideo(context: Activity?, mw: MediaWrapper, position: Int, fromStart: Boolean = false, forceAll:Boolean = false) {
+ internal fun playVideo(context: FragmentActivity?, mw: MediaWrapper, position: Int, fromStart: Boolean = false, forceAll:Boolean = false) {
if (context === null) return
if (!mw.isPresent) {
UiTools.snackerMissing(context)
More information about the Android
mailing list