Skip to content
Merged
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: 4 additions & 0 deletions packages/android_intent_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## pre-release

- Add launchChooser method, which uses Intent.createChooser internally.

## 0.4.1

- Renamed Method Channel and changed Java package to avoid collision with android_intent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ void send(Intent intent) {
}
}

/**
* Like with {@code send}, creates and launches an intent with the given params, but wraps the
* {@code Intent} with {@code Intent.createChooser}.
*/
public void launchChooser(Intent intent, String title) {
send(Intent.createChooser(intent, title));
}

/**
* Verifies the given intent and returns whether the application context class can resolve it.
*
Expand All @@ -68,9 +76,9 @@ void send(Intent intent) {
* <p>This currently only supports resolving activities.
*
* @param intent Fully built intent.
* @see #buildIntent(String, Integer, String, Uri, Bundle, String, ComponentName, String)
* @return Whether the package manager found {@link android.content.pm.ResolveInfo} using its
* {@link PackageManager#resolveActivity(Intent, int)} method.
* @see #buildIntent(String, Integer, String, Uri, Bundle, String, ComponentName, String)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the java formatter moved this line

*/
boolean canResolveActivity(Intent intent) {
if (applicationContext == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if ("launch".equalsIgnoreCase(call.method)) {
sender.send(intent);

result.success(null);
} else if ("launchChooser".equalsIgnoreCase(call.method)) {
String title = call.argument("chooserTitle");
sender.launchChooser(intent, title);
result.success(null);
} else if ("canResolveActivity".equalsIgnoreCase(call.method)) {
result.success(sender.canResolveActivity(intent));
Expand Down
22 changes: 19 additions & 3 deletions packages/android_intent_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ class MyHomePage extends StatelessWidget {
onPressed: _createAlarm,
),
RaisedButton(
child: const Text('Tap here to test explicit intents.'),
onPressed: () => _openExplicitIntentsView(context)),
child: const Text(
'Tap here to launch Intent with Chooser',
),
onPressed: _openChooser,
),
RaisedButton(
child: const Text('Tap here to test explicit intents.'),
onPressed: () => _openExplicitIntentsView(context),
),
],
),
);
Expand All @@ -80,6 +87,15 @@ class MyHomePage extends StatelessWidget {
body: Center(child: body),
);
}

void _openChooser() {
final intent = const AndroidIntent(
action: 'android.intent.action.SEND',
type: 'plain/text',
data: 'text example',
);
intent.launchChooser('Chose an app');
}
}

/// Launches intents to specific Android activities.
Expand Down Expand Up @@ -204,7 +220,7 @@ class ExplicitIntentsWidget extends StatelessWidget {
'Tap here to open Application Details',
),
onPressed: _openApplicationDetails,
)
),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
(Widget widget) =>
widget is Text && widget.data.startsWith('Tap here'),
),
findsNWidgets(2),
findsNWidgets(3),
);
} else {
expect(
Expand All @@ -49,6 +49,15 @@ void main() {
}));
}, skip: !Platform.isAndroid);

testWidgets('#launchChooser should not throw', (WidgetTester tester) async {
final intent = const AndroidIntent(
action: 'android.intent.action.SEND',
type: 'plain/text',
data: 'text example',
);
await intent.launchChooser('title');
}, skip: !Platform.isAndroid);

testWidgets('#canResolveActivity returns true when example Activity is found',
(WidgetTester tester) async {
final intent = AndroidIntent(
Expand Down
16 changes: 16 additions & 0 deletions packages/android_intent_plus/lib/android_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ class AndroidIntent {
await _channel.invokeMethod<void>('launch', _buildArguments());
}

/// Launch the intent with 'createChooser(intent, title)'.
///
/// This works only on Android platforms.
Future<void> launchChooser(String title) async {
if (!_platform.isAndroid) {
return;
}

final buildArguments = _buildArguments();
buildArguments['chooserTitle'] = title;
await _channel.invokeMethod<void>(
'launchChooser',
buildArguments,
);
}

/// Check whether the intent can be resolved to an activity.
///
/// This works only on Android platforms.
Expand Down
15 changes: 15 additions & 0 deletions packages/android_intent_plus/test/android_intent_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ void main() {
verifyZeroInteractions(mockChannel);
});
});

group('launchChooser', () {
test('pass title', () async {
androidIntent = AndroidIntent.private(
action: 'action_view',
channel: mockChannel,
platform: FakePlatform(operatingSystem: 'android'),
);
await androidIntent.launchChooser('title');
verify(mockChannel.invokeMethod<void>('launchChooser', <String, Object>{
'action': 'action_view',
'chooserTitle': 'title',
}));
});
});
});

group('convertFlags ', () {
Expand Down