[Android] Oreo: Start services in foreground when needed

Geoffrey Métais git at videolan.org
Fri Sep 1 12:59:22 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Sep  1 12:50:37 2017 +0200| [8008b7af2b87eeeb07e80157e4808a7c238e932f] | committer: Geoffrey Métais

Oreo: Start services in foreground when needed

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

 vlc-android/src/org/videolan/vlc/MediaParsingService.java     |  4 +++-
 vlc-android/src/org/videolan/vlc/PlaybackService.java         | 11 ++++++-----
 .../src/org/videolan/vlc/RemoteControlClientReceiver.java     |  7 ++++---
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaParsingService.java b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
index 1fecf448f..b1e9807ad 100644
--- a/vlc-android/src/org/videolan/vlc/MediaParsingService.java
+++ b/vlc-android/src/org/videolan/vlc/MediaParsingService.java
@@ -120,8 +120,10 @@ public class MediaParsingService extends Service implements DevicesDiscoveryCb {
         if (intent == null)
             return START_NOT_STICKY;
         synchronized (MediaParsingService.this) {
+            // Set 1s delay before displaying scan icon
+            // Except for Android 8+ which expects startForeground immediatly
             if (mLastNotificationTime <= 0L)
-                mLastNotificationTime = System.currentTimeMillis();
+                mLastNotificationTime = VLCApplication.isForeground() ? System.currentTimeMillis() : 0L;
         }
         switch (intent.getAction()) {
             case ACTION_INIT:
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 7f5acec81..dcde1ee17 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1214,7 +1214,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
         @Override
         public void onPlayFromSearch(final String query, final Bundle extras) {
             if (!mMedialibrary.isInitiated()) {
-                startService(new Intent(MediaParsingService.ACTION_INIT, null, PlaybackService.this, MediaParsingService.class));
+                Util.startService(PlaybackService.this, new Intent(MediaParsingService.ACTION_INIT, null, PlaybackService.this, MediaParsingService.class));
                 final BroadcastReceiver libraryReadyReceiver = new BroadcastReceiver() {
                     @Override
                     public void onReceive(Context context, Intent intent) {
@@ -1538,7 +1538,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
                 }
             };
             lbm.registerReceiver(mLibraryReceiver, new IntentFilter(VLCApplication.ACTION_MEDIALIBRARY_READY));
-            startService(new Intent(MediaParsingService.ACTION_INIT, null, this, MediaParsingService.class));
+            Util.startService(PlaybackService.this, new Intent(MediaParsingService.ACTION_INIT, null, this, MediaParsingService.class));
         }
     }
 
@@ -2563,8 +2563,9 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
         public void connect() {
             if (mBound)
                 throw new IllegalStateException("already connected");
-            startService(mContext);
-            mBound = mContext.bindService(getServiceIntent(mContext), mServiceConnection, BIND_AUTO_CREATE);
+            final Intent serviceIntent = getServiceIntent(mContext);
+            mContext.startService(serviceIntent);
+            mBound = mContext.bindService(serviceIntent, mServiceConnection, BIND_AUTO_CREATE);
         }
 
         @MainThread
@@ -2596,7 +2597,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     public void onLoadChildren(@NonNull final String parentId, @NonNull final Result<List<MediaBrowserCompat.MediaItem>> result) {
         result.detach();
         if (!mMLInitializing && !mMedialibrary.isInitiated() && BrowserProvider.ID_ROOT.equals(parentId)) {
-            startService(new Intent(MediaParsingService.ACTION_INIT, null, this, MediaParsingService.class));
+            Util.startService(PlaybackService.this, new Intent(MediaParsingService.ACTION_INIT, null, this, MediaParsingService.class));
             mMLInitializing = true;
         }
         VLCApplication.runBackground(new Runnable() {
diff --git a/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java b/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java
index 89c90d275..02d560629 100644
--- a/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java
+++ b/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java
@@ -26,6 +26,7 @@ import android.support.v4.media.session.MediaButtonReceiver;
 import android.view.KeyEvent;
 
 import org.videolan.vlc.util.AndroidDevices;
+import org.videolan.vlc.util.Util;
 
 /**
  * Small class to receive events passed out by the remote controls (wired, bluetooth, lock screen, ...)
@@ -84,8 +85,8 @@ public class RemoteControlClientReceiver extends MediaButtonReceiver {
                     }
                     break;
                 case KeyEvent.KEYCODE_MEDIA_PLAY:
-                    context.startService(new Intent(PlaybackService.ACTION_REMOTE_PLAY, null, context, PlaybackService.class));
-                    return;
+                    i = new Intent(PlaybackService.ACTION_REMOTE_PLAY, null, context, PlaybackService.class);
+                    break;
                 case KeyEvent.KEYCODE_MEDIA_PAUSE:
                     i = new Intent(PlaybackService.ACTION_REMOTE_PAUSE, null, context, PlaybackService.class);
                     break;
@@ -103,7 +104,7 @@ public class RemoteControlClientReceiver extends MediaButtonReceiver {
             if (isOrderedBroadcast())
                 abortBroadcast();
             if (i != null) {
-                context.startService(i);
+                Util.startService(context, i);
                 return;
             }
         } else if (action.equals(PlaybackService.ACTION_REMOTE_PLAYPAUSE)) {



More information about the Android mailing list