Skip to content

Commit 7a16808

Browse files
committed
Implemented android_content.Intent#startActivity().
1 parent eea9c77 commit 7a16808

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [0.5.1] - 2020-08-xx
8+
## [0.6.0] - 2020-08-18
9+
10+
### Added
11+
12+
- `android_content.Intent#package` property
13+
- `android_content.Intent#startActivity()` method
914

1015
## [0.5.0] - 2020-08-01
1116

@@ -277,7 +282,7 @@ No functional changes.
277282
- `android_content.Context.packageName` getter
278283
- `android_content.Context.packageResourcePath` getter
279284

280-
[0.5.1]: https://github.com/drydart/flutter_android/compare/0.5.0...0.5.1
285+
[0.6.0]: https://github.com/drydart/flutter_android/compare/0.5.0...0.6.0
281286
[0.5.0]: https://github.com/drydart/flutter_android/compare/0.4.0...0.5.0
282287
[0.4.0]: https://github.com/drydart/flutter_android/compare/0.3.7...0.4.0
283288
[0.3.7]: https://github.com/drydart/flutter_android/compare/0.3.6...0.3.7
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* This is free and unencumbered software released into the public domain. */
2+
3+
package com.github.drydart.flutter_android;
4+
5+
import android.content.ComponentName;
6+
import android.content.Context;
7+
import android.content.Intent;
8+
import android.net.Uri;
9+
import androidx.annotation.NonNull;
10+
import java.io.Serializable;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
15+
import io.flutter.plugin.common.MethodCall;
16+
import io.flutter.plugin.common.MethodChannel.Result;
17+
18+
/** IntentHandler */
19+
class IntentHandler extends FlutterMethodCallHandler {
20+
static final String CHANNEL = "flutter_android/Intent";
21+
22+
IntentHandler(final @NonNull FlutterPlugin.FlutterPluginBinding binding) {
23+
super(binding);
24+
}
25+
26+
@Override
27+
public void onMethodCall(final MethodCall call, final Result result) {
28+
assert(call != null);
29+
assert(result != null);
30+
31+
final Context context = this.binding.getApplicationContext();
32+
assert(context != null);
33+
34+
assert(call.method != null);
35+
switch (call.method) {
36+
case "startActivity": {
37+
final String action = getOptionalArgument(call, "action");
38+
final String data = getOptionalArgument(call, "data");
39+
final List<String> categories = getOptionalArgument(call, "categories");
40+
final String type = getOptionalArgument(call, "type");
41+
final String component = getOptionalArgument(call, "component");
42+
final Map<String, Object> extras = getOptionalArgument(call, "extras");
43+
final Integer flags = getOptionalArgument(call, "flags");
44+
final String package = getOptionalArgument(call, "package");
45+
46+
final Intent intent = new Intent();
47+
if (action != null && !action.isEmpty()) {
48+
intent.setAction(action);
49+
}
50+
if (data != null && type != null) {
51+
intent.setDataAndTypeAndNormalize(Uri.parse(data), type);
52+
}
53+
else if (data != null && type == null) {
54+
intent.setDataAndNormalize(Uri.parse(data));
55+
}
56+
else if (data == null && type != null) {
57+
intent.setTypeAndNormalize(type);
58+
}
59+
if (categories != null) {
60+
for (final String category : categories) {
61+
if (!category.isEmpty()) {
62+
intent.addCategory(category);
63+
}
64+
}
65+
}
66+
if (component != null && !component.isEmpty()) {
67+
intent.setComponent(ComponentName.unflattenFromString(component));
68+
}
69+
if (extras != null) {
70+
for (Map.Entry<String, Object> entry : extras.entrySet()) {
71+
if (entry.getValue() instanceof Serializable) {
72+
intent.putExtra(entry.getKey(), (Serializable)entry.getValue());
73+
}
74+
}
75+
}
76+
if (flags != null) {
77+
intent.addFlags(flags);
78+
}
79+
if (package != null && !package.isEmpty()) {
80+
intent.setPackage(package);
81+
}
82+
context.startActivity(intent);
83+
result.success(null);
84+
break;
85+
}
86+
default: {
87+
result.notImplemented();
88+
}
89+
}
90+
}
91+
}

android/src/main/java/com/github/drydart/flutter_android/FlutterAndroidPlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public void onAttachedToEngine(final @NonNull FlutterPlugin.FlutterPluginBinding
5252
(new MethodChannel(messenger, FaceDetectorHandler.CHANNEL))
5353
.setMethodCallHandler(new FaceDetectorHandler(binding));
5454

55+
(new MethodChannel(messenger, IntentHandler.CHANNEL))
56+
.setMethodCallHandler(new IntentHandler(binding));
57+
5558
(new MethodChannel(messenger, LocationHandler.CHANNEL))
5659
.setMethodCallHandler(new LocationHandler(binding));
5760

lib/src/content/intent.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* This is free and unencumbered software released into the public domain. */
22

3+
import 'dart:async' show Future;
4+
5+
import 'package:flutter/services.dart' show MethodChannel;
6+
37
import '../os/bundle.dart' show Bundle;
48
import '../os/parcelable.dart' show Parcelable;
59
import 'component_name.dart';
@@ -14,6 +18,9 @@ import 'component_name.dart';
1418
///
1519
/// See: https://developer.android.com/reference/android/content/Intent
1620
class Intent implements Parcelable {
21+
static const MethodChannel _channel =
22+
MethodChannel('flutter_android/Intent');
23+
1724
/// The general action to be performed.
1825
final String action;
1926

@@ -37,6 +44,9 @@ class Intent implements Parcelable {
3744
/// See: https://developer.android.com/reference/android/content/Intent#flags
3845
final int flags;
3946

47+
/// Specifies the application package name this intent is limited to.
48+
final String package;
49+
4050
Intent({
4151
this.action,
4252
this.data,
@@ -45,10 +55,28 @@ class Intent implements Parcelable {
4555
this.component,
4656
this.extras,
4757
this.flags,
58+
this.package,
4859
});
4960

5061
/// Gives additional information about the action to execute.
5162
///
5263
/// If this intent has multiple categories, returns the first of them.
5364
String get category => categories.isNotEmpty ? categories.first : null;
65+
66+
/// Launches a new activity.
67+
///
68+
/// See: https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent)
69+
Future<void> startActivity() async {
70+
final request = <String, dynamic>{
71+
'action': action,
72+
'data': data.toString(),
73+
'categories': categories,
74+
'type': type,
75+
'component': component.flattenToString(),
76+
'extras': extras.mappings,
77+
'flags': flags,
78+
'package': package,
79+
};
80+
await _channel.invokeMethod('startActivity', request);
81+
}
5482
}

0 commit comments

Comments
 (0)