[Android] Synchronize Application context creation & access

Geoffrey Métais git at videolan.org
Fri May 19 15:54:01 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri May 19 12:44:20 2017 +0200| [465d65dc91c46b499a6b9614ef35475dac8536ce] | committer: Geoffrey Métais

Synchronize Application context creation & access

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

 .../src/org/videolan/vlc/VLCApplication.java       | 66 +++++++++++-----------
 1 file changed, 34 insertions(+), 32 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index aabd957..9b72f41 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -85,39 +85,41 @@ public class VLCApplication extends Application {
 
     @Override
     public void onCreate() {
-        super.onCreate();
-
-        // Are we using advanced debugging - locale?
-        mSettings = PreferenceManager.getDefaultSharedPreferences(this);
-        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);
+        synchronized (getClass()) {
+            super.onCreate();
+
+            // Are we using advanced debugging - locale?
+            mSettings = PreferenceManager.getDefaultSharedPreferences(this);
+            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;
+                getResources().updateConfiguration(config,
+                        getResources().getDisplayMetrics());
             }
-            Locale.setDefault(locale);
-            Configuration config = new Configuration();
-            config.locale = locale;
-            getResources().updateConfiguration(config,
-                    getResources().getDisplayMetrics());
+            instance = this;
         }
-        instance = this;
 
         runBackground(new Runnable() {
             @Override
@@ -160,7 +162,7 @@ public class VLCApplication extends Application {
     /**
      * @return the main context of the Application
      */
-    public static Context getAppContext()
+    public static synchronized Context getAppContext()
     {
         return instance;
     }



More information about the Android mailing list