Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 14 Support (#1) #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
compileSdkVersion 34

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 24
minSdkVersion 26
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import java.io.Serializable
class CustomBroadcastReceiver(
val id: Int,
private val names: List<String>,
private val exported: Int,
private val listener: (Any) -> Unit
) : BroadcastReceiver() {
companion object {
Expand Down Expand Up @@ -46,7 +47,11 @@ class CustomBroadcastReceiver(
}

fun start(context: Context) {
context.registerReceiver(this, intentFilter)
if (exported == 1) {
context.registerReceiver(this, intentFilter, Context.RECEIVER_EXPORTED)
} else {
context.registerReceiver(this, intentFilter, Context.RECEIVER_NOT_EXPORTED)
}
Log.d(TAG, "starting to listen for broadcasts: " + names.joinToString(";"))
}

Expand Down Expand Up @@ -95,15 +100,17 @@ class MethodCallHandlerImpl(
private fun withReceiverArgs(
call: MethodCall,
result: Result,
func: (id: Int, names: List<String>) -> Unit
func: (id: Int, names: List<String>, exported: Int) -> Unit
) {
val id = call.argument<Int>("id")
?: return result.error("1", "no receiver id provided", null)

val names = call.argument<List<String>>("names")
?: return result.error("1", "no names provided", null)

func(id, names)
val exported = call.argument<Int>("exported") ?: 0

func(id, names, exported)
}

private fun withBroadcastArgs(
Expand All @@ -118,16 +125,16 @@ 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, exported ->
broadcastManager.startReceiver(CustomBroadcastReceiver(id, names, exported) { broadcast ->
channel?.invokeMethod("receiveBroadcast", broadcast)
})
result.success(null)
}
}

private fun onStopReceiver(call: MethodCall, result: Result) {
withReceiverArgs(call, result) { id, _ ->
withReceiverArgs(call, result) { id, _, _ ->
broadcastManager.stopReceiver(id)
result.success(null)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/receiver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class BroadcastReceiver {
final List<String> names;

StreamSubscription? _subscription;
final bool exported;

/// Creates a new [BroadcastReceiver], which subscribes to the given [names].
///
/// At least one name needs to be provided.
BroadcastReceiver({required this.names})
BroadcastReceiver({required this.names, this.exported = false})
: assert(names.length > 0),
_id = ++_index;

Expand Down Expand Up @@ -63,6 +64,7 @@ class BroadcastReceiver {
Map<String, dynamic> toMap() => <String, dynamic>{
'id': _id,
'names': names,
'exported': exported ? 1 : 0,
};

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_broadcasts
description: A plugin for sending and receiving broadcasts with Android intents and iOS notifications.
version: 0.4.0
version: 0.4.1
repository: https://github.com/kevlatus/flutter_broadcasts

environment:
Expand Down
Loading