|
| 1 | +package com.osserva.app |
| 2 | + |
| 3 | +import android.appwidget.AppWidgetManager |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.content.SharedPreferences |
| 7 | +import android.graphics.BitmapFactory |
| 8 | +import android.media.AudioManager |
| 9 | +import android.net.Uri |
| 10 | +import android.os.SystemClock |
| 11 | +import android.view.KeyEvent |
| 12 | +import android.widget.RemoteViews |
| 13 | +import android.app.PendingIntent |
| 14 | +import es.antonborri.home_widget.HomeWidgetProvider |
| 15 | +import es.antonborri.home_widget.HomeWidgetLaunchIntent |
| 16 | + |
| 17 | +class MusicPlayerWidget : HomeWidgetProvider() { |
| 18 | + |
| 19 | + companion object { |
| 20 | + private const val ACTION_PLAY_PAUSE = "com.osserva.app.WIDGET_PLAY_PAUSE" |
| 21 | + private const val ACTION_NEXT = "com.osserva.app.WIDGET_NEXT" |
| 22 | + private const val ACTION_PREV = "com.osserva.app.WIDGET_PREV" |
| 23 | + } |
| 24 | + |
| 25 | + override fun onUpdate( |
| 26 | + context: Context, |
| 27 | + appWidgetManager: AppWidgetManager, |
| 28 | + appWidgetIds: IntArray, |
| 29 | + widgetData: SharedPreferences |
| 30 | + ) { |
| 31 | + for (widgetId in appWidgetIds) { |
| 32 | + updateWidget(context, appWidgetManager, widgetId, widgetData) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + override fun onReceive(context: Context, intent: Intent) { |
| 37 | + super.onReceive(context, intent) |
| 38 | + when (intent.action) { |
| 39 | + ACTION_PLAY_PAUSE -> sendMediaButton(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) |
| 40 | + ACTION_NEXT -> sendMediaButton(context, KeyEvent.KEYCODE_MEDIA_NEXT) |
| 41 | + ACTION_PREV -> sendMediaButton(context, KeyEvent.KEYCODE_MEDIA_PREVIOUS) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private fun sendMediaButton(context: Context, keyCode: Int) { |
| 46 | + val eventTime = SystemClock.uptimeMillis() |
| 47 | + |
| 48 | + // ACTION_DOWN |
| 49 | + val downIntent = Intent(Intent.ACTION_MEDIA_BUTTON) |
| 50 | + downIntent.setClassName(context.packageName, "com.ryanheise.audioservice.MediaButtonReceiver") |
| 51 | + downIntent.putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0)) |
| 52 | + context.sendBroadcast(downIntent) |
| 53 | + |
| 54 | + // ACTION_UP |
| 55 | + val upIntent = Intent(Intent.ACTION_MEDIA_BUTTON) |
| 56 | + upIntent.setClassName(context.packageName, "com.ryanheise.audioservice.MediaButtonReceiver") |
| 57 | + upIntent.putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0)) |
| 58 | + context.sendBroadcast(upIntent) |
| 59 | + } |
| 60 | + |
| 61 | + private fun updateWidget( |
| 62 | + context: Context, |
| 63 | + appWidgetManager: AppWidgetManager, |
| 64 | + widgetId: Int, |
| 65 | + widgetData: SharedPreferences |
| 66 | + ) { |
| 67 | + val title = widgetData.getString("song_title", "") ?: "" |
| 68 | + val artist = widgetData.getString("artist", "") ?: "" |
| 69 | + val isPlaying = widgetData.getBoolean("is_playing", false) |
| 70 | + val isShuffle = widgetData.getBoolean("is_shuffle", false) |
| 71 | + val artPath = widgetData.getString("art_path", "") ?: "" |
| 72 | + |
| 73 | + val views = RemoteViews(context.packageName, R.layout.music_widget) |
| 74 | + |
| 75 | + // Text |
| 76 | + views.setTextViewText(R.id.widget_song_title, if (title.isEmpty()) "Not Playing" else title) |
| 77 | + views.setTextViewText(R.id.widget_artist, artist) |
| 78 | + |
| 79 | + // Album art |
| 80 | + if (artPath.isNotEmpty()) { |
| 81 | + val bitmap = BitmapFactory.decodeFile(artPath) |
| 82 | + if (bitmap != null) { |
| 83 | + views.setImageViewBitmap(R.id.widget_album_art, bitmap) |
| 84 | + } else { |
| 85 | + views.setImageViewResource(R.id.widget_album_art, R.drawable.ic_default_art) |
| 86 | + } |
| 87 | + } else { |
| 88 | + views.setImageViewResource(R.id.widget_album_art, R.drawable.ic_default_art) |
| 89 | + } |
| 90 | + |
| 91 | + // Play/Pause icon |
| 92 | + val playPauseIcon = if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play |
| 93 | + views.setImageViewResource(R.id.widget_btn_play_pause, playPauseIcon) |
| 94 | + |
| 95 | + // Shuffle tint (active = accent color, inactive = white with opacity) |
| 96 | + val shuffleTint = if (isShuffle) 0xFF1DB954.toInt() else 0x99FFFFFF.toInt() |
| 97 | + views.setInt(R.id.widget_btn_shuffle, "setColorFilter", shuffleTint) |
| 98 | + |
| 99 | + // Button intents |
| 100 | + views.setOnClickPendingIntent( |
| 101 | + R.id.widget_btn_play_pause, |
| 102 | + broadcastIntent(context, ACTION_PLAY_PAUSE, widgetId), |
| 103 | + ) |
| 104 | + views.setOnClickPendingIntent( |
| 105 | + R.id.widget_btn_next, |
| 106 | + broadcastIntent(context, ACTION_NEXT, widgetId), |
| 107 | + ) |
| 108 | + views.setOnClickPendingIntent( |
| 109 | + R.id.widget_btn_prev, |
| 110 | + broadcastIntent(context, ACTION_PREV, widgetId), |
| 111 | + ) |
| 112 | + // Shuffle -> launches Flutter via HomeWidget URI |
| 113 | + views.setOnClickPendingIntent( |
| 114 | + R.id.widget_btn_shuffle, |
| 115 | + shuffleLaunchIntent(context), |
| 116 | + ) |
| 117 | + |
| 118 | + appWidgetManager.updateAppWidget(widgetId, views) |
| 119 | + } |
| 120 | + |
| 121 | + private fun broadcastIntent(context: Context, action: String, widgetId: Int): PendingIntent { |
| 122 | + val intent = Intent(context, MusicPlayerWidget::class.java).apply { |
| 123 | + this.action = action |
| 124 | + } |
| 125 | + return PendingIntent.getBroadcast( |
| 126 | + context, |
| 127 | + widgetId, |
| 128 | + intent, |
| 129 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + private fun shuffleLaunchIntent(context: Context): PendingIntent { |
| 134 | + val intent = HomeWidgetLaunchIntent.getActivity( |
| 135 | + context, |
| 136 | + MainActivity::class.java, |
| 137 | + Uri.parse("audiography://widget/shuffle") |
| 138 | + ) |
| 139 | + return intent |
| 140 | + } |
| 141 | +} |
0 commit comments