[Android] Limit scan notification rate to 1Hz

Geoffrey Métais git at videolan.org
Mon Jan 30 10:32:32 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan 30 10:27:01 2017 +0100| [7f3cacbe85c8a86e8073c8e923281dc096856a5e] | committer: Geoffrey Métais

Limit scan notification rate to 1Hz

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

 .../src/org/videolan/vlc/MediaParsingService.java  | 61 ++++++++++++++--------
 1 file changed, 40 insertions(+), 21 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index cb10038..9df4458 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -33,12 +33,13 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
     public final static String ACTION_DISCOVER = "medialibrary_discover";
 
     public final static String EXTRA_PATH = "extra_path";
+    public static final long NOTIFICATION_DELAY = 1000L;
 
     private final IBinder mBinder = new LocalBinder();
     private Medialibrary mMedialibrary;
     private int mParsing = 0;
     private String mCurrentProgress = null;
-    private StringBuilder sb = new StringBuilder();
+    private long mLastNotificationTime = 0;
 
     @Nullable
     @Override
@@ -110,29 +111,47 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
     }
 
     private void showNotification() {
-        sb.setLength(0);
-        if (mParsing > 0)
-            sb.append(getString(R.string.ml_parse_media)).append(' ').append(mParsing).append("%");
-        else if (mCurrentProgress != null)
-            sb.append(getString(R.string.ml_discovering)).append(' ').append(Uri.decode(Strings.removeFileProtocole(mCurrentProgress)));
-        else
-            sb.append(getString(R.string.ml_parse_media));
-        NotificationCompat.Builder builder = new NotificationCompat.Builder(MediaParsingService.this)
-                .setSmallIcon(R.drawable.ic_notif_scan)
-                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
-                .setContentTitle(getString(R.string.ml_scanning))
-                .setContentText(sb.toString())
-                .setAutoCancel(false)
-                .setOngoing(true);
-
-        Notification notification = builder.build();
-        try {
-            NotificationManagerCompat.from(MediaParsingService.this).notify(43, notification);
-        } catch (IllegalArgumentException ignored) {}
+        final long currentTime = System.currentTimeMillis();
+        synchronized (this) {
+            if (mLastNotificationTime == -1L || currentTime-mLastNotificationTime < NOTIFICATION_DELAY)
+                return;
+            mLastNotificationTime = currentTime;
+        }
+        VLCApplication.runBackground(new Runnable() {
+            final StringBuilder sb = new StringBuilder();
+            @Override
+            public void run() {
+                if (mParsing > 0)
+                    sb.append(getString(R.string.ml_parse_media)).append(' ').append(mParsing).append("%");
+                else if (mCurrentProgress != null)
+                    sb.append(getString(R.string.ml_discovering)).append(' ').append(Uri.decode(Strings.removeFileProtocole(mCurrentProgress)));
+                else
+                    sb.append(getString(R.string.ml_parse_media));
+                NotificationCompat.Builder builder = new NotificationCompat.Builder(MediaParsingService.this)
+                        .setSmallIcon(R.drawable.ic_notif_scan)
+                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
+                        .setContentTitle(getString(R.string.ml_scanning))
+                        .setContentText(sb.toString())
+                        .setAutoCancel(false)
+                        .setOngoing(true);
+
+                final Notification notification = builder.build();
+                synchronized (MediaParsingService.this) {
+                    if (mLastNotificationTime != -1L) {
+                        try {
+                            NotificationManagerCompat.from(MediaParsingService.this).notify(43, notification);
+                        } catch (IllegalArgumentException ignored) {}
+                    }
+                }
+            }
+        });
     }
 
     private void hideNotification() {
-        NotificationManagerCompat.from(MediaParsingService.this).cancel(43);
+        synchronized (this) {
+            mLastNotificationTime = -1L;
+            NotificationManagerCompat.from(MediaParsingService.this).cancel(43);
+        }
     }
 
     @Override



More information about the Android mailing list