diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Api.java b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Api.java index 32d0d1e..4f1cbc4 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Api.java +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/Api.java @@ -93,6 +93,18 @@ public class Api { } } + public enum FallbackIconType { + NONE(0), + PERSON(1), + NOTES(2); + + final int index; + + private FallbackIconType(final int index) { + this.index = index; + } + } + /** Generated class from Pigeon that represents data sent in messages. */ public static final class NotificationMessageContent { /** The textual body of the message. */ @@ -1072,6 +1084,8 @@ public class Api { @NonNull String getCacheDataPath(); + /** Contacts APIs */ + void recordSentMessage(@NonNull String name, @NonNull String jid, @Nullable String avatarPath, @NonNull FallbackIconType fallbackIcon); /** Cryptography APIs */ void encryptFile(@NonNull String sourcePath, @NonNull String destPath, @NonNull byte[] key, @NonNull byte[] iv, @NonNull CipherAlgorithm algorithm, @NonNull String hashSpec, @NonNull Result result); @@ -1268,6 +1282,33 @@ public class Api { String output = api.getCacheDataPath(); wrapped.add(0, output); } + catch (Throwable exception) { + ArrayList wrappedError = wrapError(exception); + wrapped = wrappedError; + } + reply.reply(wrapped); + }); + } else { + channel.setMessageHandler(null); + } + } + { + BasicMessageChannel channel = + new BasicMessageChannel<>( + binaryMessenger, "dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.recordSentMessage", getCodec()); + if (api != null) { + channel.setMessageHandler( + (message, reply) -> { + ArrayList wrapped = new ArrayList(); + ArrayList args = (ArrayList) message; + String nameArg = (String) args.get(0); + String jidArg = (String) args.get(1); + String avatarPathArg = (String) args.get(2); + FallbackIconType fallbackIconArg = args.get(3) == null ? null : FallbackIconType.values()[(int) args.get(3)]; + try { + api.recordSentMessage(nameArg, jidArg, avatarPathArg, fallbackIconArg); + wrapped.add(0, null); + } catch (Throwable exception) { ArrayList wrappedError = wrapError(exception); wrapped = wrappedError; diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/MoxplatformAndroidPlugin.java b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/MoxplatformAndroidPlugin.java index c9d3ce6..664b7ea 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/MoxplatformAndroidPlugin.java +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/MoxplatformAndroidPlugin.java @@ -18,6 +18,7 @@ import android.content.SharedPreferences; import android.util.Log; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -166,11 +167,6 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt } result.success(true); break; - case "recordSentMessage": - ArrayList rargs = (ArrayList) call.arguments; - recordSentMessage(context, (String) rargs.get(0), (String) rargs.get(1), (String) rargs.get(2), (int) rargs.get(3)); - result.success(true); - break; default: result.notImplemented(); break; @@ -261,6 +257,11 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt return context.getCacheDir().getPath(); } + @Override + public void recordSentMessage(@NonNull String name, @NonNull String jid, @Nullable String avatarPath, @NonNull FallbackIconType fallbackIcon) { + systemRecordSentMessage(context, name, jid, avatarPath, fallbackIcon); + } + @Override public void encryptFile(@NonNull String sourcePath, @NonNull String destPath, @NonNull byte[] key, @NonNull byte[] iv, @NonNull CipherAlgorithm algorithm, @NonNull String hashSpec, @NonNull Api.Result result) { CryptoKt.encryptAndHash( diff --git a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/RecordSentMessage.kt b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/RecordSentMessage.kt index dce2063..bdaaede 100644 --- a/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/RecordSentMessage.kt +++ b/packages/moxplatform_android/android/src/main/java/me/polynom/moxplatform_android/RecordSentMessage.kt @@ -7,12 +7,15 @@ import androidx.core.app.Person import androidx.core.content.pm.ShortcutInfoCompat import androidx.core.content.pm.ShortcutManagerCompat import androidx.core.graphics.drawable.IconCompat +import me.polynom.moxplatform_android.Api.FallbackIconType /* * Uses Android's direct share API to create dynamic share targets that are compatible * with share_handler's media handling. + * NOTE: The "system" prefix is to prevent confusion between pigeon's abstract recordSentMessage + * method and this one. * */ -fun recordSentMessage(context: Context, name: String, jid: String, avatarPath: String?, fallbackIconType: Int) { +fun systemRecordSentMessage(context: Context, name: String, jid: String, avatarPath: String?, fallbackIcon: FallbackIconType) { val pkgName = context.packageName val intent = Intent(context, Class.forName("$pkgName.MainActivity")).apply { action = Intent.ACTION_SEND @@ -43,9 +46,9 @@ fun recordSentMessage(context: Context, name: String, jid: String, avatarPath: S shortcutBuilder.setIcon(icon) personBuilder.setIcon(icon) } else { - val resourceId = when(fallbackIconType) { - 0 -> R.mipmap.person - 1 -> R.mipmap.notes + val resourceId = when(fallbackIcon) { + FallbackIconType.PERSON -> R.mipmap.person + FallbackIconType.NOTES -> R.mipmap.notes // "Fallthrough" else -> R.mipmap.person } diff --git a/packages/moxplatform_android/lib/src/contacts_android.dart b/packages/moxplatform_android/lib/src/contacts_android.dart index 6671897..c21ea4a 100644 --- a/packages/moxplatform_android/lib/src/contacts_android.dart +++ b/packages/moxplatform_android/lib/src/contacts_android.dart @@ -1,8 +1,8 @@ -import 'package:flutter/services.dart'; import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart'; +import 'package:moxplatform_platform_interface/src/api.g.dart'; class AndroidContactsImplementation extends ContactsImplementation { - final _methodChannel = const MethodChannel('me.polynom.moxplatform_android'); + final MoxplatformApi _api = MoxplatformApi(); @override Future recordSentMessage( @@ -19,14 +19,11 @@ class AndroidContactsImplementation extends ContactsImplementation { ); } - await _methodChannel.invokeMethod( - 'recordSentMessage', - [ - name, - jid, - avatarPath, - fallbackIcon.id, - ], + return _api.recordSentMessage( + name, + jid, + avatarPath, + fallbackIcon, ); } } diff --git a/packages/moxplatform_platform_interface/lib/src/api.g.dart b/packages/moxplatform_platform_interface/lib/src/api.g.dart index a2fe506..0725d2e 100644 --- a/packages/moxplatform_platform_interface/lib/src/api.g.dart +++ b/packages/moxplatform_platform_interface/lib/src/api.g.dart @@ -26,6 +26,12 @@ enum CipherAlgorithm { aes256CbcPkcs7, } +enum FallbackIconType { + none, + person, + notes, +} + class NotificationMessageContent { NotificationMessageContent({ this.body, @@ -568,6 +574,29 @@ class MoxplatformApi { } } + /// Contacts APIs + Future recordSentMessage(String arg_name, String arg_jid, String? arg_avatarPath, FallbackIconType arg_fallbackIcon) async { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.recordSentMessage', codec, + binaryMessenger: _binaryMessenger); + final List? replyList = + await channel.send([arg_name, arg_jid, arg_avatarPath, arg_fallbackIcon.index]) as List?; + if (replyList == null) { + throw PlatformException( + code: 'channel-error', + message: 'Unable to establish connection on channel.', + ); + } else if (replyList.length > 1) { + throw PlatformException( + code: replyList[0]! as String, + message: replyList[1] as String?, + details: replyList[2], + ); + } else { + return; + } + } + /// Cryptography APIs Future encryptFile(String arg_sourcePath, String arg_destPath, Uint8List arg_key, Uint8List arg_iv, CipherAlgorithm arg_algorithm, String arg_hashSpec) async { final BasicMessageChannel channel = BasicMessageChannel( diff --git a/packages/moxplatform_platform_interface/lib/src/contacts.dart b/packages/moxplatform_platform_interface/lib/src/contacts.dart index 3ee6a22..ab6ad01 100644 --- a/packages/moxplatform_platform_interface/lib/src/contacts.dart +++ b/packages/moxplatform_platform_interface/lib/src/contacts.dart @@ -1,14 +1,4 @@ -// The type of icon to use when no avatar path is provided. -enum FallbackIconType { - none(-1), - person(0), - notes(1); - - const FallbackIconType(this.id); - - // The ID of the fallback icon. - final int id; -} +import 'package:moxplatform_platform_interface/src/api.g.dart'; // Wrapper around various contact APIs. // ignore: one_member_abstracts diff --git a/packages/moxplatform_platform_interface/lib/src/contacts_stub.dart b/packages/moxplatform_platform_interface/lib/src/contacts_stub.dart index 51a6dae..939e9c2 100644 --- a/packages/moxplatform_platform_interface/lib/src/contacts_stub.dart +++ b/packages/moxplatform_platform_interface/lib/src/contacts_stub.dart @@ -1,3 +1,4 @@ +import 'package:moxplatform_platform_interface/src/api.g.dart'; import 'package:moxplatform_platform_interface/src/contacts.dart'; class StubContactsImplementation extends ContactsImplementation { diff --git a/pigeons/api.dart b/pigeons/api.dart index 8dd2ca7..a872b24 100644 --- a/pigeons/api.dart +++ b/pigeons/api.dart @@ -161,6 +161,13 @@ class CryptographyResult { final Uint8List ciphertextHash; } +// The type of icon to use when no avatar path is provided. +enum FallbackIconType { + none, + person, + notes; +} + @HostApi() abstract class MoxplatformApi { /// Notification APIs @@ -175,6 +182,9 @@ abstract class MoxplatformApi { String getPersistentDataPath(); String getCacheDataPath(); + /// Contacts APIs + void recordSentMessage(String name, String jid, String? avatarPath, FallbackIconType fallbackIcon); + /// Cryptography APIs @async CryptographyResult? encryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec); @async CryptographyResult? decryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec);