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 bcded76..0dd27db 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/MoxxyNativePlugin.kt @@ -10,9 +10,9 @@ import androidx.activity.result.PickVisualMediaRequest import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.NonNull import androidx.localbroadcastmanager.content.LocalBroadcastManager +import io.flutter.embedding.engine.plugins.FlutterPlugin 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 @@ -99,7 +99,7 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, BroadcastR channel = MethodChannel(flutterPluginBinding.getBinaryMessenger(), SERVICE_FOREGROUND_METHOD_CHANNEL_KEY) LocalBroadcastManager.getInstance(context!!).registerReceiver( this, - IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY) + IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY), ) // Register the picker handler @@ -110,7 +110,7 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, BroadcastR override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { LocalBroadcastManager.getInstance(context!!).registerReceiver( this, - IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY), + IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY), ) Log.d(TAG, "Detached from engine") } 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 1f25981..a7ec3da 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 @@ -66,6 +66,13 @@ object BackgroundServiceStatic { ) } + fun setConfiguration(context: Context, handle: Long, extraData: String) { + context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).edit() + .putLong(SERVICE_ENTRYPOINT_KEY, handle) + .putString(SERVICE_EXTRA_DATA_KEY, extraData) + .apply() + } + fun getStartAtBoot(context: Context): Boolean { return context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE) .getBoolean( @@ -87,6 +94,19 @@ object BackgroundServiceStatic { false, ) } + + fun setManuallyStopped(context: Context, value: Boolean) { + context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).edit() + .putBoolean(SERVICE_MANUALLY_STOPPED_KEY, value) + .apply() + } + + fun getHandle(context: Context): Long { + return context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getLong( + SERVICE_ENTRYPOINT_KEY, + 0, + ) + } } class BackgroundService : Service(), MoxxyBackgroundServiceApi { @@ -110,19 +130,6 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi { private var notificationTitle: String = SERVICE_DEFAULT_TITLE private var notificationBody: String = SERVICE_DEFAULT_BODY - private fun setManuallyStopped(context: Context, value: Boolean) { - context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).edit() - .putBoolean(SERVICE_MANUALLY_STOPPED_KEY, value) - .apply() - } - - private fun getHandle(): Long { - return getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getLong( - SERVICE_ENTRYPOINT_KEY, - 0, - ) - } - private fun updateNotificationInfo() { val mutable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0 @@ -167,7 +174,7 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi { null, ) val callback: FlutterCallbackInformation = - FlutterCallbackInformation.lookupCallbackInformation(getHandle()) + FlutterCallbackInformation.lookupCallbackInformation(BackgroundServiceStatic.getHandle(this)) if (callback == null) { Log.e(TAG, "Callback handle not found") return @@ -209,7 +216,7 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi { if (!isManuallyStopped) { BackgroundServiceStatic.enqueue(this) } else { - setManuallyStopped(applicationContext, true) + BackgroundServiceStatic.setManuallyStopped(applicationContext, true) } // Dispose of the engine @@ -232,17 +239,13 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi { } override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { - setManuallyStopped(this, false) + BackgroundServiceStatic.setManuallyStopped(this, false) BackgroundServiceStatic.enqueue(this) runService() return START_STICKY } - override fun getHandler(): Long { - return getHandle() - } - override fun getExtraData(): String { return getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getString( SERVICE_EXTRA_DATA_KEY, diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/service/ServiceImplementation.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/service/ServiceImplementation.kt index 77f8891..bc2737c 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/service/ServiceImplementation.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/service/ServiceImplementation.kt @@ -6,9 +6,6 @@ import android.content.Intent import android.util.Log import androidx.core.content.ContextCompat import org.moxxy.moxxy_native.MoxxyNativePlugin -import org.moxxy.moxxy_native.SERVICE_ENTRYPOINT_KEY -import org.moxxy.moxxy_native.SERVICE_EXTRA_DATA_KEY -import org.moxxy.moxxy_native.SERVICE_SHARED_PREFERENCES_KEY import org.moxxy.moxxy_native.TAG import org.moxxy.moxxy_native.service.BackgroundServiceStatic.setStartAtBoot @@ -18,10 +15,11 @@ object PluginTracker { class ServiceImplementation(private val context: Context) : MoxxyServiceApi { override fun configure(handle: Long, extraData: String) { - context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).edit() - .putLong(SERVICE_ENTRYPOINT_KEY, handle) - .putString(SERVICE_EXTRA_DATA_KEY, extraData) - .apply() + BackgroundServiceStatic.setConfiguration( + context, + handle, + extraData, + ) } override fun isRunning(): Boolean { diff --git a/android/src/main/kotlin/org/moxxy/moxxy_native/service/background/BackgroundServiceApi.kt b/android/src/main/kotlin/org/moxxy/moxxy_native/service/background/BackgroundServiceApi.kt index c3d9a61..d192b55 100644 --- a/android/src/main/kotlin/org/moxxy/moxxy_native/service/background/BackgroundServiceApi.kt +++ b/android/src/main/kotlin/org/moxxy/moxxy_native/service/background/BackgroundServiceApi.kt @@ -43,7 +43,6 @@ class FlutterError( /** Generated interface from Pigeon that represents a handler of messages from Flutter. */ interface MoxxyBackgroundServiceApi { - fun getHandler(): Long fun getExtraData(): String fun setNotificationBody(body: String) fun sendData(data: String) @@ -58,22 +57,6 @@ interface MoxxyBackgroundServiceApi { /** Sets up an instance of `MoxxyBackgroundServiceApi` to handle messages through the `binaryMessenger`. */ @Suppress("UNCHECKED_CAST") fun setUp(binaryMessenger: BinaryMessenger, api: MoxxyBackgroundServiceApi?) { - run { - val channel = BasicMessageChannel(binaryMessenger, "dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getHandler", codec) - if (api != null) { - channel.setMessageHandler { _, reply -> - var wrapped: List - try { - wrapped = listOf(api.getHandler()) - } catch (exception: Throwable) { - wrapped = wrapError(exception) - } - reply.reply(wrapped) - } - } else { - channel.setMessageHandler(null) - } - } run { val channel = BasicMessageChannel(binaryMessenger, "dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData", codec) if (api != null) { diff --git a/lib/pigeon/background_service.g.dart b/lib/pigeon/background_service.g.dart index 9aac2ee..16df43a 100644 --- a/lib/pigeon/background_service.g.dart +++ b/lib/pigeon/background_service.g.dart @@ -18,39 +18,12 @@ class MoxxyBackgroundServiceApi { static const MessageCodec codec = StandardMessageCodec(); - Future getHandler() async { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getHandler', - codec, - binaryMessenger: _binaryMessenger); - final List? replyList = await channel.send(null) as List?; - 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 if (replyList[0] == null) { - throw PlatformException( - code: 'null-error', - message: 'Host platform returned null value for non-null return value.', - ); - } else { - return (replyList[0] as int?)!; - } - } - Future getExtraData() async { final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData', - codec, + 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData', codec, binaryMessenger: _binaryMessenger); - final List? replyList = await channel.send(null) as List?; + final List? replyList = + await channel.send(null) as List?; if (replyList == null) { throw PlatformException( code: 'channel-error', @@ -74,8 +47,7 @@ class MoxxyBackgroundServiceApi { Future setNotificationBody(String arg_body) async { final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody', - codec, + 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody', codec, binaryMessenger: _binaryMessenger); final List? replyList = await channel.send([arg_body]) as List?; @@ -97,8 +69,7 @@ class MoxxyBackgroundServiceApi { Future sendData(String arg_data) async { final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData', - codec, + 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData', codec, binaryMessenger: _binaryMessenger); final List? replyList = await channel.send([arg_data]) as List?; @@ -122,7 +93,8 @@ class MoxxyBackgroundServiceApi { final BasicMessageChannel channel = BasicMessageChannel( 'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.stop', codec, binaryMessenger: _binaryMessenger); - final List? replyList = await channel.send(null) as List?; + final List? replyList = + await channel.send(null) as List?; if (replyList == null) { throw PlatformException( code: 'channel-error', diff --git a/pigeon/background_service.dart b/pigeon/background_service.dart index 244889a..b974d67 100644 --- a/pigeon/background_service.dart +++ b/pigeon/background_service.dart @@ -12,8 +12,6 @@ import 'package:pigeon/pigeon.dart'; ) @HostApi() abstract class MoxxyBackgroundServiceApi { - int getHandler(); - String getExtraData(); void setNotificationBody(String body);