From 309993eec6085cfa0e112cd5f45c8c55beeee5db Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 15 May 2023 17:20:15 +0200 Subject: works as intended :) --- app/java/src/MainActivity.java | 78 ++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 38 deletions(-) (limited to 'app/java/src/MainActivity.java') diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index 6d232b9..f30f91c 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -33,7 +33,7 @@ import android.provider.Settings; public class MainActivity extends Activity { private Button buttonStart; - private Intent dnsProxyService; + private Button buttonStop; private BroadcastReceiver receiveDNSProxyService = new BroadcastReceiver() { @@ -42,7 +42,7 @@ public class MainActivity extends Activity { */ @Override public void onReceive(Context context, Intent intent) { - setStateButtonStart(); + setStateButtonStartStop(); } }; @@ -53,7 +53,7 @@ public class MainActivity extends Activity { */ @Override public void onReceive(Context context, Intent intent) { - setStateButtonStart(); + setStateButtonStartStop(); } }; @@ -64,17 +64,19 @@ public class MainActivity extends Activity { setContentView(R.layout.main); this.buttonStart = (Button) findViewById(R.id.button_start); - this.dnsProxyService = new Intent(this, DNSProxyService.class); + this.buttonStop = (Button) findViewById(R.id.button_stop); } @Override public void onResume() { super.onResume(); - this.setStateButtonStart(); - this.toggleWifiListenerService(); + this.setStateButtonStartStop(); registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION)); registerReceiver(this.receiveNetworkChange, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); + + WifiListenerService.toggle(this); + WifiListenerService.OnActivationListener.askLocationIfNeeded(this); } @Override @@ -98,8 +100,7 @@ public class MainActivity extends Activity { int itemId = item.getItemId(); if (itemId == R.id.menu__settings) { - Intent intent = new Intent(MainActivity.this, SettingsActivity.class); - startActivity(intent); + startActivity(new Intent(this, SettingsActivity.class)); return true; } @@ -121,46 +122,57 @@ public class MainActivity extends Activity { * button_start is clicked */ public void onClickButtonStart(View view) { - if (!DNSProxyService.isRunning()) { //start - Intent intent = VpnService.prepare(this); - - if (intent != null) { - startActivityForResult(intent, 0); - } else { - this.onActivityResult(0, RESULT_OK, null); - } - } - else { // stop - this.stopDNSProxyService(); + this.prepareVpnService(); + } + + /** + * button_stop is clicked + */ + public void onClickButtonStop(View view) { + this.stopDNSProxyService(); + } + + /** + * prepare VpnService properly + * and ask for confirmation + */ + private void prepareVpnService() { + Intent intent = VpnService.prepare(this); + + if (intent != null) { + startActivityForResult(intent, 0); + } else { + this.onActivityResult(0, RESULT_OK, null); } } /** * Set button_start state */ - private void setStateButtonStart() { - // change text + private void setStateButtonStartStop() { if (!DNSProxyService.isRunning()) { - this.buttonStart.setText(getString(R.string.button_start__start)); + this.buttonStart.setVisibility(View.VISIBLE); + this.buttonStop.setVisibility(View.GONE); } else { - this.buttonStart.setText(getString(R.string.button_start__stop)); + this.buttonStart.setVisibility(View.GONE); + this.buttonStop.setVisibility(View.VISIBLE); } - // toggle enabled - ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + // toggle whether enabled + /* ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (networkInfo.isConnected() || DNSProxyService.isRunning()) { this.buttonStart.setEnabled(true); } else if (!networkInfo.isConnected() && !DNSProxyService.isRunning()) { this.buttonStart.setEnabled(false); - } + } */ } /** * Start DNSProxyService */ private void startDNSProxyService() { - startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_START)); + DNSProxyService.start(this); Toast .makeText( @@ -175,7 +187,7 @@ public class MainActivity extends Activity { * Stop DNSProxyService */ private void stopDNSProxyService() { - startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); + DNSProxyService.stop(this); Toast .makeText( @@ -185,14 +197,4 @@ public class MainActivity extends Activity { ) .show(); } - - private void toggleWifiListenerService() { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - - if (sharedPreferences.getBoolean("use_wifi_listener", false)) { - startService(new Intent(this, WifiListenerService.class)); - } else { - stopService(new Intent(this, WifiListenerService.class)); - } - } } -- cgit v1.2.3