Skip to content

Commit 2da5a5d

Browse files
mvanbeusekomJDDV
andauthored
Feature/android support (#16)
* Add Android + Foundation generation and Dart implementation * Add ios project to example * Remove ios * Add ios to example * Add iOS native implementation * Add Android native implementation * Bump version * Fix formatting * Expose NSObject and JavaObject * Fix formatting * Added foundation-implementation to umbrella file, updated FLTObject to use super init, not self init (#17) --------- Co-authored-by: Jan-Derk de Vries <47666699+JDDV@users.noreply.github.com>
1 parent 4a409eb commit 2da5a5d

28 files changed

+1399
-90
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.3.0
2+
3+
* Adds support for automatic object clean up for Android and iOS.
4+
* Android: Implement the JObject (Dart) classes.
5+
* iOS: Implement the NSObject (Dart) and FLTObject (native) classes.
6+
17
# 0.2.0
28

39
* Adds iOS support for the instance manager.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
// Autogenerated from Pigeon (v11.0.1), do not edit directly.
5+
// See also: https://pub.dev/packages/pigeon
6+
7+
package com.baseflow.instancemanager;
8+
9+
import android.util.Log;
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import io.flutter.plugin.common.BasicMessageChannel;
13+
import io.flutter.plugin.common.BinaryMessenger;
14+
import io.flutter.plugin.common.MessageCodec;
15+
import io.flutter.plugin.common.StandardMessageCodec;
16+
import java.io.ByteArrayOutputStream;
17+
import java.nio.ByteBuffer;
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
/** Generated class from Pigeon. */
26+
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression", "serial"})
27+
public class AndroidInstanceManagerPigeon {
28+
29+
/** Error class for passing custom error details to Flutter via a thrown PlatformException. */
30+
public static class FlutterError extends RuntimeException {
31+
32+
/** The error code. */
33+
public final String code;
34+
35+
/** The error details. Must be a datatype supported by the api codec. */
36+
public final Object details;
37+
38+
public FlutterError(@NonNull String code, @Nullable String message, @Nullable Object details)
39+
{
40+
super(message);
41+
this.code = code;
42+
this.details = details;
43+
}
44+
}
45+
46+
@NonNull
47+
protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
48+
ArrayList<Object> errorList = new ArrayList<Object>(3);
49+
if (exception instanceof FlutterError) {
50+
FlutterError error = (FlutterError) exception;
51+
errorList.add(error.code);
52+
errorList.add(error.getMessage());
53+
errorList.add(error.details);
54+
} else {
55+
errorList.add(exception.toString());
56+
errorList.add(exception.getClass().getSimpleName());
57+
errorList.add(
58+
"Cause: " + exception.getCause() + ", Stacktrace: " + Log.getStackTraceString(exception));
59+
}
60+
return errorList;
61+
}
62+
/**
63+
* Host API for managing the native `InstanceManager`.
64+
*
65+
* Generated interface from Pigeon that represents a handler of messages from Flutter.
66+
*/
67+
public interface AndroidInstanceManagerHostApi {
68+
/**
69+
* Clear the native `InstanceManager`.
70+
*
71+
* This is typically only used after a hot restart.
72+
*/
73+
void clear();
74+
75+
/** The codec used by AndroidInstanceManagerHostApi. */
76+
static @NonNull MessageCodec<Object> getCodec() {
77+
return new StandardMessageCodec();
78+
}
79+
/**Sets up an instance of `AndroidInstanceManagerHostApi` to handle messages through the `binaryMessenger`. */
80+
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable AndroidInstanceManagerHostApi api) {
81+
{
82+
BasicMessageChannel<Object> channel =
83+
new BasicMessageChannel<>(
84+
binaryMessenger, "dev.flutter.pigeon.flutter_instance_manager.AndroidInstanceManagerHostApi.clear", getCodec());
85+
if (api != null) {
86+
channel.setMessageHandler(
87+
(message, reply) -> {
88+
ArrayList<Object> wrapped = new ArrayList<Object>();
89+
try {
90+
api.clear();
91+
wrapped.add(0, null);
92+
}
93+
catch (Throwable exception) {
94+
ArrayList<Object> wrappedError = wrapError(exception);
95+
wrapped = wrappedError;
96+
}
97+
reply.reply(wrapped);
98+
});
99+
} else {
100+
channel.setMessageHandler(null);
101+
}
102+
}
103+
}
104+
}
105+
/**
106+
* Handles methods calls to the native Java Object class.
107+
*
108+
* Also handles calls to remove the reference to an instance with `dispose`.
109+
*
110+
* See https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html.
111+
*
112+
* Generated interface from Pigeon that represents a handler of messages from Flutter.
113+
*/
114+
public interface JavaObjectHostApi {
115+
116+
void dispose(@NonNull String identifier);
117+
118+
/** The codec used by JavaObjectHostApi. */
119+
static @NonNull MessageCodec<Object> getCodec() {
120+
return new StandardMessageCodec();
121+
}
122+
/**Sets up an instance of `JavaObjectHostApi` to handle messages through the `binaryMessenger`. */
123+
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable JavaObjectHostApi api) {
124+
{
125+
BasicMessageChannel<Object> channel =
126+
new BasicMessageChannel<>(
127+
binaryMessenger, "dev.flutter.pigeon.flutter_instance_manager.JavaObjectHostApi.dispose", getCodec());
128+
if (api != null) {
129+
channel.setMessageHandler(
130+
(message, reply) -> {
131+
ArrayList<Object> wrapped = new ArrayList<Object>();
132+
ArrayList<Object> args = (ArrayList<Object>) message;
133+
String identifierArg = (String) args.get(0);
134+
try {
135+
api.dispose(identifierArg);
136+
wrapped.add(0, null);
137+
}
138+
catch (Throwable exception) {
139+
ArrayList<Object> wrappedError = wrapError(exception);
140+
wrapped = wrappedError;
141+
}
142+
reply.reply(wrapped);
143+
});
144+
} else {
145+
channel.setMessageHandler(null);
146+
}
147+
}
148+
}
149+
}
150+
/**
151+
* Handles callbacks methods for the native Java Object class.
152+
*
153+
* See https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html.
154+
*
155+
* Generated class from Pigeon that represents Flutter messages that can be called from Java.
156+
*/
157+
public static class JavaObjectFlutterApi {
158+
private final @NonNull BinaryMessenger binaryMessenger;
159+
160+
public JavaObjectFlutterApi(@NonNull BinaryMessenger argBinaryMessenger) {
161+
this.binaryMessenger = argBinaryMessenger;
162+
}
163+
164+
/** Public interface for sending reply. */
165+
@SuppressWarnings("UnknownNullness")
166+
public interface Reply<T> {
167+
void reply(T reply);
168+
}
169+
/** The codec used by JavaObjectFlutterApi. */
170+
static @NonNull MessageCodec<Object> getCodec() {
171+
return new StandardMessageCodec();
172+
}
173+
public void dispose(@NonNull String identifierArg, @NonNull Reply<Void> callback) {
174+
BasicMessageChannel<Object> channel =
175+
new BasicMessageChannel<>(
176+
binaryMessenger, "dev.flutter.pigeon.flutter_instance_manager.JavaObjectFlutterApi.dispose", getCodec());
177+
channel.send(
178+
new ArrayList<Object>(Collections.singletonList(identifierArg)),
179+
channelReply -> callback.reply(null));
180+
}
181+
}
182+
}

android/src/main/java/com/baseflow/instancemanager/InstanceManager.java

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.UUID;
1616
import java.util.WeakHashMap;
1717

18+
import io.flutter.plugin.common.BinaryMessenger;
19+
1820
/**
1921
* Maintains instances used to communicate with the corresponding objects in
2022
* Dart.
@@ -68,6 +70,27 @@ public interface FinalizationListener {
6870

6971
private boolean hasFinalizationListenerStopped = false;
7072

73+
/**
74+
* Instantiate a new manager.
75+
*
76+
* <p>
77+
* When the manager is no longer needed, {@link #stopFinalizationListener()}
78+
* must be called.
79+
* This method will inform Dart about the finalization through the
80+
* {@link com.baseflow.instancemanager.AndroidInstanceManagerPigeon.JavaObjectFlutterApi}. If
81+
* you prefer to provide a custom finalizer please use the {@link #create(FinalizationListener)}
82+
* instead.
83+
* </p>
84+
*
85+
* @param binaryMessenger the binaryMessenger used to communicate with Dart.
86+
* @return a new `InstanceManager`.
87+
*/
88+
public static InstanceManager create(@NonNull BinaryMessenger binaryMessenger) {
89+
return new InstanceManager(identifier ->
90+
new AndroidInstanceManagerPigeon.JavaObjectFlutterApi(binaryMessenger)
91+
.dispose(identifier.toString(), reply -> {}));
92+
}
93+
7194
/**
7295
* Instantiate a new manager.
7396
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package com.baseflow.instancemanager;
6+
7+
import androidx.annotation.NonNull;
8+
9+
import com.baseflow.instancemanager.AndroidInstanceManagerPigeon.JavaObjectHostApi;
10+
11+
import java.util.UUID;
12+
13+
/**
14+
* A pigeon Host API implementation that handles creating {@link Object}s and invoking its static
15+
* and instance methods.
16+
*
17+
* <p>{@link Object} instances created by {@link JavaObjectHostApiImpl} are used to intercommunicate
18+
* with a paired Dart object.
19+
*/
20+
public class JavaObjectHostApiImpl implements JavaObjectHostApi {
21+
private final InstanceManager instanceManager;
22+
23+
/**
24+
* Constructs a {@link JavaObjectHostApiImpl}.
25+
*
26+
* @param instanceManager maintains instances stored to communicate with Dart objects
27+
*/
28+
public JavaObjectHostApiImpl(@NonNull InstanceManager instanceManager) {
29+
this.instanceManager = instanceManager;
30+
}
31+
32+
@Override
33+
public void dispose(@NonNull String identifier) {
34+
instanceManager.remove(UUID.fromString(identifier));
35+
}
36+
}

example/.metadata

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This file should be version controlled and should not be manually edited.
55

66
version:
7-
revision: "ba393198430278b6595976de84fe170f553cc728"
7+
revision: "5dcb86f68f239346676ceb1ed1ea385bd215fba1"
88
channel: "stable"
99

1010
project_type: app
@@ -13,11 +13,11 @@ project_type: app
1313
migration:
1414
platforms:
1515
- platform: root
16-
create_revision: ba393198430278b6595976de84fe170f553cc728
17-
base_revision: ba393198430278b6595976de84fe170f553cc728
16+
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
17+
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
1818
- platform: ios
19-
create_revision: ba393198430278b6595976de84fe170f553cc728
20-
base_revision: ba393198430278b6595976de84fe170f553cc728
19+
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
20+
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
2121

2222
# User provided section
2323

example/ios/Podfile.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ EXTERNAL SOURCES:
2020

2121
SPEC CHECKSUMS:
2222
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
23-
flutter_instance_manager: 02c8307bca749e95c498f5618b916badc23438f8
24-
integration_test: 13825b8a9334a850581300559b8839134b124670
23+
flutter_instance_manager: fd2581b919cbd3f83c541e6166ddaa4fe8e261f6
24+
integration_test: ce0a3ffa1de96d1a89ca0ac26fca7ea18a749ef4
2525

2626
PODFILE CHECKSUM: 6e700dec67e6deac9b1c69bb14c49a2217a12d15
2727

28-
COCOAPODS: 1.14.3
28+
COCOAPODS: 1.15.2

0 commit comments

Comments
 (0)