[Android] AppContextProvider: create a dedicated init fun

Geoffrey Métais git at videolan.org
Tue Jan 14 11:00:23 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jan 14 09:08:20 2020 +0100| [f42d35f12c70f82174c52e4ad05ff5cba3d1ee80] | committer: Nicolas Pomepuy

AppContextProvider: create a dedicated init fun

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

 .../java/org/videolan/resources/AppContextProvider.kt  | 18 ++++++++++--------
 vlc-android/src/org/videolan/vlc/VLCApplication.kt     |  6 +++---
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/resources/src/main/java/org/videolan/resources/AppContextProvider.kt b/resources/src/main/java/org/videolan/resources/AppContextProvider.kt
index deef4f8d5..2779a8602 100644
--- a/resources/src/main/java/org/videolan/resources/AppContextProvider.kt
+++ b/resources/src/main/java/org/videolan/resources/AppContextProvider.kt
@@ -30,28 +30,30 @@ import android.content.res.Resources
 import java.lang.reflect.InvocationTargetException
 
 object AppContextProvider {
-    lateinit var _appContext: Context
+    private lateinit var context: Context
 
     val appContext: Context
         get() {
-            return if (::_appContext.isInitialized)
-                _appContext
+            return if (::context.isInitialized) context
             else {
                 try {
-                    _appContext = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication").invoke(null) as Application
+                    context = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication").invoke(null) as Application
                 } catch (ignored: IllegalAccessException) {
                 } catch (ignored: InvocationTargetException) {
                 } catch (ignored: NoSuchMethodException) {
                 } catch (ignored: ClassNotFoundException) {
-                } catch (ignored: ClassCastException) {
-                }
-                _appContext
+                } catch (ignored: ClassCastException) {}
+                context
             }
         }
 
+    fun init(context: Context) {
+        this.context = context
+    }
+
     /**
      * @return the main resources from the Application
      */
     val appResources: Resources
         get() = appContext.resources
-}
\ No newline at end of file
+}
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.kt b/vlc-android/src/org/videolan/vlc/VLCApplication.kt
index d6516c819..ab9ddf576 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.kt
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.kt
@@ -54,7 +54,7 @@ import org.videolan.vlc.util.VLCInstance
 class VLCApplication : MultiDexApplication(), Dialog.Callbacks by DialogDelegate {
 
     init {
-        AppContextProvider._appContext = this
+        AppContextProvider.init(this)
         AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
 
         FactoryManager.registerFactory(IMediaFactory.factoryId, MediaFactory())
@@ -69,7 +69,7 @@ class VLCApplication : MultiDexApplication(), Dialog.Callbacks by DialogDelegate
         Thread(Runnable {
             locale = Settings.getInstance(this).getString("set_locale", "")
             locale.takeIf { !it.isNullOrEmpty() }?.let {
-                AppContextProvider._appContext = ContextWrapper(this).wrap(locale!!)
+                AppContextProvider.init(ContextWrapper(this).wrap(it))
             }
 
             AppScope.launch(Dispatchers.IO) {
@@ -90,7 +90,7 @@ class VLCApplication : MultiDexApplication(), Dialog.Callbacks by DialogDelegate
     override fun onConfigurationChanged(newConfig: Configuration) {
         super.onConfigurationChanged(newConfig)
         locale.takeIf { !it.isNullOrEmpty() }?.let {
-            AppContextProvider._appContext = ContextWrapper(this).wrap(locale!!)
+            AppContextProvider.init(ContextWrapper(this).wrap(it))
         }
     }
 



More information about the Android mailing list