Skip to content

Commit 8edc09a

Browse files
authored
Merge pull request #7 from UnifiedPush/single_isolate
Single isolate
2 parents f91b627 + 7dbe550 commit 8edc09a

File tree

6 files changed

+242
-133
lines changed

6 files changed

+242
-133
lines changed

android/src/main/kotlin/org/unifiedpush/flutter/connector/Plugin.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.unifiedpush.flutter.connector
33
import android.app.Activity
44
import android.content.Context
55
import android.util.Log
6-
import android.content.Intent
76
import io.flutter.embedding.engine.plugins.FlutterPlugin
87
import io.flutter.embedding.engine.plugins.activity.ActivityAware
98
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
@@ -16,10 +15,11 @@ import org.unifiedpush.android.connector.Registration
1615
class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
1716
private var mContext : Context? = null
1817
private var mActivity : Activity? = null
18+
var withReceiverChannel: MethodChannel? = null
1919

2020
companion object {
2121

22-
var channel: MethodChannel? = null
22+
var withCallbackChannel: MethodChannel? = null
2323
private var up = Registration()
2424

2525
/**
@@ -73,26 +73,36 @@ class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
7373
}
7474

7575
@JvmStatic
76-
private fun initializeService(context: Context, args: ArrayList<*>?, result: Result) {
77-
val callbackHandle = args!![0] as Long
76+
private fun initializeCallback(context: Context, args: ArrayList<*>?, result: Result) {
77+
val callbackHandle = args?.get(0) as? Long ?: 0
7878
context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
7979
.edit()
8080
.putLong(CALLBACK_DISPATCHER_HANDLE_KEY, callbackHandle)
8181
.apply()
8282
result.success(true)
8383
}
84+
85+
fun isWithCallback(context: Context): Boolean {
86+
val method = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
87+
.getLong(CALLBACK_DISPATCHER_HANDLE_KEY, 0)
88+
Log.d("Plugin","isWithCallback: ${method > 0}")
89+
return method > 0
90+
}
8491
}
8592

8693
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
8794
Log.d("Plugin", "onAttachedToEngine")
8895
mContext = binding.applicationContext
89-
channel = MethodChannel(binding.binaryMessenger, PLUGIN_CHANNEL)
90-
channel?.setMethodCallHandler(this)
91-
96+
withReceiverChannel = MethodChannel(binding.binaryMessenger, PLUGIN_CHANNEL)
97+
withReceiverChannel?.setMethodCallHandler(this)
98+
withCallbackChannel = MethodChannel(binding.binaryMessenger, PLUGIN_CHANNEL)
99+
withCallbackChannel?.setMethodCallHandler(this)
92100
}
93101

94102
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
95103
Log.d("Plugin", "onDetachedFromEngine")
104+
withReceiverChannel?.setMethodCallHandler(null)
105+
withCallbackChannel?.setMethodCallHandler(null)
96106
mContext = null
97107
}
98108

@@ -107,7 +117,7 @@ class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
107117
}
108118

109119
override fun onDetachedFromActivityForConfigChanges() {
110-
Log.d("Plugin", "onDetachedFromActivityForConfigChanges")
120+
Log.d("Plugin", "onDetachedFromActivityForConfigChanges")
111121
mActivity = null
112122
}
113123

@@ -120,7 +130,7 @@ class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
120130
Log.d("Plugin","Method: ${call.method}")
121131
val args = call.arguments<ArrayList<*>>()
122132
when(call.method) {
123-
PLUGIN_EVENT_INITIALIZE_CALLBACK -> initializeService(mContext!!, args, result)
133+
PLUGIN_EVENT_INITIALIZE_CALLBACK -> initializeCallback(mContext!!, args, result)
124134
PLUGIN_EVENT_REGISTER_APP_WITH_DIALOG -> registerAppWithDialog(mActivity!!, result)
125135
PLUGIN_EVENT_GET_DISTRIBUTORS -> getDistributors(mActivity!!, result)
126136
PLUGIN_EVENT_SAVE_DISTRIBUTOR -> saveDistributor(mActivity!!, args, result)

android/src/main/kotlin/org/unifiedpush/flutter/connector/Receiver.kt

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ package org.unifiedpush.flutter.connector
22

33
import android.content.Context
44
import android.content.Intent
5+
import android.os.Handler
56
import android.util.Log
7+
import io.flutter.embedding.engine.FlutterEngine
68
import io.flutter.view.FlutterMain
79
import org.unifiedpush.android.connector.MessagingReceiver
810
import org.unifiedpush.android.connector.MessagingReceiverHandler
911

12+
/***
13+
* Handler used when there is a callback
14+
*/
15+
1016
val handler = object : MessagingReceiverHandler {
1117

1218
override fun onMessage(context: Context?, message: String) {
1319
Log.d("Receiver","OnMessage")
1420
FlutterMain.startInitialization(context!!)
1521
FlutterMain.ensureInitializationComplete(context, null)
16-
if (Plugin.channel != null && !CallbackService.sServiceStarted.get()){
22+
if (Plugin.withCallbackChannel != null && !CallbackService.sServiceStarted.get()){
1723
Log.d("Receiver","foregroundChannel")
18-
Plugin.channel?.invokeMethod("onMessage", message)
24+
Plugin.withCallbackChannel?.invokeMethod("onMessage", message)
1925
} else {
2026
Log.d("Receiver","CallbackChannel")
2127
val intent = Intent(context, CallbackService::class.java)
@@ -29,8 +35,8 @@ val handler = object : MessagingReceiverHandler {
2935
Log.d("Receiver","OnNewEndpoint")
3036
FlutterMain.startInitialization(context!!)
3137
FlutterMain.ensureInitializationComplete(context, null)
32-
if (Plugin.channel != null && !CallbackService.sServiceStarted.get()) {
33-
Plugin.channel?.invokeMethod("onNewEndpoint", endpoint)
38+
if (Plugin.withCallbackChannel != null && !CallbackService.sServiceStarted.get()) {
39+
Plugin.withCallbackChannel?.invokeMethod("onNewEndpoint", endpoint)
3440
} else {
3541
val intent = Intent(context, CallbackService::class.java)
3642
intent.putExtra(EXTRA_CALLBACK_EVENT, CALLBACK_EVENT_NEW_ENDPOINT)
@@ -41,20 +47,20 @@ val handler = object : MessagingReceiverHandler {
4147

4248
override fun onRegistrationFailed(context: Context?) {
4349
Log.d("Receiver","OnRegistrationFailed")
44-
Plugin.channel?.invokeMethod("onRegistrationFailed", null)
50+
Plugin.withCallbackChannel?.invokeMethod("onRegistrationFailed", null)
4551
}
4652

4753
override fun onRegistrationRefused(context: Context?) {
4854
Log.d("Receiver","OnRegistrationRefused")
49-
Plugin.channel?.invokeMethod("onRegistrationRefused", null)
55+
Plugin.withCallbackChannel?.invokeMethod("onRegistrationRefused", null)
5056
}
5157

5258
override fun onUnregistered(context: Context?) {
5359
Log.d("Receiver","OnUnregistered")
5460
FlutterMain.startInitialization(context!!)
5561
FlutterMain.ensureInitializationComplete(context, null)
56-
if (Plugin.channel != null && !CallbackService.sServiceStarted.get()) {
57-
Plugin.channel?.invokeMethod("onUnregistered", null)
62+
if (Plugin.withCallbackChannel != null && !CallbackService.sServiceStarted.get()) {
63+
Plugin.withCallbackChannel?.invokeMethod("onUnregistered", null)
5864
} else {
5965
val intent = Intent(context, CallbackService::class.java)
6066
intent.putExtra(EXTRA_CALLBACK_EVENT, CALLBACK_EVENT_UNREGISTERED)
@@ -63,4 +69,64 @@ val handler = object : MessagingReceiverHandler {
6369
}
6470
}
6571

66-
class Receiver : MessagingReceiver(handler)
72+
class Receiver : MessagingReceiver(handler) {
73+
override fun onReceive(context: Context?, intent: Intent?) {
74+
if (Plugin.isWithCallback(context!!)) {
75+
super.onReceive(context, intent)
76+
}
77+
}
78+
}
79+
80+
/***
81+
* Handler used when the Receiver is defined in the app
82+
*/
83+
84+
abstract class UnifiedPushHandler : MessagingReceiverHandler {
85+
abstract fun getEngine(context: Context): FlutterEngine
86+
87+
private val handler = Handler()
88+
89+
private fun getPlugin(context: Context): Plugin {
90+
val registry = getEngine(context).getPlugins()
91+
var plugin = registry.get(Plugin::class.java) as? Plugin
92+
if (plugin == null) {
93+
plugin = Plugin()
94+
registry.add(plugin)
95+
}
96+
return plugin;
97+
}
98+
99+
override fun onMessage(context: Context?, message: String) {
100+
Log.d("Receiver","OnMessage")
101+
handler.post {
102+
getPlugin(context!!).withReceiverChannel?.invokeMethod("onMessage", message)
103+
}
104+
}
105+
106+
override fun onNewEndpoint(context: Context?, endpoint: String) {
107+
Log.d("Receiver","OnNewEndpoint")
108+
handler.post {
109+
getPlugin(context!!).withReceiverChannel?.invokeMethod("onNewEndpoint", endpoint)
110+
}
111+
}
112+
113+
override fun onRegistrationFailed(context: Context?) {
114+
handler.post {
115+
getPlugin(context!!).withReceiverChannel?.invokeMethod("onRegistrationFailed", null)
116+
}
117+
}
118+
119+
override fun onRegistrationRefused(context: Context?) {
120+
Log.d("Receiver","OnRegistrationRefused")
121+
handler.post {
122+
getPlugin(context!!).withReceiverChannel?.invokeMethod("onRegistrationRefused", null)
123+
}
124+
}
125+
126+
override fun onUnregistered(context: Context?) {
127+
Log.d("Receiver","OnUnregistered")
128+
handler.post {
129+
getPlugin(context!!).withReceiverChannel?.invokeMethod("onUnregistered", null)
130+
}
131+
}
132+
}

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class _MyAppState extends State<MyApp> {
2323

2424
@override
2525
void initState() {
26-
UnifiedPush.initialize(
26+
UnifiedPush.initializeWithCallback(
2727
onNewEndpoint,
2828
onRegistrationFailed,
2929
onRegistrationRefused,

lib/CallbackDispatcher.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:flutter/foundation.dart';
44
import 'package:flutter/services.dart';
55
import 'package:shared_preferences/shared_preferences.dart';
66
import 'package:unifiedpush/Constants.dart';
7-
import 'main.dart';
7+
import 'unifiedpush.dart';
88

99
void callbackDispatcher() {
1010

lib/main.dart

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)