Add VPN support with Mullvad WireGuard integration
This commit is contained in:
@@ -6,7 +6,9 @@ import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.VpnService;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
@@ -30,12 +32,14 @@ import java.util.Map;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = "MainActivity";
|
||||
private RecyclerView sectionList;
|
||||
private RecyclerView contentList;
|
||||
private ProgressBar loadingIndicator;
|
||||
private TextView messageView;
|
||||
private TextView contentTitle;
|
||||
private Button refreshButton;
|
||||
private Button vpnButton;
|
||||
|
||||
private ChannelAdapter channelAdapter;
|
||||
private EventAdapter eventAdapter;
|
||||
@@ -62,6 +66,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
messageView = findViewById(R.id.message_view);
|
||||
contentTitle = findViewById(R.id.content_title);
|
||||
refreshButton = findViewById(R.id.refresh_button);
|
||||
vpnButton = findViewById(R.id.vpn_button);
|
||||
|
||||
VpnManager.init(this);
|
||||
updateVpnButton();
|
||||
|
||||
vpnButton.setOnClickListener(v -> toggleVpn());
|
||||
|
||||
refreshButton.setOnClickListener(v -> {
|
||||
loadEvents(true);
|
||||
@@ -386,6 +396,66 @@ public class MainActivity extends AppCompatActivity {
|
||||
Toast.makeText(this, R.string.device_blocked_copy_success, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void toggleVpn() {
|
||||
VpnManager vpnManager = VpnManager.getInstance();
|
||||
if (vpnManager.isConnected()) {
|
||||
vpnManager.disconnect();
|
||||
Toast.makeText(this, R.string.vpn_disconnected, Toast.LENGTH_SHORT).show();
|
||||
updateVpnButton();
|
||||
} else {
|
||||
// Check if VPN permission is needed
|
||||
try {
|
||||
Intent prepareIntent = com.wireguard.android.backend.GoBackend.VpnService.prepare(this);
|
||||
if (prepareIntent != null) {
|
||||
// Need permission
|
||||
try {
|
||||
startActivityForResult(prepareIntent, 1001);
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error checking VPN permission: " + e.getMessage());
|
||||
}
|
||||
|
||||
vpnButton.setText(R.string.vpn_connecting);
|
||||
vpnButton.setEnabled(false);
|
||||
new Thread(() -> {
|
||||
boolean success = vpnManager.connect();
|
||||
runOnUiThread(() -> {
|
||||
vpnButton.setEnabled(true);
|
||||
if (success) {
|
||||
Toast.makeText(this, R.string.vpn_connected, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(this, R.string.vpn_error, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
updateVpnButton();
|
||||
});
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == 1001 && resultCode == RESULT_OK) {
|
||||
// Permission granted, try to connect
|
||||
toggleVpn();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateVpnButton() {
|
||||
VpnManager vpnManager = VpnManager.getInstance();
|
||||
if (vpnManager.isConnected()) {
|
||||
vpnButton.setText(R.string.vpn_disconnect);
|
||||
vpnButton.setSelected(true);
|
||||
} else {
|
||||
vpnButton.setText(R.string.vpn_connect);
|
||||
vpnButton.setSelected(false);
|
||||
}
|
||||
}
|
||||
|
||||
private int getSpanCount() {
|
||||
return getResources().getInteger(R.integer.channel_grid_span);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user