Skip to content

Commit

Permalink
fix: IllegalStateException thrown while establishing the first conn…
Browse files Browse the repository at this point in the history
…ection (#49)
  • Loading branch information
2770862886 authored Jun 19, 2023
1 parent 808e183 commit f03bb10
Showing 1 changed file with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.VpnService;
import android.os.Bundle;
import android.os.IBinder;
Expand All @@ -24,10 +23,13 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
Expand Down Expand Up @@ -80,6 +82,8 @@
import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

// TODO: clear up
Expand Down Expand Up @@ -110,6 +114,11 @@ public void onServiceDisconnected(ComponentName componentName) {
private TextView nodeClientVersionView;

private View emptyView = null;

private ActivityResultLauncher<Intent> vpnAuthLauncher;

private ViewModelState state;

final private RecyclerView.AdapterDataObserver checkIfEmptyObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
Expand Down Expand Up @@ -177,6 +186,23 @@ public void doUnbindService() {
}
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
// 初始化 VPN 授权结果回调
vpnAuthLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), (activityResult) -> {
var result = activityResult.getResultCode();
Log.d(TAG, "Returned from AUTH_VPN");
if (result == -1) {
// 得到授权,连接网络
startService(this.state.getNetworkId());
} else if (result == 0) {
// 未授权
updateNetworkListAndNotify();
}
});
}

@Override
public void onStart() {
super.onStart();
Expand Down Expand Up @@ -241,17 +267,8 @@ private void sendStartServiceIntent(long networkId) {
var prepare = VpnService.prepare(getActivity());
if (prepare != null) {
// 等待 VPN 授权后连接网络
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), (activityResult) -> {
var result = activityResult.getResultCode();
Log.d(TAG, "Returned from AUTH_VPN");
if (result == -1) {
// 授权
startService(networkId);
} else if (result == 0) {
// 未授权
updateNetworkListAndNotify();
}
}).launch(prepare);
this.state.setNetworkId(networkId);
vpnAuthLauncher.launch(prepare);
return;
}
Log.d(TAG, "Intent is NULL. Already approved.");
Expand All @@ -264,6 +281,7 @@ public void onCreate(Bundle bundle) {
super.onCreate(bundle);
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
setHasOptionsMenu(true);
this.state = new ViewModelProvider(requireActivity()).get(ViewModelState.class);
}

@Override
Expand Down Expand Up @@ -824,4 +842,13 @@ public void onSwitchCheckedChanged(CompoundButton buttonView, boolean isChecked)
}
}
}

/**
* 维护当前 ViewModel 的状态
*/
@Getter
@Setter
public static class ViewModelState extends ViewModel {
private long networkId;
}
}

0 comments on commit f03bb10

Please sign in to comment.