Skip to content

Commit

Permalink
[android_intent] support for setType() and setDataAndType() (flutter#…
Browse files Browse the repository at this point in the history
…1983)

Added support for [setType](https://developer.android.com/reference/android/content/Intent.html#setType(java.lang.String)) and [setDataAndType](https://developer.android.com/reference/android/content/Intent.html#setDataAndType(android.net.Uri,%20java.lang.String)) parameters. 
This is needed if IE. you want to send intent to stream video. You would need to set both data (some http Uri) and mime type (ie. 'video/*'). Keep in mind, Intent documentation specifies

> To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().
> 
> Caution: If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
  • Loading branch information
shaxxx authored and Michael Klimushyn committed Jan 13, 2020
1 parent ee5f424 commit a9f97e8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/android_intent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.5

* Add support for [setType](https://developer.android.com/reference/android/content/Intent.html#setType(java.lang.String)) and [setDataAndType](https://developer.android.com/reference/android/content/Intent.html#setDataAndType(android.net.Uri,%20java.lang.String)) parameters.

## 0.3.4+8

* Remove the deprecated `author:` field from pubspec.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ public IntentSender(@Nullable Activity activity, @Nullable Context applicationCo
* @param action the Intent action, such as {@code ACTION_VIEW}.
* @param flags forwarded to {@link Intent#addFlags(int)} if non-null.
* @param category forwarded to {@link Intent#addCategory(String)} if non-null.
* @param data forwarded to {@link Intent#setData(Uri)} if non-null.
* @param data forwarded to {@link Intent#setData(Uri)} if non-null and 'type' parameter is null.
* If both 'data' and 'type' is non-null they're forwarded to {@link
* Intent#setDataAndType(Uri, String)}
* @param arguments forwarded to {@link Intent#putExtras(Bundle)} if non-null.
* @param packageName forwarded to {@link Intent#setPackage(String)} if non-null. This is forced
* to null if it can't be resolved.
* @param componentName forwarded to {@link Intent#setComponent(ComponentName)} if non-null.
* @param type forwarded to {@link Intent#setType(String)} if non-null and 'data' parameter is
* null. If both 'data' and 'type' is non-null they're forwarded to {@link
* Intent#setDataAndType(Uri, String)}
*/
void send(
String action,
Expand All @@ -58,7 +63,8 @@ void send(
@Nullable Uri data,
@Nullable Bundle arguments,
@Nullable String packageName,
@Nullable ComponentName componentName) {
@Nullable ComponentName componentName,
@Nullable String type) {
if (applicationContext == null) {
Log.wtf(TAG, "Trying to send an intent before the applicationContext was initialized.");
return;
Expand All @@ -72,9 +78,15 @@ void send(
if (!TextUtils.isEmpty(category)) {
intent.addCategory(category);
}
if (data != null) {
if (data != null && type == null) {
intent.setData(data);
}
if (type != null && data == null) {
intent.setType(type);
}
if (type != null && data != null) {
intent.setDataAndType(data, type);
}
if (arguments != null) {
intent.putExtras(arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
&& !TextUtils.isEmpty((String) call.argument("componentName")))
? new ComponentName(packageName, (String) call.argument("componentName"))
: null;
String type = call.argument("type");

sender.send(action, flags, category, data, arguments, packageName, componentName);
sender.send(action, flags, category, data, arguments, packageName, componentName, type);

result.success(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,41 @@ public void onMethodCall_setsComponentName() {
assertEquals(expectedComponent.getPackageName(), intent.getPackage());
assertEquals(expectedComponent.flattenToString(), intent.getComponent().flattenToString());
}

@Test
public void onMethodCall_setsType() {
sender.setApplicationContext(context);
Map<String, Object> args = new HashMap<>();
args.put("action", "foo");
String type = "video/*";
args.put("type", type);
Result result = mock(Result.class);

methodCallHandler.onMethodCall(new MethodCall("launch", args), result);

verify(result, times(1)).success(null);
Intent intent = shadowOf((Application) context).getNextStartedActivity();
assertNotNull(intent);
assertEquals(type, intent.getType());
}

@Test
public void onMethodCall_setsDataAndType() {
sender.setApplicationContext(context);
Map<String, Object> args = new HashMap<>();
args.put("action", "foo");
Uri data = Uri.parse("http://flutter.dev");
args.put("data", data.toString());
String type = "video/*";
args.put("type", type);
Result result = mock(Result.class);

methodCallHandler.onMethodCall(new MethodCall("launch", args), result);

verify(result, times(1)).success(null);
Intent intent = shadowOf((Application) context).getNextStartedActivity();
assertNotNull(intent);
assertEquals(type, intent.getType());
assertEquals(data, intent.getData());
}
}
11 changes: 11 additions & 0 deletions packages/android_intent/lib/android_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AndroidIntent {
/// [package] refers to the package parameter of the intent, can be null.
/// [componentName] refers to the component name of the intent, can be null.
/// If not null, then [package] but also be provided.
/// [type] refers to the type of the intent, can be null.
const AndroidIntent({
@required this.action,
this.flags,
Expand All @@ -36,6 +37,7 @@ class AndroidIntent {
this.package,
this.componentName,
Platform platform,
this.type,
}) : assert(action != null),
_channel = const MethodChannel(_kChannelName),
_platform = platform ?? const LocalPlatform();
Expand All @@ -53,6 +55,7 @@ class AndroidIntent {
this.arguments,
this.package,
this.componentName,
this.type,
}) : _channel = channel,
_platform = platform;

Expand Down Expand Up @@ -97,6 +100,11 @@ class AndroidIntent {
final MethodChannel _channel;
final Platform _platform;

/// Set an explicit MIME data type.
///
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
final String type;

bool _isPowerOfTwo(int x) {
/* First x in the below expression is for the case when x is 0 */
return x != 0 && ((x & (x - 1)) == 0);
Expand Down Expand Up @@ -142,6 +150,9 @@ class AndroidIntent {
args['componentName'] = componentName;
}
}
if (type != null) {
args['type'] = type;
}
await _channel.invokeMethod<void>('launch', args);
}
}
2 changes: 1 addition & 1 deletion packages/android_intent/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: android_intent
description: Flutter plugin for launching Android Intents. Not supported on iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/android_intent
version: 0.3.4+8
version: 0.3.5

flutter:
plugin:
Expand Down
4 changes: 3 additions & 1 deletion packages/android_intent/test/android_intent_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ void main() {
data: Uri.encodeFull('https://flutter.io'),
flags: <int>[Flag.FLAG_ACTIVITY_NEW_TASK],
channel: mockChannel,
platform: FakePlatform(operatingSystem: 'android'));
platform: FakePlatform(operatingSystem: 'android'),
type: 'video/*');
await androidIntent.launch();
verify(mockChannel.invokeMethod<void>('launch', <String, Object>{
'action': 'action_view',
'data': Uri.encodeFull('https://flutter.io'),
'flags': androidIntent.convertFlags(<int>[Flag.FLAG_ACTIVITY_NEW_TASK]),
'type': 'video/*',
}));
});
test('pass null value to action param', () async {
Expand Down

0 comments on commit a9f97e8

Please sign in to comment.