Skip to content

Commit

Permalink
implement Android Auto voice search
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Masdorp committed May 4, 2023
1 parent 2131328 commit 3d17ffe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
minSdkVersion 26
//noinspection OldTargetApi
targetSdkVersion 33
versionCode 190
versionName "2.1.0"
versionCode 192
versionName "2.2.1"
}
buildTypes {
release {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@
<activity
android:name=".ui.NederadioActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ class AndroidStreamManager(
if (currentStreamIndex != -1) {
val currentStream = streams[currentStreamIndex]
val currentStreamInUse = controller.currentMediaItem
if (controller.mediaItemCount == 0) {
if (currentStreamInUse?.mediaId != currentStream.mediaId) {
controller.setMediaItems(streams, currentStreamIndex, 0L)
} else if (currentStreamInUse?.mediaId != currentStream.mediaId) {
controller.seekTo(currentStreamIndex, 0L)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.nielsmasdorp.nederadio.R
import com.nielsmasdorp.nederadio.domain.settings.GetLastPlayedId
import com.nielsmasdorp.nederadio.domain.stream.SetActiveStream
import com.nielsmasdorp.nederadio.playback.library.StreamLibrary
import com.nielsmasdorp.nederadio.playback.library.StreamLibrary.Companion.STATIONS_ITEM_ID
import com.nielsmasdorp.nederadio.ui.NederadioActivity
import com.nielsmasdorp.nederadio.util.connectedDeviceName
import com.nielsmasdorp.nederadio.util.moveToFront
Expand Down Expand Up @@ -107,6 +106,7 @@ class StreamService :
EXTRAS_KEY_CONTENT_STYLE_PLAYABLE,
EXTRAS_VALUE_CONTENT_STYLE_CATEGORY_GRID_ITEM
)
putBoolean(MEDIA_SEARCH_SUPPORTED, true)
}
val newParams = LibraryParams.Builder()
.setExtras(extras)
Expand Down Expand Up @@ -295,17 +295,29 @@ class StreamService :
val streams = streamLibrary
.browsableContent
.first()
.getChildren(nodeId = STATIONS_ITEM_ID)
if (mediaItems.size == 1) { // User has selected an item in Android Auto
val item = mediaItems[0]
// Replace single items by all items with selected item as first item
streams
.toMutableList()
.apply { moveToFront { it.mediaId == item.mediaId } }
if (mediaItems.size == 1) {
val singleItem = mediaItems[0]
if (singleItem.mediaId.isBlank() &&
singleItem.requestMetadata.searchQuery != null
) {
// User has preformed a voice search in Android Auto
streams.search(query = singleItem.requestMetadata.searchQuery)
.toMutableList()
} else {
// User has selected an item in Android Auto
// It is expected behavior, but might get solved in the future
// That the rest of the queue is removed by Android Auto
// So we have to recreate the queue with the selected item in front
streams
.getAllPlayableItems()
.toMutableList()
.apply { moveToFront { it.mediaId == singleItem.mediaId } }
}
} else {
// Just use the [MediaItem] from the content library since the URI exists there
val allContent = streams.getAllPlayableItems()
mediaItems.map { mediaItem ->
streams.find { it.mediaId == mediaItem.mediaId }!!
allContent.find { it.mediaId == mediaItem.mediaId }!!
}.toMutableList()
}
}
Expand Down Expand Up @@ -417,6 +429,8 @@ class StreamService :

companion object {

const val MEDIA_SEARCH_SUPPORTED = "android.media.browse.SEARCH_SUPPORTED"

const val START_TIMER_COMMAND = "start_timer"
const val START_TIMER_COMMAND_VALUE_KEY = "start_timer_value"
const val TIMER_UPDATED_COMMAND = "timer_updated"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Tree(val rootNode: MediaItemNode) {
}
}

/**
* Return all playable items
*/
fun getAllPlayableItems() = getChildren(nodeId = STATIONS_ITEM_ID)

/**
* Return a single playable item for a given id
*/
Expand Down

0 comments on commit 3d17ffe

Please sign in to comment.