diff --git a/README.md b/README.md index 2d09187..b4d48ac 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ First install the package via [pub.dev](https://pub.dev/packages/flutter_broadca ```dart BroadcastReceiver receiver = BroadcastReceiver( names: ["de.kevlatus.broadcast"], + listenToBroadcastsFromOtherApps: false, ); receiver.messages.listen(print); receiver.start(); diff --git a/android/build.gradle b/android/build.gradle index 15914ce..93893b2 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,40 +1,42 @@ -group 'de.kevlatus.flutter_broadcasts' -version '1.0-SNAPSHOT' - -buildscript { - ext.kotlin_version = '1.5.20' - repositories { - google() - jcenter() - } - - dependencies { - classpath 'com.android.tools.build:gradle:7.4.0' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -rootProject.allprojects { - repositories { - google() - jcenter() - } +plugins { + id 'com.android.library' + id 'kotlin-android' } -apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' +group = 'de.kevlatus.flutter_broadcasts' +version = '1.0-SNAPSHOT' android { - compileSdkVersion 31 + namespace 'de.kevlatus.flutter_broadcasts' + compileSdk = 34 + + defaultConfig { + minSdk = 24 + targetSdk = 34 + } sourceSets { main.java.srcDirs += 'src/main/kotlin' } - defaultConfig { - minSdkVersion 24 + + buildFeatures { + buildConfig = true + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + kotlinOptions { + jvmTarget = "17" } } +repositories { + google() + mavenCentral() +} + dependencies { - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22" } diff --git a/android/src/main/kotlin/de/kevlatus/flutter_broadcasts/FlutterBroadcastsPlugin.kt b/android/src/main/kotlin/de/kevlatus/flutter_broadcasts/FlutterBroadcastsPlugin.kt index b59e4c5..d50a0ff 100644 --- a/android/src/main/kotlin/de/kevlatus/flutter_broadcasts/FlutterBroadcastsPlugin.kt +++ b/android/src/main/kotlin/de/kevlatus/flutter_broadcasts/FlutterBroadcastsPlugin.kt @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.os.Build import android.util.Log import androidx.annotation.NonNull import io.flutter.embedding.engine.plugins.FlutterPlugin @@ -17,6 +18,7 @@ import java.io.Serializable class CustomBroadcastReceiver( val id: Int, private val names: List, + private val listenToBroadcastsFromOtherApps: Boolean, private val listener: (Any) -> Unit ) : BroadcastReceiver() { companion object { @@ -46,7 +48,17 @@ class CustomBroadcastReceiver( } fun start(context: Context) { - context.registerReceiver(this, intentFilter) + if (Build.VERSION.SDK_INT >= 33) { + val receiverFlags = if (listenToBroadcastsFromOtherApps) { + Context.RECEIVER_EXPORTED + } else { + Context.RECEIVER_NOT_EXPORTED + } + context.registerReceiver(this, intentFilter, receiverFlags) + } else { + context.registerReceiver(this, intentFilter) + } + Log.d(TAG, "starting to listen for broadcasts: " + names.joinToString(";")) } @@ -95,7 +107,7 @@ class MethodCallHandlerImpl( private fun withReceiverArgs( call: MethodCall, result: Result, - func: (id: Int, names: List) -> Unit + func: (id: Int, names: List, listenToBroadcastsFromOtherApps: Boolean) -> Unit ) { val id = call.argument("id") ?: return result.error("1", "no receiver id provided", null) @@ -103,7 +115,10 @@ class MethodCallHandlerImpl( val names = call.argument>("names") ?: return result.error("1", "no names provided", null) - func(id, names) + val listenToBroadcastsFromOtherApps = call.argument("listenToBroadcastsFromOtherApps") + ?: return result.error("1", "listenToBroadcastsFromOtherApps is not provided", null) + + func(id, names, listenToBroadcastsFromOtherApps) } private fun withBroadcastArgs( @@ -118,8 +133,8 @@ class MethodCallHandlerImpl( } private fun onStartReceiver(call: MethodCall, result: Result) { - withReceiverArgs(call, result) { id, names -> - broadcastManager.startReceiver(CustomBroadcastReceiver(id, names) { broadcast -> + withReceiverArgs(call, result) { id, names, listenToBroadcastsFromOtherApps -> + broadcastManager.startReceiver(CustomBroadcastReceiver(id, names, listenToBroadcastsFromOtherApps) { broadcast -> channel?.invokeMethod("receiveBroadcast", broadcast) }) result.success(null) @@ -127,7 +142,7 @@ class MethodCallHandlerImpl( } private fun onStopReceiver(call: MethodCall, result: Result) { - withReceiverArgs(call, result) { id, _ -> + withReceiverArgs(call, result) { id, _, _ -> broadcastManager.stopReceiver(id) result.success(null) } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 31da576..ebf028f 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -36,8 +36,8 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "de.kevlatus.flutter_broadcasts_example" minSdkVersion 24 - targetSdkVersion 33 - compileSdkVersion 31 + targetSdkVersion 34 + compileSdkVersion 34 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 8c68fb8..cc1dff5 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -9,7 +9,8 @@ android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" - android:windowSoftInputMode="adjustResize"> + android:windowSoftInputMode="adjustResize" + android:exported="true">