Skip to content

Commit

Permalink
Feature/android support (#16)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mvanbeusekom and JDDV authored Jun 12, 2024
1 parent 4a409eb commit 2da5a5d
Show file tree
Hide file tree
Showing 28 changed files with 1,399 additions and 90 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.3.0

* Adds support for automatic object clean up for Android and iOS.
* Android: Implement the JObject (Dart) classes.
* iOS: Implement the NSObject (Dart) and FLTObject (native) classes.

# 0.2.0

* Adds iOS support for the instance manager.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v11.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package com.baseflow.instancemanager;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.StandardMessageCodec;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** Generated class from Pigeon. */
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression", "serial"})
public class AndroidInstanceManagerPigeon {

/** Error class for passing custom error details to Flutter via a thrown PlatformException. */
public static class FlutterError extends RuntimeException {

/** The error code. */
public final String code;

/** The error details. Must be a datatype supported by the api codec. */
public final Object details;

public FlutterError(@NonNull String code, @Nullable String message, @Nullable Object details)
{
super(message);
this.code = code;
this.details = details;
}
}

@NonNull
protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
ArrayList<Object> errorList = new ArrayList<Object>(3);
if (exception instanceof FlutterError) {
FlutterError error = (FlutterError) exception;
errorList.add(error.code);
errorList.add(error.getMessage());
errorList.add(error.details);
} else {
errorList.add(exception.toString());
errorList.add(exception.getClass().getSimpleName());
errorList.add(
"Cause: " + exception.getCause() + ", Stacktrace: " + Log.getStackTraceString(exception));
}
return errorList;
}
/**
* Host API for managing the native `InstanceManager`.
*
* Generated interface from Pigeon that represents a handler of messages from Flutter.
*/
public interface AndroidInstanceManagerHostApi {
/**
* Clear the native `InstanceManager`.
*
* This is typically only used after a hot restart.
*/
void clear();

/** The codec used by AndroidInstanceManagerHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
/**Sets up an instance of `AndroidInstanceManagerHostApi` to handle messages through the `binaryMessenger`. */
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable AndroidInstanceManagerHostApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.flutter_instance_manager.AndroidInstanceManagerHostApi.clear", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
try {
api.clear();
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
/**
* Handles methods calls to the native Java Object class.
*
* Also handles calls to remove the reference to an instance with `dispose`.
*
* See https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html.
*
* Generated interface from Pigeon that represents a handler of messages from Flutter.
*/
public interface JavaObjectHostApi {

void dispose(@NonNull String identifier);

/** The codec used by JavaObjectHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
/**Sets up an instance of `JavaObjectHostApi` to handle messages through the `binaryMessenger`. */
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable JavaObjectHostApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.flutter_instance_manager.JavaObjectHostApi.dispose", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String identifierArg = (String) args.get(0);
try {
api.dispose(identifierArg);
wrapped.add(0, null);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
/**
* Handles callbacks methods for the native Java Object class.
*
* See https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html.
*
* Generated class from Pigeon that represents Flutter messages that can be called from Java.
*/
public static class JavaObjectFlutterApi {
private final @NonNull BinaryMessenger binaryMessenger;

public JavaObjectFlutterApi(@NonNull BinaryMessenger argBinaryMessenger) {
this.binaryMessenger = argBinaryMessenger;
}

/** Public interface for sending reply. */
@SuppressWarnings("UnknownNullness")
public interface Reply<T> {
void reply(T reply);
}
/** The codec used by JavaObjectFlutterApi. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
public void dispose(@NonNull String identifierArg, @NonNull Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.flutter_instance_manager.JavaObjectFlutterApi.dispose", getCodec());
channel.send(
new ArrayList<Object>(Collections.singletonList(identifierArg)),
channelReply -> callback.reply(null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.UUID;
import java.util.WeakHashMap;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Maintains instances used to communicate with the corresponding objects in
* Dart.
Expand Down Expand Up @@ -68,6 +70,27 @@ public interface FinalizationListener {

private boolean hasFinalizationListenerStopped = false;

/**
* Instantiate a new manager.
*
* <p>
* When the manager is no longer needed, {@link #stopFinalizationListener()}
* must be called.
* This method will inform Dart about the finalization through the
* {@link com.baseflow.instancemanager.AndroidInstanceManagerPigeon.JavaObjectFlutterApi}. If
* you prefer to provide a custom finalizer please use the {@link #create(FinalizationListener)}
* instead.
* </p>
*
* @param binaryMessenger the binaryMessenger used to communicate with Dart.
* @return a new `InstanceManager`.
*/
public static InstanceManager create(@NonNull BinaryMessenger binaryMessenger) {
return new InstanceManager(identifier ->
new AndroidInstanceManagerPigeon.JavaObjectFlutterApi(binaryMessenger)
.dispose(identifier.toString(), reply -> {}));
}

/**
* Instantiate a new manager.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package com.baseflow.instancemanager;

import androidx.annotation.NonNull;

import com.baseflow.instancemanager.AndroidInstanceManagerPigeon.JavaObjectHostApi;

import java.util.UUID;

/**
* A pigeon Host API implementation that handles creating {@link Object}s and invoking its static
* and instance methods.
*
* <p>{@link Object} instances created by {@link JavaObjectHostApiImpl} are used to intercommunicate
* with a paired Dart object.
*/
public class JavaObjectHostApiImpl implements JavaObjectHostApi {
private final InstanceManager instanceManager;

/**
* Constructs a {@link JavaObjectHostApiImpl}.
*
* @param instanceManager maintains instances stored to communicate with Dart objects
*/
public JavaObjectHostApiImpl(@NonNull InstanceManager instanceManager) {
this.instanceManager = instanceManager;
}

@Override
public void dispose(@NonNull String identifier) {
instanceManager.remove(UUID.fromString(identifier));
}
}
10 changes: 5 additions & 5 deletions example/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: "ba393198430278b6595976de84fe170f553cc728"
revision: "5dcb86f68f239346676ceb1ed1ea385bd215fba1"
channel: "stable"

project_type: app
Expand All @@ -13,11 +13,11 @@ project_type: app
migration:
platforms:
- platform: root
create_revision: ba393198430278b6595976de84fe170f553cc728
base_revision: ba393198430278b6595976de84fe170f553cc728
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
- platform: ios
create_revision: ba393198430278b6595976de84fe170f553cc728
base_revision: ba393198430278b6595976de84fe170f553cc728
create_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1
base_revision: 5dcb86f68f239346676ceb1ed1ea385bd215fba1

# User provided section

Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_instance_manager: 02c8307bca749e95c498f5618b916badc23438f8
integration_test: 13825b8a9334a850581300559b8839134b124670
flutter_instance_manager: fd2581b919cbd3f83c541e6166ddaa4fe8e261f6
integration_test: ce0a3ffa1de96d1a89ca0ac26fca7ea18a749ef4

PODFILE CHECKSUM: 6e700dec67e6deac9b1c69bb14c49a2217a12d15

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
Loading

0 comments on commit 2da5a5d

Please sign in to comment.