[Android] Set Locale on activity re-creation

Geoffrey Métais git at videolan.org
Tue Jul 11 18:05:29 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jul 11 18:04:41 2017 +0200| [442134d81102e6d54e1708f95d4f50392a0b0cee] | committer: Geoffrey Métais

Set Locale on activity re-creation

Should fix locale failure on multiwindowing with recent Android versions

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

 .../src/org/videolan/vlc/VLCApplication.java       | 68 +++++++++++-----------
 .../src/org/videolan/vlc/gui/AboutFragment.java    |  2 +-
 .../vlc/gui/AudioPlayerContainerActivity.java      |  1 +
 3 files changed, 36 insertions(+), 35 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index 19d2de7c0..087abdcbf 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -83,45 +83,13 @@ public class VLCApplication extends Application {
 
     private static int sDialogCounter = 0;
 
-    public static void setLocale(Context context){
-        // Are we using advanced debugging - locale?
-        String p = mSettings.getString("set_locale", "");
-        if (!p.equals("")) {
-            Locale locale;
-            // workaround due to region code
-            if(p.equals("zh-TW")) {
-                locale = Locale.TRADITIONAL_CHINESE;
-            } else if(p.startsWith("zh")) {
-                locale = Locale.CHINA;
-            } else if(p.equals("pt-BR")) {
-                locale = new Locale("pt", "BR");
-            } else if(p.equals("bn-IN") || p.startsWith("bn")) {
-                locale = new Locale("bn", "IN");
-            } else {
-                /**
-                 * Avoid a crash of
-                 * java.lang.AssertionError: couldn't initialize LocaleData for locale
-                 * if the user enters nonsensical region codes.
-                 */
-                if(p.contains("-"))
-                    p = p.substring(0, p.indexOf('-'));
-                locale = new Locale(p);
-            }
-            Locale.setDefault(locale);
-            Configuration config = new Configuration();
-            config.locale = locale;
-            context.getResources().updateConfiguration(config,
-                    context.getResources().getDisplayMetrics());
-        }
-    }
-
     @Override
     public void onCreate() {
         super.onCreate();
         instance = this;
         mSettings = PreferenceManager.getDefaultSharedPreferences(this);
 
-        setLocale(this);
+        setLocale();
 
         runBackground(new Runnable() {
             @Override
@@ -145,7 +113,7 @@ public class VLCApplication extends Application {
     @Override
     public void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
-        setLocale(this);
+        setLocale();
     }
 
     /**
@@ -264,4 +232,36 @@ public class VLCApplication extends Application {
         }
         return sMedialibraryInstance;
     }
+
+    public static void setLocale(){
+        // Are we using advanced debugging - locale?
+        String p = mSettings.getString("set_locale", "");
+        if (!p.equals("")) {
+            Locale locale;
+            // workaround due to region code
+            if(p.equals("zh-TW")) {
+                locale = Locale.TRADITIONAL_CHINESE;
+            } else if(p.startsWith("zh")) {
+                locale = Locale.CHINA;
+            } else if(p.equals("pt-BR")) {
+                locale = new Locale("pt", "BR");
+            } else if(p.equals("bn-IN") || p.startsWith("bn")) {
+                locale = new Locale("bn", "IN");
+            } else {
+                /**
+                 * Avoid a crash of
+                 * java.lang.AssertionError: couldn't initialize LocaleData for locale
+                 * if the user enters nonsensical region codes.
+                 */
+                if(p.contains("-"))
+                    p = p.substring(0, p.indexOf('-'));
+                locale = new Locale(p);
+            }
+            Locale.setDefault(locale);
+            Configuration config = new Configuration();
+            config.locale = locale;
+            getAppResources().updateConfiguration(config,
+                    getAppResources().getDisplayMetrics());
+        }
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java
index 4722742d2..128dfaecf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java
@@ -56,7 +56,7 @@ public class AboutFragment extends Fragment {
         //Fix android 7 Locale problem with webView
         //https://stackoverflow.com/questions/40398528/android-webview-locale-changes-abruptly-on-android-n
         if (AndroidUtil.isNougatOrLater)
-            VLCApplication.setLocale(getContext());
+            VLCApplication.setLocale();
 
         View aboutMain = v.findViewById(R.id.about_main);
         WebView webView = (WebView)v.findViewById(R.id.webview);
diff --git a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
index a7bf2cd88..b9dd1d088 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
@@ -99,6 +99,7 @@ public class AudioPlayerContainerActivity extends BaseActivity implements Playba
     protected void onCreate(Bundle savedInstanceState) {
         //Init Medialibrary if KO
         if (savedInstanceState != null) {
+            VLCApplication.setLocale();
             if (!VLCApplication.getMLInstance().isInitiated() && Permissions.canReadStorage())
                 startService(new Intent(MediaParsingService.ACTION_INIT, null, this, MediaParsingService.class));
         }



More information about the Android mailing list