From e04d1bd6a378e7592aaaa9bef318c6307e66e4b4 Mon Sep 17 00:00:00 2001 From: rguntha Date: Sun, 31 Oct 2021 15:36:17 +0530 Subject: [PATCH 1/9] Fixing for Android 12 java.lang.RuntimeException: Unable to create service com.google.flutter.plugins.audiofileplayer.AudiofileplayerService: java.lang.IllegalArgumentException: com.layamritam.app: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. --- .../audiofileplayer/AudiofileplayerService.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 6859878..7582d63 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -136,8 +136,14 @@ public void onAudioFocusChange(int focusChange) { public void setPendingIntentActivity(Activity activity) { Context context = activity.getApplicationContext(); Intent intent = new Intent(context, activity.getClass()); - PendingIntent pendingIntent = - PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + pendingIntent = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_IMMUTABLE); + } + else + { + pendingIntent = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT); + } mediaSession.setSessionActivity(pendingIntent); } From 286845dbeed892526ad5250d613c7ddb34160d34 Mon Sep 17 00:00:00 2001 From: rguntha Date: Sun, 31 Oct 2021 16:45:34 +0530 Subject: [PATCH 2/9] Fixing for Android 12 --- .../audiofileplayer/AudiofileplayerPlugin.java | 14 ++++++++++++-- .../audiofileplayer/AudiofileplayerService.java | 10 ++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java index a12fc3b..0ed9306 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java @@ -676,8 +676,18 @@ private NotificationCompat.Action customMediaButtonMapToAction(Map customMediaBu Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(component); intent.putExtra(CUSTOM_MEDIA_BUTTON_EXTRA_KEY, eventId); - PendingIntent pendingIntent = - PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); + PendingIntent pendingIntent = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + pendingIntent = + PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); + } + else + { + pendingIntent = + PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); + } +// PendingIntent pendingIntent = +// PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); return new NotificationCompat.Action(resourceId, title, pendingIntent); } diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 7582d63..03c8536 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -62,8 +62,14 @@ public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); instance = this; - - mediaSession = new MediaSessionCompat(this, TAG); + val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON) + val pendingItent = PendingIntent.getBroadcast( + baseContext, + 0, mediaButtonIntent, + PendingIntent.FLAG_IMMUTABLE + ) + + mediaSession = new MediaSessionCompat(this, TAG,null,pendingItent); mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); From 9451d45ee336c33ec7a3f6b3302f950eabb11d4a Mon Sep 17 00:00:00 2001 From: rguntha Date: Sun, 31 Oct 2021 16:48:18 +0530 Subject: [PATCH 3/9] Update AudiofileplayerService.java --- .../flutter/plugins/audiofileplayer/AudiofileplayerService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 03c8536..96139b1 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -67,7 +67,7 @@ public void onCreate() { baseContext, 0, mediaButtonIntent, PendingIntent.FLAG_IMMUTABLE - ) + ); mediaSession = new MediaSessionCompat(this, TAG,null,pendingItent); mediaSession.setFlags( From 0cdd310de6f066bdebda74cc3eb250a7c1f9467d Mon Sep 17 00:00:00 2001 From: rguntha Date: Sun, 31 Oct 2021 16:49:00 +0530 Subject: [PATCH 4/9] Update AudiofileplayerService.java --- .../flutter/plugins/audiofileplayer/AudiofileplayerService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 96139b1..e37091b 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -62,7 +62,7 @@ public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); instance = this; - val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON) + val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON); val pendingItent = PendingIntent.getBroadcast( baseContext, 0, mediaButtonIntent, From 768c8a80c887fa908c2c8411af172952ff51bdc0 Mon Sep 17 00:00:00 2001 From: rguntha Date: Sun, 31 Oct 2021 18:49:05 +0530 Subject: [PATCH 5/9] Fixing for Android 12 --- packages/audiofileplayer/android/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 5 ++ .../AudiofileplayerPlugin.java | 29 +++------ .../AudiofileplayerService.java | 11 ++-- .../MediaButtonReceiver12.java | 61 +++++++++++++++++++ 5 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 packages/audiofileplayer/android/gradle/wrapper/gradle-wrapper.properties create mode 100644 packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java diff --git a/packages/audiofileplayer/android/build.gradle b/packages/audiofileplayer/android/build.gradle index d7b1e95..5d09228 100644 --- a/packages/audiofileplayer/android/build.gradle +++ b/packages/audiofileplayer/android/build.gradle @@ -22,7 +22,7 @@ rootProject.allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 29 + compileSdkVersion 31 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/packages/audiofileplayer/android/gradle/wrapper/gradle-wrapper.properties b/packages/audiofileplayer/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..da9702f --- /dev/null +++ b/packages/audiofileplayer/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java index 0ed9306..8f1636b 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java @@ -620,43 +620,43 @@ private NotificationCompat.Action mediaButtonTypeToAction(String mediaButtonType return new NotificationCompat.Action( R.drawable.ic_pause_black_36dp, context.getString(R.string.pause), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_PAUSE)); case MEDIA_PLAY: return new NotificationCompat.Action( R.drawable.ic_play_arrow_black_36dp, context.getString(R.string.play), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_PLAY)); case MEDIA_STOP: return new NotificationCompat.Action( R.drawable.ic_stop_black_36dp, context.getString(R.string.stop), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_STOP)); case MEDIA_NEXT: return new NotificationCompat.Action( R.drawable.ic_skip_next_black_36dp, context.getString(R.string.skipForward), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)); case MEDIA_PREVIOUS: return new NotificationCompat.Action( R.drawable.ic_skip_previous_black_36dp, context.getString(R.string.skipBackward), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)); case MEDIA_SEEK_FORWARD: return new NotificationCompat.Action( R.drawable.ic_fast_forward_black_36dp, context.getString(R.string.seekForward), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_FAST_FORWARD)); case MEDIA_SEEK_BACKWARD: return new NotificationCompat.Action( R.drawable.ic_fast_rewind_black_36dp, context.getString(R.string.seekBackward), - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_REWIND)); default: Log.e(TAG, "unsupported mediaButtonType:" + mediaButtonType); @@ -676,18 +676,9 @@ private NotificationCompat.Action customMediaButtonMapToAction(Map customMediaBu Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.setComponent(component); intent.putExtra(CUSTOM_MEDIA_BUTTON_EXTRA_KEY, eventId); - PendingIntent pendingIntent = null; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { - pendingIntent = - PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); - } - else - { - pendingIntent = - PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); - } -// PendingIntent pendingIntent = -// PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); + + PendingIntent pendingIntent = + PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT); return new NotificationCompat.Action(resourceId, title, pendingIntent); } diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index e37091b..86c01af 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -62,10 +62,9 @@ public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); instance = this; - val mediaButtonIntent = Intent(Intent.ACTION_MEDIA_BUTTON); - val pendingItent = PendingIntent.getBroadcast( - baseContext, - 0, mediaButtonIntent, + PendingIntent pendingItent = PendingIntent.getBroadcast( + getApplicationContext(), + 0, new Intent(Intent.ACTION_MEDIA_BUTTON), PendingIntent.FLAG_IMMUTABLE ); @@ -283,7 +282,7 @@ private Notification buildNotification() { .setContentIntent(mediaSession.getController().getSessionActivity()) // Stop the service when the notification is swiped away .setDeleteIntent( - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( this, PlaybackStateCompat.ACTION_STOP)) // Make the transport controls visible on the lockscreen .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) @@ -294,7 +293,7 @@ private Notification buildNotification() { .setShowActionsInCompactView(compactNotificationActionIndices) .setShowCancelButton(true) .setCancelButtonIntent( - MediaButtonReceiver.buildMediaButtonPendingIntent( + MediaButtonReceiver12.buildMediaButtonPendingIntent( this, PlaybackStateCompat.ACTION_STOP))); // Add the actions specified by the client. diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java new file mode 100644 index 0000000..8a22117 --- /dev/null +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java @@ -0,0 +1,61 @@ +package com.google.flutter.plugins.audiofileplayer; + +import android.app.PendingIntent; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v4.media.session.PlaybackStateCompat; +import android.util.Log; +import android.view.KeyEvent; + +import java.util.List; + +public class MediaButtonReceiver12 extends androidx.media.session.MediaButtonReceiver{ + + public static ComponentName getMediaButtonReceiverComponent(Context context) { + Intent queryIntent = new Intent("android.intent.action.MEDIA_BUTTON"); + queryIntent.setPackage(context.getPackageName()); + PackageManager pm = context.getPackageManager(); + List resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0); + if (resolveInfos.size() == 1) { + ResolveInfo resolveInfo = (ResolveInfo)resolveInfos.get(0); + return new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name); + } else { + if (resolveInfos.size() > 1) { + Log.w("MediaButtonReceiver12", "More than one BroadcastReceiver that handles android.intent.action.MEDIA_BUTTON was found, returning null."); + } + + return null; + } + } + + public static PendingIntent buildMediaButtonPendingIntent(Context context, long action) { + ComponentName mbrComponent = getMediaButtonReceiverComponent(context); + if (mbrComponent == null) { + Log.w("MediaButtonReceiver12", "A unique media button receiver could not be found in the given context, so couldn't build a pending intent."); + return null; + } else { + return buildMediaButtonPendingIntent(context, mbrComponent, action); + } + } + + public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, long action) { + if (mbrComponent == null) { + Log.w("MediaButtonReceiver12", "The component name of media button receiver should be provided."); + return null; + } else { + int keyCode = PlaybackStateCompat.toKeyCode(action); + if (keyCode == 0) { + Log.w("MediaButtonReceiver12", "Cannot build a media button pending intent with the given action: " + action); + return null; + } else { + Intent intent = new Intent("android.intent.action.MEDIA_BUTTON"); + intent.setComponent(mbrComponent); + intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, keyCode)); + return PendingIntent.getBroadcast(context, keyCode, intent, PendingIntent.FLAG_IMMUTABLE | 0); + } + } + } +} From a42032a47aa463d7871c2db84fafb3432eb725d5 Mon Sep 17 00:00:00 2001 From: rguntha Date: Sun, 31 Oct 2021 18:49:42 +0530 Subject: [PATCH 6/9] Update MediaButtonReceiver12.java --- .../flutter/plugins/audiofileplayer/MediaButtonReceiver12.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java index 8a22117..01eeaa3 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java @@ -12,7 +12,7 @@ import java.util.List; -public class MediaButtonReceiver12 extends androidx.media.session.MediaButtonReceiver{ +public class MediaButtonReceiver12{ public static ComponentName getMediaButtonReceiverComponent(Context context) { Intent queryIntent = new Intent("android.intent.action.MEDIA_BUTTON"); From 9747e5b228d7b2bf0d235b1876a7cb69c5361506 Mon Sep 17 00:00:00 2001 From: rguntha Date: Mon, 1 Nov 2021 10:02:11 +0530 Subject: [PATCH 7/9] Implemented review comments. Upgraded media library dependancy to 1.3.0 --- packages/audiofileplayer/android/build.gradle | 2 +- .../audiofileplayer/AudiofileplayerPlugin.java | 14 +++++++------- .../audiofileplayer/AudiofileplayerService.java | 16 +++++----------- .../audiofileplayer/MediaButtonReceiver12.java | 5 ++--- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/packages/audiofileplayer/android/build.gradle b/packages/audiofileplayer/android/build.gradle index 5d09228..62b25a4 100644 --- a/packages/audiofileplayer/android/build.gradle +++ b/packages/audiofileplayer/android/build.gradle @@ -40,5 +40,5 @@ android { dependencies { implementation 'androidx.core:core:1.0.0' - implementation 'androidx.media:media:1.0.0' + implementation 'androidx.media:media:1.3.0' } \ No newline at end of file diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java index 8f1636b..dfcdbb8 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java @@ -620,43 +620,43 @@ private NotificationCompat.Action mediaButtonTypeToAction(String mediaButtonType return new NotificationCompat.Action( R.drawable.ic_pause_black_36dp, context.getString(R.string.pause), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_PAUSE)); case MEDIA_PLAY: return new NotificationCompat.Action( R.drawable.ic_play_arrow_black_36dp, context.getString(R.string.play), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_PLAY)); case MEDIA_STOP: return new NotificationCompat.Action( R.drawable.ic_stop_black_36dp, context.getString(R.string.stop), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_STOP)); case MEDIA_NEXT: return new NotificationCompat.Action( R.drawable.ic_skip_next_black_36dp, context.getString(R.string.skipForward), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)); case MEDIA_PREVIOUS: return new NotificationCompat.Action( R.drawable.ic_skip_previous_black_36dp, context.getString(R.string.skipBackward), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)); case MEDIA_SEEK_FORWARD: return new NotificationCompat.Action( R.drawable.ic_fast_forward_black_36dp, context.getString(R.string.seekForward), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_FAST_FORWARD)); case MEDIA_SEEK_BACKWARD: return new NotificationCompat.Action( R.drawable.ic_fast_rewind_black_36dp, context.getString(R.string.seekBackward), - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( context, PlaybackStateCompat.ACTION_REWIND)); default: Log.e(TAG, "unsupported mediaButtonType:" + mediaButtonType); diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java index 86c01af..967e483 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerService.java @@ -68,7 +68,7 @@ public void onCreate() { PendingIntent.FLAG_IMMUTABLE ); - mediaSession = new MediaSessionCompat(this, TAG,null,pendingItent); + mediaSession = new MediaSessionCompat(this, TAG, null, pendingItent); mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); @@ -141,14 +141,8 @@ public void onAudioFocusChange(int focusChange) { public void setPendingIntentActivity(Activity activity) { Context context = activity.getApplicationContext(); Intent intent = new Intent(context, activity.getClass()); - PendingIntent pendingIntent = null; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { - pendingIntent = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_IMMUTABLE); - } - else - { - pendingIntent = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT); - } + PendingIntent pendingIntent = + PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); mediaSession.setSessionActivity(pendingIntent); } @@ -282,7 +276,7 @@ private Notification buildNotification() { .setContentIntent(mediaSession.getController().getSessionActivity()) // Stop the service when the notification is swiped away .setDeleteIntent( - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( this, PlaybackStateCompat.ACTION_STOP)) // Make the transport controls visible on the lockscreen .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) @@ -293,7 +287,7 @@ private Notification buildNotification() { .setShowActionsInCompactView(compactNotificationActionIndices) .setShowCancelButton(true) .setCancelButtonIntent( - MediaButtonReceiver12.buildMediaButtonPendingIntent( + MediaButtonReceiver.buildMediaButtonPendingIntent( this, PlaybackStateCompat.ACTION_STOP))); // Add the actions specified by the client. diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java index 01eeaa3..14a3b9f 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java @@ -9,12 +9,11 @@ import android.support.v4.media.session.PlaybackStateCompat; import android.util.Log; import android.view.KeyEvent; - import java.util.List; -public class MediaButtonReceiver12{ +public class MediaButtonReceiver12 { - public static ComponentName getMediaButtonReceiverComponent(Context context) { + private static ComponentName getMediaButtonReceiverComponent(Context context) { Intent queryIntent = new Intent("android.intent.action.MEDIA_BUTTON"); queryIntent.setPackage(context.getPackageName()); PackageManager pm = context.getPackageManager(); From 622e5abb2d2f30a71beb8e358b4eb66570a01164 Mon Sep 17 00:00:00 2001 From: rguntha Date: Mon, 1 Nov 2021 10:23:25 +0530 Subject: [PATCH 8/9] Removing the unused exception --- .../audiofileplayer/AudiofileplayerPlugin.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java index dfcdbb8..beda276 100644 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java +++ b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/AudiofileplayerPlugin.java @@ -509,16 +509,12 @@ public void onActivityDestroyed(Activity activity) { @Override public void onConnected() { Log.i(TAG, "ConnectionCallback.onConnected"); - try { - MediaSessionCompat.Token token = mediaBrowser.getSessionToken(); - mediaController = new MediaControllerCompat(activity, token); - MediaControllerCompat.setMediaController(activity, mediaController); - mediaController.registerCallback(controllerCallback); - AudiofileplayerService.instance.setPendingIntentActivity(activity); - AudiofileplayerService.instance.setListener(AudiofileplayerPlugin.this); - } catch (RemoteException e) { - throw new RuntimeException(e); - } + MediaSessionCompat.Token token = mediaBrowser.getSessionToken(); + mediaController = new MediaControllerCompat(activity, token); + MediaControllerCompat.setMediaController(activity, mediaController); + mediaController.registerCallback(controllerCallback); + AudiofileplayerService.instance.setPendingIntentActivity(activity); + AudiofileplayerService.instance.setListener(AudiofileplayerPlugin.this); } @Override From 7722cbfbde6fbcf3f7ebb78034f15595a6b34876 Mon Sep 17 00:00:00 2001 From: rguntha Date: Mon, 1 Nov 2021 10:39:49 +0530 Subject: [PATCH 9/9] Delete MediaButtonReceiver12.java This file is not needed as the updated media library 1.3.0 is sufficient --- .../MediaButtonReceiver12.java | 60 ------------------- 1 file changed, 60 deletions(-) delete mode 100644 packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java diff --git a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java b/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java deleted file mode 100644 index 14a3b9f..0000000 --- a/packages/audiofileplayer/android/src/main/java/com/google/flutter/plugins/audiofileplayer/MediaButtonReceiver12.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.google.flutter.plugins.audiofileplayer; - -import android.app.PendingIntent; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.support.v4.media.session.PlaybackStateCompat; -import android.util.Log; -import android.view.KeyEvent; -import java.util.List; - -public class MediaButtonReceiver12 { - - private static ComponentName getMediaButtonReceiverComponent(Context context) { - Intent queryIntent = new Intent("android.intent.action.MEDIA_BUTTON"); - queryIntent.setPackage(context.getPackageName()); - PackageManager pm = context.getPackageManager(); - List resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0); - if (resolveInfos.size() == 1) { - ResolveInfo resolveInfo = (ResolveInfo)resolveInfos.get(0); - return new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name); - } else { - if (resolveInfos.size() > 1) { - Log.w("MediaButtonReceiver12", "More than one BroadcastReceiver that handles android.intent.action.MEDIA_BUTTON was found, returning null."); - } - - return null; - } - } - - public static PendingIntent buildMediaButtonPendingIntent(Context context, long action) { - ComponentName mbrComponent = getMediaButtonReceiverComponent(context); - if (mbrComponent == null) { - Log.w("MediaButtonReceiver12", "A unique media button receiver could not be found in the given context, so couldn't build a pending intent."); - return null; - } else { - return buildMediaButtonPendingIntent(context, mbrComponent, action); - } - } - - public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, long action) { - if (mbrComponent == null) { - Log.w("MediaButtonReceiver12", "The component name of media button receiver should be provided."); - return null; - } else { - int keyCode = PlaybackStateCompat.toKeyCode(action); - if (keyCode == 0) { - Log.w("MediaButtonReceiver12", "Cannot build a media button pending intent with the given action: " + action); - return null; - } else { - Intent intent = new Intent("android.intent.action.MEDIA_BUTTON"); - intent.setComponent(mbrComponent); - intent.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, keyCode)); - return PendingIntent.getBroadcast(context, keyCode, intent, PendingIntent.FLAG_IMMUTABLE | 0); - } - } - } -}