improve broadcast receiver antiduplication
This commit is contained in:
parent
a03a665a66
commit
886e0956f6
|
@ -40,7 +40,6 @@ import java.time.Duration
|
|||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
|
||||
override fun provideSplashScreen(): SplashScreen? = SplashView()
|
||||
|
||||
// Channel to get app info
|
||||
|
@ -56,6 +55,7 @@ class MainActivity: FlutterActivity() {
|
|||
// Channel to trigger contactview when an external notification is clicked
|
||||
private val CHANNEL_NOTIF_CLICK = "im.cwtch.flwtch/notificationClickHandler"
|
||||
|
||||
private var myReceiver: MyBroadcastReceiver? = null
|
||||
private var methodChan: MethodChannel? = null
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
|
@ -113,35 +113,19 @@ class MainActivity: FlutterActivity() {
|
|||
// accidentally duplicated. however, we still need to manually check if it's running or not, so
|
||||
// that we can divert this method call to ReconnectCwtchForeground instead if so.
|
||||
val works = WorkManager.getInstance(this).getWorkInfosByTag(workerTag).get()
|
||||
var alreadyRunning = false
|
||||
for (workInfo in works) {
|
||||
if (workInfo.tags.contains(uniqueTag)) {
|
||||
if (workInfo.state == WorkInfo.State.RUNNING || workInfo.state == WorkInfo.State.ENQUEUED) {
|
||||
alreadyRunning = true
|
||||
}
|
||||
} else {
|
||||
if (!workInfo.tags.contains(uniqueTag)) {
|
||||
WorkManager.getInstance(this).cancelWorkById(workInfo.id)
|
||||
}
|
||||
}
|
||||
|
||||
// register our eventbus listener. note that we observe any/all work according to its tag, which
|
||||
// results in an implicit "reconnection" to old service threads even after frontend restarts
|
||||
val mc = MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger, CWTCH_EVENTBUS)
|
||||
val filter = IntentFilter("im.cwtch.flwtch.broadcast.SERVICE_EVENT_BUS")
|
||||
LocalBroadcastManager.getInstance(applicationContext).registerReceiver(MyBroadcastReceiver(mc), filter)
|
||||
|
||||
if (alreadyRunning) {
|
||||
Log.i("MainActivity.kt", "diverting Start -> Reconnect")
|
||||
method = "ReconnectCwtchForeground"
|
||||
} else {
|
||||
Log.i("MainActivity.kt", "Start() launching foregroundservice")
|
||||
// this is where the eventbus ForegroundService gets launched. WorkManager should keep it alive after this
|
||||
val data: Data = Data.Builder().putString(FlwtchWorker.KEY_METHOD, call.method).putString(FlwtchWorker.KEY_ARGS, JSONObject(argmap).toString()).build()
|
||||
// 15 minutes is the shortest interval you can request
|
||||
val workRequest = PeriodicWorkRequestBuilder<FlwtchWorker>(15, TimeUnit.MINUTES).setInputData(data).addTag(workerTag).addTag(uniqueTag).build()
|
||||
WorkManager.getInstance(this).enqueueUniquePeriodicWork("req_$uniqueTag", ExistingPeriodicWorkPolicy.KEEP, workRequest)
|
||||
return
|
||||
}
|
||||
Log.i("MainActivity.kt", "Start() launching foregroundservice")
|
||||
// this is where the eventbus ForegroundService gets launched. WorkManager should keep it alive after this
|
||||
val data: Data = Data.Builder().putString(FlwtchWorker.KEY_METHOD, call.method).putString(FlwtchWorker.KEY_ARGS, JSONObject(argmap).toString()).build()
|
||||
// 15 minutes is the shortest interval you can request
|
||||
val workRequest = PeriodicWorkRequestBuilder<FlwtchWorker>(15, TimeUnit.MINUTES).setInputData(data).addTag(workerTag).addTag(uniqueTag).build()
|
||||
WorkManager.getInstance(this).enqueueUniquePeriodicWork("req_$uniqueTag", ExistingPeriodicWorkPolicy.REPLACE, workRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// ...otherwise fallthru to a normal ffi method call (and return the result using the result callback)
|
||||
|
@ -158,6 +142,26 @@ class MainActivity: FlutterActivity() {
|
|||
)
|
||||
}
|
||||
|
||||
// using onresume/onstop for broadcastreceiver because of extended discussion on https://stackoverflow.com/questions/7439041/how-to-unregister-broadcastreceiver
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
if (myReceiver == null) {
|
||||
val mc = MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger, CWTCH_EVENTBUS)
|
||||
val filter = IntentFilter("im.cwtch.flwtch.broadcast.SERVICE_EVENT_BUS")
|
||||
myReceiver = MyBroadcastReceiver(mc)
|
||||
LocalBroadcastManager.getInstance(applicationContext).registerReceiver(myReceiver!!, filter)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
if (myReceiver != null) {
|
||||
LocalBroadcastManager.getInstance(applicationContext).unregisterReceiver(myReceiver!!);
|
||||
myReceiver = null;
|
||||
}
|
||||
}
|
||||
|
||||
// source: https://web.archive.org/web/20210203022531/https://stackoverflow.com/questions/41928803/how-to-parse-json-in-kotlin/50468095
|
||||
// for reference:
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue