mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-28 05:53:59 +02:00
Hide widget action buttons when app closes
This commit is contained in:
parent
bc6ef96465
commit
0158ccc4c8
3 changed files with 34 additions and 17 deletions
|
@ -10,6 +10,7 @@ import android.graphics.Bitmap
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.support.v4.media.session.PlaybackStateCompat
|
import android.support.v4.media.session.PlaybackStateCompat
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.view.View
|
||||||
import android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
import androidx.media.session.MediaButtonReceiver
|
import androidx.media.session.MediaButtonReceiver
|
||||||
import com.audiobookshelf.app.data.PlaybackSession
|
import com.audiobookshelf.app.data.PlaybackSession
|
||||||
|
@ -29,18 +30,17 @@ class MediaPlayerWidget : AppWidgetProvider() {
|
||||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||||
// There may be multiple widgets active, so update all of them
|
// There may be multiple widgets active, so update all of them
|
||||||
for (appWidgetId in appWidgetIds) {
|
for (appWidgetId in appWidgetIds) {
|
||||||
updateAppWidget(context, appWidgetManager, appWidgetId, null, false)
|
updateAppWidget(context, appWidgetManager, appWidgetId, null, false, PlayerNotificationService.isClosed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onEnabled(context: Context) {
|
override fun onEnabled(context: Context) {
|
||||||
Log.w(tag, "onEnabled check context ${context.packageName}")
|
Log.i(tag, "onEnabled check context ${context.packageName}")
|
||||||
|
|
||||||
// Enter relevant functionality for when the first widget is created
|
// Enter relevant functionality for when the first widget is created
|
||||||
DeviceManager.widgetUpdater = (object : WidgetEventEmitter {
|
DeviceManager.widgetUpdater = (object : WidgetEventEmitter {
|
||||||
override fun onPlayerChanged(pns: PlayerNotificationService) {
|
override fun onPlayerChanged(pns: PlayerNotificationService) {
|
||||||
val isPlaying = pns.currentPlayer.isPlaying
|
val isPlaying = pns.currentPlayer.isPlaying
|
||||||
Log.i(tag, "onPlayerChanged | Is Playing? $isPlaying")
|
|
||||||
|
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||||
val componentName = ComponentName(context, MediaPlayerWidget::class.java)
|
val componentName = ComponentName(context, MediaPlayerWidget::class.java)
|
||||||
|
@ -49,7 +49,7 @@ class MediaPlayerWidget : AppWidgetProvider() {
|
||||||
val playbackSession = pns.getCurrentPlaybackSessionCopy()
|
val playbackSession = pns.getCurrentPlaybackSessionCopy()
|
||||||
|
|
||||||
for (widgetId in ids) {
|
for (widgetId in ids) {
|
||||||
updateAppWidget(context, appWidgetManager, widgetId, playbackSession, isPlaying)
|
updateAppWidget(context, appWidgetManager, widgetId, playbackSession, isPlaying, PlayerNotificationService.isClosed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -60,10 +60,25 @@ class MediaPlayerWidget : AppWidgetProvider() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, playbackSession: PlaybackSession?, isPlaying:Boolean) {
|
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, playbackSession: PlaybackSession?, isPlaying:Boolean, isAppClosed:Boolean) {
|
||||||
val tag = "MediaPlayerWidget"
|
val tag = "MediaPlayerWidget"
|
||||||
val views = RemoteViews(context.packageName, R.layout.media_player_widget)
|
val views = RemoteViews(context.packageName, R.layout.media_player_widget)
|
||||||
|
|
||||||
|
val wholeWidgetClickI = Intent(context, MainActivity::class.java)
|
||||||
|
wholeWidgetClickI.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
|
val wholeWidgetClickPI = PendingIntent.getActivity(
|
||||||
|
context,
|
||||||
|
System.currentTimeMillis().toInt(),
|
||||||
|
wholeWidgetClickI,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
// todo: show grayed out icons?
|
||||||
|
val viewVisibility = if (isAppClosed) View.INVISIBLE else View.VISIBLE
|
||||||
|
views.setViewVisibility(R.id.widgetPlayPauseButton, viewVisibility)
|
||||||
|
views.setViewVisibility(R.id.widgetFastForwardButton, viewVisibility)
|
||||||
|
views.setViewVisibility(R.id.widgetRewindButton, viewVisibility)
|
||||||
|
|
||||||
val playPausePI = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY_PAUSE)
|
val playPausePI = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY_PAUSE)
|
||||||
views.setOnClickPendingIntent(R.id.widgetPlayPauseButton, playPausePI)
|
views.setOnClickPendingIntent(R.id.widgetPlayPauseButton, playPausePI)
|
||||||
|
|
||||||
|
@ -73,14 +88,7 @@ internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManage
|
||||||
val rewindPI = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_REWIND)
|
val rewindPI = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_REWIND)
|
||||||
views.setOnClickPendingIntent(R.id.widgetRewindButton, rewindPI)
|
views.setOnClickPendingIntent(R.id.widgetRewindButton, rewindPI)
|
||||||
|
|
||||||
val wholeWidgetClickI = Intent(context, MainActivity::class.java)
|
|
||||||
wholeWidgetClickI.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
|
||||||
val wholeWidgetClickPI = PendingIntent.getActivity(
|
|
||||||
context,
|
|
||||||
System.currentTimeMillis().toInt(),
|
|
||||||
wholeWidgetClickI,
|
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
|
||||||
)
|
|
||||||
views.setOnClickPendingIntent(R.id.widgetBackground, wholeWidgetClickPI)
|
views.setOnClickPendingIntent(R.id.widgetBackground, wholeWidgetClickPI)
|
||||||
|
|
||||||
val imageUri = playbackSession?.getCoverUri() ?: Uri.parse("android.resource://com.audiobookshelf.app/" + R.drawable.icon)
|
val imageUri = playbackSession?.getCoverUri() ?: Uri.parse("android.resource://com.audiobookshelf.app/" + R.drawable.icon)
|
||||||
|
@ -99,7 +107,8 @@ internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManage
|
||||||
val options = RequestOptions().override(300, 300).placeholder(R.drawable.icon).error(R.drawable.icon)
|
val options = RequestOptions().override(300, 300).placeholder(R.drawable.icon).error(R.drawable.icon)
|
||||||
Glide.with(context.applicationContext).asBitmap().load(imageUri).apply(options).into(awt)
|
Glide.with(context.applicationContext).asBitmap().load(imageUri).apply(options).into(awt)
|
||||||
|
|
||||||
Log.e(tag, "Update App Widget | Is Playing? $isPlaying")
|
Log.i(tag, "Update App Widget | Is Playing=$isPlaying | isAppClosed=$isAppClosed")
|
||||||
|
|
||||||
val playPauseResource = if (isPlaying) R.drawable.ic_media_pause_dark else R.drawable.ic_media_play_dark
|
val playPauseResource = if (isPlaying) R.drawable.ic_media_pause_dark else R.drawable.ic_media_play_dark
|
||||||
views.setImageViewResource(R.id.widgetPlayPauseButton, playPauseResource)
|
views.setImageViewResource(R.id.widgetPlayPauseButton, playPauseResource)
|
||||||
|
|
||||||
|
|
|
@ -170,12 +170,16 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(tag, "onDestroy")
|
Log.d(tag, "onDestroy")
|
||||||
|
isStarted = false
|
||||||
|
isClosed = true
|
||||||
|
DeviceManager.widgetUpdater?.onPlayerChanged(this)
|
||||||
|
|
||||||
playerNotificationManager.setPlayer(null)
|
playerNotificationManager.setPlayer(null)
|
||||||
mPlayer.release()
|
mPlayer.release()
|
||||||
castPlayer?.release()
|
castPlayer?.release()
|
||||||
mediaSession.release()
|
mediaSession.release()
|
||||||
mediaProgressSyncer.reset()
|
mediaProgressSyncer.reset()
|
||||||
isStarted = false
|
|
||||||
|
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
@ -409,9 +413,12 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||||
DeviceManager.setLastPlaybackSession(playbackSession) // Save playback session to use when app is closed
|
DeviceManager.setLastPlaybackSession(playbackSession) // Save playback session to use when app is closed
|
||||||
|
|
||||||
Log.d(tag, "Set CurrentPlaybackSession MediaPlayer ${currentPlaybackSession?.mediaPlayer}")
|
Log.d(tag, "Set CurrentPlaybackSession MediaPlayer ${currentPlaybackSession?.mediaPlayer}")
|
||||||
|
// Notify client
|
||||||
clientEventEmitter?.onPlaybackSession(playbackSession)
|
clientEventEmitter?.onPlaybackSession(playbackSession)
|
||||||
|
|
||||||
|
// Update widget
|
||||||
|
DeviceManager.widgetUpdater?.onPlayerChanged(this)
|
||||||
|
|
||||||
if (mediaItems.isEmpty()) {
|
if (mediaItems.isEmpty()) {
|
||||||
Log.e(tag, "Invalid playback session no media items to play")
|
Log.e(tag, "Invalid playback session no media items to play")
|
||||||
currentPlaybackSession = null
|
currentPlaybackSession = null
|
||||||
|
|
|
@ -93,10 +93,11 @@
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/widgetFastForwardButton"
|
android:id="@+id/widgetFastForwardButton"
|
||||||
|
style="@style/Widget.Android.AppWidget.InnerView"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_weight="1"
|
|
||||||
android:layout_margin="8dp"
|
android:layout_margin="8dp"
|
||||||
|
android:layout_weight="1"
|
||||||
android:src="@drawable/exo_icon_fastforward" />
|
android:src="@drawable/exo_icon_fastforward" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue