[Android] Avoid using the static Application to override getApplicationContext

Nicolas Pomepuy git at videolan.org
Tue Aug 13 10:17:14 CEST 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Tue Aug 13 07:23:43 2019 +0200| [bbe6f44d8da7400f79573ccdbe32c4879f67dc03] | committer: Nicolas Pomepuy

Avoid using the static Application to override getApplicationContext

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

 vlc-android/src/org/videolan/vlc/DebugLogService.kt               | 2 +-
 vlc-android/src/org/videolan/vlc/MediaParsingService.kt           | 2 +-
 vlc-android/src/org/videolan/vlc/PlaybackService.kt               | 2 +-
 vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt      | 2 +-
 vlc-android/src/org/videolan/vlc/RecommendationsService.kt        | 2 +-
 vlc-android/src/org/videolan/vlc/StartActivity.kt                 | 2 +-
 vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt              | 3 +--
 .../src/org/videolan/vlc/gui/helpers/NotificationHelper.kt        | 8 ++------
 vlc-android/src/org/videolan/vlc/gui/tv/browser/BaseTvActivity.kt | 2 +-
 vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt | 8 ++++++++
 10 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/DebugLogService.kt b/vlc-android/src/org/videolan/vlc/DebugLogService.kt
index 0244e24d5..5316bb839 100644
--- a/vlc-android/src/org/videolan/vlc/DebugLogService.kt
+++ b/vlc-android/src/org/videolan/vlc/DebugLogService.kt
@@ -46,7 +46,7 @@ class DebugLogService : Service(), Logcat.Callback, Runnable {
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     override fun onBind(intent: Intent): IBinder? {
diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index 717844f47..487d8d8bd 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -96,7 +96,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     @SuppressLint("WakelockTimeout")
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index fac4c3458..5022a3099 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -448,7 +448,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     override fun onCreate() {
diff --git a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
index 50973d305..3dde387c7 100644
--- a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
+++ b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
@@ -38,7 +38,7 @@ class PreviewVideoInputService : TvInputService(), CoroutineScope {
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     private inner class PreviewSession(context: Context
diff --git a/vlc-android/src/org/videolan/vlc/RecommendationsService.kt b/vlc-android/src/org/videolan/vlc/RecommendationsService.kt
index 04e450452..8742ed8e7 100644
--- a/vlc-android/src/org/videolan/vlc/RecommendationsService.kt
+++ b/vlc-android/src/org/videolan/vlc/RecommendationsService.kt
@@ -55,7 +55,7 @@ class RecommendationsService : IntentService("RecommendationService"), Coroutine
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     override fun onCreate() {
diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.kt b/vlc-android/src/org/videolan/vlc/StartActivity.kt
index 06997dc09..f431f6113 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.kt
@@ -80,7 +80,7 @@ class StartActivity : FragmentActivity() {
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     override fun onCreate(savedInstanceState: Bundle?) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt b/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt
index 4b600067e..c57a7f024 100644
--- a/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/BaseActivity.kt
@@ -9,7 +9,6 @@ import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.MainScope
 import kotlinx.coroutines.cancel
 import org.videolan.tools.KeyHelper
-import org.videolan.vlc.VLCApplication
 import org.videolan.vlc.gui.helpers.applyTheme
 import org.videolan.vlc.util.Settings
 import org.videolan.vlc.util.getContextWithLocale
@@ -30,7 +29,7 @@ open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
index aba8acc25..28ba2b3f4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
@@ -35,10 +35,7 @@ import androidx.core.app.NotificationCompat
 import androidx.media.session.MediaButtonReceiver
 import org.videolan.vlc.R
 import org.videolan.vlc.StartActivity
-import org.videolan.vlc.util.ACTION_PAUSE_SCAN
-import org.videolan.vlc.util.ACTION_RESUME_SCAN
-import org.videolan.vlc.util.AndroidDevices
-import org.videolan.vlc.util.Util
+import org.videolan.vlc.util.*
 
 object NotificationHelper {
     const val TAG = "VLC/NotificationHelper"
@@ -115,7 +112,7 @@ object NotificationHelper {
         scanCompatBuilder.setContentText(progressText)
 
         notificationIntent.action = if (paused) ACTION_RESUME_SCAN else ACTION_PAUSE_SCAN
-        val pi = PendingIntent.getBroadcast(ctx.applicationContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
+        val pi = PendingIntent.getBroadcast(ctx.applicationContext.getContextWithLocale(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
         val playpause = if (paused)
             NotificationCompat.Action(R.drawable.ic_play, ctx.getString(R.string.resume), pi)
         else
@@ -162,7 +159,6 @@ object NotificationHelper {
     @RequiresApi(api = Build.VERSION_CODES.O)
     fun createDebugServcieChannel(appCtx: Context) {
         val notificationManager = appCtx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-                ?: return
 // Playback channel
         val name = appCtx.getString(R.string.debug_logs)
         val channel = NotificationChannel(VLC_DEBUG_CHANNEL, name, NotificationManager.IMPORTANCE_LOW)
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 52eace2e7..b3fcd7260 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
@@ -61,7 +61,7 @@ abstract class BaseTvActivity : FragmentActivity(), CoroutineScope by MainScope(
     }
 
     override fun getApplicationContext(): Context {
-        return VLCApplication.appContext
+        return super.getApplicationContext().getContextWithLocale()
     }
 
     override fun onCreate(savedInstanceState: Bundle?) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index 91dc507ca..92182c3ff 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -403,6 +403,14 @@ open class VideoPlayerActivity : AppCompatActivity(), IPlaybackSettingsControlle
         }
     }
 
+    override fun attachBaseContext(newBase: Context?) {
+        super.attachBaseContext(newBase?.getContextWithLocale())
+    }
+
+    override fun getApplicationContext(): Context {
+        return super.getApplicationContext().getContextWithLocale()
+    }
+
     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)



More information about the Android mailing list