[Android] NotifiationHelper: code cleaning

Geoffrey Métais git at videolan.org
Mon Jun 17 09:46:41 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jun 17 09:45:25 2019 +0200| [88816d1fa3d50072434bc78f23b3c526b362f7a1] | committer: Geoffrey Métais

NotifiationHelper: code cleaning

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

 .../src/org/videolan/vlc/MediaParsingService.kt    |  8 +---
 .../videolan/vlc/gui/helpers/NotificationHelper.kt | 46 ++++++++++------------
 2 files changed, 22 insertions(+), 32 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
index da3d382e9..bc566987b 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.kt
@@ -46,7 +46,6 @@ import kotlinx.coroutines.channels.actor
 import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.interfaces.DevicesDiscoveryCb
-import org.videolan.vlc.extensions.ExtensionManagerService
 import org.videolan.vlc.gui.helpers.NotificationHelper
 import org.videolan.vlc.repository.DirectoryRepository
 import org.videolan.vlc.util.*
@@ -77,7 +76,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
 
     @Volatile
     private var serviceLock = false
-    private var wasWorking: Boolean = false
     internal val sb = StringBuilder()
 
     private val notificationActor by lazy {
@@ -142,7 +140,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
 
     @TargetApi(Build.VERSION_CODES.O)
     private fun forceForeground() {
-        val notification = NotificationHelper.createScanNotification(applicationContext, getString(R.string.loading_medialibrary), false, scanPaused)
+        val notification = NotificationHelper.createScanNotification(applicationContext, getString(R.string.loading_medialibrary), scanPaused)
         NotificationHelper.createNotificationChannels(applicationContext)
         startForeground(43, notification)
     }
@@ -285,10 +283,8 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope {
                 else -> sb.append(getString(R.string.ml_parse_media))
             }
             val progressText = sb.toString()
-            val updateAction = wasWorking != medialibrary.isWorking
-            if (updateAction) wasWorking = !wasWorking
             if (!isActive) return at withContext ""
-            val notification = NotificationHelper.createScanNotification(applicationContext, progressText, updateAction, scanPaused)
+            val notification = NotificationHelper.createScanNotification(applicationContext, progressText, scanPaused)
             if (lastNotificationTime != -1L) {
                 try {
                     startForeground(43, notification)
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 a9c05ece9..aba8acc25 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/NotificationHelper.kt
@@ -41,12 +41,11 @@ import org.videolan.vlc.util.AndroidDevices
 import org.videolan.vlc.util.Util
 
 object NotificationHelper {
-    val TAG = "VLC/NotificationHelper"
+    const val TAG = "VLC/NotificationHelper"
 
     private val sb = StringBuilder()
-    val VLC_DEBUG_CHANNEL = "vlc_debug"
+    const val VLC_DEBUG_CHANNEL = "vlc_debug"
 
-    private var scanCompatBuilder: NotificationCompat.Builder? = null
     private val notificationIntent = Intent()
 
     fun createPlaybackNotification(ctx: Context, video: Boolean, title: String, artist: String,
@@ -104,30 +103,25 @@ object NotificationHelper {
         return builder.build()
     }
 
-    fun createScanNotification(ctx: Context, progressText: String, updateActions: Boolean, paused: Boolean): Notification {
-        if (scanCompatBuilder == null) {
-            scanCompatBuilder = NotificationCompat.Builder(ctx, "vlc_medialibrary")
-                    .setContentIntent(PendingIntent.getActivity(ctx, 0, Intent(ctx, StartActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
-                    .setSmallIcon(R.drawable.ic_notif_scan)
-                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
-                    .setContentTitle(ctx.getString(R.string.ml_scanning))
-                    .setAutoCancel(false)
-                    .setCategory(NotificationCompat.CATEGORY_PROGRESS)
-                    .setOngoing(true)
-        }
-        scanCompatBuilder!!.setContentText(progressText)
+    fun createScanNotification(ctx: Context, progressText: String, paused: Boolean): Notification {
+        val scanCompatBuilder = NotificationCompat.Builder(ctx, "vlc_medialibrary")
+                .setContentIntent(PendingIntent.getActivity(ctx, 0, Intent(ctx, StartActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
+                .setSmallIcon(R.drawable.ic_notif_scan)
+                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
+                .setContentTitle(ctx.getString(R.string.ml_scanning))
+                .setAutoCancel(false)
+                .setCategory(NotificationCompat.CATEGORY_PROGRESS)
+                .setOngoing(true)
+        scanCompatBuilder.setContentText(progressText)
 
-        if (updateActions) {
-            notificationIntent.action = if (paused) ACTION_RESUME_SCAN else ACTION_PAUSE_SCAN
-            val pi = PendingIntent.getBroadcast(ctx.applicationContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
-            val playpause = if (paused)
-                NotificationCompat.Action(R.drawable.ic_play, ctx.getString(R.string.resume), pi)
-            else
-                NotificationCompat.Action(R.drawable.ic_pause, ctx.getString(R.string.pause), pi)
-            scanCompatBuilder!!.mActions.clear()
-            scanCompatBuilder!!.addAction(playpause)
-        }
-        return scanCompatBuilder!!.build()
+        notificationIntent.action = if (paused) ACTION_RESUME_SCAN else ACTION_PAUSE_SCAN
+        val pi = PendingIntent.getBroadcast(ctx.applicationContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
+        val playpause = if (paused)
+            NotificationCompat.Action(R.drawable.ic_play, ctx.getString(R.string.resume), pi)
+        else
+            NotificationCompat.Action(R.drawable.ic_pause, ctx.getString(R.string.pause), pi)
+        scanCompatBuilder.addAction(playpause)
+        return scanCompatBuilder.build()
     }
 
     @RequiresApi(api = Build.VERSION_CODES.O)



More information about the Android mailing list