[Android] Use custom dialog for network caching

Robert Stone git at videolan.org
Wed Apr 6 13:51:02 UTC 2022


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Sun Apr  3 08:02:24 2022 -0700| [4c46ef146f0482a6a3656fffacbad1b426f93bf4] | committer: Nicolas Pomepuy

Use custom dialog for network caching

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

 .../main/java/org/videolan/resources/VLCOptions.kt |  6 +----
 .../resources/src/main/res/values/strings.xml      |  4 ++--
 .../ui/preferences/PreferencesAdvanced.kt          | 27 ++++++++++++++++++----
 .../vlc-android/res/xml/preferences_adv.xml        |  2 +-
 .../vlc/gui/preferences/PreferencesAdvanced.kt     | 22 +++++++++++++-----
 5 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/VLCOptions.kt b/application/resources/src/main/java/org/videolan/resources/VLCOptions.kt
index f2373bf3e..68ddd34aa 100644
--- a/application/resources/src/main/java/org/videolan/resources/VLCOptions.kt
+++ b/application/resources/src/main/java/org/videolan/resources/VLCOptions.kt
@@ -80,11 +80,7 @@ object VLCOptions {
             } catch (ignored: NumberFormatException) {
             }
 
-            var networkCaching = pref.getInt("network_caching_value", 0)
-            if (networkCaching > 60000)
-                networkCaching = 60000
-            else if (networkCaching < 0) networkCaching = 0
-
+            var networkCaching = pref.getInt("network_caching_value", 0).coerceIn(0, 60000)
             val freetypeRelFontsize = pref.getString("subtitles_size", "16")
             val freetypeBold = pref.getBoolean("subtitles_bold", false)
             val freetypeColor = pref.getString("subtitles_color", "16777215")
diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index d2aa93958..088f5d7b6 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -450,12 +450,12 @@
     <string name="clear_history">Clear history</string>
     <string name="set_locale">Set locale</string>
     <string name="network_caching">Network caching value</string>
-    <string name="network_caching_summary">The amount of time to buffer network media (in ms) for software decoding\nLeave blank to reset</string>
+    <string name="network_caching_summary">The amount of time to buffer network media (in ms) for software decoding\nSet to 0 to disable</string>
     <string name="resume_playback_title">Resume playback after a call</string>
     <string name="resume_playback_summary">Stay in pause otherwise</string>
     <string name="blurred_cover_background_title">Blurred cover background</string>
     <string name="blurred_cover_background_summary">Blurred cover in audio player background</string>
-    <string name="network_caching_popup">This value must be between 0 and 6000 ms</string>
+    <string name="network_caching_popup">This value must be between 0 and 60000 ms</string>
     <string name="quit">Quit and restart application</string>
     <string name="restart_message">Changes will take effect once application has restarted.\n\nRestart now?</string>
     <string name="restart_message_OK">OK</string>
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 8d2700533..8da9cd3e3 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
@@ -32,6 +32,8 @@ import android.content.SharedPreferences
 import android.net.Uri
 import android.os.Build
 import android.os.Bundle
+import android.text.InputFilter
+import android.text.InputType
 import android.util.Log
 import android.widget.Toast
 import androidx.core.content.edit
@@ -76,6 +78,20 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
         if (FeatureFlag.values().isNotEmpty()) findPreference<Preference>("optional_features")?.isVisible = true
     }
 
+    override fun onDisplayPreferenceDialog(preference: Preference) {
+        val f = super.buildPreferenceDialogFragment(preference)
+        if (f is CustomEditTextPreferenceDialogFragment) {
+            when (preference.key) {
+                "network_caching" -> {
+                    f.setInputType(InputType.TYPE_CLASS_NUMBER)
+                    f.setFilters(arrayOf(InputFilter.LengthFilter(5)))
+                }
+            }
+            return
+        }
+        super.onDisplayPreferenceDialog(preference)
+    }
+
     override fun onStart() {
         super.onStart()
         preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
@@ -180,11 +196,14 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
             "network_caching" -> {
                 sharedPreferences.edit {
                     try {
-                        putInt("network_caching_value", Integer.parseInt(sharedPreferences.getString(key, "0")!!))
+                        val origValue = Integer.parseInt(sharedPreferences.getString(key, "0"))
+                        val newValue = origValue.coerceIn(0, 60000)
+                        putInt("network_caching_value", newValue)
+                        findPreference<EditTextPreference>(key)?.let { it.text = newValue.toString() }
+                        if (origValue != newValue) activity?.let { Toast.makeText(it, R.string.network_caching_popup, Toast.LENGTH_SHORT).show() }
                     } catch (e: NumberFormatException) {
                         putInt("network_caching_value", 0)
-                        val networkCachingPref = findPreference<EditTextPreference>(key)
-                        networkCachingPref?.text = ""
+                        findPreference<EditTextPreference>(key)?.let { it.text = "0" }
                         activity?.let { Toast.makeText(it, R.string.network_caching_popup, Toast.LENGTH_SHORT).show() }
                     }
                 }
@@ -207,7 +226,7 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
         }
     }
 
-    fun restartLibVLC() {
+    private fun restartLibVLC() {
         VLCInstance.restart()
         (activity as? PreferencesActivity)?.restartMediaPlayer()
     }
diff --git a/application/vlc-android/res/xml/preferences_adv.xml b/application/vlc-android/res/xml/preferences_adv.xml
index 646a57118..54b025402 100644
--- a/application/vlc-android/res/xml/preferences_adv.xml
+++ b/application/vlc-android/res/xml/preferences_adv.xml
@@ -12,7 +12,7 @@
             android:fragment="org.videolan.television.ui.preferences.PreferencesOptional"/>
 
     <EditTextPreference
-            android:defaultValue=""
+            android:defaultValue="0"
             android:key="network_caching"
             android:maxLength="5"
             android:summary="@string/network_caching_summary"
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 6e5a87539..d74590cad 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
@@ -31,6 +31,8 @@ import android.content.SharedPreferences
 import android.net.Uri
 import android.os.Build
 import android.os.Bundle
+import android.text.InputFilter
+import android.text.InputType
 import android.util.Log
 import android.widget.Toast
 import androidx.appcompat.app.AlertDialog
@@ -75,6 +77,11 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
         if (BuildConfig.DEBUG) findPreference<Preference>("debug_logs")?.isVisible = false
         if (FeatureFlag.values().isNotEmpty()) findPreference<Preference>("optional_features")?.isVisible = true
 
+        findPreference<EditTextPreference>("network_caching")?.setOnBindEditTextListener {
+            it.inputType = InputType.TYPE_CLASS_NUMBER
+            it.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(5))
+            it.setSelection(it.editableText.length)
+        }
     }
 
     override fun onStart() {
@@ -187,11 +194,14 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
             "network_caching" -> {
                 sharedPreferences.edit {
                     try {
-                        putInt("network_caching_value", Integer.parseInt(sharedPreferences.getString(key, "0")!!))
+                        val origValue = Integer.parseInt(sharedPreferences.getString(key, "0"))
+                        val newValue = origValue.coerceIn(0, 60000)
+                        putInt("network_caching_value", newValue)
+                        findPreference<EditTextPreference>(key)?.let { it.text = newValue.toString() }
+                        if (origValue != newValue) UiTools.snacker(requireActivity(), R.string.network_caching_popup)
                     } catch (e: NumberFormatException) {
                         putInt("network_caching_value", 0)
-                        val networkCachingPref = findPreference<EditTextPreference>(key)
-                        networkCachingPref?.text = ""
+                        findPreference<EditTextPreference>(key)?.let { it.text = "0" }
                         UiTools.snacker(requireActivity(), R.string.network_caching_popup)
                     }
                 }
@@ -210,8 +220,7 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
                 restartLibVLC()
             }
             "opengl", "chroma_format", "deblocking", "enable_frame_skip", "enable_time_stretching_audio", "enable_verbose_mode" -> {
-                VLCInstance.restart()
-                (activity as? PreferencesActivity)?.restartMediaPlayer()
+                restartLibVLC()
             }
             "prefer_smbv1" -> {
                 VLCInstance.restart()
@@ -219,7 +228,8 @@ class PreferencesAdvanced : BasePreferenceFragment(), SharedPreferences.OnShared
             }
         }
     }
-    fun restartLibVLC() {
+
+    private fun restartLibVLC() {
         VLCInstance.restart()
         (activity as? PreferencesActivity)?.restartMediaPlayer()
     }



More information about the Android mailing list