feat: Move recordSentMessage to pigeon

This commit is contained in:
PapaTutuWawa 2023-08-03 21:27:13 +02:00
parent 61de3cd565
commit b12e36da83
8 changed files with 102 additions and 30 deletions

View File

@ -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<CryptographyResult> result);
@ -1268,6 +1282,33 @@ public class Api {
String output = api.getCacheDataPath();
wrapped.add(0, output);
}
catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.recordSentMessage", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
ArrayList<Object> args = (ArrayList<Object>) 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<Object> wrappedError = wrapError(exception);
wrapped = wrappedError;

View File

@ -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<CryptographyResult> result) {
CryptoKt.encryptAndHash(

View File

@ -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
}

View File

@ -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<void> recordSentMessage(
@ -19,14 +19,11 @@ class AndroidContactsImplementation extends ContactsImplementation {
);
}
await _methodChannel.invokeMethod<void>(
'recordSentMessage',
[
return _api.recordSentMessage(
name,
jid,
avatarPath,
fallbackIcon.id,
],
fallbackIcon,
);
}
}

View File

@ -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<void> recordSentMessage(String arg_name, String arg_jid, String? arg_avatarPath, FallbackIconType arg_fallbackIcon) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.recordSentMessage', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_name, arg_jid, arg_avatarPath, arg_fallbackIcon.index]) as List<Object?>?;
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<CryptographyResult?> encryptFile(String arg_sourcePath, String arg_destPath, Uint8List arg_key, Uint8List arg_iv, CipherAlgorithm arg_algorithm, String arg_hashSpec) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(

View File

@ -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

View File

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

View File

@ -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);