Skip to content

Commit

Permalink
Pigeon + method invocation example (#1180)
Browse files Browse the repository at this point in the history
Adds the infrastructure for Android code mirroring
  • Loading branch information
JeroenWeener authored Oct 19, 2023
1 parent f92618b commit ff711ee
Show file tree
Hide file tree
Showing 29 changed files with 1,308 additions and 528 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baseflow.permissionhandler;

import android.app.Activity;
import android.content.ActivityNotFoundException;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.baseflow.instancemanager.InstanceManager;
import com.baseflow.permissionhandler.PermissionHandlerPigeon.*;

import java.util.UUID;

import io.flutter.plugin.common.BinaryMessenger;

/**
* Host API implementation for `ActivityCompat`.
*
* <p>This class may handle instantiating and adding native object instances that are attached to a
* Dart instance or handle method calls on the associated native class or an instance of the class.
*/
public class ActivityCompatHostApiImpl implements ActivityCompatHostApi {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

/**
* Constructs an {@link ActivityCompatHostApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ActivityCompatHostApiImpl(
@NonNull BinaryMessenger binaryMessenger,
@NonNull InstanceManager instanceManager
) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
}

@NonNull
@Override
public Boolean shouldShowRequestPermissionRationale(
@NonNull String activityInstanceId,
@NonNull String permission
) {
final UUID activityInstanceUuid = UUID.fromString(activityInstanceId);
final Activity activity = instanceManager.getInstance(activityInstanceUuid);
if (activity == null) {
throw new ActivityNotFoundException();
}
return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.baseflow.permissionhandler;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.baseflow.instancemanager.InstanceManager;
import com.baseflow.permissionhandler.PermissionHandlerPigeon.ActivityFlutterApi;

import io.flutter.plugin.common.BinaryMessenger;

import java.util.UUID;

/**
* Flutter API implementation for `Activity`.
*
* <p>This class may handle adding native instances that are attached to a Dart instance or passing
* arguments of callbacks methods to a Dart instance.
*/
public class ActivityFlutterApiImpl {
// To ease adding additional methods, this value is added prematurely.
@SuppressWarnings({"unused", "FieldCanBeLocal"})
private final BinaryMessenger binaryMessenger;

private final InstanceManager instanceManager;

private final ActivityFlutterApi api;

/**
* Constructs a {@link ActivityFlutterApiImpl}.
*
* @param binaryMessenger used to communicate with Dart over asynchronous messages
* @param instanceManager maintains instances stored to communicate with attached Dart objects
*/
public ActivityFlutterApiImpl(
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) {
this.binaryMessenger = binaryMessenger;
this.instanceManager = instanceManager;
api = new ActivityFlutterApi(binaryMessenger);
}

/**
* Stores the `Activity` instance and notifies Dart to create and store a new `Activity`
* instance that is attached to this one. If `instance` has already been added, this method does
* nothing.
*/
public void create(@NonNull Activity instance) {
if (!instanceManager.containsInstance(instance)) {
final UUID activityInstanceUuid = instanceManager.addHostCreatedInstance(instance);
api.create(activityInstanceUuid.toString(), reply -> {});
}
}

/**
* Disposes of the `Activity` instance in the instance manager and notifies Dart to do the same.
* If `instance` was already disposed, this method does nothing.
*/
public void dispose(Activity instance) {
final UUID activityInstanceUuid = instanceManager.getIdentifierForStrongReference(instance);
if (activityInstanceUuid != null) {
api.dispose(activityInstanceUuid.toString(), reply -> {});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Autogenerated from Pigeon (v11.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package com.baseflow.permissionhandler;

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 PermissionHandlerPigeon {

/** 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 `ActivityCompat`.
*
* This class may handle instantiating and adding native object instances that
* are attached to a Dart instance or handle method calls on the associated
* native class or an instance of the class.
*
* See https://developer.android.com/reference/androidx/core/app/ActivityCompat.
*
* Generated interface from Pigeon that represents a handler of messages from Flutter.
*/
public interface ActivityCompatHostApi {
/** Gets whether you should show UI with rationale before requesting a permission. */
@NonNull
Boolean shouldShowRequestPermissionRationale(@NonNull String activityInstanceId, @NonNull String permission);

/** The codec used by ActivityCompatHostApi. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
/**Sets up an instance of `ActivityCompatHostApi` to handle messages through the `binaryMessenger`. */
static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable ActivityCompatHostApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityCompatHostApi.shouldShowRequestPermissionRationale", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) message;
String activityInstanceIdArg = (String) args.get(0);
String permissionArg = (String) args.get(1);
try {
Boolean output = api.shouldShowRequestPermissionRationale(activityInstanceIdArg, permissionArg);
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
/**
* Flutter API for `Activity`.
*
* This class may handle instantiating and adding Dart instances that are
* attached to a native instance or receiving callback methods from an
* overridden native class.
*
* See https://developer.android.com/reference/android/app/Activity.
*
* Generated class from Pigeon that represents Flutter messages that can be called from Java.
*/
public static class ActivityFlutterApi {
private final @NonNull BinaryMessenger binaryMessenger;

public ActivityFlutterApi(@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 ActivityFlutterApi. */
static @NonNull MessageCodec<Object> getCodec() {
return new StandardMessageCodec();
}
/** Create a new Dart instance and add it to the `InstanceManager`. */
public void create(@NonNull String instanceIdArg, @NonNull Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityFlutterApi.create", getCodec());
channel.send(
new ArrayList<Object>(Collections.singletonList(instanceIdArg)),
channelReply -> callback.reply(null));
}
/** Dispose of the Dart instance and remove it from the `InstanceManager`. */
public void dispose(@NonNull String instanceIdArg, @NonNull Reply<Void> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.permission_handler_android.ActivityFlutterApi.dispose", getCodec());
channel.send(
new ArrayList<Object>(Collections.singletonList(instanceIdArg)),
channelReply -> callback.reply(null));
}
}
}
Loading

0 comments on commit ff711ee

Please sign in to comment.