|
| 1 | +package org.stypox.dicio.skills.music |
| 2 | + |
| 3 | +import android.content.pm.PackageManager |
| 4 | +import android.util.Log |
| 5 | +import androidx.compose.foundation.Image |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.Spacer |
| 9 | +import androidx.compose.foundation.layout.aspectRatio |
| 10 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 11 | +import androidx.compose.foundation.layout.width |
| 12 | +import androidx.compose.material3.MaterialTheme |
| 13 | +import androidx.compose.material3.Text |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.remember |
| 16 | +import androidx.compose.ui.Alignment |
| 17 | +import androidx.compose.ui.Modifier |
| 18 | +import androidx.compose.ui.platform.LocalContext |
| 19 | +import androidx.compose.ui.text.style.TextAlign |
| 20 | +import androidx.compose.ui.unit.dp |
| 21 | +import com.google.accompanist.drawablepainter.rememberDrawablePainter |
| 22 | +import org.dicio.skill.context.SkillContext |
| 23 | +import org.dicio.skill.skill.SkillOutput |
| 24 | +import org.stypox.dicio.R |
| 25 | +import org.stypox.dicio.io.graphical.Headline |
| 26 | +import org.stypox.dicio.util.getString |
| 27 | + |
| 28 | +private val TAG = MusicOutput::class.simpleName |
| 29 | + |
| 30 | +class MusicOutput( |
| 31 | + private val appName: String?, |
| 32 | + private val packageName: String?, |
| 33 | +) : SkillOutput { |
| 34 | + override fun getSpeechOutput(ctx: SkillContext): String = if (packageName == null) { |
| 35 | + ctx.getString(R.string.skill_music_no_app_found) |
| 36 | + } else { |
| 37 | + ctx.getString(R.string.skill_open_opening, appName) |
| 38 | + } |
| 39 | + |
| 40 | + @Composable |
| 41 | + override fun GraphicalOutput(ctx: SkillContext) { |
| 42 | + if (appName == null || packageName == null) { |
| 43 | + Headline(text = getSpeechOutput(ctx)) |
| 44 | + |
| 45 | + } else { |
| 46 | + Row( |
| 47 | + modifier = Modifier.fillMaxWidth(), |
| 48 | + verticalAlignment = Alignment.CenterVertically, |
| 49 | + ) { |
| 50 | + val context = LocalContext.current |
| 51 | + val icon = remember { |
| 52 | + try { |
| 53 | + context.packageManager.getApplicationIcon(packageName) |
| 54 | + } catch (e: PackageManager.NameNotFoundException) { |
| 55 | + Log.e(TAG, "Could not load icon for $packageName", e) |
| 56 | + null |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (icon != null) { |
| 61 | + Image( |
| 62 | + painter = rememberDrawablePainter(icon), |
| 63 | + contentDescription = appName, |
| 64 | + modifier = Modifier |
| 65 | + .fillMaxWidth(0.2f) |
| 66 | + .aspectRatio(1.0f), |
| 67 | + ) |
| 68 | + |
| 69 | + Spacer(modifier = Modifier.width(8.dp)) |
| 70 | + } |
| 71 | + |
| 72 | + Column( |
| 73 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 74 | + modifier = Modifier.fillMaxWidth(), |
| 75 | + ) { |
| 76 | + Text( |
| 77 | + text = getSpeechOutput(ctx), |
| 78 | + style = MaterialTheme.typography.headlineMedium, |
| 79 | + textAlign = TextAlign.Center, |
| 80 | + modifier = Modifier.fillMaxWidth(), |
| 81 | + ) |
| 82 | + |
| 83 | + Text( |
| 84 | + text = packageName, |
| 85 | + textAlign = TextAlign.Center, |
| 86 | + modifier = Modifier.fillMaxWidth(), |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments