From 3d395355e948002bc943812199a7f2e5f7e49432 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Tue, 5 Oct 2021 13:25:13 +1100 Subject: [PATCH 1/3] Make PendingIntents immutable for Android 12. --- .../ryanheise/audioservice/AudioService.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java b/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java index 95496ae6..4ee95035 100644 --- a/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java +++ b/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java @@ -336,7 +336,11 @@ public void configure(AudioServiceConfig config) { intent.setComponent(new ComponentName(context, config.activityClassName)); //Intent intent = new Intent(context, config.activityClassName); intent.setAction(NOTIFICATION_CLICK_ACTION); - contentIntent = PendingIntent.getActivity(context, REQUEST_CONTENT_INTENT, intent, PendingIntent.FLAG_UPDATE_CURRENT); + int flags = PendingIntent.FLAG_UPDATE_CURRENT; + if (Build.VERSION.SDK_INT >= 23) { + flags |= PendingIntent.FLAG_IMMUTABLE; + } + contentIntent = PendingIntent.getActivity(context, REQUEST_CONTENT_INTENT, intent, flags); } else { contentIntent = null; } @@ -365,13 +369,21 @@ PendingIntent buildMediaButtonPendingIntent(long action) { Intent intent = new Intent(this, MediaButtonReceiver.class); intent.setAction(Intent.ACTION_MEDIA_BUTTON); intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); - return PendingIntent.getBroadcast(this, keyCode, intent, 0); + int flags = 0; + if (Build.VERSION.SDK_INT >= 23) { + flags |= PendingIntent.FLAG_IMMUTABLE; + } + return PendingIntent.getBroadcast(this, keyCode, intent, flags); } PendingIntent buildDeletePendingIntent() { Intent intent = new Intent(this, MediaButtonReceiver.class); intent.setAction(MediaButtonReceiver.ACTION_NOTIFICATION_DELETE); - return PendingIntent.getBroadcast(this, 0, intent, 0); + int flags = 0; + if (Build.VERSION.SDK_INT >= 23) { + flags |= PendingIntent.FLAG_IMMUTABLE; + } + return PendingIntent.getBroadcast(this, 0, intent, flags); } void setState(List actions, long actionBits, int[] compactActionIndices, AudioProcessingState processingState, boolean playing, long position, long bufferedPosition, float speed, long updateTime, Integer errorCode, String errorMessage, int repeatMode, int shuffleMode, boolean captioningEnabled, Long queueIndex) { From 6c29b4cbce37e96977ffaa69a9e0731e63157128 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Tue, 9 Nov 2021 01:12:01 +1100 Subject: [PATCH 2/3] Update plugin/example to target Android API 31. --- audio_service/android/build.gradle | 7 +++---- .../android/gradle/wrapper/gradle-wrapper.properties | 2 +- audio_service/example/android/app/build.gradle | 4 ++-- .../example/android/app/src/main/AndroidManifest.xml | 10 ++++++---- audio_service/example/android/build.gradle | 2 +- .../android/gradle/wrapper/gradle-wrapper.properties | 2 +- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/audio_service/android/build.gradle b/audio_service/android/build.gradle index 75fb6da6..942e3acf 100644 --- a/audio_service/android/build.gradle +++ b/audio_service/android/build.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:4.1.0' + classpath 'com.android.tools.build:gradle:7.0.2' } } @@ -27,7 +27,7 @@ project.getTasks().withType(JavaCompile) { apply plugin: 'com.android.library' android { - compileSdkVersion 30 + compileSdkVersion 31 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -43,6 +43,5 @@ android { } dependencies { - implementation 'androidx.core:core:1.6.0' - implementation 'androidx.media:media:1.4.1' + implementation 'androidx.media:media:1.4.3' } diff --git a/audio_service/android/gradle/wrapper/gradle-wrapper.properties b/audio_service/android/gradle/wrapper/gradle-wrapper.properties index 3c9d0852..297f2fec 100644 --- a/audio_service/android/gradle/wrapper/gradle-wrapper.properties +++ b/audio_service/android/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip diff --git a/audio_service/example/android/app/build.gradle b/audio_service/example/android/app/build.gradle index f8a453b2..078858ab 100644 --- a/audio_service/example/android/app/build.gradle +++ b/audio_service/example/android/app/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 30 + compileSdkVersion 31 lintOptions { disable 'InvalidPackage' @@ -34,7 +34,7 @@ android { defaultConfig { applicationId "com.ryanheise.audioserviceexample" minSdkVersion 21 - targetSdkVersion 28 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/audio_service/example/android/app/src/main/AndroidManifest.xml b/audio_service/example/android/app/src/main/AndroidManifest.xml index 724353e9..e05a13b6 100644 --- a/audio_service/example/android/app/src/main/AndroidManifest.xml +++ b/audio_service/example/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,5 @@ @@ -335,14 +335,16 @@ Additionally: - + - + @@ -351,6 +353,8 @@ Additionally: ``` +Note: when targeting Android 12 or above, you must set `android:exported` on each component that has an intent filter (the main activity, the service and the receiver). If the manifest merging process causes `"Instantiable"` lint warnings, use `tools:ignore="Instantiable"` (as above) to suppress them. + 2. If you use any custom icons in notification, create the file `android/app/src/main/res/raw/keep.xml` to prevent them from being stripped during the build process: ```xml diff --git a/audio_service/example/android/app/src/main/AndroidManifest.xml b/audio_service/example/android/app/src/main/AndroidManifest.xml index e05a13b6..d520ff02 100644 --- a/audio_service/example/android/app/src/main/AndroidManifest.xml +++ b/audio_service/example/android/app/src/main/AndroidManifest.xml @@ -2,19 +2,10 @@ xmlns:tools="http://schemas.android.com/tools" package="com.ryanheise.audioserviceexample"> - - @@ -25,20 +16,21 @@ android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" - android:exported="true"> + android:exported="true" + tools:ignore="Instantiatable"> - + - + diff --git a/audio_service/pubspec.yaml b/audio_service/pubspec.yaml index 82ff66b3..3218939f 100644 --- a/audio_service/pubspec.yaml +++ b/audio_service/pubspec.yaml @@ -1,6 +1,6 @@ name: audio_service description: Flutter plugin to play audio in the background while the screen is off. -version: 0.18.2 +version: 0.18.3 homepage: https://github.com/ryanheise/audio_service/tree/master/audio_service environment: