From aafd58b96601e31f2b89c9893c30aa3e4341445e Mon Sep 17 00:00:00 2001 From: atavism Date: Fri, 21 Jun 2024 14:28:09 -0700 Subject: [PATCH] Remove use of org.greenrobot.eventbus and implement event bus using shared flow (#1103) * Remove use of org.greenrobot.eventbus * Remove use of org.greenrobot.eventbus * Add EventHandler * Update how we subscribe to app events * Update how we subscribe to app events * Update how we subscribe to app events * clean-ups * handle LocaleEvent * Add proguard rules for J2ObjC Annotations --- android/app/build.gradle | 10 - android/app/proguard-rules.pro | 4 + .../mobilesdk/model/SessionManager.kt | 10 +- .../mobilesdk/util/DnsDetector.java | 2 - .../org/getlantern/lantern/MainActivity.kt | 225 +++++++++--------- .../org/getlantern/lantern/event/EventBus.kt | 113 +++++++++ .../lantern/model/LanternSessionManager.kt | 2 - .../notification/NotificationReceiver.kt | 5 +- .../lantern/service/LanternService.kt | 21 +- pubspec.lock | 11 +- pubspec.yaml | 8 + 11 files changed, 255 insertions(+), 156 deletions(-) create mode 100644 android/app/src/main/kotlin/org/getlantern/lantern/event/EventBus.kt diff --git a/android/app/build.gradle b/android/app/build.gradle index c12fd8560..436c5d9e5 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -384,16 +384,6 @@ dependencies { // Google Play libraries implementation 'com.android.billingclient:billing:6.2.0' - // lib that simplifies event bus communication between activities, fragments, threads, services, etc - implementation 'org.greenrobot:eventbus:3.3.1' - - // https://mvnrepository.com/artifact/net.jodah/expiringmap -// implementation group: 'net.jodah', name: 'expiringmap', version: '0.5.9' -// implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0' -// implementation group: 'commons-codec', name: 'commons-codec', version: '1.12' -// https://mvnrepository.com/artifact/javax.mail/mail -// implementation group: 'javax.mail', name: 'mail', version: '1.4.7' - implementation 'com.stripe:stripe-android:20.17.0' annotationProcessor "org.androidannotations:androidannotations:$androidAnnotationsVersion" implementation("org.androidannotations:androidannotations-api:$androidAnnotationsVersion") diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro index dbfd1470a..84bca66ab 100644 --- a/android/app/proguard-rules.pro +++ b/android/app/proguard-rules.pro @@ -109,6 +109,10 @@ -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken +# J2ObjC Annotations +-dontwarn com.google.j2objc.annotations.ReflectionSupport$Level +-dontwarn com.google.j2objc.annotations.ReflectionSupport +-dontwarn com.google.j2objc.annotations.RetainedWith ## Lifecycle #-keep class androidx.lifecycle.** {*;} diff --git a/android/app/src/main/java/org/getlantern/mobilesdk/model/SessionManager.kt b/android/app/src/main/java/org/getlantern/mobilesdk/model/SessionManager.kt index d422991c2..b9bcc7ad1 100644 --- a/android/app/src/main/java/org/getlantern/mobilesdk/model/SessionManager.kt +++ b/android/app/src/main/java/org/getlantern/mobilesdk/model/SessionManager.kt @@ -25,6 +25,7 @@ import io.lantern.model.Vpn import org.getlantern.lantern.BuildConfig import org.getlantern.lantern.LanternApp import org.getlantern.lantern.model.Bandwidth +import org.getlantern.lantern.event.EventHandler import org.getlantern.lantern.model.Stats import org.getlantern.lantern.model.Utils import org.getlantern.mobilesdk.Logger @@ -32,7 +33,6 @@ import org.getlantern.mobilesdk.Settings import org.getlantern.mobilesdk.StartResult import org.getlantern.mobilesdk.util.DnsDetector import org.getlantern.mobilesdk.util.LanguageHelper -import org.greenrobot.eventbus.EventBus import java.io.PrintWriter import java.io.StringWriter import java.lang.reflect.InvocationTargetException @@ -136,7 +136,7 @@ abstract class SessionManager(application: Application) : Session { val oldLocale = prefs.getString(LANG, "") prefs.edit().putString(LANG, locale.toString()).apply() if (locale.language != oldLocale) { - EventBus.getDefault().post(locale) + EventHandler.postLocaleEvent(locale) } } } @@ -183,7 +183,7 @@ abstract class SessionManager(application: Application) : Session { } override fun updateAdSettings(adSettings: AdSettings) { - EventBus.getDefault().post(adSettings) + } /** @@ -356,7 +356,7 @@ abstract class SessionManager(application: Application) : Session { val b = Bandwidth(percent, remaining, allowed, ttlSeconds) Logger.debug("bandwidth", b.toString()) saveLatestBandwidth(b) - EventBus.getDefault().postSticky(b) + EventHandler.postBandwidthEvent(b) } fun setSurveyLinkOpened(url: String?) { @@ -401,7 +401,7 @@ abstract class SessionManager(application: Application) : Session { } val st = Stats(city, country, countryCode, httpsUpgrades, adsBlocked, hasSucceedingProxy) - EventBus.getDefault().postSticky(st) + EventHandler.postStatsEvent(st) // save last location received prefs.edit().putString(SERVER_COUNTRY, country) diff --git a/android/app/src/main/java/org/getlantern/mobilesdk/util/DnsDetector.java b/android/app/src/main/java/org/getlantern/mobilesdk/util/DnsDetector.java index 3afa957df..486015e26 100644 --- a/android/app/src/main/java/org/getlantern/mobilesdk/util/DnsDetector.java +++ b/android/app/src/main/java/org/getlantern/mobilesdk/util/DnsDetector.java @@ -14,7 +14,6 @@ import org.getlantern.mobilesdk.Logger; import org.getlantern.mobilesdk.model.Event; -import org.greenrobot.eventbus.EventBus; import java.net.Inet6Address; import java.net.InetAddress; @@ -114,7 +113,6 @@ private String doGetDnsServer() { public void publishNetworkAvailability() { if (findActiveNetwork() == null) { Logger.debug(TAG, "No network available"); - EventBus.getDefault().postSticky(Event.NoNetworkAvailable); } } diff --git a/android/app/src/main/kotlin/org/getlantern/lantern/MainActivity.kt b/android/app/src/main/kotlin/org/getlantern/lantern/MainActivity.kt index bdbcd0a12..7831e55ff 100644 --- a/android/app/src/main/kotlin/org/getlantern/lantern/MainActivity.kt +++ b/android/app/src/main/kotlin/org/getlantern/lantern/MainActivity.kt @@ -27,20 +27,19 @@ import io.lantern.model.VpnModel import kotlinx.coroutines.* import okhttp3.Response import org.getlantern.lantern.activity.WebViewActivity_ +import org.getlantern.lantern.event.AppEvent +import org.getlantern.lantern.event.AppEvent.* +import org.getlantern.lantern.event.EventHandler import org.getlantern.lantern.event.EventManager import org.getlantern.lantern.model.AccountInitializationStatus -import org.getlantern.lantern.model.Bandwidth import org.getlantern.lantern.model.LanternHttpClient import org.getlantern.lantern.model.LanternHttpClient.PlansV3Callback import org.getlantern.lantern.model.LanternHttpClient.ProUserCallback -import org.getlantern.lantern.model.LanternStatus import org.getlantern.lantern.model.PaymentMethods import org.getlantern.lantern.model.ProError import org.getlantern.lantern.model.ProPlan import org.getlantern.lantern.model.ProUser -import org.getlantern.lantern.model.Stats import org.getlantern.lantern.model.Utils -import org.getlantern.lantern.model.VpnState import org.getlantern.lantern.notification.NotificationHelper import org.getlantern.lantern.notification.NotificationReceiver import org.getlantern.lantern.plausible.Plausible @@ -53,13 +52,12 @@ import org.getlantern.mobilesdk.model.Event import org.getlantern.mobilesdk.model.LoConf import org.getlantern.mobilesdk.model.LoConf.Companion.fetch import org.getlantern.mobilesdk.model.Survey -import org.greenrobot.eventbus.EventBus -import org.greenrobot.eventbus.Subscribe -import org.greenrobot.eventbus.ThreadMode import java.util.Locale import java.util.concurrent.* -class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, +class MainActivity : + FlutterActivity(), + MethodChannel.MethodCallHandler, CoroutineScope by MainScope() { private lateinit var messagingModel: MessagingModel private lateinit var vpnModel: VpnModel @@ -84,31 +82,72 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, replicaModel = ReplicaModel(this, flutterEngine) receiver = NotificationReceiver() notifications = NotificationHelper(this, receiver) - eventManager = object : EventManager("lantern_event_channel", flutterEngine) { - override fun onListen(event: Event) { - if (LanternApp.getSession().lanternDidStart()) { - Plausible.init(applicationContext) - Logger.debug(TAG, "Plausible initialized") - Plausible.enable(true) - fetchLoConf() - Logger.debug( - TAG, - "fetchLoConf() finished at ${System.currentTimeMillis() - start}", + eventManager = + object : EventManager("lantern_event_channel", flutterEngine) { + override fun onListen(event: Event) { + if (LanternApp.getSession().lanternDidStart()) { + Plausible.init(applicationContext) + Logger.debug(TAG, "Plausible initialized") + Plausible.enable(true) + fetchLoConf() + Logger.debug( + TAG, + "fetchLoConf() finished at ${System.currentTimeMillis() - start}", + ) + } + LanternApp.getSession().dnsDetector.publishNetworkAvailability() + } + } + EventHandler.subscribeAppEvents { appEvent -> + when (appEvent) { + is AppEvent.AccountInitializationEvent -> onInitializingAccount(appEvent.status) + is AppEvent.BandwidthEvent -> { + val event = appEvent as AppEvent.BandwidthEvent + Logger.debug("bandwidth updated", event.bandwidth.toString()) + vpnModel.updateBandwidth(event.bandwidth) + } + is AppEvent.LoConfEvent -> { + doProcessLoconf(appEvent.loconf) + } + is AppEvent.LocaleEvent -> { + // Recreate the activity when the language changes + recreate() + } + is AppEvent.StatsEvent -> { + val stats = appEvent.stats + Logger.debug("Stats updated", stats.toString()) + sessionModel.saveServerInfo( + Vpn.ServerInfo + .newBuilder() + .setCity(stats.city) + .setCountry(stats.country) + .setCountryCode(stats.countryCode) + .build(), ) } - LanternApp.getSession().dnsDetector.publishNetworkAvailability() + is AppEvent.StatusEvent -> { + updateUserData() + updatePaymentMethods() + updateCurrencyList() + } + is AppEvent.VpnStateEvent -> { + updateStatus(appEvent.vpnState.useVpn) + } + else -> { + Logger.debug(TAG, "Unknown app event " + appEvent) + } } } - MethodChannel( flutterEngine.dartExecutor.binaryMessenger, "lantern_method_channel", ).setMethodCallHandler(this) - flutterNavigation = MethodChannel( - flutterEngine.dartExecutor.binaryMessenger, - "navigation", - ) + flutterNavigation = + MethodChannel( + flutterEngine.dartExecutor.binaryMessenger, + "navigation", + ) flutterNavigation.setMethodCallHandler { call, _ -> if (call.method == "ready") { @@ -131,10 +170,6 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, super.onCreate(savedInstanceState) Logger.debug(TAG, "Default Locale is %1\$s", Locale.getDefault()) - if (!EventBus.getDefault().isRegistered(this)) { - EventBus.getDefault().register(this) - } - Logger.debug(TAG, "EventBus.register finished at ${System.currentTimeMillis() - start}") val intent = Intent(this, LanternService_::class.java) context.startService(intent) @@ -198,7 +233,6 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, vpnModel.destroy() sessionModel.destroy() replicaModel.destroy() - EventBus.getDefault().unregister(this) } override fun onMethodCall( @@ -220,14 +254,13 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, * settings */ private fun fetchLoConf() { - fetch { loconf -> runOnUiThread { processLoconf(loconf) } } + fetch { loconf -> runOnUiThread { doProcessLoconf(loconf) } } } - @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) - fun onInitializingAccount(status: AccountInitializationStatus) { + fun onInitializingAccount(status: AccountInitializationStatus.Status) { val appName = getString(R.string.app_name) - when (status.status) { + when (status) { AccountInitializationStatus.Status.PROCESSING -> { accountInitDialog = AlertDialog.Builder(this).create() accountInitDialog?.setCancelable(false) @@ -237,7 +270,6 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, val tvMessage: TextView = dialogView.findViewById(R.id.tvMessage) tvMessage.text = getString(R.string.init_account, appName) dialogView.findViewById(R.id.btnCancel).setOnClickListener { - EventBus.getDefault().removeStickyEvent(status) accountInitDialog?.dismiss() finish() } @@ -245,12 +277,10 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, } AccountInitializationStatus.Status.SUCCESS -> { - EventBus.getDefault().removeStickyEvent(status) accountInitDialog?.let { it.dismiss() } } AccountInitializationStatus.Status.FAILURE -> { - EventBus.getDefault().removeStickyEvent(status) accountInitDialog?.let { it.dismiss() } Utils.showAlertDialog( @@ -266,38 +296,6 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, } } - @Subscribe(threadMode = ThreadMode.MAIN) - fun vpnStateChanged(state: VpnState) { - updateStatus(state.useVpn) - } - - @Subscribe(threadMode = ThreadMode.MAIN) - fun lanternStarted(status: LanternStatus) { - updateUserData() - updatePaymentMethods() - updateCurrencyList(); - } - - @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) - fun onEvent(event: Event) { - eventManager.onNewEvent(event = event) - } - - @Subscribe(threadMode = ThreadMode.MAIN) - fun statsUpdated(stats: Stats) { - Logger.debug("Stats updated", stats.toString()) - sessionModel.saveServerInfo( - Vpn.ServerInfo.newBuilder().setCity(stats.city).setCountry(stats.country) - .setCountryCode(stats.countryCode).build(), - ) - } - - @Subscribe(threadMode = ThreadMode.MAIN) - fun bandwidthUpdated(bandwidth: Bandwidth) { - Logger.debug("bandwidth updated", bandwidth.toString()) - vpnModel.updateBandwidth(bandwidth) - } - private fun updateUserData() { lanternClient.userData( object : ProUserCallback { @@ -332,50 +330,51 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, override fun onSuccess( proPlans: Map, paymentMethods: List, - - ) { + ) { Logger.debug( TAG, - "Successfully fetched payment methods with payment methods: $paymentMethods and plans $proPlans" + "Successfully fetched payment methods with payment methods: $paymentMethods and plans $proPlans", ) sessionModel.processPaymentMethods(proPlans, paymentMethods) } - } + }, ) } private fun updateCurrencyList() { val url = LanternHttpClient.createProUrl("/supported-currencies") - lanternClient.get(url, object : LanternHttpClient.ProCallback { - override fun onFailure(throwable: Throwable?, error: ProError?) { - Logger.error(TAG, "Unable to fetch currency list: $error", throwable) + lanternClient.get( + url, + object : LanternHttpClient.ProCallback { + override fun onFailure( + throwable: Throwable?, + error: ProError?, + ) { + Logger.error(TAG, "Unable to fetch currency list: $error", throwable) /* retry to fetch currency list again fetch until we get the currency list retry after 5 seconds - */ - CoroutineScope(Dispatchers.IO).launch { - delay(5000) - updateCurrencyList() + */ + CoroutineScope(Dispatchers.IO).launch { + delay(5000) + updateCurrencyList() + } } - } - override fun onSuccess(response: Response?, result: JsonObject?) { - val currencies = result?.getAsJsonArray("supported-currencies") - val currencyList = mutableListOf() - currencies?.forEach { - currencyList.add(it.asString.lowercase()) + override fun onSuccess( + response: Response?, + result: JsonObject?, + ) { + val currencies = result?.getAsJsonArray("supported-currencies") + val currencyList = mutableListOf() + currencies?.forEach { + currencyList.add(it.asString.lowercase()) + } + LanternApp.getSession().setCurrencyList(currencyList) } - LanternApp.getSession().setCurrencyList(currencyList) - } - - }) - } - - - @Subscribe(threadMode = ThreadMode.MAIN) - fun processLoconf(loconf: LoConf) { - doProcessLoconf(loconf) + }, + ) } private fun doProcessLoconf(loconf: LoConf) { @@ -498,13 +497,14 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, msg.append(" ") var description = "..." try { - description = getString( - resources.getIdentifier( - permission, - "string", - "org.getlantern.lantern", - ), - ) + description = + getString( + resources.getIdentifier( + permission, + "string", + "org.getlantern.lantern", + ), + ) } catch (t: Throwable) { Logger.warn( PERMISSIONS_TAG, @@ -563,7 +563,7 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, } } else { sendBroadcast(notifications.disconnectIntent()) - //Update VPN status + // Update VPN status vpnModel.updateStatus(false) } } @@ -632,12 +632,13 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, } private fun startVpnService() { - val intent: Intent = Intent( - this, - LanternVpnService::class.java, - ).apply { - action = LanternVpnService.ACTION_CONNECT - } + val intent: Intent = + Intent( + this, + LanternVpnService::class.java, + ).apply { + action = LanternVpnService.ACTION_CONNECT + } startService(intent) notifications.vpnConnectedNotification() @@ -650,12 +651,6 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, vpnModel.updateStatus(useVpn) } - // Recreate the activity when the language changes - @Subscribe(threadMode = ThreadMode.MAIN) - fun languageChanged(locale: Locale) { - recreate() - } - companion object { private val TAG = MainActivity::class.java.simpleName private val SURVEY_TAG = "$TAG.survey" diff --git a/android/app/src/main/kotlin/org/getlantern/lantern/event/EventBus.kt b/android/app/src/main/kotlin/org/getlantern/lantern/event/EventBus.kt new file mode 100644 index 000000000..add2f715a --- /dev/null +++ b/android/app/src/main/kotlin/org/getlantern/lantern/event/EventBus.kt @@ -0,0 +1,113 @@ +package org.getlantern.lantern.event + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.launch +import org.getlantern.lantern.model.AccountInitializationStatus +import org.getlantern.lantern.model.Bandwidth +import org.getlantern.lantern.model.LanternStatus +import org.getlantern.lantern.model.LanternStatus.Status +import org.getlantern.lantern.model.Stats +import org.getlantern.lantern.model.VpnState +import org.getlantern.mobilesdk.model.LoConf +import kotlin.coroutines.coroutineContext +import java.util.Locale + +object EventBus { + private val _events = MutableSharedFlow() + val events = _events.asSharedFlow() + + suspend fun publish(event: Any) { + _events.emit(event) + } + + suspend inline fun subscribe(crossinline onEvent: (T) -> Unit) { + events + .filterIsInstance() + .collectLatest { event -> + coroutineContext.ensureActive() + onEvent(event) + } + } +} + +// EventHandler is used to publish app events that subscribers can listen to from anywhere +internal object EventHandler { + fun postAccountInitializationStatus(status: AccountInitializationStatus.Status) { + postAppEvent(AppEvent.AccountInitializationEvent(status)) + } + + fun postBandwidthEvent(bandwidth: Bandwidth) { + postAppEvent(AppEvent.BandwidthEvent(bandwidth)) + } + + fun postLocaleEvent(locale: Locale) { + postAppEvent(AppEvent.LocaleEvent(locale)) + } + + fun postLoConfEvent(loconf: LoConf) { + postAppEvent(AppEvent.LoConfEvent(loconf)) + } + + fun postStatsEvent(stats: Stats) { + postAppEvent(AppEvent.StatsEvent(stats)) + } + + fun postStatusEvent(status: LanternStatus) { + postAppEvent(AppEvent.StatusEvent(status)) + } + + fun postVpnStateEvent(vpnState: VpnState) { + postAppEvent(AppEvent.VpnStateEvent(vpnState)) + } + + fun postAppEvent(appEvent: AppEvent) { + CoroutineScope(Dispatchers.IO).launch { + EventBus.publish(appEvent) + } + } + + fun subscribeAppEvents(onEvent: (AppEvent) -> Unit) { + CoroutineScope(Dispatchers.Main + SupervisorJob()).launch { + EventBus.subscribe { appEvent -> + onEvent(appEvent) + } + } + } +} + +sealed class AppEvent { + data class AccountInitializationEvent( + val status: AccountInitializationStatus.Status, + ) : AppEvent() + + data class BandwidthEvent( + val bandwidth: Bandwidth, + ) : AppEvent() + + data class LocaleEvent( + val locale: Locale, + ) : AppEvent() + + data class LoConfEvent( + val loconf: LoConf, + ) : AppEvent() + + data class StatsEvent( + val stats: Stats, + ) : AppEvent() + + data class StatusEvent( + val status: LanternStatus, + ) : AppEvent() + + data class VpnStateEvent( + val vpnState: VpnState, + ) : AppEvent() +} diff --git a/android/app/src/main/kotlin/org/getlantern/lantern/model/LanternSessionManager.kt b/android/app/src/main/kotlin/org/getlantern/lantern/model/LanternSessionManager.kt index cd18fa7fe..c54c37af4 100644 --- a/android/app/src/main/kotlin/org/getlantern/lantern/model/LanternSessionManager.kt +++ b/android/app/src/main/kotlin/org/getlantern/lantern/model/LanternSessionManager.kt @@ -8,7 +8,6 @@ import io.lantern.model.Vpn import org.getlantern.lantern.util.PlansUtil import org.getlantern.mobilesdk.Logger import org.getlantern.mobilesdk.model.SessionManager -import org.greenrobot.eventbus.EventBus import org.joda.time.LocalDateTime import java.text.SimpleDateFormat import java.util.Currency @@ -336,7 +335,6 @@ class LanternSessionManager(application: Application) : SessionManager(applicati } if (user.isProUser) { - EventBus.getDefault().post(UserStatus(user.isActive, user.monthsLeft().toLong())) prefs.edit().putInt(PRO_MONTHS_LEFT, user.monthsLeft()) .putInt(PRO_DAYS_LEFT, user.daysLeft()) .apply() diff --git a/android/app/src/main/kotlin/org/getlantern/lantern/notification/NotificationReceiver.kt b/android/app/src/main/kotlin/org/getlantern/lantern/notification/NotificationReceiver.kt index 12b0eb78e..50a934256 100644 --- a/android/app/src/main/kotlin/org/getlantern/lantern/notification/NotificationReceiver.kt +++ b/android/app/src/main/kotlin/org/getlantern/lantern/notification/NotificationReceiver.kt @@ -4,12 +4,11 @@ import android.app.NotificationManager import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import org.getlantern.lantern.event.EventHandler import org.getlantern.lantern.model.Utils import org.getlantern.lantern.model.VpnState import org.getlantern.lantern.vpn.LanternVpnService import org.getlantern.mobilesdk.Logger -import org.greenrobot.eventbus.EventBus - class NotificationReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -21,7 +20,7 @@ class NotificationReceiver : BroadcastReceiver() { LanternVpnService::class.java, ) ) { - EventBus.getDefault().post(VpnState(false)) + EventHandler.postVpnStateEvent(VpnState(false)) context.startService( Intent( context, diff --git a/android/app/src/main/kotlin/org/getlantern/lantern/service/LanternService.kt b/android/app/src/main/kotlin/org/getlantern/lantern/service/LanternService.kt index d912d0992..9094a7ddd 100644 --- a/android/app/src/main/kotlin/org/getlantern/lantern/service/LanternService.kt +++ b/android/app/src/main/kotlin/org/getlantern/lantern/service/LanternService.kt @@ -13,6 +13,7 @@ import org.androidannotations.annotations.EService import org.getlantern.lantern.BuildConfig import org.getlantern.lantern.LanternApp import org.getlantern.lantern.R +import org.getlantern.lantern.event.EventHandler import org.getlantern.lantern.model.AccountInitializationStatus import org.getlantern.lantern.model.LanternHttpClient import org.getlantern.lantern.model.LanternStatus @@ -27,7 +28,6 @@ import org.getlantern.mobilesdk.Logger import org.getlantern.mobilesdk.StartResult import org.getlantern.mobilesdk.model.LoConf import org.getlantern.mobilesdk.model.LoConfCallback -import org.greenrobot.eventbus.EventBus import java.util.Random import java.util.concurrent.atomic.AtomicBoolean @@ -104,9 +104,7 @@ open class LanternService : Service(), Runnable { private fun afterStart() { if (LanternApp.getSession().userId().toInt() == 0) { // create a user if no user id is stored - EventBus.getDefault().post( - AccountInitializationStatus(AccountInitializationStatus.Status.PROCESSING), - ) + EventHandler.postAccountInitializationStatus(AccountInitializationStatus.Status.PROCESSING) createUser(0) } @@ -114,13 +112,12 @@ open class LanternService : Service(), Runnable { // check if an update is available autoUpdater.checkForUpdates() } - - EventBus.getDefault().postSticky(LanternStatus(Status.ON)) + EventHandler.postStatusEvent(LanternStatus(Status.ON)) // fetch latest loconf LoConf.Companion.fetch(object : LoConfCallback { override fun onSuccess(loconf: LoConf) { - EventBus.getDefault().post(loconf) + EventHandler.postLoConfEvent(loconf) } }) } @@ -147,9 +144,7 @@ open class LanternService : Service(), Runnable { override fun onFailure(@Nullable throwable: Throwable?, @Nullable error: ProError?) { if (attempts >= MAX_CREATE_USER_TRIES) { Logger.error(TAG, "Max. number of tries made to create Pro user") - EventBus.getDefault().postSticky( - AccountInitializationStatus(AccountInitializationStatus.Status.FAILURE), - ) + EventHandler.postAccountInitializationStatus(AccountInitializationStatus.Status.FAILURE) return } attempts++ @@ -169,10 +164,8 @@ open class LanternService : Service(), Runnable { if (!referral.isEmpty()) { LanternApp.getSession().setCode(referral) } - EventBus.getDefault().postSticky(LanternStatus(Status.ON)) - EventBus.getDefault().postSticky( - AccountInitializationStatus(AccountInitializationStatus.Status.SUCCESS), - ) + EventHandler.postStatusEvent(LanternStatus(Status.ON)) + EventHandler.postAccountInitializationStatus(AccountInitializationStatus.Status.SUCCESS) } } diff --git a/pubspec.lock b/pubspec.lock index 9cc9afc65..202ecd349 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -612,12 +612,13 @@ packages: source: hosted version: "6.0.0" flutter_inappwebview_android: - dependency: transitive + dependency: "direct overridden" description: - name: flutter_inappwebview_android - sha256: d247f6ed417f1f8c364612fa05a2ecba7f775c8d0c044c1d3b9ee33a6515c421 - url: "https://pub.dev" - source: hosted + path: flutter_inappwebview_android + ref: d89b1d32638b49dfc58c4b7c84153be0c269d057 + resolved-ref: d89b1d32638b49dfc58c4b7c84153be0c269d057 + url: "https://github.com/holzgeist/flutter_inappwebview" + source: git version: "1.0.13" flutter_inappwebview_internal_annotations: dependency: transitive diff --git a/pubspec.yaml b/pubspec.yaml index 622131342..20942b49d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -152,6 +152,14 @@ dev_dependencies: # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec +dependency_overrides: + # TODO: Recheck once flutter_inappwebview version >6.0.0 is released + flutter_inappwebview_android: + git: + url: https://github.com/holzgeist/flutter_inappwebview + path: flutter_inappwebview_android + ref: d89b1d32638b49dfc58c4b7c84153be0c269d057 + # The following section is specific to Flutter. flutter: fonts: