mirror of
https://github.com/flutter/samples.git
synced 2026-04-05 19:22:08 +00:00
updated add_to_app sample to pigeon 1.0 (#894)
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
// Autogenerated from Pigeon (v0.1.17), do not edit directly.
|
||||
// Autogenerated from Pigeon (v1.0.1), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
|
||||
package dev.flutter.example.books;
|
||||
|
||||
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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Generated class from Pigeon. */
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings(
|
||||
{"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression"})
|
||||
public class Api {
|
||||
|
||||
/** Generated class from Pigeon that represents data sent in messages. */
|
||||
@@ -33,23 +40,33 @@ public class Api {
|
||||
|
||||
private String publishDate;
|
||||
public String getPublishDate() { return publishDate; }
|
||||
public void setPublishDate(String setterArg) { this.publishDate = setterArg; }
|
||||
public void setPublishDate(String setterArg) {
|
||||
this.publishDate = setterArg;
|
||||
}
|
||||
|
||||
private Long pageCount;
|
||||
public Long getPageCount() { return pageCount; }
|
||||
public void setPageCount(Long setterArg) { this.pageCount = setterArg; }
|
||||
|
||||
HashMap toMap() {
|
||||
HashMap<String, Object> toMapResult = new HashMap<>();
|
||||
private Thumbnail thumbnail;
|
||||
public Thumbnail getThumbnail() { return thumbnail; }
|
||||
public void setThumbnail(Thumbnail setterArg) {
|
||||
this.thumbnail = setterArg;
|
||||
}
|
||||
|
||||
Map<String, Object> toMap() {
|
||||
Map<String, Object> toMapResult = new HashMap<>();
|
||||
toMapResult.put("title", title);
|
||||
toMapResult.put("subtitle", subtitle);
|
||||
toMapResult.put("author", author);
|
||||
toMapResult.put("summary", summary);
|
||||
toMapResult.put("publishDate", publishDate);
|
||||
toMapResult.put("pageCount", pageCount);
|
||||
toMapResult.put("thumbnail",
|
||||
(thumbnail == null) ? null : thumbnail.toMap());
|
||||
return toMapResult;
|
||||
}
|
||||
static Book fromMap(HashMap map) {
|
||||
static Book fromMap(Map<String, Object> map) {
|
||||
Book fromMapResult = new Book();
|
||||
Object title = map.get("title");
|
||||
fromMapResult.title = (String)title;
|
||||
@@ -62,48 +79,135 @@ public class Api {
|
||||
Object publishDate = map.get("publishDate");
|
||||
fromMapResult.publishDate = (String)publishDate;
|
||||
Object pageCount = map.get("pageCount");
|
||||
fromMapResult.pageCount = (pageCount == null) ? null : ((pageCount instanceof Integer) ? (Integer)pageCount : (Long)pageCount);
|
||||
fromMapResult.pageCount =
|
||||
(pageCount == null)
|
||||
? null
|
||||
: ((pageCount instanceof Integer) ? (Integer)pageCount
|
||||
: (Long)pageCount);
|
||||
Object thumbnail = map.get("thumbnail");
|
||||
fromMapResult.thumbnail = Thumbnail.fromMap((Map)thumbnail);
|
||||
return fromMapResult;
|
||||
}
|
||||
}
|
||||
|
||||
/** Generated class from Pigeon that represents Flutter messages that can be called from Java.*/
|
||||
/** Generated class from Pigeon that represents data sent in messages. */
|
||||
public static class Thumbnail {
|
||||
private String url;
|
||||
public String getUrl() { return url; }
|
||||
public void setUrl(String setterArg) { this.url = setterArg; }
|
||||
|
||||
Map<String, Object> toMap() {
|
||||
Map<String, Object> toMapResult = new HashMap<>();
|
||||
toMapResult.put("url", url);
|
||||
return toMapResult;
|
||||
}
|
||||
static Thumbnail fromMap(Map<String, Object> map) {
|
||||
Thumbnail fromMapResult = new Thumbnail();
|
||||
Object url = map.get("url");
|
||||
fromMapResult.url = (String)url;
|
||||
return fromMapResult;
|
||||
}
|
||||
}
|
||||
private static class FlutterBookApiCodec extends StandardMessageCodec {
|
||||
public static final FlutterBookApiCodec INSTANCE =
|
||||
new FlutterBookApiCodec();
|
||||
private FlutterBookApiCodec() {}
|
||||
@Override
|
||||
protected Object readValueOfType(byte type, ByteBuffer buffer) {
|
||||
switch (type) {
|
||||
case (byte)128:
|
||||
return Book.fromMap((Map<String, Object>)readValue(buffer));
|
||||
|
||||
default:
|
||||
return super.readValueOfType(type, buffer);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void writeValue(ByteArrayOutputStream stream, Object value) {
|
||||
if (value instanceof Book) {
|
||||
stream.write(128);
|
||||
writeValue(stream, ((Book)value).toMap());
|
||||
} else {
|
||||
super.writeValue(stream, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated class from Pigeon that represents Flutter messages that can be
|
||||
* called from Java.
|
||||
*/
|
||||
public static class FlutterBookApi {
|
||||
private final BinaryMessenger binaryMessenger;
|
||||
public FlutterBookApi(BinaryMessenger argBinaryMessenger){
|
||||
public FlutterBookApi(BinaryMessenger argBinaryMessenger) {
|
||||
this.binaryMessenger = argBinaryMessenger;
|
||||
}
|
||||
public interface Reply<T> {
|
||||
void reply(T reply);
|
||||
}
|
||||
public void displayBookDetails(Book argInput, Reply<Void> callback) {
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.FlutterBookApi.displayBookDetails", new StandardMessageCodec());
|
||||
HashMap inputMap = argInput.toMap();
|
||||
channel.send(inputMap, channelReply -> {
|
||||
callback.reply(null);
|
||||
});
|
||||
static MessageCodec<Object> getCodec() {
|
||||
return FlutterBookApiCodec.INSTANCE;
|
||||
}
|
||||
|
||||
public void displayBookDetails(Book bookArg, Reply<Void> callback) {
|
||||
BasicMessageChannel<Object> channel = new BasicMessageChannel<>(
|
||||
binaryMessenger,
|
||||
"dev.flutter.pigeon.FlutterBookApi.displayBookDetails", getCodec());
|
||||
channel.send(new ArrayList<Object>(Arrays.asList(bookArg)),
|
||||
channelReply -> { callback.reply(null); });
|
||||
}
|
||||
}
|
||||
private static class HostBookApiCodec extends StandardMessageCodec {
|
||||
public static final HostBookApiCodec INSTANCE = new HostBookApiCodec();
|
||||
private HostBookApiCodec() {}
|
||||
@Override
|
||||
protected Object readValueOfType(byte type, ByteBuffer buffer) {
|
||||
switch (type) {
|
||||
case (byte)128:
|
||||
return Book.fromMap((Map<String, Object>)readValue(buffer));
|
||||
|
||||
default:
|
||||
return super.readValueOfType(type, buffer);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void writeValue(ByteArrayOutputStream stream, Object value) {
|
||||
if (value instanceof Book) {
|
||||
stream.write(128);
|
||||
writeValue(stream, ((Book)value).toMap());
|
||||
} else {
|
||||
super.writeValue(stream, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Generated interface from Pigeon that represents a handler of messages from Flutter.*/
|
||||
/**
|
||||
* Generated interface from Pigeon that represents a handler of messages from
|
||||
* Flutter.
|
||||
*/
|
||||
public interface HostBookApi {
|
||||
void cancel();
|
||||
void finishEditingBook(Book arg);
|
||||
void finishEditingBook(Book book);
|
||||
|
||||
/** Sets up an instance of `HostBookApi` to handle messages through the `binaryMessenger` */
|
||||
/** The codec used by HostBookApi. */
|
||||
static MessageCodec<Object> getCodec() { return HostBookApiCodec.INSTANCE; }
|
||||
|
||||
/**
|
||||
* Sets up an instance of `HostBookApi` to handle messages through the
|
||||
* `binaryMessenger`.
|
||||
*/
|
||||
static void setup(BinaryMessenger binaryMessenger, HostBookApi api) {
|
||||
{
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.HostBookApi.cancel", new StandardMessageCodec());
|
||||
BasicMessageChannel<Object> channel = new BasicMessageChannel<>(
|
||||
binaryMessenger, "dev.flutter.pigeon.HostBookApi.cancel",
|
||||
getCodec());
|
||||
if (api != null) {
|
||||
channel.setMessageHandler((message, reply) -> {
|
||||
HashMap<String, HashMap> wrapped = new HashMap<>();
|
||||
Map<String, Object> wrapped = new HashMap<>();
|
||||
try {
|
||||
api.cancel();
|
||||
wrapped.put("result", null);
|
||||
}
|
||||
catch (Exception exception) {
|
||||
} catch (Error | RuntimeException exception) {
|
||||
wrapped.put("error", wrapError(exception));
|
||||
}
|
||||
reply.reply(wrapped);
|
||||
@@ -113,18 +217,21 @@ public class Api {
|
||||
}
|
||||
}
|
||||
{
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.HostBookApi.finishEditingBook", new StandardMessageCodec());
|
||||
BasicMessageChannel<Object> channel = new BasicMessageChannel<>(
|
||||
binaryMessenger, "dev.flutter.pigeon.HostBookApi.finishEditingBook",
|
||||
getCodec());
|
||||
if (api != null) {
|
||||
channel.setMessageHandler((message, reply) -> {
|
||||
HashMap<String, HashMap> wrapped = new HashMap<>();
|
||||
Map<String, Object> wrapped = new HashMap<>();
|
||||
try {
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
Book input = Book.fromMap((HashMap)message);
|
||||
api.finishEditingBook(input);
|
||||
ArrayList<Object> args = (ArrayList<Object>)message;
|
||||
Book bookArg = (Book)args.get(0);
|
||||
if (bookArg == null) {
|
||||
throw new NullPointerException("bookArg unexpectedly null.");
|
||||
}
|
||||
api.finishEditingBook(bookArg);
|
||||
wrapped.put("result", null);
|
||||
}
|
||||
catch (Exception exception) {
|
||||
} catch (Error | RuntimeException exception) {
|
||||
wrapped.put("error", wrapError(exception));
|
||||
}
|
||||
reply.reply(wrapped);
|
||||
@@ -135,8 +242,8 @@ public class Api {
|
||||
}
|
||||
}
|
||||
}
|
||||
private static HashMap wrapError(Exception exception) {
|
||||
HashMap<String, Object> errorMap = new HashMap<>();
|
||||
private static Map<String, Object> wrapError(Throwable exception) {
|
||||
Map<String, Object> errorMap = new HashMap<>();
|
||||
errorMap.put("message", exception.toString());
|
||||
errorMap.put("code", exception.getClass().getSimpleName());
|
||||
errorMap.put("details", null);
|
||||
|
||||
@@ -43,7 +43,7 @@ class FlutterBookActivity: FlutterActivity() {
|
||||
// TODO(gaaclarke): the Pigeon generated data class should just implement
|
||||
// Serializable so we won't need 'toMap()' here
|
||||
// https://github.com/flutter/flutter/issues/58909
|
||||
book.toMap()
|
||||
HashMap(book.toMap())
|
||||
)
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ class FlutterBookActivity: FlutterActivity() {
|
||||
* activity's {@code onActivityResult}.
|
||||
*/
|
||||
fun getBookFromResultIntent(resultIntent: Intent): Api.Book {
|
||||
return Api.Book.fromMap(resultIntent.getSerializableExtra(FlutterBookActivity.EXTRA_BOOK) as HashMap<*, *>);
|
||||
return Api.Book.fromMap((resultIntent.getSerializableExtra(FlutterBookActivity.EXTRA_BOOK) as HashMap<String, Any>));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class FlutterBookActivity: FlutterActivity() {
|
||||
|
||||
// The book to give to Flutter is passed in from the MainActivity via this activity's
|
||||
// source intent getter. The intent contains the book serialized as on extra.
|
||||
val bookToShow = Api.Book.fromMap(intent.getSerializableExtra(EXTRA_BOOK) as HashMap<*, *>)
|
||||
val bookToShow = Api.Book.fromMap(intent.getSerializableExtra(EXTRA_BOOK) as HashMap<String, Any>)
|
||||
|
||||
// Register the HostBookApiHandler callback class to get results from Flutter.
|
||||
Api.HostBookApi.setup(flutterEngine.dartExecutor, HostBookApiHandler())
|
||||
@@ -94,7 +94,7 @@ class FlutterBookActivity: FlutterActivity() {
|
||||
}
|
||||
// Flutter returned an edited book instance. Return it to the MainActivity via the
|
||||
// standard Android Activity set result mechanism.
|
||||
setResult(Activity.RESULT_OK, Intent().putExtra(EXTRA_BOOK, book.toMap()))
|
||||
setResult(Activity.RESULT_OK, Intent().putExtra(EXTRA_BOOK, HashMap(book.toMap())))
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user