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.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.annotation.NonNull
|
import androidx.annotation.NonNull
|
||||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
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.ActivityAware
|
||||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
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.ServiceAware
|
||||||
import io.flutter.embedding.engine.plugins.service.ServicePluginBinding
|
import io.flutter.embedding.engine.plugins.service.ServicePluginBinding
|
||||||
import io.flutter.plugin.common.EventChannel
|
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)
|
channel = MethodChannel(flutterPluginBinding.getBinaryMessenger(), SERVICE_FOREGROUND_METHOD_CHANNEL_KEY)
|
||||||
LocalBroadcastManager.getInstance(context!!).registerReceiver(
|
LocalBroadcastManager.getInstance(context!!).registerReceiver(
|
||||||
this,
|
this,
|
||||||
IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY)
|
IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register the picker handler
|
// Register the picker handler
|
||||||
@ -110,7 +110,7 @@ class MoxxyNativePlugin : FlutterPlugin, ActivityAware, ServiceAware, BroadcastR
|
|||||||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
||||||
LocalBroadcastManager.getInstance(context!!).registerReceiver(
|
LocalBroadcastManager.getInstance(context!!).registerReceiver(
|
||||||
this,
|
this,
|
||||||
IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY),
|
IntentFilter(SERVICE_FOREGROUND_METHOD_CHANNEL_KEY),
|
||||||
)
|
)
|
||||||
Log.d(TAG, "Detached from engine")
|
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 {
|
fun getStartAtBoot(context: Context): Boolean {
|
||||||
return context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
|
return context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
|
||||||
.getBoolean(
|
.getBoolean(
|
||||||
@ -87,6 +94,19 @@ object BackgroundServiceStatic {
|
|||||||
false,
|
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 {
|
class BackgroundService : Service(), MoxxyBackgroundServiceApi {
|
||||||
@ -110,19 +130,6 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi {
|
|||||||
private var notificationTitle: String = SERVICE_DEFAULT_TITLE
|
private var notificationTitle: String = SERVICE_DEFAULT_TITLE
|
||||||
private var notificationBody: String = SERVICE_DEFAULT_BODY
|
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() {
|
private fun updateNotificationInfo() {
|
||||||
val mutable =
|
val mutable =
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0
|
||||||
@ -167,7 +174,7 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi {
|
|||||||
null,
|
null,
|
||||||
)
|
)
|
||||||
val callback: FlutterCallbackInformation =
|
val callback: FlutterCallbackInformation =
|
||||||
FlutterCallbackInformation.lookupCallbackInformation(getHandle())
|
FlutterCallbackInformation.lookupCallbackInformation(BackgroundServiceStatic.getHandle(this))
|
||||||
if (callback == null) {
|
if (callback == null) {
|
||||||
Log.e(TAG, "Callback handle not found")
|
Log.e(TAG, "Callback handle not found")
|
||||||
return
|
return
|
||||||
@ -209,7 +216,7 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi {
|
|||||||
if (!isManuallyStopped) {
|
if (!isManuallyStopped) {
|
||||||
BackgroundServiceStatic.enqueue(this)
|
BackgroundServiceStatic.enqueue(this)
|
||||||
} else {
|
} else {
|
||||||
setManuallyStopped(applicationContext, true)
|
BackgroundServiceStatic.setManuallyStopped(applicationContext, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispose of the engine
|
// Dispose of the engine
|
||||||
@ -232,17 +239,13 @@ class BackgroundService : Service(), MoxxyBackgroundServiceApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||||
setManuallyStopped(this, false)
|
BackgroundServiceStatic.setManuallyStopped(this, false)
|
||||||
BackgroundServiceStatic.enqueue(this)
|
BackgroundServiceStatic.enqueue(this)
|
||||||
runService()
|
runService()
|
||||||
|
|
||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getHandler(): Long {
|
|
||||||
return getHandle()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getExtraData(): String {
|
override fun getExtraData(): String {
|
||||||
return getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getString(
|
return getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getString(
|
||||||
SERVICE_EXTRA_DATA_KEY,
|
SERVICE_EXTRA_DATA_KEY,
|
||||||
|
@ -6,9 +6,6 @@ import android.content.Intent
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import org.moxxy.moxxy_native.MoxxyNativePlugin
|
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.TAG
|
||||||
import org.moxxy.moxxy_native.service.BackgroundServiceStatic.setStartAtBoot
|
import org.moxxy.moxxy_native.service.BackgroundServiceStatic.setStartAtBoot
|
||||||
|
|
||||||
@ -18,10 +15,11 @@ object PluginTracker {
|
|||||||
|
|
||||||
class ServiceImplementation(private val context: Context) : MoxxyServiceApi {
|
class ServiceImplementation(private val context: Context) : MoxxyServiceApi {
|
||||||
override fun configure(handle: Long, extraData: String) {
|
override fun configure(handle: Long, extraData: String) {
|
||||||
context.getSharedPreferences(SERVICE_SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).edit()
|
BackgroundServiceStatic.setConfiguration(
|
||||||
.putLong(SERVICE_ENTRYPOINT_KEY, handle)
|
context,
|
||||||
.putString(SERVICE_EXTRA_DATA_KEY, extraData)
|
handle,
|
||||||
.apply()
|
extraData,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isRunning(): Boolean {
|
override fun isRunning(): Boolean {
|
||||||
|
@ -43,7 +43,6 @@ class FlutterError(
|
|||||||
|
|
||||||
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
|
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
|
||||||
interface MoxxyBackgroundServiceApi {
|
interface MoxxyBackgroundServiceApi {
|
||||||
fun getHandler(): Long
|
|
||||||
fun getExtraData(): String
|
fun getExtraData(): String
|
||||||
fun setNotificationBody(body: String)
|
fun setNotificationBody(body: String)
|
||||||
fun sendData(data: String)
|
fun sendData(data: String)
|
||||||
@ -58,22 +57,6 @@ interface MoxxyBackgroundServiceApi {
|
|||||||
/** Sets up an instance of `MoxxyBackgroundServiceApi` to handle messages through the `binaryMessenger`. */
|
/** Sets up an instance of `MoxxyBackgroundServiceApi` to handle messages through the `binaryMessenger`. */
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun setUp(binaryMessenger: BinaryMessenger, api: MoxxyBackgroundServiceApi?) {
|
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 {
|
run {
|
||||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData", codec)
|
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData", codec)
|
||||||
if (api != null) {
|
if (api != null) {
|
||||||
|
@ -18,39 +18,12 @@ class MoxxyBackgroundServiceApi {
|
|||||||
|
|
||||||
static const MessageCodec<Object?> codec = StandardMessageCodec();
|
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 {
|
Future<String> getExtraData() async {
|
||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData',
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData', codec,
|
||||||
codec,
|
|
||||||
binaryMessenger: _binaryMessenger);
|
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) {
|
if (replyList == null) {
|
||||||
throw PlatformException(
|
throw PlatformException(
|
||||||
code: 'channel-error',
|
code: 'channel-error',
|
||||||
@ -74,8 +47,7 @@ class MoxxyBackgroundServiceApi {
|
|||||||
|
|
||||||
Future<void> setNotificationBody(String arg_body) async {
|
Future<void> setNotificationBody(String arg_body) async {
|
||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody',
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody', codec,
|
||||||
codec,
|
|
||||||
binaryMessenger: _binaryMessenger);
|
binaryMessenger: _binaryMessenger);
|
||||||
final List<Object?>? replyList =
|
final List<Object?>? replyList =
|
||||||
await channel.send(<Object?>[arg_body]) as List<Object?>?;
|
await channel.send(<Object?>[arg_body]) as List<Object?>?;
|
||||||
@ -97,8 +69,7 @@ class MoxxyBackgroundServiceApi {
|
|||||||
|
|
||||||
Future<void> sendData(String arg_data) async {
|
Future<void> sendData(String arg_data) async {
|
||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData',
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData', codec,
|
||||||
codec,
|
|
||||||
binaryMessenger: _binaryMessenger);
|
binaryMessenger: _binaryMessenger);
|
||||||
final List<Object?>? replyList =
|
final List<Object?>? replyList =
|
||||||
await channel.send(<Object?>[arg_data]) as List<Object?>?;
|
await channel.send(<Object?>[arg_data]) as List<Object?>?;
|
||||||
@ -122,7 +93,8 @@ class MoxxyBackgroundServiceApi {
|
|||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.stop', codec,
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.stop', codec,
|
||||||
binaryMessenger: _binaryMessenger);
|
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) {
|
if (replyList == null) {
|
||||||
throw PlatformException(
|
throw PlatformException(
|
||||||
code: 'channel-error',
|
code: 'channel-error',
|
||||||
|
@ -12,8 +12,6 @@ import 'package:pigeon/pigeon.dart';
|
|||||||
)
|
)
|
||||||
@HostApi()
|
@HostApi()
|
||||||
abstract class MoxxyBackgroundServiceApi {
|
abstract class MoxxyBackgroundServiceApi {
|
||||||
int getHandler();
|
|
||||||
|
|
||||||
String getExtraData();
|
String getExtraData();
|
||||||
|
|
||||||
void setNotificationBody(String body);
|
void setNotificationBody(String body);
|
||||||
|
Loading…
Reference in New Issue
Block a user