Skip to content

Commit

Permalink
Add hasPrepared method for Android & Fix prepare crash (X-dea#56)
Browse files Browse the repository at this point in the history
* Add hasPrepared method to help us show configure screen, and don't throw errors for prepare issues
* fix build issue
* unregister to fix duplicate calls firing
* bump version
  • Loading branch information
kamranpirwani authored May 18, 2020
1 parent 20399b0 commit 3b6b903
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
27 changes: 20 additions & 7 deletions android/src/main/kotlin/io/xdea/flutter_vpn/FlutterVpnPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import org.strongswan.android.logic.VpnStateService
import io.flutter.plugin.common.PluginRegistry

/** FlutterVpnPlugin */
class FlutterVpnPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
Expand Down Expand Up @@ -99,23 +100,35 @@ class FlutterVpnPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
"hasPrepared" -> {
val intent = VpnService.prepare(activityBinding.activity.applicationContext)
result.success(intent == null)
}
"prepare" -> {
val intent = VpnService.prepare(activityBinding.activity.applicationContext)
if (intent != null) {
activityBinding.addActivityResultListener { req, res, _ ->
if (req == 0 && res == RESULT_OK)
result.success(null)
else
result.error("PrepareError", "Failed to prepare", false)
var listener: PluginRegistry.ActivityResultListener? = null
listener = PluginRegistry.ActivityResultListener { req, res, _ ->
if (req == 0 && res == RESULT_OK) {
result.success(true)
} else {
result.success(false)
}
listener?.let { activityBinding.removeActivityResultListener(it) };
true
}
activityBinding.addActivityResultListener(listener)
activityBinding.activity.startActivityForResult(intent, 0)
} else {
// If intent is null, already prepared
result.success(true)
}
}
"connect" -> {
val intent = VpnService.prepare(activityBinding.activity.applicationContext)
if (intent != null) {
result.error("PrepareError", "Not prepared", false)
// Not prepared yet
result.success(false)
return
}

Expand All @@ -129,7 +142,7 @@ class FlutterVpnPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
profileInfo.putInt("MTU", map["mtu"]?.toInt() ?: 1400)

vpnStateService?.connect(profileInfo, true)
result.success(null)
result.success(true)
}
"getCurrentState" -> {
if (vpnStateService?.errorState != VpnStateService.ErrorState.NO_ERROR)
Expand Down
10 changes: 8 additions & 2 deletions lib/flutter_vpn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ class FlutterVpn {
/// you should prepare again before reconnect.
///
/// Do nothing in iOS.
static Future<Null> prepare() async {
if (!Platform.isAndroid) return Null;
static Future<bool> prepare() async {
if (!Platform.isAndroid) return true;
return await _channel.invokeMethod('prepare');
}

static Future<bool> hasPrepared() async {
if (!Platform.isAndroid) return true;
final response = await _channel.invokeMethod('hasPrepared');
return response;
}

/// Disconnect and stop VPN service.
static Future<Null> disconnect() async {
await _channel.invokeMethod('disconnect');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_vpn
description: Plugin for developers to access VPN service in their flutter app.
version: 0.8.0
version: 0.8.1
authors:
- Jason C.H <[email protected]>
- Jerry Wang <[email protected]>
Expand Down

0 comments on commit 3b6b903

Please sign in to comment.