fix: Fix foreground/background communication

This commit is contained in:
2023-09-09 20:18:56 +02:00
parent 403c6225f1
commit 3b5e331aca
4 changed files with 47 additions and 4 deletions

View File

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

View File

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

View File

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