diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-05-14 23:00:00 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-05-14 23:00:00 +0200 |
commit | 08b40e81c62420e977e82189482fc07c0d6ace31 (patch) | |
tree | 59848b81e4c24945a35846f42215c336aaeb13ae /app | |
parent | de738691db03ff61bf292b75bfa716b75fe34af1 (diff) |
menu / settings
Diffstat (limited to 'app')
-rw-r--r-- | app/java/res/layout/main.xml | 1 | ||||
-rw-r--r-- | app/java/res/layout/settings.xml | 26 | ||||
-rw-r--r-- | app/java/res/xml/settings.xml | 30 | ||||
-rw-r--r-- | app/java/src/MainActivity.java | 61 | ||||
-rw-r--r-- | app/java/src/SettingsActivity.java | 22 | ||||
-rw-r--r-- | app/java/src/WifiListenerReceiver.java | 4 |
6 files changed, 101 insertions, 43 deletions
diff --git a/app/java/res/layout/main.xml b/app/java/res/layout/main.xml index 1658965..cc1780c 100644 --- a/app/java/res/layout/main.xml +++ b/app/java/res/layout/main.xml @@ -19,7 +19,6 @@ /> <Button - style="@style/button_start__start" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/button_start" diff --git a/app/java/res/layout/settings.xml b/app/java/res/layout/settings.xml deleted file mode 100644 index 5ba8c78..0000000 --- a/app/java/res/layout/settings.xml +++ /dev/null @@ -1,26 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<FrameLayout - xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:tools="http://schemas.android.com/tools" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" - android:paddingBottom="@dimen/activity_vertical_margin" - android:orientation="vertical" - tools:context=".SettingsActivity" -> - - <TextureView - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:id="@+id/surface_view" - /> - - <!-- USE_WIFI_LISTENER --> - <!-- SELECT_FROM_KNOWN_SSIDS --> - - <!-- USE_AUTOMATIC_DNS/PIHOLE_SERVER_DISCOVERY --> - <!-- SET_MANUAL_DNS/PIHOLE_SERVER_IP --> -</FrameLayout> diff --git a/app/java/res/xml/settings.xml b/app/java/res/xml/settings.xml new file mode 100644 index 0000000..ea44ce0 --- /dev/null +++ b/app/java/res/xml/settings.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="utf-8"?> +<PreferenceScreen + xmlns:android="http://schemas.android.com/apk/res/android" +> + <SwitchPreference + android:key="use_automatic_dns_server_discovery" + android:defaultValue="true" + android:title="Use automatic DNS Server / Pihole discovery" + android:summary="Whether to use the automatic discovery mechanism to set the DNS server" + /> + + <SwitchPreference + android:key="use_wifi_listener" + android:defaultValue="false" + android:title="Use WiFi listener" + android:summary="Automatically de/activates the proxy on WiFi dis/connect" + /> + + <!-- USE_WIFI_LISTENER --> + <!-- SELECT_FROM_KNOWN_SSIDS --> + <!-- <MultiSelectListPreference --> + <!-- android:id="@+id/known_ssids" --> + <!-- android:key="known_ssids" --> + <!-- android:entries="" --> + <!-- android:entryValues="" --> + <!-- /> --> + + <!-- USE_AUTOMATIC_DNS/PIHOLE_SERVER_DISCOVERY --> + <!-- SET_MANUAL_DNS/PIHOLE_SERVER_IP --> +</PreferenceScreen> diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index bfad05a..6d232b9 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -7,11 +7,17 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.net.VpnService; +import android.net.wifi.WifiManager; import android.os.Bundle; +import android.preference.PreferenceManager; + import android.util.Log; import android.view.Menu; @@ -29,7 +35,7 @@ public class MainActivity extends Activity { private Button buttonStart; private Intent dnsProxyService; - private BroadcastReceiver receiver = new BroadcastReceiver() { + private BroadcastReceiver receiveDNSProxyService = new BroadcastReceiver() { /** * Set button_start state on receive from DNSProxyService @@ -40,6 +46,17 @@ public class MainActivity extends Activity { } }; + private BroadcastReceiver receiveNetworkChange = new BroadcastReceiver() { + + /** + * Set button_start state on receive of network change + */ + @Override + public void onReceive(Context context, Intent intent) { + setStateButtonStart(); + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -48,9 +65,6 @@ public class MainActivity extends Activity { this.buttonStart = (Button) findViewById(R.id.button_start); this.dnsProxyService = new Intent(this, DNSProxyService.class); - - // if setting.get(USER_WIFI_LISTENER) - // startService(new Intent(this, WifiListenerService.class)); } @Override @@ -58,14 +72,17 @@ public class MainActivity extends Activity { super.onResume(); this.setStateButtonStart(); - registerReceiver(this.receiver, new IntentFilter(DNSProxyService.NOTIFICATION)); + this.toggleWifiListenerService(); + registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION)); + registerReceiver(this.receiveNetworkChange, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); } @Override public void onPause() { super.onPause(); - unregisterReceiver(receiver); + unregisterReceiver(this.receiveDNSProxyService); + unregisterReceiver(this.receiveNetworkChange); } @Override @@ -96,7 +113,7 @@ public class MainActivity extends Activity { @Override public void onActivityResult(int request, int result, Intent data) { if (result == RESULT_OK) { - this.start(); + this.startDNSProxyService(); } } @@ -114,25 +131,35 @@ public class MainActivity extends Activity { } } else { // stop - this.stop(); + this.stopDNSProxyService(); } } /** - * Set button_start text + * Set button_start state */ - public void setStateButtonStart() { + private void setStateButtonStart() { + // change text if (!DNSProxyService.isRunning()) { this.buttonStart.setText(getString(R.string.button_start__start)); } else { this.buttonStart.setText(getString(R.string.button_start__stop)); } + + // toggle 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 */ - public void start() { + private void startDNSProxyService() { startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_START)); Toast @@ -147,7 +174,7 @@ public class MainActivity extends Activity { /** * Stop DNSProxyService */ - public void stop() { + private void stopDNSProxyService() { startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); Toast @@ -158,4 +185,14 @@ 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)); + } + } } diff --git a/app/java/src/SettingsActivity.java b/app/java/src/SettingsActivity.java index 83e2661..c71693a 100644 --- a/app/java/src/SettingsActivity.java +++ b/app/java/src/SettingsActivity.java @@ -12,6 +12,10 @@ import android.net.VpnService; import android.os.Bundle; +import android.preference.MultiSelectListPreference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceFragment; + import android.util.Log; import android.view.Menu; @@ -24,13 +28,17 @@ import android.widget.Button; import android.widget.Toast; import android.provider.Settings; -public class SettingsActivity extends Activity { +import java.util.List; + +public class SettingsActivity extends PreferenceActivity { + + private MultiSelectListPreference knownSsids; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.settings); + getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit(); // if (setting.get(USE_WIFI_LISTENER)) // requestPermissions(new String[]{ @@ -38,4 +46,14 @@ public class SettingsActivity extends Activity { // }, 1234); // startService(WifiListenerService); } + + public static class SettingsFragment extends PreferenceFragment { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + addPreferencesFromResource(R.xml.settings); + } + } } diff --git a/app/java/src/WifiListenerReceiver.java b/app/java/src/WifiListenerReceiver.java index ca22965..d34df2b 100644 --- a/app/java/src/WifiListenerReceiver.java +++ b/app/java/src/WifiListenerReceiver.java @@ -25,7 +25,7 @@ public class WifiListenerReceiver extends BroadcastReceiver // start if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { // wait a few moments for wifi to be fully there - (new Handler()).postDelayed(new Runnable() { + /* (new Handler()).postDelayed(new Runnable() { @Override public void run() { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); @@ -35,7 +35,7 @@ public class WifiListenerReceiver extends BroadcastReceiver context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_START)); } - }, 1000); + }, 1000); */ } // stop |