diff --git a/android/app/src/main/java/com/audiobookshelf/app/MediaPlayerWidget.kt b/android/app/src/main/java/com/audiobookshelf/app/MediaPlayerWidget.kt index 77feb36e..5bb5823c 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/MediaPlayerWidget.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/MediaPlayerWidget.kt @@ -37,6 +37,16 @@ class MediaPlayerWidget : AppWidgetProvider() { override fun onEnabled(context: Context) { Log.i(tag, "onEnabled check context ${context.packageName}") + DeviceManager.deviceData.lastPlaybackSession?.let { + val appWidgetManager = AppWidgetManager.getInstance(context) + val componentName = ComponentName(context, MediaPlayerWidget::class.java) + val ids = appWidgetManager.getAppWidgetIds(componentName) + Log.d(tag, "Setting initial widget state with last playback session ${it.displayTitle}") + for (widgetId in ids) { + updateAppWidget(context, appWidgetManager, widgetId, it, false, true) + } + } + // Enter relevant functionality for when the first widget is created DeviceManager.widgetUpdater = (object : WidgetEventEmitter { override fun onPlayerChanged(pns: PlayerNotificationService) { @@ -73,12 +83,6 @@ internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManage 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) views.setOnClickPendingIntent(R.id.widgetPlayPauseButton, playPausePI) @@ -88,6 +92,8 @@ internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManage val rewindPI = MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_REWIND) views.setOnClickPendingIntent(R.id.widgetRewindButton, rewindPI) + // Show/Hide button container + views.setViewVisibility(R.id.widgetButtonContainer, if (isAppClosed) View.GONE else View.VISIBLE) views.setOnClickPendingIntent(R.id.widgetBackground, wholeWidgetClickPI) diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt index e9343cdb..f21b93a2 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt @@ -1,6 +1,8 @@ package com.audiobookshelf.app.player import android.app.* +import android.appwidget.AppWidgetManager +import android.content.ComponentName import android.content.Context import android.content.Intent import android.graphics.Bitmap @@ -21,12 +23,15 @@ import android.support.v4.media.session.MediaControllerCompat import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import android.util.Log +import android.view.View +import android.widget.RemoteViews import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat import androidx.media.MediaBrowserServiceCompat import androidx.media.utils.MediaConstants import com.audiobookshelf.app.BuildConfig +import com.audiobookshelf.app.MediaPlayerWidget import com.audiobookshelf.app.R import com.audiobookshelf.app.data.* import com.audiobookshelf.app.data.DeviceInfo @@ -188,6 +193,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { override fun onTaskRemoved(rootIntent: Intent?) { super.onTaskRemoved(rootIntent) Log.d(tag, "onTaskRemoved") + stopSelf() } diff --git a/android/app/src/main/res/drawable-nodpi/example_appwidget_preview.png b/android/app/src/main/res/drawable-nodpi/example_appwidget_preview.png deleted file mode 100644 index 894b069a..00000000 Binary files a/android/app/src/main/res/drawable-nodpi/example_appwidget_preview.png and /dev/null differ diff --git a/android/app/src/main/res/drawable-nodpi/media_player_widget_preview.png b/android/app/src/main/res/drawable-nodpi/media_player_widget_preview.png index f286b61f..27a07efe 100644 Binary files a/android/app/src/main/res/drawable-nodpi/media_player_widget_preview.png and b/android/app/src/main/res/drawable-nodpi/media_player_widget_preview.png differ diff --git a/android/app/src/main/res/layout/media_player_widget.xml b/android/app/src/main/res/layout/media_player_widget.xml index a732480d..a71a7d33 100644 --- a/android/app/src/main/res/layout/media_player_widget.xml +++ b/android/app/src/main/res/layout/media_player_widget.xml @@ -3,13 +3,13 @@ android:id="@+id/widgetBackground" android:layout_width="fill_parent" android:layout_height="fill_parent" - android:orientation="vertical" + android:orientation="horizontal" android:theme="@style/AppTheme.AppWidgetContainer" style="@style/Widget.Android.AppWidget.Container"> @@ -42,12 +45,49 @@ + + + + + + + + + - - - - - - - - - diff --git a/android/app/src/main/res/values/styles.xml b/android/app/src/main/res/values/styles.xml index 862b1660..4234d998 100644 --- a/android/app/src/main/res/values/styles.xml +++ b/android/app/src/main/res/values/styles.xml @@ -37,6 +37,6 @@ diff --git a/android/app/src/main/res/values/themes.xml b/android/app/src/main/res/values/themes.xml index 25c0e184..fc7585c3 100644 --- a/android/app/src/main/res/values/themes.xml +++ b/android/app/src/main/res/values/themes.xml @@ -2,21 +2,16 @@ - - diff --git a/android/app/src/main/res/xml/media_player_widget_info.xml b/android/app/src/main/res/xml/media_player_widget_info.xml index e1e9d251..45dab3b3 100644 --- a/android/app/src/main/res/xml/media_player_widget_info.xml +++ b/android/app/src/main/res/xml/media_player_widget_info.xml @@ -3,8 +3,8 @@ android:description="@string/app_widget_description" android:initialKeyguardLayout="@layout/media_player_widget" android:initialLayout="@layout/media_player_widget" - android:minWidth="272dp" - android:minHeight="56dp" + android:minWidth="275dp" + android:minHeight="50dp" android:previewImage="@drawable/media_player_widget_preview" android:previewLayout="@layout/media_player_widget" android:resizeMode="horizontal"