Skip to content

Commit

Permalink
Merge branch 'fix/android_intent_flags' into minor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Dec 24, 2021
2 parents 1f5799f + 9437376 commit 0ced1ac
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 29 deletions.
4 changes: 4 additions & 0 deletions audio_service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.18.3

* Fix build when targeting Android 12.

## 0.18.2

* Guard against NPE when Android service is destroyed quickly.
Expand Down
10 changes: 7 additions & 3 deletions audio_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ Additionally:
1. Make the following changes to your project's `AndroidManifest.xml` file:

```xml
<manifest ...>
<manifest xmlns:tools="http://schemas.android.com/tools" ...>
<!-- ADD THESE TWO PERMISSIONS -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Expand All @@ -335,14 +335,16 @@ Additionally:
</activity>

<!-- ADD THIS "SERVICE" element -->
<service android:name="com.ryanheise.audioservice.AudioService">
<service android:name="com.ryanheise.audioservice.AudioService"
android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>

<!-- ADD THIS "RECEIVER" element -->
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" >
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
Expand All @@ -351,6 +353,8 @@ Additionally:
</manifest>
```

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
Expand Down
7 changes: 3 additions & 4 deletions audio_service/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:7.0.2'
}
}

Expand All @@ -27,7 +27,7 @@ project.getTasks().withType(JavaCompile) {
apply plugin: 'com.android.library'

android {
compileSdkVersion 30
compileSdkVersion 31

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<MediaControl> 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) {
Expand Down
4 changes: 2 additions & 2 deletions audio_service/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
Expand Down
20 changes: 7 additions & 13 deletions audio_service/example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ryanheise.audioserviceexample">

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:label="Audio Service Demo"
android:icon="@mipmap/ic_launcher">
Expand All @@ -23,24 +15,26 @@
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
android:windowSoftInputMode="adjustResize"
android:exported="true"
tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<service android:name="com.ryanheise.audioservice.AudioService">
<service android:name="com.ryanheise.audioservice.AudioService" android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>

<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" >
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</receiver>

<meta-data
android:name="flutterEmbedding"
Expand Down
2 changes: 1 addition & 1 deletion audio_service/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:7.0.2'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,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
2 changes: 1 addition & 1 deletion audio_service/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 0ced1ac

Please sign in to comment.