WIP for android auto

This commit is contained in:
hemanthStack 2019-09-18 18:53:04 +05:30
parent 3858d65a57
commit 103cbf2270
4 changed files with 69 additions and 14 deletions

View file

@ -131,8 +131,8 @@ class MainActivity : AbsSlidingMusicPanelActivity(), SharedPreferences.OnSharedP
val uri = intent.data
val mimeType = intent.type
var handled = false
if (intent.action != null && intent.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH) {
println("uri -> $uri")
/*if (intent.action != null && intent.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH) {
val songs = SearchQueryHelper.getSongs(this, intent.extras!!)
if (MusicPlayerRemote.shuffleMode == MusicService.SHUFFLE_MODE_SHUFFLE) {
MusicPlayerRemote.openAndShuffleQueue(songs, true)
@ -140,7 +140,7 @@ class MainActivity : AbsSlidingMusicPanelActivity(), SharedPreferences.OnSharedP
MusicPlayerRemote.openQueue(songs, 0, true)
}
handled = true
}
}*/
if (uri != null && uri.toString().isNotEmpty()) {
MusicPlayerRemote.playFromUri(uri)

View file

@ -0,0 +1,15 @@
package code.name.monkey.retromusic.auto;
import android.app.UiModeManager;
import android.content.Context;
import android.content.res.Configuration;
public class CarHelper {
private static final String TAG = "CarHelper";
public static boolean isCarUiMode(Context c) {
UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR;
}
}

View file

@ -17,7 +17,9 @@ package code.name.monkey.retromusic.service
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.media.session.MediaSessionCompat
import android.text.TextUtils
import code.name.monkey.retromusic.auto.AutoMediaIDHelper
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.helper.MusicPlayerRemote.cycleRepeatMode
@ -35,7 +37,8 @@ import java.util.*
*/
class MediaSessionCallback(private val context: Context,
private val musicService: MusicService) : MediaSessionCompat.Callback() {
private val musicService: MusicService
) : MediaSessionCompat.Callback() {
override fun onPlay() {
super.onPlay()
@ -101,12 +104,10 @@ class MediaSessionCallback(private val context: Context,
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_TOP_TRACKS,
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_QUEUE -> {
val tracks: List<Song>
if (category.equals(AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY)) {
tracks = TopAndRecentlyPlayedTracksLoader.getRecentlyPlayedTracks(context)
} else if (category.equals(AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_TOP_TRACKS)) {
tracks = TopAndRecentlyPlayedTracksLoader.getTopTracks(context)
} else {
tracks = MusicPlaybackQueueStore.getInstance(context).savedOriginalPlayingQueue
tracks = when (category) {
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY -> TopAndRecentlyPlayedTracksLoader.getRecentlyPlayedTracks(context)
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_TOP_TRACKS -> TopAndRecentlyPlayedTracksLoader.getTopTracks(context)
else -> MusicPlaybackQueueStore.getInstance(context).savedOriginalPlayingQueue
}
songs.addAll(tracks)
var songIndex = MusicUtil.indexOfSongInList(tracks, itemId)
@ -124,6 +125,42 @@ class MediaSessionCallback(private val context: Context,
}
override fun onPlayFromSearch(query: String?, extras: Bundle?) {
print("query: $query -> extras: ${extras.toString()}")
if (TextUtils.isEmpty(query)) {
if (MusicPlayerRemote.playingQueue.isEmpty()) {
openQueue(SongLoader.getAllSongs(context), 0, true);
} else {
MusicPlayerRemote.resumePlaying()
}
} else {
handlePlayFromSearch(query, extras)
}
}
private fun handlePlayFromSearch(query: String?, extras: Bundle?) {
val mediaFocus: String? = extras?.getString(MediaStore.EXTRA_MEDIA_FOCUS)
println(mediaFocus)
when (mediaFocus) {
MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE -> {
extras.getString(MediaStore.EXTRA_MEDIA_ARTIST)?.let { artist ->
println("Artist name $artist")
}
}
MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE -> {
extras.getString(MediaStore.EXTRA_MEDIA_ALBUM)?.let { album ->
println("Artist name $album")
}
}
MediaStore.Audio.Genres.ENTRY_CONTENT_TYPE -> {
extras.getString(MediaStore.EXTRA_MEDIA_GENRE)?.let { genre ->
println("Artist name $genre")
}
}
}
}
override fun onCustomAction(action: String, extras: Bundle?) {
when (action) {
CYCLE_REPEAT -> {

View file

@ -68,6 +68,7 @@ import code.name.monkey.retromusic.appwidgets.AppWidgetSmall;
import code.name.monkey.retromusic.appwidgets.AppWidgetText;
import code.name.monkey.retromusic.auto.AutoMediaIDHelper;
import code.name.monkey.retromusic.auto.AutoMusicProvider;
import code.name.monkey.retromusic.auto.CarHelper;
import code.name.monkey.retromusic.glide.BlurTransformation;
import code.name.monkey.retromusic.glide.SongGlideRequest;
import code.name.monkey.retromusic.helper.ShuffleHelper;
@ -740,13 +741,15 @@ public class MusicService extends MediaBrowserServiceCompat implements
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.getDuration())
.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition() + 1)
.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, song.getYear())
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null);
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null)
.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
if (CarHelper.isCarUiMode(this)) {
mediaSession.setMetadata(metaData.build());
}
if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
if (PreferenceUtil.getInstance(this).albumArtOnLockscreen() || CarHelper.isCarUiMode(this)) {
final Point screenSize = RetroUtil.getScreenSize(MusicService.this);
final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
.checkIgnoreMediaStore(MusicService.this)