fix: Cleanup of the Android side of things
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<Any?>(binaryMessenger, "dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getHandler", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { _, reply ->
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
wrapped = listOf<Any?>(api.getHandler())
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
} else {
|
||||
channel.setMessageHandler(null)
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData", codec)
|
||||
if (api != null) {
|
||||
|
||||
Reference in New Issue
Block a user