From 3b5e331aca5f9c87fa4a896dc3180473a22cdeee Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sat, 9 Sep 2023 20:18:56 +0200 Subject: [PATCH] fix: Fix foreground/background communication --- .../org/moxxy/moxxy_native/Constants.kt | 1 + .../moxxy/moxxy_native/MoxxyNativePlugin.kt | 29 +++++++++++++++++-- .../moxxy_native/service/BackgroundService.kt | 3 +- example/lib/main.dart | 18 +++++++++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt index 4fd96cc..cb4c290 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/Constants.kt @@ -49,4 +49,5 @@ const val SERVICE_MANUALLY_STOPPED_KEY = "manually_stopped" const val SERVICE_WAKELOCK_DURATION = 10 * 60 * 1000L const val SERVICE_DEFAULT_TITLE = "Moxxy" const val SERVICE_DEFAULT_BODY = "Preparing..." +const val SERVICE_FOREGROUND_METHOD_CHANNEL_KEY = "org.moxxy.moxxy_native/foreground" const val SERVICE_BACKGROUND_METHOD_CHANNEL_KEY = "org.moxxy.moxxy_native/background" diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt index eb517bb..bcded76 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt @@ -1,18 +1,22 @@ package org.moxxy.moxxy_native import android.app.Activity +import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.util.Log import androidx.activity.result.PickVisualMediaRequest import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.NonNull -import io.flutter.embedding.engine.plugins.FlutterPlugin +import androidx.localbroadcastmanager.content.LocalBroadcastManager import io.flutter.embedding.engine.plugins.activity.ActivityAware import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding +import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.service.ServiceAware import io.flutter.embedding.engine.plugins.service.ServicePluginBinding import io.flutter.plugin.common.EventChannel +import io.flutter.plugin.common.MethodChannel import org.moxxy.moxxy_native.contacts.ContactsImplementation import org.moxxy.moxxy_native.contacts.MoxxyContactsApi import org.moxxy.moxxy_native.cryptography.CryptographyImplementation @@ -56,7 +60,7 @@ object NotificationCache { var lastEvent: NotificationEvent? = null } -class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, MoxxyPickerApi { +class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, BroadcastReceiver(), MoxxyPickerApi { private var context: Context? = null private var activity: Activity? = null private lateinit var pickerListener: PickerResultListener @@ -69,6 +73,8 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, MoxxyPicke var service: BackgroundService? = null + var channel: MethodChannel? = null + init { PluginTracker.instances.add(this) } @@ -89,12 +95,23 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, MoxxyPicke MoxxyMediaApi.setUp(flutterPluginBinding.binaryMessenger, mediaImplementation) MoxxyServiceApi.setUp(flutterPluginBinding.binaryMessenger, serviceImplementation) + // Special handling for the service APIs + channel = MethodChannel(flutterPluginBinding.getBinaryMessenger(), SERVICE_FOREGROUND_METHOD_CHANNEL_KEY) + LocalBroadcastManager.getInstance(context!!).registerReceiver( + this, + IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY) + ) + // Register the picker handler pickerListener = PickerResultListener(context!!) Log.d(TAG, "Attached to engine") } override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { + LocalBroadcastManager.getInstance(context!!).registerReceiver( + this, + IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY), + ) Log.d(TAG, "Detached from engine") } @@ -184,4 +201,12 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, MoxxyPicke val pickIntent = contract.createIntent(context!!, PickVisualMediaRequest(pickType)) activity?.startActivityForResult(pickIntent, PICK_FILE_WITH_DATA_REQUEST) } + + override fun onReceive(context: Context, intent: Intent) { + Log.d(TAG, "Received intent with ${intent.action}") + if (intent.action?.equals(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY) == true) { + val data = intent.getStringExtra("data") + channel?.invokeMethod("dataReceived", data) + } + } } diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/service/BackgroundService.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/service/BackgroundService.kt index c0627f5..1f25981 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/service/BackgroundService.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/service/BackgroundService.kt @@ -24,6 +24,7 @@ import org.moxxy.moxxy_native.SERVICE_DEFAULT_BODY import org.moxxy.moxxy_native.SERVICE_DEFAULT_TITLE import org.moxxy.moxxy_native.SERVICE_ENTRYPOINT_KEY import org.moxxy.moxxy_native.SERVICE_EXTRA_DATA_KEY +import org.moxxy.moxxy_native.SERVICE_FOREGROUND_METHOD_CHANNEL_KEY import org.moxxy.moxxy_native.SERVICE_MANUALLY_STOPPED_KEY import org.moxxy.moxxy_native.SERVICE_SHARED_PREFERENCES_KEY import org.moxxy.moxxy_native.SERVICE_START_AT_BOOT_KEY @@ -256,7 +257,7 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi { override fun sendData(data: String) { LocalBroadcastManager.getInstance(applicationContext).sendBroadcast( - Intent(SERVICE_BACKGROUND_METHOD_CHANNEL_KEY).apply { + Intent(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY).apply { putExtra("data", data) }, ) diff --git a/example/lib/main.dart b/example/lib/main.dart index 95d08a4..e1f2950 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'dart:typed_data'; import 'dart:ui'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:moxxy_native/moxxy_native.dart'; import 'package:permission_handler/permission_handler.dart'; @@ -10,8 +11,19 @@ Future entrypoint() async { WidgetsFlutterBinding.ensureInitialized(); print('CALLED FROM NEW FLUTTERENGINE'); - final extra = await MoxxyBackgroundServiceApi().getExtraData(); + final api = MoxxyBackgroundServiceApi(); + final extra = await api.getExtraData(); print('EXTRA DATA: $extra'); + + MethodChannel('org.moxxy.moxxy_native/background').setMethodCallHandler((call) async { + print('[BG] Received ${call.method} with ${call.arguments}'); + }); + + print('Waiting...'); + await Future.delayed(const Duration(seconds: 5)); + + await api.sendData('Hello from the foreground service'); + print('Data sent'); } void main() { @@ -130,6 +142,10 @@ class MyAppState extends State { .toRawHandle(); final api = MoxxyServiceApi(); await api.configure(handle, 'lol'); + MethodChannel("org.moxxy.moxxy_native/foreground").setMethodCallHandler((call) async { + print('[FG] Received ${call.method} with ${call.arguments}'); + await api.sendData('Hello from the foreground'); + }); await api.start(); }, child: const Text('Start foreground service')),