diff --git a/packages/android_intent_plus/CHANGELOG.md b/packages/android_intent_plus/CHANGELOG.md index b6348c264a..41b5d19637 100644 --- a/packages/android_intent_plus/CHANGELOG.md +++ b/packages/android_intent_plus/CHANGELOG.md @@ -1,6 +1,7 @@ -## pre-release +## 0.4.2 - Add launchChooser method, which uses Intent.createChooser internally. +- Add sendBroadcast method, which uses context.sendBroadcast() internally. ## 0.4.1 diff --git a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java index d66e8a0269..aa909ea7ce 100644 --- a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java +++ b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java @@ -67,6 +67,17 @@ public void launchChooser(Intent intent, String title) { send(Intent.createChooser(intent, title)); } + /** Creates an intent and sends it as Broadcast. */ + public void sendBroadcast(Intent intent) { + if (applicationContext == null) { + Log.wtf(TAG, "Trying to send broadcast before the applicationContext was initialized."); + return; + } + + Log.v(TAG, "Sending broadcast " + intent); + applicationContext.sendBroadcast(intent); + } + /** * Verifies the given intent and returns whether the application context class can resolve it. * diff --git a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java index 46ca514cd7..7200e0528f 100644 --- a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java +++ b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java @@ -97,6 +97,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { String title = call.argument("chooserTitle"); sender.launchChooser(intent, title); result.success(null); + } else if ("sendBroadcast".equalsIgnoreCase(call.method)) { + sender.sendBroadcast(intent); + result.success(null); } else if ("canResolveActivity".equalsIgnoreCase(call.method)) { result.success(sender.canResolveActivity(intent)); } else { diff --git a/packages/android_intent_plus/example/android/app/build.gradle b/packages/android_intent_plus/example/android/app/build.gradle index 48178f2be0..7862f2a7af 100644 --- a/packages/android_intent_plus/example/android/app/build.gradle +++ b/packages/android_intent_plus/example/android/app/build.gradle @@ -54,6 +54,7 @@ flutter { } dependencies { + implementation 'androidx.appcompat:appcompat:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' diff --git a/packages/android_intent_plus/example/android/app/src/main/AndroidManifest.xml b/packages/android_intent_plus/example/android/app/src/main/AndroidManifest.xml index 761c35fd64..c955419551 100644 --- a/packages/android_intent_plus/example/android/app/src/main/AndroidManifest.xml +++ b/packages/android_intent_plus/example/android/app/src/main/AndroidManifest.xml @@ -1,43 +1,51 @@ + - + + + + + android:label="android_intent_example"> - - - + + - - + + + - + + + + + + + + - - - diff --git a/packages/android_intent_plus/example/android/app/src/main/java/io/flutter/plugins/androidintentexample/MainActivity.java b/packages/android_intent_plus/example/android/app/src/main/java/io/flutter/plugins/androidintentexample/MainActivity.java new file mode 100644 index 0000000000..0c26573915 --- /dev/null +++ b/packages/android_intent_plus/example/android/app/src/main/java/io/flutter/plugins/androidintentexample/MainActivity.java @@ -0,0 +1,21 @@ +package io.flutter.plugins.androidintentexample; + +import android.content.IntentFilter; +import android.os.Bundle; +import dev.fluttercommunity.plus.androidintent.AndroidIntentPlugin; +import io.flutter.app.FlutterActivity; + +public class MainActivity extends FlutterActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + AndroidIntentPlugin.registerWith( + registrarFor("dev.fluttercommunity.plus.androidintent.AndroidIntentPlugin")); + + IntentFilter filter = new IntentFilter("com.example.broadcast"); + MyBroadcastReceiver receiver = new MyBroadcastReceiver(); + registerReceiver(receiver, filter); + } +} diff --git a/packages/android_intent_plus/example/android/app/src/main/java/io/flutter/plugins/androidintentexample/MyBroadcastReceiver.java b/packages/android_intent_plus/example/android/app/src/main/java/io/flutter/plugins/androidintentexample/MyBroadcastReceiver.java new file mode 100644 index 0000000000..ab66f6759f --- /dev/null +++ b/packages/android_intent_plus/example/android/app/src/main/java/io/flutter/plugins/androidintentexample/MyBroadcastReceiver.java @@ -0,0 +1,15 @@ +package io.flutter.plugins.androidintentexample; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; +import android.widget.Toast; + +public class MyBroadcastReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + Log.d("MyBroadcastReceiver", "Got Intent: " + intent.toString()); + Toast.makeText(context, "Broadcast Received!", Toast.LENGTH_LONG).show(); + } +} diff --git a/packages/android_intent_plus/example/android/app/src/main/res/values/strings.xml b/packages/android_intent_plus/example/android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000000..7abc06d3ba --- /dev/null +++ b/packages/android_intent_plus/example/android/app/src/main/res/values/strings.xml @@ -0,0 +1 @@ + diff --git a/packages/android_intent_plus/example/lib/main.dart b/packages/android_intent_plus/example/lib/main.dart index 9824f5af5b..352daf1f9e 100644 --- a/packages/android_intent_plus/example/lib/main.dart +++ b/packages/android_intent_plus/example/lib/main.dart @@ -65,11 +65,13 @@ class MyHomePage extends StatelessWidget { onPressed: _createAlarm, ), RaisedButton( - child: const Text( - 'Tap here to launch Intent with Chooser', - ), + child: const Text('Tap here to launch Intent with Chooser'), onPressed: _openChooser, ), + RaisedButton( + child: const Text('Tap here to send Intent as broadcast'), + onPressed: _sendBroadcast, + ), RaisedButton( child: const Text('Tap here to test explicit intents.'), onPressed: () => _openExplicitIntentsView(context), @@ -96,6 +98,13 @@ class MyHomePage extends StatelessWidget { ); intent.launchChooser('Chose an app'); } + + void _sendBroadcast() { + final intent = const AndroidIntent( + action: 'com.example.broadcast', + ); + intent.sendBroadcast(); + } } /// Launches intents to specific Android activities. diff --git a/packages/android_intent_plus/example/test_driver/android_intent_plus_e2e.dart b/packages/android_intent_plus/example/test_driver/android_intent_plus_e2e.dart index b6d9aed169..7e82d5c662 100644 --- a/packages/android_intent_plus/example/test_driver/android_intent_plus_e2e.dart +++ b/packages/android_intent_plus/example/test_driver/android_intent_plus_e2e.dart @@ -24,7 +24,7 @@ void main() { (Widget widget) => widget is Text && widget.data.startsWith('Tap here'), ), - findsNWidgets(3), + findsNWidgets(4), ); } else { expect( @@ -58,6 +58,13 @@ void main() { await intent.launchChooser('title'); }, skip: !Platform.isAndroid); + testWidgets('#sendBroadcast should not throw', (WidgetTester tester) async { + final intent = const AndroidIntent( + action: 'com.example.broadcast', + ); + await intent.sendBroadcast(); + }, skip: !Platform.isAndroid); + testWidgets('#canResolveActivity returns true when example Activity is found', (WidgetTester tester) async { final intent = AndroidIntent( diff --git a/packages/android_intent_plus/lib/android_intent.dart b/packages/android_intent_plus/lib/android_intent.dart index b939f0e375..c35089e18d 100644 --- a/packages/android_intent_plus/lib/android_intent.dart +++ b/packages/android_intent_plus/lib/android_intent.dart @@ -154,6 +154,20 @@ class AndroidIntent { ); } + /// Sends intent as broadcast. + /// + /// This works only on Android platforms. + Future sendBroadcast() async { + if (!_platform.isAndroid) { + return; + } + + await _channel.invokeMethod( + 'sendBroadcast', + _buildArguments(), + ); + } + /// Check whether the intent can be resolved to an activity. /// /// This works only on Android platforms. diff --git a/packages/android_intent_plus/pubspec.yaml b/packages/android_intent_plus/pubspec.yaml index 78973fb850..4d1ef5a977 100644 --- a/packages/android_intent_plus/pubspec.yaml +++ b/packages/android_intent_plus/pubspec.yaml @@ -1,6 +1,6 @@ name: android_intent_plus description: Flutter plugin for launching Android Intents. Not supported on iOS. -version: 0.4.1 +version: 0.4.2 homepage: https://plus.fluttercommunity.dev/ repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/ diff --git a/packages/android_intent_plus/test/android_intent_test.dart b/packages/android_intent_plus/test/android_intent_test.dart index d9c94e1a80..50b34b3c3b 100644 --- a/packages/android_intent_plus/test/android_intent_test.dart +++ b/packages/android_intent_plus/test/android_intent_test.dart @@ -155,6 +155,20 @@ void main() { })); }); }); + + group('sendBroadcast', () { + test('send a broadcast', () async { + androidIntent = AndroidIntent.private( + action: 'com.example.broadcast', + channel: mockChannel, + platform: FakePlatform(operatingSystem: 'android'), + ); + await androidIntent.sendBroadcast(); + verify(mockChannel.invokeMethod('sendBroadcast', { + 'action': 'com.example.broadcast', + })); + }); + }); }); group('convertFlags ', () {