[Android] Fix locale switching on Android TV

Nicolas Pomepuy git at videolan.org
Tue May 18 11:13:27 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue May 18 11:39:11 2021 +0200| [f53ab21ac9933d894a47560b30a432588ebd0472] | committer: Nicolas Pomepuy

Fix locale switching on Android TV

Fixes #1627

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

 .../org/videolan/television/ui/SearchActivity.kt   |  6 ++++--
 .../television/ui/browser/BaseTvActivity.kt        |  4 ++++
 .../videolan/television/ui/browser/TVActivity.kt   |  5 +++--
 .../ui/preferences/PreferencesActivity.kt          |  8 +++++---
 .../television/ui/preferences/PreferencesUi.kt     | 24 +++++++++++++++++-----
 .../videolan/television/viewmodel/MainTvModel.kt   |  3 ++-
 6 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/SearchActivity.kt b/application/television/src/main/java/org/videolan/television/ui/SearchActivity.kt
index 04c8b33d8..ead66cfca 100644
--- a/application/television/src/main/java/org/videolan/television/ui/SearchActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/SearchActivity.kt
@@ -25,13 +25,13 @@ import android.os.Build
 import android.os.Bundle
 import android.view.View
 import android.widget.TextView
-import androidx.fragment.app.FragmentActivity
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import org.videolan.television.R
+import org.videolan.television.ui.browser.BaseTvActivity
 
 @ExperimentalCoroutinesApi
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-class SearchActivity : FragmentActivity() {
+class SearchActivity : BaseTvActivity() {
 
     private lateinit var fragment: SearchFragment
     private var emptyView: TextView? = null
@@ -43,6 +43,8 @@ class SearchActivity : FragmentActivity() {
         emptyView = findViewById(R.id.empty)
     }
 
+    override fun refresh() { }
+
     fun updateEmptyView(empty: Boolean) {
         emptyView!!.visibility = if (empty) View.VISIBLE else View.GONE
     }
diff --git a/application/television/src/main/java/org/videolan/television/ui/browser/BaseTvActivity.kt b/application/television/src/main/java/org/videolan/television/ui/browser/BaseTvActivity.kt
index 272425a63..07ed0186e 100644
--- a/application/television/src/main/java/org/videolan/television/ui/browser/BaseTvActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/browser/BaseTvActivity.kt
@@ -55,6 +55,7 @@ import org.videolan.vlc.gui.helpers.UiTools
 
 private const val TAG = "VLC/BaseTvActivity"
 const val REQUEST_CODE_NO_CONNECTION = 100
+const val REQUEST_CODE_RESTART_APP = 101
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
@@ -154,6 +155,9 @@ abstract class BaseTvActivity : FragmentActivity() {
                     }
                 }
             }
+            REQUEST_CODE_RESTART_APP -> {
+                android.os.Process.killProcess(android.os.Process.myPid())
+            }
         }
         super.onActivityResult(requestCode, resultCode, data)
     }
diff --git a/application/television/src/main/java/org/videolan/television/ui/browser/TVActivity.kt b/application/television/src/main/java/org/videolan/television/ui/browser/TVActivity.kt
index 48de6f11d..20238face 100644
--- a/application/television/src/main/java/org/videolan/television/ui/browser/TVActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/browser/TVActivity.kt
@@ -1,14 +1,13 @@
 package org.videolan.television.ui.browser
 
 import android.os.Bundle
-import androidx.appcompat.app.AppCompatActivity
 import androidx.fragment.app.Fragment
 import org.videolan.television.R
 import org.videolan.vlc.gui.network.MRLPanelFragment
 import org.videolan.television.ui.MainTvActivity
 import org.videolan.resources.HEADER_STREAM
 
-class TVActivity : AppCompatActivity() {
+class TVActivity : BaseTvActivity() {
 
     private lateinit var fragment: Fragment
 
@@ -28,4 +27,6 @@ class TVActivity : AppCompatActivity() {
                     .commit()
         }
     }
+
+    override fun refresh() { }
 }
\ No newline at end of file
diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesActivity.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesActivity.kt
index 232d1fea8..1bf539bb8 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesActivity.kt
@@ -27,19 +27,19 @@ import android.annotation.TargetApi
 import android.os.Build
 import android.os.Bundle
 import android.view.MenuItem
-import androidx.fragment.app.FragmentActivity
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
-import org.videolan.vlc.PlaybackService
 import org.videolan.television.R
+import org.videolan.television.ui.browser.BaseTvActivity
 import org.videolan.tools.RESULT_RESTART
 import org.videolan.tools.RESULT_RESTART_APP
 import org.videolan.tools.Settings
+import org.videolan.vlc.PlaybackService
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
-class PreferencesActivity : FragmentActivity() {
+class PreferencesActivity : BaseTvActivity() {
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -47,6 +47,8 @@ class PreferencesActivity : FragmentActivity() {
         setContentView(R.layout.tv_preferences_activity)
     }
 
+    override fun refresh() {}
+
     override fun onOptionsItemSelected(item: MenuItem): Boolean {
         if (item.itemId == android.R.id.home) {
             if (!fragmentManager.popBackStackImmediate()) finish()
diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesUi.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesUi.kt
index b22d97c56..ee0479c60 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesUi.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesUi.kt
@@ -24,6 +24,7 @@
 package org.videolan.television.ui.preferences
 
 import android.annotation.TargetApi
+import android.content.Intent
 import android.content.SharedPreferences
 import android.os.Build
 import android.os.Bundle
@@ -33,16 +34,20 @@ import androidx.preference.TwoStatePreference
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.resources.AndroidDevices
+import org.videolan.resources.AppContextProvider
+import org.videolan.television.ui.browser.REQUEST_CODE_RESTART_APP
+import org.videolan.television.ui.dialogs.ConfirmationTvActivity
 import org.videolan.tools.*
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
-import org.videolan.vlc.gui.helpers.UiTools
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
 
+    private var currentLocale: String? = null
+
     override fun getXml() = R.xml.preferences_ui
 
     override fun getTitleId() = R.string.interface_prefs_screen
@@ -57,6 +62,19 @@ class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPrefer
         findPreference<Preference>(KEY_APP_THEME)?.isVisible = false
         findPreference<Preference>(LIST_TITLE_ELLIPSIZE)?.isVisible = false
         prepareLocaleList()
+        currentLocale = AppContextProvider.locale
+    }
+
+    override fun onResume() {
+        super.onResume()
+        val setLocale = Settings.getInstance(activity).getString("set_locale", "")
+        if (currentLocale != setLocale) {
+            val intent = Intent(activity, ConfirmationTvActivity::class.java)
+            intent.putExtra(ConfirmationTvActivity.CONFIRMATION_DIALOG_TITLE, getString(R.string.restart_vlc))
+            intent.putExtra(ConfirmationTvActivity.CONFIRMATION_DIALOG_TEXT, getString(R.string.restart_message))
+            activity.startActivityForResult(intent, REQUEST_CODE_RESTART_APP)
+            currentLocale = setLocale
+        }
     }
 
     override fun onStart() {
@@ -72,10 +90,6 @@ class PreferencesUi : BasePreferenceFragment(), SharedPreferences.OnSharedPrefer
 
     override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
         when (key) {
-            "set_locale" -> {
-                (activity as PreferencesActivity).setRestartApp()
-                UiTools.restartDialog(activity)
-            }
             PREF_TV_UI -> {
                 Settings.tvUI = sharedPreferences.getBoolean(PREF_TV_UI, false)
                 (activity as PreferencesActivity).setRestartApp()
diff --git a/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt b/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt
index b114bc8de..f608c9b0a 100644
--- a/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt
+++ b/application/television/src/main/java/org/videolan/television/viewmodel/MainTvModel.kt
@@ -48,6 +48,7 @@ import org.videolan.television.ui.browser.VerticalGridActivity
 import org.videolan.tools.NetworkMonitor
 import org.videolan.tools.PLAYBACK_HISTORY
 import org.videolan.tools.Settings
+import org.videolan.tools.getContextWithLocale
 import org.videolan.vlc.ExternalMonitor
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
@@ -69,7 +70,7 @@ private const val TAG = "MainTvModel"
 class MainTvModel(app: Application) : AndroidViewModel(app), Medialibrary.OnMedialibraryReadyListener,
         Medialibrary.OnDeviceChangeListener {
 
-    val context = getApplication<Application>().baseContext!!
+    val context = getApplication<Application>().getContextWithLocale(AppContextProvider.locale)
     private val medialibrary = Medialibrary.getInstance()
     private val networkMonitor = NetworkMonitor.getInstance(context)
     val settings = Settings.getInstance(context)



More information about the Android mailing list