Skip to content

Commit 889db83

Browse files
committed
[android_intent_plus]: Added a new argument to explicitly pass array values to an intent
1 parent 33b21a7 commit 889db83

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import android.provider.Settings;
88
import android.text.TextUtils;
99
import android.util.Log;
10+
1011
import androidx.annotation.NonNull;
1112
import androidx.annotation.Nullable;
13+
14+
import java.util.ArrayList;
15+
import java.util.Map;
16+
1217
import io.flutter.plugin.common.BinaryMessenger;
1318
import io.flutter.plugin.common.MethodCall;
1419
import io.flutter.plugin.common.MethodChannel;
1520
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
1621
import io.flutter.plugin.common.MethodChannel.Result;
17-
import java.util.ArrayList;
18-
import java.util.Map;
1922

2023
/** Forwards incoming {@link MethodCall}s to {@link IntentSender#send}. */
2124
public final class MethodCallHandlerImpl implements MethodCallHandler {
@@ -77,6 +80,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
7780
String category = call.argument("category");
7881
Uri data = call.argument("data") != null ? Uri.parse((String) call.argument("data")) : null;
7982
Bundle arguments = convertArguments((Map<String, ?>) call.argument("arguments"));
83+
Bundle arrayArguments = convertArrayArguments((Map<String, ?>) call.argument("arrayArguments"));
84+
arguments.putAll(arrayArguments);
8085
String packageName = call.argument("package");
8186
ComponentName componentName =
8287
(!TextUtils.isEmpty(packageName)
@@ -166,6 +171,51 @@ private static Bundle convertArguments(Map<String, ?> arguments) {
166171
return bundle;
167172
}
168173

174+
private static Bundle convertArrayArguments(Map<String, ?> arrayArguments) {
175+
Bundle bundle = new Bundle();
176+
if (arrayArguments == null) {
177+
return bundle;
178+
}
179+
for (String key : arrayArguments.keySet()) {
180+
Object value = arrayArguments.get(key);
181+
if (isTypedArrayList(value, Boolean.class)) {
182+
ArrayList<Boolean> list = (ArrayList<Boolean>) value;
183+
boolean[] array = new boolean[list.size()];
184+
for (int i = 0; i < list.size(); i++) {
185+
array[i] = list.get(i);
186+
}
187+
bundle.putBooleanArray(key, array);
188+
} else if (isTypedArrayList(value, Integer.class)) {
189+
ArrayList<Integer> list = (ArrayList<Integer>) value;
190+
int[] array = new int[list.size()];
191+
for (int i = 0; i < list.size(); i++) {
192+
array[i] = list.get(i);
193+
}
194+
bundle.putIntArray(key, array);
195+
} else if (isTypedArrayList(value, Long.class)) {
196+
ArrayList<Long> list = (ArrayList<Long>) value;
197+
long[] array = new long[list.size()];
198+
for (int i = 0; i < list.size(); i++) {
199+
array[i] = list.get(i);
200+
}
201+
bundle.putLongArray(key, array);
202+
} else if (isTypedArrayList(value, Double.class)) {
203+
ArrayList<Double> list = (ArrayList<Double>) value;
204+
double[] array = new double[list.size()];
205+
for (int i = 0; i < list.size(); i++) {
206+
array[i] = list.get(i);
207+
}
208+
bundle.putDoubleArray(key, array);
209+
} else if (isTypedArrayList(value, String.class)) {
210+
ArrayList<String> list = (ArrayList<String>) value;
211+
bundle.putStringArray(key, list.toArray(new String[list.size()]));
212+
} else {
213+
throw new UnsupportedOperationException("Unsupported type " + value);
214+
}
215+
}
216+
return bundle;
217+
}
218+
169219
private static boolean isTypedArrayList(Object value, Class<?> type) {
170220
if (!(value instanceof ArrayList)) {
171221
return false;

packages/android_intent_plus/example/lib/main.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,29 @@ class ExplicitIntentsWidget extends StatelessWidget {
184184
intent.launch();
185185
}
186186

187+
void _openGmail() {
188+
const intent = AndroidIntent(
189+
action: 'android.intent.action.SEND',
190+
arguments: {'android.intent.extra.SUBJECT': 'I am the subject'},
191+
arrayArguments: {
192+
'android.intent.extra.EMAIL': ['eidac@me.com', 'overbom@mac.com'],
193+
'android.intent.extra.CC': ['john@app.com', 'user@app.com'],
194+
'android.intent.extra.BCC': ['liam@me.abc', 'abel@me.com'],
195+
},
196+
package: 'com.google.android.gm',
197+
type: 'message/rfc822',
198+
);
199+
intent.launch();
200+
}
201+
187202
@override
188203
Widget build(BuildContext context) {
189204
return Scaffold(
190205
appBar: AppBar(
191206
title: const Text('Test explicit intents'),
192207
),
193208
body: Center(
194-
child: Padding(
209+
child: SingleChildScrollView(
195210
padding: const EdgeInsets.symmetric(vertical: 15.0),
196211
child: Column(
197212
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
@@ -235,6 +250,12 @@ class ExplicitIntentsWidget extends StatelessWidget {
235250
'Tap here to open Application Details',
236251
),
237252
),
253+
ElevatedButton(
254+
onPressed: _openGmail,
255+
child: const Text(
256+
'Tap here to open gmail app with details',
257+
),
258+
),
238259
],
239260
),
240261
),

packages/android_intent_plus/lib/android_intent.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class AndroidIntent {
2424
/// intent.
2525
/// [arguments] is the map that will be converted into an extras bundle and
2626
/// passed to the intent.
27+
/// [arrayArguments] is a map that will be converted into an extra bundle
28+
/// as in an array and passed to the intent.
2729
/// [package] refers to the package parameter of the intent, can be null.
2830
/// [componentName] refers to the component name of the intent, can be null.
2931
/// If not null, then [package] but also be provided.
@@ -34,6 +36,7 @@ class AndroidIntent {
3436
this.category,
3537
this.data,
3638
this.arguments,
39+
this.arrayArguments,
3740
this.package,
3841
this.componentName,
3942
Platform? platform,
@@ -54,6 +57,7 @@ class AndroidIntent {
5457
this.category,
5558
this.data,
5659
this.arguments,
60+
this.arrayArguments,
5761
this.package,
5862
this.componentName,
5963
this.type,
@@ -87,9 +91,17 @@ class AndroidIntent {
8791
/// The equivalent of `extras`, a generic `Bundle` of data that the Intent can
8892
/// carry. This is a slot for extraneous data that the listener may use.
8993
///
94+
/// If the argument contains a list value, then the value will be put in as an
95+
/// array list.
96+
///
9097
/// See https://developer.android.com/reference/android/content/Intent.html#intent-structure.
9198
final Map<String, dynamic>? arguments;
9299

100+
/// Similar to [arguments], but in this case the arguments are an array and
101+
/// will be added to the intent as in an array extra instead of of an array
102+
/// list.
103+
final Map<String, List<dynamic>>? arrayArguments;
104+
93105
/// Sets the [data] to only resolve within this given package.
94106
///
95107
/// See https://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String).
@@ -190,6 +202,7 @@ class AndroidIntent {
190202
if (category != null) 'category': category,
191203
if (data != null) 'data': data,
192204
if (arguments != null) 'arguments': arguments,
205+
if (arrayArguments != null) 'arrayArguments': arrayArguments,
193206
if (package != null) ...{
194207
'package': package,
195208
if (componentName != null) 'componentName': componentName,

0 commit comments

Comments
 (0)