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. */ /** Generated class from Pigeon that represents data sent in messages. */
public static final class NotificationMessageContent { public static final class NotificationMessageContent {
/** The textual body of the message. */ /** The textual body of the message. */
@ -1072,6 +1084,8 @@ public class Api {
@NonNull @NonNull
String getCacheDataPath(); String getCacheDataPath();
/** Contacts APIs */
void recordSentMessage(@NonNull String name, @NonNull String jid, @Nullable String avatarPath, @NonNull FallbackIconType fallbackIcon);
/** Cryptography APIs */ /** 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); 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(); String output = api.getCacheDataPath();
wrapped.add(0, output); 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) { catch (Throwable exception) {
ArrayList<Object> wrappedError = wrapError(exception); ArrayList<Object> wrappedError = wrapError(exception);
wrapped = wrappedError; wrapped = wrappedError;

View File

@ -18,6 +18,7 @@ import android.content.SharedPreferences;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -166,11 +167,6 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt
} }
result.success(true); result.success(true);
break; 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: default:
result.notImplemented(); result.notImplemented();
break; break;
@ -261,6 +257,11 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt
return context.getCacheDir().getPath(); 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 @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) { 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( CryptoKt.encryptAndHash(

View File

@ -7,12 +7,15 @@ import androidx.core.app.Person
import androidx.core.content.pm.ShortcutInfoCompat import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat 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 * Uses Android's direct share API to create dynamic share targets that are compatible
* with share_handler's media handling. * 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 pkgName = context.packageName
val intent = Intent(context, Class.forName("$pkgName.MainActivity")).apply { val intent = Intent(context, Class.forName("$pkgName.MainActivity")).apply {
action = Intent.ACTION_SEND action = Intent.ACTION_SEND
@ -43,9 +46,9 @@ fun recordSentMessage(context: Context, name: String, jid: String, avatarPath: S
shortcutBuilder.setIcon(icon) shortcutBuilder.setIcon(icon)
personBuilder.setIcon(icon) personBuilder.setIcon(icon)
} else { } else {
val resourceId = when(fallbackIconType) { val resourceId = when(fallbackIcon) {
0 -> R.mipmap.person FallbackIconType.PERSON -> R.mipmap.person
1 -> R.mipmap.notes FallbackIconType.NOTES -> R.mipmap.notes
// "Fallthrough" // "Fallthrough"
else -> R.mipmap.person 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/moxplatform_platform_interface.dart';
import 'package:moxplatform_platform_interface/src/api.g.dart';
class AndroidContactsImplementation extends ContactsImplementation { class AndroidContactsImplementation extends ContactsImplementation {
final _methodChannel = const MethodChannel('me.polynom.moxplatform_android'); final MoxplatformApi _api = MoxplatformApi();
@override @override
Future<void> recordSentMessage( Future<void> recordSentMessage(
@ -19,14 +19,11 @@ class AndroidContactsImplementation extends ContactsImplementation {
); );
} }
await _methodChannel.invokeMethod<void>( return _api.recordSentMessage(
'recordSentMessage',
[
name, name,
jid, jid,
avatarPath, avatarPath,
fallbackIcon.id, fallbackIcon,
],
); );
} }
} }

View File

@ -26,6 +26,12 @@ enum CipherAlgorithm {
aes256CbcPkcs7, aes256CbcPkcs7,
} }
enum FallbackIconType {
none,
person,
notes,
}
class NotificationMessageContent { class NotificationMessageContent {
NotificationMessageContent({ NotificationMessageContent({
this.body, 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 /// Cryptography APIs
Future<CryptographyResult?> encryptFile(String arg_sourcePath, String arg_destPath, Uint8List arg_key, Uint8List arg_iv, CipherAlgorithm arg_algorithm, String arg_hashSpec) async { 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?>( final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(

View File

@ -1,14 +1,4 @@
// The type of icon to use when no avatar path is provided. import 'package:moxplatform_platform_interface/src/api.g.dart';
enum FallbackIconType {
none(-1),
person(0),
notes(1);
const FallbackIconType(this.id);
// The ID of the fallback icon.
final int id;
}
// Wrapper around various contact APIs. // Wrapper around various contact APIs.
// ignore: one_member_abstracts // 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'; import 'package:moxplatform_platform_interface/src/contacts.dart';
class StubContactsImplementation extends ContactsImplementation { class StubContactsImplementation extends ContactsImplementation {

View File

@ -161,6 +161,13 @@ class CryptographyResult {
final Uint8List ciphertextHash; final Uint8List ciphertextHash;
} }
// The type of icon to use when no avatar path is provided.
enum FallbackIconType {
none,
person,
notes;
}
@HostApi() @HostApi()
abstract class MoxplatformApi { abstract class MoxplatformApi {
/// Notification APIs /// Notification APIs
@ -175,6 +182,9 @@ abstract class MoxplatformApi {
String getPersistentDataPath(); String getPersistentDataPath();
String getCacheDataPath(); String getCacheDataPath();
/// Contacts APIs
void recordSentMessage(String name, String jid, String? avatarPath, FallbackIconType fallbackIcon);
/// Cryptography APIs /// Cryptography APIs
@async CryptographyResult? encryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec); @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); @async CryptographyResult? decryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec);