[Android] Remove Util.startService

Geoffrey Métais git at videolan.org
Wed Jul 25 09:48:00 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jul 25 09:47:14 2018 +0200| [4c196dbee256bd094bd3b94bdf9539fdab2975c3] | committer: Geoffrey Métais

Remove Util.startService

Not needed anymore thank to AppCompat 27+

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

 vlc-android/src/org/videolan/vlc/PlaybackService.kt | 10 +++++-----
 vlc-android/src/org/videolan/vlc/StartActivity.java |  4 ++--
 vlc-android/src/org/videolan/vlc/util/Util.java     | 13 -------------
 3 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index aee1e9aab..b285edece 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -32,6 +32,7 @@ import android.preference.PreferenceManager
 import android.support.annotation.MainThread
 import android.support.v4.app.NotificationManagerCompat
 import android.support.v4.app.ServiceCompat
+import android.support.v4.content.ContextCompat
 import android.support.v4.media.MediaBrowserCompat
 import android.support.v4.media.MediaBrowserServiceCompat
 import android.support.v4.media.MediaDescriptionCompat
@@ -1230,7 +1231,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
             if (mBound) throw IllegalStateException("already connected")
             val serviceIntent = getServiceIntent(mContext!!)
             if (mContext is Activity) mContext.startService(serviceIntent)
-            else Util.startService(mContext, serviceIntent)
+            else ContextCompat.startForegroundService(mContext, serviceIntent)
             mBound = mContext.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE)
         }
 
@@ -1250,7 +1251,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
             }
 
             private fun startService(context: Context) {
-                Util.startService(context, getServiceIntent(context))
+                ContextCompat.startForegroundService(context, getServiceIntent(context))
             }
 
             private fun stopService(context: Context) {
@@ -1315,9 +1316,8 @@ class PlaybackService : MediaBrowserServiceCompat() {
         }
 
         fun loadLastAudio(context: Context) {
-            val i = PlaybackService.Client.getServiceIntent(context)
-            i.action = Constants.ACTION_REMOTE_LAST_PLAYLIST
-            Util.startService(context, i)
+            val i = PlaybackService.Client.getServiceIntent(context).apply { action = Constants.ACTION_REMOTE_LAST_PLAYLIST }
+            ContextCompat.startForegroundService(context, i)
         }
 
         private const val PLAYBACK_BASE_ACTIONS = (PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 67f552968..ccd068bf9 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -31,6 +31,7 @@ import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.provider.MediaStore;
 import android.support.v4.app.FragmentActivity;
+import android.support.v4.content.ContextCompat;
 import android.text.TextUtils;
 
 import org.videolan.libvlc.util.AndroidUtil;
@@ -43,7 +44,6 @@ import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Constants;
-import org.videolan.vlc.util.Util;
 
 import videolan.org.commontools.TvChannelUtilsKt;
 
@@ -92,7 +92,7 @@ public class StartActivity extends FragmentActivity implements StoragePermission
         } else if (MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH.equals(action)) {
             final Intent serviceInent = new Intent(Constants.ACTION_PLAY_FROM_SEARCH, null, this, PlaybackService.class)
                     .putExtra(Constants.EXTRA_SEARCH_BUNDLE, intent.getExtras());
-            Util.startService(this, serviceInent);
+            ContextCompat.startForegroundService(this, serviceInent);
         } else if(Intent.ACTION_VIEW.equals(action) && intent.getData() != null) { //launch from TV Channel
             final Uri data = intent.getData();
             final String path = data.getPath();
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 79706cd53..1da9c12ec 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -179,19 +179,6 @@ public class Util {
         return contentBuilder.toString();
     }
 
-    /**
-     * Starts a service in Foreground in Android 8+
-     * To be removed with AppCompat 26+
-     * @param ctx the context
-     * @param intent service intent to start
-     */
-    public static void startService(Context ctx, Intent intent) {
-        if (!AndroidUtil.isOOrLater || VLCApplication.isForeground())
-            ctx.startService(intent);
-        else
-            ctx.startForegroundService(intent);
-    }
-
     public static boolean byPassChromecastDialog(Dialog.QuestionDialog dialog) {
         if ("Insecure site".equals(dialog.getTitle())) {
             if ("View certificate".equals(dialog.getAction1Text())) dialog.postAction(1);



More information about the Android mailing list