fix: Fix foreground/background communication
This commit is contained in:
parent
403c6225f1
commit
3b5e331aca
@ -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"
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
},
|
||||
)
|
||||
|
@ -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<void> 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<void>.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<MyApp> {
|
||||
.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')),
|
||||
|
Loading…
Reference in New Issue
Block a user