[Android] Reduce setLocale calls

Geoffrey Métais git at videolan.org
Mon Jul 29 16:37:18 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jul 26 16:36:40 2019 +0200| [6c12e4c4e7d2f4d4bf5b9d6ab7d12cc760d6db62] | committer: Geoffrey Métais

Reduce setLocale calls

(cherry picked from commit d7c13409b69c9688b13c4192239c0cff73a9d2f2)

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

 vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt             | 9 ++++++++-
 vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt          | 4 ++--
 .../src/org/videolan/vlc/gui/preferences/PreferencesUi.kt        | 9 +++++++--
 .../src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.kt        | 9 ++++++++-
 .../src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.kt     | 7 ++++++-
 5 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt b/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt
index f7a31841d..3603fccca 100644
--- a/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt
@@ -17,7 +17,10 @@ open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
     lateinit var settings: SharedPreferences
 
     override fun onCreate(savedInstanceState: Bundle?) {
-        UiTools.setLocale(this)
+        if (!localeSet) {
+            UiTools.setLocale(this)
+            localeSet = true
+        }
         settings = Settings.getInstance(this)
         /* Theme must be applied before super.onCreate */
         applyTheme()
@@ -38,4 +41,8 @@ open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
         cancel()
         super.onDestroy()
     }
+
+    companion object {
+        var localeSet = false
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
index 3f71abcc8..13cdd8c21 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.kt
@@ -481,8 +481,8 @@ object UiTools {
     }
 
     fun setLocale(context: Context) {
-        if (!VLCApplication.locale.isNullOrEmpty()) {
-            val locale = getLocaleFromString(VLCApplication.locale!!)
+        VLCApplication.locale.takeIf { !it.isNullOrEmpty() }?.let {
+            val locale = getLocaleFromString(it)
             Locale.setDefault(locale)
             val config = Configuration()
             config.locale = locale
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt
index 2e7243c15..be5ec128d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.kt
@@ -32,6 +32,7 @@ import androidx.preference.TwoStatePreference
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.vlc.R
+import org.videolan.vlc.gui.BaseActivity
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.util.*
 
@@ -94,7 +95,7 @@ class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPrefer
                 (activity as PreferencesActivity).setRestart()
                 return true
             }
-            "media_seen" -> activity!!.setResult(RESULT_UPDATE_SEEN_MEDIA)
+            "media_seen" -> requireActivity().setResult(RESULT_UPDATE_SEEN_MEDIA)
             KEY_ARTISTS_SHOW_ALL -> (activity as PreferencesActivity).updateArtists()
         }
         return super.onPreferenceTreeClick(preference)
@@ -102,7 +103,11 @@ class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPrefer
 
     override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
         when (key) {
-            "set_locale" -> UiTools.restartDialog(requireActivity())
+            "set_locale" -> {
+                BaseActivity.localeSet = false
+                (activity as PreferencesActivity).setRestart()
+                UiTools.restartDialog(requireActivity())
+            }
             "browser_show_all_files" -> (activity as PreferencesActivity).setRestart()
             KEY_APP_THEME -> (activity as PreferencesActivity).exitAndRescan()
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.kt
index 42160d325..907c42be6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.kt
@@ -57,7 +57,10 @@ abstract class BaseTvActivity : FragmentActivity(), CoroutineScope by MainScope(
 
     override fun onCreate(savedInstanceState: Bundle?) {
         //Init Medialibrary if KO
-        UiTools.setLocale(this)
+        if (!localeSet) {
+            UiTools.setLocale(this)
+            localeSet = true
+        }
         if (savedInstanceState != null) startMedialibrary(firstRun = false, upgrade = false, parse = true)
         super.onCreate(savedInstanceState)
         mediaLibrary = AbstractMedialibrary.getInstance()
@@ -120,4 +123,8 @@ abstract class BaseTvActivity : FragmentActivity(), CoroutineScope by MainScope(
             MediaParsingService.newStorages.value = null
         })
     }
+
+    companion object {
+        var localeSet = false
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.kt b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.kt
index 13d1d229f..1a5e3a2e5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.kt
@@ -34,6 +34,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.helpers.UiTools
+import org.videolan.vlc.gui.tv.browser.BaseTvActivity
 import org.videolan.vlc.util.*
 
 
@@ -72,7 +73,11 @@ class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPrefer
 
     override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
         when (key) {
-            "set_locale" -> UiTools.restartDialog(activity)
+            "set_locale" -> {
+                BaseTvActivity.localeSet = false
+                (activity as PreferencesActivity).setRestartApp()
+                UiTools.restartDialog(activity)
+            }
             PREF_TV_UI -> {
                 Settings.tvUI = sharedPreferences.getBoolean(PREF_TV_UI, false)
                 (activity as PreferencesActivity).setRestartApp()



More information about the Android mailing list