redo widget layout, hide buttons on force close

This commit is contained in:
Alex 2023-02-25 18:02:20 -06:00
parent bc6ef96465
commit a5ba6c64c9
6 changed files with 72 additions and 52 deletions

View file

@ -1,6 +1,8 @@
package com.audiobookshelf.app.player package com.audiobookshelf.app.player
import android.app.* import android.app.*
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap 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.MediaSessionCompat
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 androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.media.MediaBrowserServiceCompat import androidx.media.MediaBrowserServiceCompat
import androidx.media.utils.MediaConstants import androidx.media.utils.MediaConstants
import com.audiobookshelf.app.BuildConfig import com.audiobookshelf.app.BuildConfig
import com.audiobookshelf.app.MediaPlayerWidget
import com.audiobookshelf.app.R import com.audiobookshelf.app.R
import com.audiobookshelf.app.data.* import com.audiobookshelf.app.data.*
import com.audiobookshelf.app.data.DeviceInfo import com.audiobookshelf.app.data.DeviceInfo
@ -184,6 +189,13 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
override fun onTaskRemoved(rootIntent: Intent?) { override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent) super.onTaskRemoved(rootIntent)
Log.d(tag, "onTaskRemoved") Log.d(tag, "onTaskRemoved")
// Updates widget to hide buttons when app is force closed
val appWidgetManager = AppWidgetManager.getInstance(this)
val remoteViews = RemoteViews(this.packageName, R.layout.media_player_widget)
val thisWidget = ComponentName(this, MediaPlayerWidget::class.java)
remoteViews.setViewVisibility(R.id.widgetButtonContainer, View.GONE)
appWidgetManager.updateAppWidget(thisWidget, remoteViews)
stopSelf() stopSelf()
} }
@ -334,6 +346,13 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
initializeMPlayer() initializeMPlayer()
currentPlayer = mPlayer currentPlayer = mPlayer
// Updates widget to show buttons when app is opened
val appWidgetManager = AppWidgetManager.getInstance(this)
val remoteViews = RemoteViews(this.packageName, R.layout.media_player_widget)
val thisWidget = ComponentName(this, MediaPlayerWidget::class.java)
remoteViews.setViewVisibility(R.id.widgetButtonContainer, View.VISIBLE)
appWidgetManager.updateAppWidget(thisWidget, remoteViews)
} }
private fun initializeMPlayer() { private fun initializeMPlayer() {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 89 KiB

Before After
Before After

View file

@ -3,13 +3,13 @@
android:id="@+id/widgetBackground" android:id="@+id/widgetBackground"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:orientation="horizontal"
android:theme="@style/AppTheme.AppWidgetContainer" android:theme="@style/AppTheme.AppWidgetContainer"
style="@style/Widget.Android.AppWidget.Container"> style="@style/Widget.Android.AppWidget.Container">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="match_parent"
android:layout_weight="3" android:layout_weight="3"
android:background="?android:attr/colorAccent" android:background="?android:attr/colorAccent"
android:theme="@style/MediaPlayerWidgetTheme" android:theme="@style/MediaPlayerWidgetTheme"
@ -19,22 +19,25 @@
android:id="@+id/widgetAlbumArt" android:id="@+id/widgetAlbumArt"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="2"
android:layout_margin="8dp" android:scaleType="centerCrop"
android:src="@drawable/icon" /> android:src="@drawable/icon" />
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="3" android:layout_weight="3"
android:layout_marginVertical="8dp"
android:orientation="vertical" android:orientation="vertical"
android:gravity="center_vertical"> android:gravity="center_vertical">
<TextView <TextView
android:id="@+id/widgetArtistText" android:id="@+id/widgetArtistText"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="0dp"
android:layout_marginHorizontal="8dp" android:layout_weight="2"
android:layout_marginLeft="8dp"
android:gravity="bottom"
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1" android:maxLines="1"
android:text="Artist"/> android:text="Artist"/>
@ -42,12 +45,49 @@
<TextView <TextView
android:id="@+id/widgetMediaTitle" android:id="@+id/widgetMediaTitle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="0dp"
android:layout_marginHorizontal="8dp" android:layout_weight="2"
android:layout_marginLeft="8dp"
android:gravity="top"
android:ellipsize="end" android:ellipsize="end"
android:maxLines="1" android:maxLines="1"
android:text="Title"/> android:text="Title"/>
<LinearLayout
android:id="@+id/widgetButtonContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal"
android:background="?android:attr/colorAccent"
android:theme="@style/MediaPlayerWidgetTheme">
<ImageView
android:id="@+id/widgetRewindButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:src="@drawable/exo_icon_rewind"
style="@style/Widget.Android.AppWidget.InnerView"/>
<ImageView
android:id="@+id/widgetPlayPauseButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="6dp"
android:src="@drawable/ic_media_play_dark" />
<ImageView
android:id="@+id/widgetFastForwardButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:src="@drawable/exo_icon_fastforward" />
</LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
@ -65,38 +105,4 @@
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
android:background="?android:attr/colorBackground"
android:theme="@style/MediaPlayerWidgetTheme">
<ImageView
android:id="@+id/widgetRewindButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:src="@drawable/exo_icon_rewind"
style="@style/Widget.Android.AppWidget.InnerView"/>
<ImageView
android:id="@+id/widgetPlayPauseButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:src="@android:drawable/ic_media_play" />
<ImageView
android:id="@+id/widgetFastForwardButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:src="@drawable/exo_icon_fastforward" />
</LinearLayout>
</LinearLayout> </LinearLayout>

View file

@ -37,6 +37,6 @@
<style name="Widget.Android.AppWidget.InnerView" parent="android:Widget"> <style name="Widget.Android.AppWidget.InnerView" parent="android:Widget">
<!-- <item name="android:background">?android:attr/colorBackground</item>--> <!-- <item name="android:background">?android:attr/colorBackground</item>-->
<!-- <item name="android:textColor">?android:attr/textColorPrimary</item>--> <!-- <item name="android:textColor">?android:attr/textColorPrimary</item>-->
<item name="android:padding">48dp</item> <item name="android:padding">0dp</item>
</style> </style>
</resources> </resources>

View file

@ -2,21 +2,16 @@
<style name="AppTheme.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault"> <style name="AppTheme.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault">
<!-- Radius of the outer bound of widgets to make the rounded corners --> <!-- Radius of the outer bound of widgets to make the rounded corners -->
<item name="appWidgetRadius">16dp</item> <item name="appWidgetRadius">0dp</item>
<!-- <!--
Radius of the inner view's bound of widgets to make the rounded corners. Radius of the inner view's bound of widgets to make the rounded corners.
It needs to be 8dp or less than the value of appWidgetRadius It needs to be 8dp or less than the value of appWidgetRadius
--> -->
<item name="appWidgetInnerRadius">8dp</item> <item name="appWidgetInnerRadius">0dp</item>
</style> </style>
<style name="AppTheme.AppWidgetContainer" parent="AppTheme.AppWidgetContainerParent"> <style name="AppTheme.AppWidgetContainer" parent="AppTheme.AppWidgetContainerParent">
<!-- Apply padding to avoid the content of the widget colliding with the rounded corners --> <!-- Apply padding to avoid the content of the widget colliding with the rounded corners -->
<item name="appWidgetPadding">16dp</item> <item name="appWidgetPadding">0dp</item>
</style>
<style name="AppTheme.AppWidgetButton" parent="AppTheme.AppWidgetContainerParent">
<!-- Apply padding to avoid the content of the widget colliding with the rounded corners -->
<item name="appWidgetPadding">8dp</item>
</style> </style>
</resources> </resources>