-
-
Notifications
You must be signed in to change notification settings - Fork 136
add music skill for playing songs #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gkuznik
wants to merge
3
commits into
Stypox:master
Choose a base branch
from
gkuznik:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/main/kotlin/org/stypox/dicio/skills/music/MusicInfo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.stypox.dicio.skills.music | ||
|
|
||
| import android.content.Context | ||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.automirrored.filled.Notes | ||
| import androidx.compose.material.icons.automirrored.filled.OpenInNew | ||
| import androidx.compose.material.icons.automirrored.filled.PlaylistPlay | ||
| import androidx.compose.material.icons.automirrored.filled.QueueMusic | ||
| import androidx.compose.material.icons.filled.Directions | ||
| import androidx.compose.material.icons.filled.OpenInNew | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
| import androidx.fragment.app.Fragment | ||
| import org.dicio.skill.skill.Skill | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.SkillInfo | ||
| import org.stypox.dicio.R | ||
| import org.stypox.dicio.sentences.Sentences | ||
| import org.stypox.dicio.skills.open.OpenSkill | ||
|
|
||
| object MusicInfo : SkillInfo("music") { | ||
| override fun name(context: Context) = | ||
| context.getString(R.string.skill_name_music) | ||
|
|
||
| override fun sentenceExample(context: Context) = | ||
| context.getString(R.string.skill_sentence_example_music) | ||
|
|
||
| @Composable | ||
| override fun icon() = | ||
| rememberVectorPainter(Icons.AutoMirrored.Filled.QueueMusic) | ||
|
|
||
| override fun isAvailable(ctx: SkillContext): Boolean { | ||
| return Sentences.Music[ctx.sentencesLanguage] != null | ||
| } | ||
|
|
||
| override fun build(ctx: SkillContext): Skill<*> { | ||
| return MusicSkill(MusicInfo, Sentences.Music[ctx.sentencesLanguage]!!) | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
app/src/main/kotlin/org/stypox/dicio/skills/music/MusicOutput.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package org.stypox.dicio.skills.music | ||
|
|
||
| import android.content.pm.PackageManager | ||
| import android.util.Log | ||
| import androidx.compose.foundation.Image | ||
| import androidx.compose.foundation.layout.BoxWithConstraints | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.aspectRatio | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.requiredWidth | ||
| import androidx.compose.foundation.layout.width | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.text.style.TextAlign | ||
| import androidx.compose.ui.unit.dp | ||
| import com.google.accompanist.drawablepainter.rememberDrawablePainter | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.stypox.dicio.R | ||
| import org.stypox.dicio.io.graphical.Headline | ||
| import org.stypox.dicio.util.getString | ||
|
|
||
| private val TAG = MusicOutput::class.simpleName | ||
|
|
||
| class MusicOutput( | ||
| private val appName: String?, | ||
| private val packageName: String?, | ||
| private val songName: String?, | ||
| ) : SkillOutput { | ||
| override fun getSpeechOutput(ctx: SkillContext): String = if (packageName == null) { | ||
| ctx.getString(R.string.skill_music_no_app_found) | ||
| } else { | ||
| ctx.getString(R.string.skill_music_playing, songName, appName) | ||
| } | ||
|
|
||
| @Composable | ||
| override fun GraphicalOutput(ctx: SkillContext) { | ||
| if (appName == null || packageName == null) { | ||
| Headline(text = getSpeechOutput(ctx)) | ||
|
|
||
| } else { | ||
| Row( | ||
| modifier = Modifier.fillMaxWidth(), | ||
| verticalAlignment = Alignment.CenterVertically, | ||
| ) { | ||
| val context = LocalContext.current | ||
| val icon = remember { | ||
| try { | ||
| context.packageManager.getApplicationIcon(packageName) | ||
| } catch (e: PackageManager.NameNotFoundException) { | ||
| Log.e(TAG, "Could not load icon for $packageName", e) | ||
| null | ||
| } | ||
| } | ||
|
|
||
| if (icon != null) { | ||
| BoxWithConstraints { | ||
| Image( | ||
| painter = rememberDrawablePainter(icon), | ||
| contentDescription = appName, | ||
| modifier = Modifier | ||
| .requiredWidth(minOf(maxWidth * 0.2f, 80.dp)) | ||
| .aspectRatio(1.0f), | ||
| ) | ||
| } | ||
|
|
||
| Spacer(modifier = Modifier.width(8.dp)) | ||
| } | ||
|
|
||
| Column( | ||
| horizontalAlignment = Alignment.CenterHorizontally, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| ) { | ||
| Text( | ||
| text = getSpeechOutput(ctx), | ||
| style = MaterialTheme.typography.headlineMedium, | ||
| textAlign = TextAlign.Center, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| ) | ||
|
|
||
| Text( | ||
| text = packageName, | ||
| textAlign = TextAlign.Center, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
app/src/main/kotlin/org/stypox/dicio/skills/music/MusicSkill.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package org.stypox.dicio.skills.music | ||
|
|
||
| import android.app.SearchManager | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import android.provider.MediaStore | ||
| import org.dicio.skill.context.SkillContext | ||
| import org.dicio.skill.skill.SkillInfo | ||
| import org.dicio.skill.skill.SkillOutput | ||
| import org.dicio.skill.standard.StandardRecognizerData | ||
| import org.dicio.skill.standard.StandardRecognizerSkill | ||
| import org.stypox.dicio.sentences.Sentences.Music | ||
|
|
||
| class MusicSkill(correspondingSkillInfo: SkillInfo, data: StandardRecognizerData<Music>) : | ||
| StandardRecognizerSkill<Music>(correspondingSkillInfo, data) { | ||
|
|
||
| override suspend fun generateOutput(ctx: SkillContext, inputData: Music): SkillOutput { | ||
| val (song, artist) = when (inputData) { | ||
| is Music.Query -> Pair(inputData.song, inputData.artist) | ||
| } | ||
|
|
||
| // https://developer.android.com/guide/components/intents-common#PlaySearch | ||
| val intent = Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH).apply { | ||
| putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Media.ENTRY_CONTENT_TYPE) | ||
| putExtra(MediaStore.EXTRA_MEDIA_TITLE, song) | ||
|
|
||
| if (artist != null) { | ||
| putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist) | ||
| putExtra(SearchManager.QUERY, "$artist $song") | ||
| } else { | ||
| putExtra(SearchManager.QUERY, song) | ||
| } | ||
| } | ||
| intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||
|
|
||
| val packageManager: PackageManager = ctx.android.packageManager | ||
| val componentName = intent.resolveActivity(packageManager) | ||
| if (componentName == null) { | ||
| return MusicOutput(appName = null, packageName = null, songName = null) | ||
| } | ||
| ctx.android.startActivity(intent) | ||
|
|
||
| val applicationInfo = packageManager.getApplicationInfo(componentName.packageName, 0) | ||
| return MusicOutput( | ||
| appName = applicationInfo.loadLabel(packageManager).toString(), | ||
| packageName = applicationInfo.packageName, | ||
| songName = song | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| query: | ||
| - play .song. (by .artist.)? | ||
gkuznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - start playing .song. (by .artist.)? | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.