Fixed Widget and Notification crashes and added a better notification layout for Android 12

main
Prathamesh More 2021-11-22 17:44:07 +05:30
parent 8e64f117f9
commit 574b990984
14 changed files with 254 additions and 27 deletions

View File

@ -180,7 +180,7 @@
<receiver
android:name=".service.MediaButtonIntentReceiver"
android:exported="false">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
@ -188,7 +188,7 @@
<receiver
android:name=".appwidgets.BootReceiver"
android:exported="false">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
@ -197,7 +197,7 @@
<receiver
android:name=".appwidgets.AppWidgetBig"
android:exported="false"
android:exported="true"
android:label="@string/app_widget_big_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
@ -210,7 +210,7 @@
<receiver
android:name=".appwidgets.AppWidgetClassic"
android:exported="false"
android:exported="true"
android:label="@string/app_widget_classic_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
@ -222,7 +222,7 @@
</receiver>
<receiver
android:name=".appwidgets.AppWidgetSmall"
android:exported="false"
android:exported="true"
android:label="@string/app_widget_small_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
@ -234,7 +234,7 @@
</receiver>
<receiver
android:name=".appwidgets.AppWidgetText"
android:exported="false"
android:exported="true"
android:label="@string/app_widget_text_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
@ -246,7 +246,7 @@
</receiver>
<receiver
android:name=".appwidgets.AppWidgetCard"
android:exported="false"
android:exported="true"
android:label="@string/app_widget_card_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

View File

@ -74,7 +74,7 @@ class AppWidgetBig : BaseAppWidget() {
)
)
appWidgetView.setImageViewBitmap(
R.id.button_toggle_play_pause, BaseAppWidget.Companion.createBitmap(
R.id.button_toggle_play_pause, BaseAppWidget.createBitmap(
RetroUtil.getTintedVectorDrawable(
context,
R.drawable.ic_play_arrow_white_32dp,
@ -202,13 +202,13 @@ class AppWidgetBig : BaseAppWidget() {
MainActivity.EXPAND_PANEL,
PreferenceUtil.isExpandPanel
)
var pendingIntent: PendingIntent
val serviceName = ComponentName(context, MusicService::class.java)
// Home
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
pendingIntent = PendingIntent.getActivity(context, 0, action, 0)
var pendingIntent =
PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.clickable_area, pendingIntent)
// Previous track

View File

@ -141,11 +141,11 @@ class AppWidgetCard : BaseAppWidget() {
if (imageSize == 0) {
imageSize =
service.resources.getDimensionPixelSize(code.name.monkey.retromusic.R.dimen.app_widget_card_image_size)
service.resources.getDimensionPixelSize(R.dimen.app_widget_card_image_size)
}
if (cardRadius == 0f) {
cardRadius =
service.resources.getDimension(code.name.monkey.retromusic.R.dimen.app_widget_card_radius)
service.resources.getDimension(R.dimen.app_widget_card_radius)
}
// Load the album cover async and push the update on completion
@ -225,13 +225,13 @@ class AppWidgetCard : BaseAppWidget() {
MainActivity.EXPAND_PANEL,
PreferenceUtil.isExpandPanel
)
var pendingIntent: PendingIntent
val serviceName = ComponentName(context, MusicService::class.java)
// Home
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
pendingIntent = PendingIntent.getActivity(context, 0, action, 0)
var pendingIntent =
PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.image, pendingIntent)
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent)

View File

@ -216,13 +216,12 @@ class AppWidgetClassic : BaseAppWidget() {
MainActivity.EXPAND_PANEL,
PreferenceUtil.isExpandPanel
)
var pendingIntent: PendingIntent
val serviceName = ComponentName(context, MusicService::class.java)
// Home
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
pendingIntent = PendingIntent.getActivity(context, 0, action, 0)
var pendingIntent = PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.image, pendingIntent)
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent)

View File

@ -201,13 +201,13 @@ class AppWidgetSmall : BaseAppWidget() {
MainActivity.EXPAND_PANEL,
PreferenceUtil.isExpandPanel
)
var pendingIntent: PendingIntent
val serviceName = ComponentName(context, MusicService::class.java)
// Home
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
pendingIntent = PendingIntent.getActivity(context, 0, action, 0)
var pendingIntent =
PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.image, pendingIntent)
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent)

View File

@ -18,6 +18,7 @@ import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.provider.MediaStore
import android.text.TextUtils
import android.view.View
import android.widget.RemoteViews
@ -83,13 +84,12 @@ class AppWidgetText : BaseAppWidget() {
MainActivity.EXPAND_PANEL,
PreferenceUtil.isExpandPanel
)
var pendingIntent: PendingIntent
val serviceName = ComponentName(context, MusicService::class.java)
// Home
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
pendingIntent = PendingIntent.getActivity(context, 0, action, 0)
var pendingIntent = PendingIntent.getActivity(context, 0, action, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.image, pendingIntent)
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent)

View File

@ -97,9 +97,9 @@ abstract class BaseAppWidget : AppWidgetProvider() {
val intent = Intent(action)
intent.component = serviceName
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.getForegroundService(context, 0, intent, 0)
PendingIntent.getForegroundService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
} else {
PendingIntent.getService(context, 0, intent, 0)
PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}
}

View File

@ -26,6 +26,7 @@ import androidx.core.app.NotificationCompat
import code.name.monkey.appthemehelper.util.ATHUtil.resolveColor
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.MaterialValueHelper
import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.MainActivity
import code.name.monkey.retromusic.glide.GlideApp
@ -78,7 +79,12 @@ class PlayingNotificationOreo : PlayingNotification() {
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
val clickIntent = PendingIntent
.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT)
.getActivity(
service,
0,
action,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val deleteIntent = buildPendingIntent(service, ACTION_QUIT, null)
val builder = NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID)
@ -143,10 +149,16 @@ class PlayingNotificationOreo : PlayingNotification() {
)
}
if (!PreferenceUtil.isColoredNotification) {
bgColorFinal = resolveColor(service, R.attr.colorPrimary, Color.WHITE)
// Android 12 applies a standard Notification template to every notification
// which will in turn have a default background so setting a different background
// than that, looks weird
if (!VersionUtils.hasS()) {
if (!PreferenceUtil.isColoredNotification) {
bgColorFinal =
resolveColor(service, R.attr.colorPrimary, Color.WHITE)
}
setBackgroundColor(bgColorFinal)
}
setBackgroundColor(bgColorFinal)
setNotificationContent(ColorUtil.isColorLight(bgColorFinal))
if (stopped) {
@ -250,7 +262,7 @@ class PlayingNotificationOreo : PlayingNotification() {
): PendingIntent {
val intent = Intent(action)
intent.component = serviceName
return PendingIntent.getService(context, 0, intent, 0)
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}

View File

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:background="@color/md_red_500"
tools:ignore="ContentDescription">
<RelativeLayout
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/content"
android:layout_alignParentEnd="true">
<ImageView
android:id="@+id/largeIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
tools:src="@tools:sample/avatars" />
</RelativeLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/actions"
android:orientation="vertical"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingBottom="8dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:singleLine="true"
android:textAppearance="@style/Theme.RetroMusic.Notification"
android:textStyle="bold"
tools:text="@tools:sample/lorem/random" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:singleLine="true"
android:textAppearance="@style/Theme.RetroMusic.Notification.Title"
tools:text="@tools:sample/lorem/random" />
</LinearLayout>
<LinearLayout
android:id="@+id/actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/content"
android:layout_alignBottom="@id/content"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom"
android:layout_marginEnd="48dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/action_prev"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
android:src="@drawable/ic_skip_previous_round_white_32dp"
tools:tint="?colorOnPrimary" />
<ImageButton
android:id="@+id/action_play_pause"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
android:src="@drawable/ic_pause"
tools:tint="?colorOnPrimary" />
<ImageButton
android:id="@+id/action_next"
android:layout_width="38dp"
android:layout_height="38dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
android:src="@drawable/ic_skip_next_round_white_32dp"
tools:tint="?colorOnPrimary" />
</LinearLayout>
</RelativeLayout>

View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:background="@color/md_red_500">
<RelativeLayout
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/content"
android:layout_alignParentEnd="true">
<ImageView
android:id="@+id/largeIcon"
android:layout_width="@dimen/notification_big_image_size"
android:layout_height="@dimen/notification_big_image_size"
android:layout_alignParentEnd="true"
android:scaleType="centerCrop"
tools:src="@tools:sample/avatars" />
</RelativeLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:orientation="vertical"
android:paddingStart="0dp"
android:paddingTop="8dp"
android:paddingEnd="144dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:paddingStart="16dp"
android:paddingEnd="0dp"
android:singleLine="true"
android:textAppearance="@style/Theme.RetroMusic.Notification"
android:textStyle="bold"
tools:text="@tools:sample/lorem/random" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:paddingStart="16dp"
android:paddingEnd="0dp"
android:singleLine="true"
android:textAppearance="@style/Theme.RetroMusic.Notification.Title"
tools:text="@tools:sample/lorem/random" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingStart="8dp"
android:paddingEnd="0dp">
<ImageButton
android:id="@+id/action_prev"
android:layout_width="42dp"
android:layout_height="42dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
tools:src="@drawable/ic_skip_previous_round_white_32dp"
tools:tint="?colorOnPrimary" />
<ImageButton
android:id="@+id/action_play_pause"
android:layout_width="42dp"
android:layout_height="42dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
tools:src="@drawable/ic_pause_white_48dp"
tools:tint="?colorOnPrimary" />
<ImageButton
android:id="@+id/action_next"
android:layout_width="42dp"
android:layout_height="42dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
tools:src="@drawable/ic_skip_next_round_white_32dp"
tools:tint="?colorOnPrimary" />
<ImageButton
android:id="@+id/action_quit"
android:layout_width="42dp"
android:layout_height="42dp"
android:background="@drawable/notification_selector"
android:scaleType="centerInside"
tools:src="@drawable/ic_close"
tools:tint="?colorOnPrimary" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

View File

@ -2,4 +2,6 @@
<resources>
<bool name="md3_available">true</bool>
<bool name="md3_enabled">true</bool>
<bool name="colored_notification_available">false</bool>
</resources>

View File

@ -26,4 +26,6 @@
<string name="pref_title_md3">Material You</string>
<bool name="md3_available">false</bool>
<bool name="md3_enabled">false</bool>
<bool name="colored_notification_available">true</bool>
</resources>

View File

@ -13,6 +13,7 @@
android:defaultValue="true"
android:key="colored_notification"
android:layout="@layout/list_item_view_switch"
app:isPreferenceVisible="@bool/colored_notification_available"
android:summary="@string/pref_summary_colored_notification"
android:title="@string/pref_title_colored_notification" />

View File

@ -8,6 +8,13 @@ import android.os.Build
object VersionUtils {
/**
* @return true if device is running API >= 21
*/
fun hasLollipop(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
}
/**
* @return true if device is running API >= 23
*/