-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #932 from getlantern/ox/ios-migrate/android
- Loading branch information
Showing
26 changed files
with
388 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Git LFS file not shown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
android/app/src/main/kotlin/io/lantern/model/GoModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package io.lantern.model | ||
|
||
import internalsdk.Arguments | ||
import internalsdk.SubscriptionRequest | ||
import internalsdk.UpdaterModel | ||
import io.flutter.embedding.engine.FlutterEngine | ||
import io.flutter.plugin.common.EventChannel | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.lantern.db.DB | ||
import minisql.Value | ||
import org.getlantern.mobilesdk.Logger | ||
|
||
open class GoModel<M : internalsdk.Model> constructor( | ||
val name: String, | ||
flutterEngine: FlutterEngine, | ||
db: DB, | ||
protected val model: M, | ||
) : BaseModel(name, flutterEngine, db) { | ||
override fun doOnMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
try { | ||
val resultVal = model.invokeMethod(call.method, Arguments(call)) | ||
result.success(resultVal.toJava()) | ||
} catch (t: Throwable) { | ||
result.error("unknownError", t.message, null) | ||
} | ||
} | ||
|
||
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { | ||
activeSink.set(events) | ||
val args = arguments as Map<String, Any> | ||
val subscriberID = args["subscriberID"] as String | ||
val path = args["path"] as String | ||
val details = args["details"]?.let { it as Boolean } ?: false | ||
activeSubscribers.add(subscriberID) | ||
|
||
val req = SubscriptionRequest() | ||
req.receiveInitial = true | ||
req.id = subscriberID | ||
req.joinDetails = details | ||
req.pathPrefixes = path | ||
req.updater = UpdaterModel { | ||
val updates = HashMap<String, Any>() | ||
val deletions = ArrayList<String>() | ||
while (it.hasUpdate()) { | ||
val update = it.popUpdate() | ||
updates[update.path] = update.value.toJava() | ||
} | ||
while (it.hasDelete()) { | ||
deletions.add(it.popDelete()) | ||
} | ||
Logger.debug("GoModel", "notifying $activeSink.get() on path $path updates: $updates") | ||
mainHandler.post { | ||
synchronized(this@GoModel) { | ||
activeSink.get()?.success( | ||
mapOf( | ||
"s" to subscriberID, | ||
"u" to updates, | ||
"d" to deletions, | ||
) | ||
) | ||
} | ||
} | ||
} | ||
model.subscribe(req) | ||
} | ||
|
||
override fun onCancel(arguments: Any?) { | ||
if (arguments == null) { | ||
return | ||
} | ||
val args = arguments as Map<String, Any> | ||
val subscriberID = args["subscriberID"] as String | ||
model.unsubscribe(subscriberID) | ||
activeSubscribers.remove(subscriberID) | ||
} | ||
} | ||
|
||
private class Arguments constructor(private val call: MethodCall) : Arguments { | ||
override fun get(key: String): Value? = | ||
when (val arg = call.argument<Any>(key)) { | ||
is Long -> minisql.Value(arg) | ||
is Int -> minisql.Value(arg.toLong()) | ||
is String -> minisql.Value(arg) | ||
is Boolean -> minisql.Value(arg) | ||
is ByteArray -> minisql.Value(arg) | ||
else -> null | ||
} | ||
|
||
override fun scalar(): Value? = | ||
when (val arg = call.arguments) { | ||
is Long -> minisql.Value(arg) | ||
is Int -> minisql.Value(arg.toLong()) | ||
is String -> minisql.Value(arg) | ||
is Boolean -> minisql.Value(arg) | ||
is ByteArray -> minisql.Value(arg) | ||
else -> null | ||
} | ||
} | ||
|
||
fun Value.toJava(): Any = when (type) { | ||
0L -> bytes() | ||
1L -> string() | ||
2L -> int_() | ||
3L -> bool() | ||
else -> throw RuntimeException("unknown value type $type") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,42 @@ | ||
package io.lantern.model | ||
|
||
import android.app.Activity | ||
import internalsdk.VPNManager | ||
import internalsdk.VPNModel | ||
import io.flutter.embedding.engine.FlutterEngine | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import org.getlantern.lantern.util.castToBoolean | ||
import org.getlantern.mobilesdk.Logger | ||
import io.lantern.model.dbadapter.DBAdapter | ||
|
||
class VpnModel( | ||
activity: Activity, | ||
flutterEngine: FlutterEngine, | ||
private var switchLanternHandler: ((vpnOn: Boolean) -> Unit)? = null, | ||
) : BaseModel("vpn", flutterEngine, masterDB.withSchema(VPN_SCHEMA)) { | ||
|
||
companion object { | ||
private const val TAG = "VpnModel" | ||
const val VPN_SCHEMA = "vpn" | ||
|
||
const val PATH_VPN_STATUS = "/vpn_status" | ||
const val PATH_SERVER_INFO = "/server_info" | ||
const val PATH_BANDWIDTH = "/bandwidth" | ||
} | ||
) : GoModel<VPNModel>( | ||
"vpn", | ||
flutterEngine, | ||
masterDB.withSchema("vpn"), | ||
VPNModel(DBAdapter(masterDB.db)) | ||
) { | ||
|
||
init { | ||
val start = System.currentTimeMillis() | ||
db.registerType(1000, Vpn.ServerInfo::class.java) | ||
db.registerType(1001, Vpn.Bandwidth::class.java) | ||
db.mutate { tx -> | ||
// initialize vpn status for fresh install | ||
tx.put(PATH_VPN_STATUS, tx.get<String>(PATH_VPN_STATUS) ?: "disconnected") | ||
} | ||
Logger.debug(TAG, "db.mutate finished at ${System.currentTimeMillis() - start}") | ||
} | ||
model.setManager(object : VPNManager { | ||
override fun startVPN() { | ||
switchLanternHandler?.invoke(true) | ||
} | ||
|
||
override fun doMethodCall(call: MethodCall, notImplemented: () -> Unit): Any? { | ||
return when (call.method) { | ||
"switchVPN" -> { | ||
val on = call.argument<Boolean>("on") ?: false | ||
saveVpnStatus(if (on) "connecting" else "disconnecting") | ||
switchLantern(on) | ||
override fun stopVPN() { | ||
switchLanternHandler?.invoke(false) | ||
} | ||
else -> super.doMethodCall(call, notImplemented) | ||
} | ||
}) | ||
} | ||
|
||
fun isConnectedToVpn(): Boolean { | ||
val vpnStatus = vpnStatus() | ||
val vpnStatus = model.vpnStatus | ||
return vpnStatus == "connected" || vpnStatus == "disconnecting" | ||
} | ||
|
||
private fun vpnStatus(): String { | ||
return db.get(PATH_VPN_STATUS) ?: "" | ||
} | ||
|
||
private fun switchLantern(value: Boolean) { | ||
switchLanternHandler?.invoke(value) | ||
} | ||
|
||
fun setVpnOn(vpnOn: Boolean) { | ||
val vpnStatus = if (vpnOn) "connected" else "disconnected" | ||
saveVpnStatus(vpnStatus) | ||
} | ||
|
||
fun saveVpnStatus(vpnStatus: String) { | ||
db.mutate { tx -> | ||
tx.put(PATH_VPN_STATUS, vpnStatus) | ||
} | ||
} | ||
|
||
fun saveServerInfo(serverInfo: Vpn.ServerInfo) { | ||
db.mutate { tx -> | ||
tx.put(PATH_SERVER_INFO, serverInfo) | ||
} | ||
model.switchVPN(vpnOn) | ||
} | ||
|
||
fun saveBandwidth(bandwidth: Vpn.Bandwidth) { | ||
Logger.d( | ||
TAG, "Bandwidth updated to " + bandwidth.remaining + " remaining out of " + | ||
bandwidth.allowed + " allowed" | ||
) | ||
db.mutate { tx -> | ||
tx.put(PATH_BANDWIDTH, bandwidth) | ||
} | ||
fun updateStatus(vpnOn: Boolean) { | ||
model.saveVPNStatus(if (vpnOn) "connected" else "disconnected") | ||
} | ||
} |
Oops, something went wrong.