[Android] Always use ContextCompat.startForegroundService()
Geoffrey Métais
git at videolan.org
Thu Jan 10 15:05:59 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 10 11:13:50 2019 +0100| [706339b85be42a39d5af221eb07f56f4ebeabe9d] | committer: Geoffrey Métais
Always use ContextCompat.startForegroundService()
> https://code.videolan.org/videolan/vlc-android/commit/706339b85be42a39d5af221eb07f56f4ebeabe9d
---
vlc-android/src/org/videolan/vlc/ExternalMonitor.kt | 10 +++++-----
vlc-android/src/org/videolan/vlc/PlaybackService.kt | 7 +++----
.../src/org/videolan/vlc/gui/helpers/MedialibraryUtils.java | 6 ++++--
vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java | 8 +++-----
4 files changed, 15 insertions(+), 16 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt b/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
index fbacb93be..245b6ba05 100644
--- a/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
+++ b/vlc-android/src/org/videolan/vlc/ExternalMonitor.kt
@@ -37,13 +37,11 @@ import android.net.NetworkCapabilities
import android.net.Uri
import android.os.Build
import android.text.TextUtils
+import androidx.core.content.ContextCompat
import androidx.lifecycle.*
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.actor
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.launch
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.gui.helpers.hf.OtgAccess
@@ -189,12 +187,14 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
checkNewStorages(ctx)
}
+ @ExperimentalCoroutinesApi
+ @ObsoleteCoroutinesApi
private fun checkNewStorages(ctx: Context) {
if (VLCApplication.getMLInstance().isStarted) {
val scanOpt = if (AndroidDevices.showTvUi(ctx)) ML_SCAN_ON
else Settings.getInstance(ctx).getInt(KEY_MEDIALIBRARY_SCAN, -1)
if (scanOpt == ML_SCAN_ON)
- AppScope.launch { ctx.startService(Intent(ACTION_CHECK_STORAGES, null, ctx, MediaParsingService::class.java)) }
+ AppScope.launch { ContextCompat.startForegroundService(ctx,Intent(ACTION_CHECK_STORAGES, null, ctx, MediaParsingService::class.java)) }
}
val usbManager = ctx.getSystemService(Context.USB_SERVICE) as? UsbManager ?: return
devices.add(ArrayList(usbManager.deviceList.values))
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 983e94340..b08be9319 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -70,6 +70,7 @@ import java.util.*
private const val TAG = "VLC/PlaybackService"
+ at ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOwner {
override val coroutineContext = Dispatchers.Main.immediate
@@ -517,7 +518,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
}
ACTION_PLAY_FROM_SEARCH -> {
if (!this::mediaSession.isInitialized) initMediaSession()
- val extras = intent!!.getBundleExtra(EXTRA_SEARCH_BUNDLE)
+ val extras = intent.getBundleExtra(EXTRA_SEARCH_BUNDLE)
mediaSession.controller.transportControls
.playFromSearch(extras.getString(SearchManager.QUERY), extras)
}
@@ -615,7 +616,6 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
}
private class PlaybackServiceHandler(owner: PlaybackService) : WeakHandler<PlaybackService>(owner) {
- private var lastPublicationDate = 0L
override fun handleMessage(msg: Message) {
val service = owner ?: return
@@ -1246,8 +1246,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
fun connect() {
if (mBound) throw IllegalStateException("already connected")
val serviceIntent = getServiceIntent(mContext!!)
- if (mContext is Activity) mContext.startService(serviceIntent)
- else ContextCompat.startForegroundService(mContext, serviceIntent)
+ ContextCompat.startForegroundService(mContext, serviceIntent)
mBound = mContext.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE)
}
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/MedialibraryUtils.java b/vlc-android/src/org/videolan/vlc/gui/helpers/MedialibraryUtils.java
index 344aa962a..daa49aa2f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/MedialibraryUtils.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/MedialibraryUtils.java
@@ -9,6 +9,8 @@ import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.util.Constants;
import org.videolan.vlc.util.WorkersKt;
+import androidx.core.content.ContextCompat;
+
public class MedialibraryUtils {
public static void removeDir(final String path) {
@@ -27,12 +29,12 @@ public class MedialibraryUtils {
public static void addDir(final String path, Context context) {
final Intent intent = new Intent(Constants.ACTION_DISCOVER, null, context, MediaParsingService.class);
intent.putExtra(Constants.EXTRA_PATH, path);
- context.startService(intent);
+ ContextCompat.startForegroundService(context, intent);
}
public static void addDevice(final String path, Context context) {
final Intent intent = new Intent(Constants.ACTION_DISCOVER_DEVICE, null, context, MediaParsingService.class);
intent.putExtra(Constants.EXTRA_PATH, path);
- context.startService(intent);
+ ContextCompat.startForegroundService(context, intent);
}
}
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
index 937264ae0..beafcc1eb 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
@@ -389,7 +389,7 @@ public class UiTools {
final String uuid = FileUtils.getFileNameFromPath(path);
final String deviceName = FileUtils.getStorageTag(uuid);
final String message = String.format(activity.getString(R.string.ml_external_storage_msg), deviceName != null ? deviceName : uuid);
- final Intent serviceInent = new Intent(Constants.ACTION_DISCOVER_DEVICE, null, activity, MediaParsingService.class)
+ final Intent si = new Intent(Constants.ACTION_DISCOVER_DEVICE, null, activity, MediaParsingService.class)
.putExtra(Constants.EXTRA_PATH, path);
if (activity instanceof AppCompatActivity) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
@@ -398,8 +398,7 @@ public class UiTools {
.setMessage(message)
.setPositiveButton(R.string.ml_external_storage_accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- if (activity != null)
- activity.startService(serviceInent);
+ if (activity != null) ContextCompat.startForegroundService(activity, si);
}
})
.setNegativeButton(R.string.ml_external_storage_decline, new DialogInterface.OnClickListener() {
@@ -419,8 +418,7 @@ public class UiTools {
.setMessage(message)
.setPositiveButton(R.string.ml_external_storage_accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- if (activity != null)
- activity.startService(serviceInent);
+ if (activity != null) ContextCompat.startForegroundService(activity, si);
}
})
.setNegativeButton(R.string.ml_external_storage_decline, new DialogInterface.OnClickListener() {
More information about the Android
mailing list