fix: Cleanup of the Android side of things
This commit is contained in:
parent
d6ce224956
commit
9f8c148162
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -18,39 +18,12 @@ class MoxxyBackgroundServiceApi {
|
||||
|
||||
static const MessageCodec<Object?> codec = StandardMessageCodec();
|
||||
|
||||
Future<int> getHandler() async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getHandler',
|
||||
codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||
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<String> getExtraData() async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData',
|
||||
codec,
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData', codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||
final List<Object?>? replyList =
|
||||
await channel.send(null) as List<Object?>?;
|
||||
if (replyList == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
@ -74,8 +47,7 @@ class MoxxyBackgroundServiceApi {
|
||||
|
||||
Future<void> setNotificationBody(String arg_body) async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody',
|
||||
codec,
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody', codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList =
|
||||
await channel.send(<Object?>[arg_body]) as List<Object?>?;
|
||||
@ -97,8 +69,7 @@ class MoxxyBackgroundServiceApi {
|
||||
|
||||
Future<void> sendData(String arg_data) async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData',
|
||||
codec,
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData', codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList =
|
||||
await channel.send(<Object?>[arg_data]) as List<Object?>?;
|
||||
@ -122,7 +93,8 @@ class MoxxyBackgroundServiceApi {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.stop', codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||
final List<Object?>? replyList =
|
||||
await channel.send(null) as List<Object?>?;
|
||||
if (replyList == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
|
@ -12,8 +12,6 @@ import 'package:pigeon/pigeon.dart';
|
||||
)
|
||||
@HostApi()
|
||||
abstract class MoxxyBackgroundServiceApi {
|
||||
int getHandler();
|
||||
|
||||
String getExtraData();
|
||||
|
||||
void setNotificationBody(String body);
|
||||
|
Loading…
Reference in New Issue
Block a user