Skip to content

Commit f2d13e5

Browse files
committed
🐞 fix(android): 修复多次拒绝开启 VPN 导致的闪退
1 parent 4ddb84b commit f2d13e5

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

android/src/main/java/com/norcod/rnovpn/RNSimpleOpenvpnModule.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.support.annotation.Nullable;
99
import android.util.Log;
1010

11+
import com.facebook.react.bridge.ActivityEventListener;
1112
import com.facebook.react.bridge.Arguments;
1213
import com.facebook.react.bridge.BaseActivityEventListener;
1314
import com.facebook.react.bridge.Promise;
@@ -43,6 +44,23 @@ public class RNSimpleOpenvpnModule extends ReactContextBaseJavaModule implements
4344
private static final int START_VPN_PROFILE = 70;
4445
private OpenVPNThread vpnThread = new OpenVPNThread();
4546
private VpnProfile vpnProfile;
47+
private Promise vpnPromise;
48+
49+
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
50+
@Override
51+
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
52+
if (requestCode == START_VPN_PROFILE) {
53+
if (vpnPromise != null) {
54+
if (resultCode == RESULT_OK) {
55+
startVpn(vpnPromise);
56+
} else {
57+
vpnPromise.reject("E_PREPARE_ERROR", "Prepare VPN failed");
58+
vpnPromise = null;
59+
}
60+
}
61+
}
62+
}
63+
};
4664

4765
private enum VpnState {
4866
VPN_STATE_DISCONNECTED,
@@ -57,6 +75,7 @@ private enum VpnState {
5775
public RNSimpleOpenvpnModule(ReactApplicationContext context) {
5876
super(context);
5977
reactContext = context;
78+
reactContext.addActivityEventListener(mActivityEventListener);
6079
VpnStatus.addStateListener(this);
6180
}
6281

@@ -110,25 +129,15 @@ private void prepareVpn(final Promise promise) {
110129
return;
111130
}
112131

132+
vpnPromise = promise;
113133
Intent intent = VpnService.prepare(currentActivity);
114134

115135
if (intent != null) {
116-
reactContext.addActivityEventListener(new BaseActivityEventListener() {
117-
@Override
118-
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
119-
if (resultCode == RESULT_OK && requestCode == START_VPN_PROFILE) {
120-
startVpn(promise);
121-
} else {
122-
promise.reject("E_PREPARE_ERRROR", "Prepare VPN failed");
123-
}
124-
}
125-
});
126-
127136
currentActivity.startActivityForResult(intent, START_VPN_PROFILE);
128137
return;
129138
}
130139

131-
startVpn(promise);
140+
startVpn(vpnPromise);
132141
}
133142

134143
private void startVpn(Promise promise) {
@@ -162,6 +171,7 @@ private void startVpn(Promise promise) {
162171
}
163172
} catch (Exception e) {
164173
promise.reject("E_READ_OVPN_CONFIG_ERROR", "Read ovpn config failed: " + e.toString());
174+
promise = null;
165175
return;
166176
}
167177

@@ -182,6 +192,8 @@ private void startVpn(Promise promise) {
182192
} catch (Exception e) {
183193
promise.reject("E_LOAD_OVPN_PROFILE_ERROR", "Load ovpn profile failed: " + e.toString());
184194
}
195+
196+
promise = null;
185197
}
186198

187199
private String getModifiedOvpnConfig(String ovpnConfig, String remoteAddress) {

0 commit comments

Comments
 (0)