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 716ad96b5..a798b8253 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 @@ -367,8 +367,9 @@ abstract class SessionManager(application: Application) : Session { override fun bandwidthUpdate(percent: Long, remaining: Long, allowed: Long, ttlSeconds: Long) { val b = Bandwidth(percent, remaining, allowed, ttlSeconds) + Logger.debug("bandwidth", b.toString()) saveLatestBandwidth(b) - EventBus.getDefault().post(b) + EventBus.getDefault().postSticky(b) } fun setSurveyLinkOpened(url: String?) { diff --git a/android/app/src/main/kotlin/io/lantern/model/VpnModel.kt b/android/app/src/main/kotlin/io/lantern/model/VpnModel.kt index bc8aa57df..f7ebcabc0 100644 --- a/android/app/src/main/kotlin/io/lantern/model/VpnModel.kt +++ b/android/app/src/main/kotlin/io/lantern/model/VpnModel.kt @@ -4,6 +4,8 @@ import internalsdk.VPNManager import internalsdk.VPNModel import io.flutter.embedding.engine.FlutterEngine import io.lantern.model.dbadapter.DBAdapter +import org.getlantern.lantern.model.Bandwidth +import org.getlantern.mobilesdk.Logger class VpnModel( flutterEngine: FlutterEngine, @@ -39,4 +41,17 @@ class VpnModel( fun updateStatus(vpnOn: Boolean) { model.saveVPNStatus(if (vpnOn) "connected" else "disconnected") } + + fun updateBandwidth(bandwidth: Bandwidth) { + try { + model.updateBandwidth( + bandwidth.percent, + bandwidth.remaining, + bandwidth.allowed, + bandwidth.ttlSeconds, + ) + } catch (t: Throwable) { + Logger.error("VPNModel","Error updating bandwidth", t) + } + } } 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 03a2671d0..83f8846d6 100644 --- a/android/app/src/main/kotlin/org/getlantern/lantern/MainActivity.kt +++ b/android/app/src/main/kotlin/org/getlantern/lantern/MainActivity.kt @@ -176,9 +176,7 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, override fun onResume() { val start = System.currentTimeMillis() - super.onResume() - Logger.debug(TAG, "super.onResume() finished at ${System.currentTimeMillis() - start}") val isServiceRunning = Utils.isServiceRunning(activity, LanternVpnService::class.java) if (vpnModel.isConnectedToVpn() && !isServiceRunning) { @@ -292,6 +290,12 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler, ) } + @Subscribe(threadMode = ThreadMode.MAIN) + fun bandwidthUpdated(bandwidth: Bandwidth) { + Logger.debug("bandwidth updated", bandwidth.toString()) + vpnModel.updateBandwidth(bandwidth) + } + private fun updateUserData() { lanternClient.userData( object : ProUserCallback { diff --git a/internalsdk/vpn_model.go b/internalsdk/vpn_model.go index de541013b..d9acbcd6a 100644 --- a/internalsdk/vpn_model.go +++ b/internalsdk/vpn_model.go @@ -2,6 +2,7 @@ package internalsdk import ( "github.com/getlantern/errors" + "github.com/getlantern/lantern-client/internalsdk/protos" "github.com/getlantern/pathdb" "github.com/getlantern/pathdb/minisql" ) @@ -30,6 +31,7 @@ func NewVPNModel(mdb minisql.DB) (*VPNModel, error) { } model := &VPNModel{baseModel: base} model.baseModel.doInvokeMethod = model.doInvokeMethod + base.db.RegisterType(5000, &protos.Bandwidth{}) return model, model.initVpnModel() } @@ -100,6 +102,18 @@ func (m *VPNModel) SaveVPNStatus(status string) error { }) } +func (m *VPNModel) UpdateBandwidth(percent int64, remaining int64, allowedint int64, ttlSeconds int64) error { + return pathdb.Mutate(m.db, func(tx pathdb.TX) error { + bandwidth := &protos.Bandwidth{ + Percent: percent, + Remaining: remaining, + Allowed: allowedint, + TtlSeconds: ttlSeconds, + } + return pathdb.Put(tx, pathBandwidth, bandwidth, "") + }) +} + func (m *VPNModel) GetVPNStatus() (string, error) { return pathdb.Get[string](m.db, pathVPNStatus) } diff --git a/lib/vpn/vpn_bandwidth.dart b/lib/vpn/vpn_bandwidth.dart index b37f1cd56..1cb8ca2da 100644 --- a/lib/vpn/vpn_bandwidth.dart +++ b/lib/vpn/vpn_bandwidth.dart @@ -1,6 +1,8 @@ import 'package:lantern/vpn/vpn.dart'; class VPNBandwidth extends StatelessWidget { + const VPNBandwidth({super.key}); + @override Widget build(BuildContext context) { return vpnModel diff --git a/lib/vpn/vpn_switch.dart b/lib/vpn/vpn_switch.dart index bbe44ab07..2c68eaac7 100644 --- a/lib/vpn/vpn_switch.dart +++ b/lib/vpn/vpn_switch.dart @@ -1,3 +1,4 @@ +import 'package:lantern/ad_helper.dart'; import 'package:lantern/common/common.dart'; import 'package:lantern/common/common_desktop.dart'; import 'package:lantern/vpn/vpn.dart'; diff --git a/lib/vpn/vpn_tab.dart b/lib/vpn/vpn_tab.dart index b7209f000..6f0362638 100644 --- a/lib/vpn/vpn_tab.dart +++ b/lib/vpn/vpn_tab.dart @@ -49,8 +49,8 @@ class VPNTab extends StatelessWidget { if(Platform.isAndroid)...{ const CDivider(height: 32.0), SplitTunnelingWidget(), - // Not sure about this - VPNBandwidth(), + if(!proUser) + const VPNBandwidth(), } ], ),