Commit 57ebfe3f authored by Geoffrey Métais's avatar Geoffrey Métais

Prevent crash on Oreo when loadlastplaylist fails

parent cedc1837
...@@ -250,6 +250,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -250,6 +250,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (AndroidUtil.isOOrLater && !VLCApplication.isForeground()) forceForeground();
if (intent == null) return START_NOT_STICKY; if (intent == null) return START_NOT_STICKY;
final String action = intent.getAction(); final String action = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(action)) { if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
...@@ -263,8 +264,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -263,8 +264,7 @@ public class PlaybackService extends MediaBrowserServiceCompat{
if (playlistManager.hasCurrentMedia()) play(); if (playlistManager.hasCurrentMedia()) play();
else loadLastAudioPlaylist(); else loadLastAudioPlaylist();
} else if (Constants.ACTION_PLAY_FROM_SEARCH.equals(action)) { } else if (Constants.ACTION_PLAY_FROM_SEARCH.equals(action)) {
if (mMediaSession == null) if (mMediaSession == null) initMediaSession();
initMediaSession();
final Bundle extras = intent.getBundleExtra(Constants.EXTRA_SEARCH_BUNDLE); final Bundle extras = intent.getBundleExtra(Constants.EXTRA_SEARCH_BUNDLE);
mMediaSession.getController().getTransportControls() mMediaSession.getController().getTransportControls()
.playFromSearch(extras.getString(SearchManager.QUERY), extras); .playFromSearch(extras.getString(SearchManager.QUERY), extras);
...@@ -1194,13 +1194,40 @@ public class PlaybackService extends MediaBrowserServiceCompat{ ...@@ -1194,13 +1194,40 @@ public class PlaybackService extends MediaBrowserServiceCompat{
private void loadLastAudioPlaylist() { private void loadLastAudioPlaylist() {
if (AndroidDevices.isAndroidTv) return; if (AndroidDevices.isAndroidTv) return;
if (mMedialibrary.isInitiated() && mLibraryReceiver == null) playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO); if (mMedialibrary.isInitiated() && mLibraryReceiver == null) {
else registerMedialibrary(new Runnable() { if (!playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO)) stopSelf();
@Override } else registerMedialibrary(new Runnable() {
public void run() { @Override
playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO); public void run() {
} if (!playlistManager.loadLastPlaylist(Constants.PLAYLIST_TYPE_AUDIO)) stopSelf();
}); }
});
}
private void forceForeground() {
final Context ctx = PlaybackService.this;
final MediaSessionCompat.Token sessionToken = mMediaSession.getSessionToken();
final Notification notification = NotificationHelper.createPlaybackNotification(ctx,
false, ctx.getResources().getString(R.string.loading), "", "",
null, false, sessionToken, getSessionPendingIntent());
PlaybackService.this.startForeground(3, notification);
mIsForeground = true;
ExecutorHolder.executorService.execute(new Runnable() {
@Override
public void run() {
final Bitmap cover = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_no_media);
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
final Notification notification = NotificationHelper.createPlaybackNotification(ctx,
false, ctx.getResources().getString(R.string.loading), "", "",
cover, false, sessionToken, getSessionPendingIntent());
PlaybackService.this.startForeground(3, notification);
mIsForeground = true;
}
});
}
});
} }
public void loadLastPlaylist(int type) { public void loadLastPlaylist(int type) {
......
...@@ -26,13 +26,12 @@ import org.videolan.vlc.gui.video.VideoPlayerActivity ...@@ -26,13 +26,12 @@ import org.videolan.vlc.gui.video.VideoPlayerActivity
import org.videolan.vlc.util.* import org.videolan.vlc.util.*
import java.util.* import java.util.*
private const val TAG = "VLC/PlaylistManager"
private const val PREVIOUS_LIMIT_DELAY = 5000L
private const val AUDIO_REPEAT_MODE_KEY = "audio_repeat_mode"
class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventListener, Media.EventListener { class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventListener, Media.EventListener {
private val TAG = "VLC/PlaylistManager"
private val PREVIOUS_LIMIT_DELAY = 5000L
private val AUDIO_REPEAT_MODE_KEY = "audio_repeat_mode"
private val medialibrary by lazy(LazyThreadSafetyMode.NONE) { Medialibrary.getInstance() } private val medialibrary by lazy(LazyThreadSafetyMode.NONE) { Medialibrary.getInstance() }
val player by lazy(LazyThreadSafetyMode.NONE) { PlayerController() } val player by lazy(LazyThreadSafetyMode.NONE) { PlayerController() }
private val settings by lazy(LazyThreadSafetyMode.NONE) { PreferenceManager.getDefaultSharedPreferences(service) } private val settings by lazy(LazyThreadSafetyMode.NONE) { PreferenceManager.getDefaultSharedPreferences(service) }
...@@ -83,11 +82,11 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList ...@@ -83,11 +82,11 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
var mediaWrapper = medialibrary.getMedia(location) var mediaWrapper = medialibrary.getMedia(location)
if (mediaWrapper === null) { if (mediaWrapper === null) {
if (!location.validateLocation()) { if (!location.validateLocation()) {
Log.w(TAG, "Invalid location " + location) Log.w(TAG, "Invalid location $location")
service.showToast(service.resources.getString(R.string.invalid_location, location), Toast.LENGTH_SHORT) service.showToast(service.resources.getString(R.string.invalid_location, location), Toast.LENGTH_SHORT)
continue continue
} }
Log.v(TAG, "Creating on-the-fly Media object for " + location) Log.v(TAG, "Creating on-the-fly Media object for $location")
mediaWrapper = MediaWrapper(Uri.parse(location)) mediaWrapper = MediaWrapper(Uri.parse(location))
} }
mediaList.add(mediaWrapper) mediaList.add(mediaWrapper)
...@@ -114,15 +113,15 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList ...@@ -114,15 +113,15 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
@Volatile @Volatile
private var loadingLastPlaylist = false private var loadingLastPlaylist = false
fun loadLastPlaylist(type: Int) { fun loadLastPlaylist(type: Int) : Boolean {
if (loadingLastPlaylist) return if (loadingLastPlaylist) return false
loadingLastPlaylist = true loadingLastPlaylist = true
val audio = type == Constants.PLAYLIST_TYPE_AUDIO
val currentMedia = settings.getString(if (audio) "current_song" else "current_media", "")
if ("" == currentMedia) return false
val locations = settings.getString(if (audio) "audio_list" else "media_list", "").split(" ".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
if (Util.isArrayEmpty(locations)) return false
launch(UI, CoroutineStart.UNDISPATCHED) { launch(UI, CoroutineStart.UNDISPATCHED) {
val audio = type == Constants.PLAYLIST_TYPE_AUDIO
val currentMedia = settings.getString(if (audio) "current_song" else "current_media", "")
if ("" == currentMedia) return@launch
val locations = settings.getString(if (audio) "audio_list" else "media_list", "").split(" ".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
if (Util.isArrayEmpty(locations)) return@launch
val playList = async { val playList = async {
locations.map { Uri.decode(it) }.mapTo(ArrayList(locations.size)) { MediaWrapper(Uri.parse(it)) } locations.map { Uri.decode(it) }.mapTo(ArrayList(locations.size)) { MediaWrapper(Uri.parse(it)) }
}.await() }.await()
...@@ -141,6 +140,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList ...@@ -141,6 +140,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
load(playList, position) load(playList, position)
loadingLastPlaylist = false loadingLastPlaylist = false
} }
return true
} }
private fun onPlaylistLoaded() { private fun onPlaylistLoaded() {
...@@ -274,12 +274,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList ...@@ -274,12 +274,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
var id = mw.id var id = mw.id
if (id == 0L) { if (id == 0L) {
var internalMedia = medialibrary.findMedia(mw) var internalMedia = medialibrary.findMedia(mw)
if (internalMedia != null && internalMedia.id != 0L) if (internalMedia != null && internalMedia.id != 0L) id = internalMedia.id
id = internalMedia.id
else { else {
internalMedia = medialibrary.addMedia(Uri.decode(mw.uri.toString())) internalMedia = medialibrary.addMedia(Uri.decode(mw.uri.toString()))
if (internalMedia != null) if (internalMedia != null) id = internalMedia.id
id = internalMedia.id
} }
} }
medialibrary.increasePlayCount(id) medialibrary.increasePlayCount(id)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment