Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
[Android Auto] Fixed a bug that crashed Android Auto if the app wasn't
running
  • Loading branch information
Carlos Pérez committed Jul 12, 2022
1 parent 4ac56f5 commit 81e7c45
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ public static void UnregisterObserver(IBroadcastObserver observer) {

public static void NotifyObservers(String action, String value) {
if (Objects.equals(action, "EX")) {
Log.e("Soniclair Exception", value);
Log.e("Soniclair Exception", value != null ? value : "Unknown error");
}
for (IBroadcastObserver observer : getInstance().observers) {
try {
observer.update(action, value);
} catch (Exception e) {
Log.e("SonicLair Globals", e.getMessage());
String message = e.getMessage();
if(message == null || message.isEmpty())
{
message = "Unknown error";
}
Log.e("SonicLair Globals", message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package tech.logica10.soniclair

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.support.v4.media.session.MediaSessionCompat
import android.util.Log
import android.view.KeyEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import tech.logica10.soniclair.services.MusicService

class SonicLairSessionCallbacks : MediaSessionCompat.Callback() {
Expand All @@ -31,6 +35,12 @@ class SonicLairSessionCallbacks : MediaSessionCompat.Callback() {
}
}

init {
// Bind to LocalService
val intent = Intent(App.context, MusicService::class.java)
App.context.bindService(intent, connection, Context.BIND_AUTO_CREATE)
}

override fun onPlay() {
super.onPlay()
if (mBound) {
Expand Down Expand Up @@ -86,40 +96,43 @@ class SonicLairSessionCallbacks : MediaSessionCompat.Callback() {
override fun onPlayFromMediaId(mediaId: String, extras: Bundle) {

val id = mediaId.subSequence(1, mediaId.length).toString()
when (mediaId.subSequence(0, 1)) {
"s" -> {
if (mBound) {
binder!!.playRadio(id)
} else {
val intent = Intent(App.context, MusicService::class.java)
intent.action = Constants.SERVICE_PLAY_RADIO
intent.putExtra("id", id)
App.context.startService(intent)
CoroutineScope(Dispatchers.IO).launch{
when (mediaId.subSequence(0, 1)) {
"s" -> {
if (mBound) {
binder!!.playRadio(id)
} else {
val intent = Intent(App.context, MusicService::class.java)
intent.action = Constants.SERVICE_PLAY_RADIO
intent.putExtra("id", id)
App.context.startService(intent)
}
}
}
"a" -> {
if (mBound) {
binder!!.playAlbum(id, 0)
} else {
val intent = Intent(App.context, MusicService::class.java)
intent.action = Constants.SERVICE_PLAY_ALBUM
intent.putExtra("id", id)
intent.putExtra("track", 0)
App.context.startService(intent)
"a" -> {
if (mBound) {
binder!!.playAlbum(id, 0)
} else {
val intent = Intent(App.context, MusicService::class.java)
intent.action = Constants.SERVICE_PLAY_ALBUM
intent.putExtra("id", id)
intent.putExtra("track", 0)
App.context.startService(intent)
}
}
}
"p" -> {
if (mBound) {
binder!!.playPlaylist(id, 0)
} else{
val intent = Intent(App.context, MusicService::class.java)
intent.action = Constants.SERVICE_PLAY_PLAYLIST
intent.putExtra("id", id)
intent.putExtra("track", 0)
App.context.startService(intent)
"p" -> {
if (mBound) {
binder!!.playPlaylist(id, 0)
} else{
val intent = Intent(App.context, MusicService::class.java)
intent.action = Constants.SERVICE_PLAY_PLAYLIST
intent.putExtra("id", id)
intent.putExtra("track", 0)
App.context.startService(intent)
}
}
}
}

}

override fun onPlayFromSearch(query: String?, extras: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tech.logica10.soniclair.services

import android.app.PendingIntent
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
Expand All @@ -25,6 +26,7 @@ class MediaBrowserService : MediaBrowserServiceCompat() {
private val subsonicClient: SubsonicClient = SubsonicClient(getActiveAccount())



override fun onCreate() {
super.onCreate()
// Set the session's token so that client activities can communicate with it.
Expand Down Expand Up @@ -145,7 +147,7 @@ class MediaBrowserService : MediaBrowserServiceCompat() {
// We have nothing for the user at this time, returning an empty list so as to not block the UI.
result.sendResult(listOf())
}
}else if (parentMediaId == "MOSTPLAYED") {
} else if (parentMediaId == "MOSTPLAYED") {
// We're not logged in on a server, we bail
if (getActiveAccount().username == null) {
result.sendResult(listOf())
Expand Down Expand Up @@ -195,7 +197,12 @@ class MediaBrowserService : MediaBrowserServiceCompat() {
val albums = subsonicClient.getTopAlbums()
KeyValueStorage.setCachedAlbums(albums)
val playlists = subsonicClient.getPlaylists()
KeyValueStorage.setCachedPlaylists(playlists.subList(0, if(playlists.size > 9) 9 else playlists.size))
KeyValueStorage.setCachedPlaylists(
playlists.subList(
0,
if (playlists.size > 9) 9 else playlists.size
)
)
} catch (e: Exception) {
// Something _awful_ happened. The user doesn't need to know about it
Log.e("MediaBrowser", e.message!!)
Expand Down

1 comment on commit 81e7c45

@vercel
Copy link

@vercel vercel bot commented on 81e7c45 Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

soniclair – ./

soniclair-thelinkin3000.vercel.app
soniclair-git-main-thelinkin3000.vercel.app
soniclair.vercel.app

Please sign in to comment.