From 6f4dc54bd119203da8dd1f91e65c4e4b99ab2ff0 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 14 May 2023 13:20:43 +0200 Subject: menu --- Readme.md | 13 ++++++ app/java/AndroidManifest.xml | 22 +++++++++- app/java/res/layout/settings.xml | 26 ++++++++++++ app/java/res/menu/settings.xml | 10 +++++ app/java/res/values/strings.xml | 8 +++- app/java/res/values/styles.xml | 11 +++++ app/java/src/MainActivity.java | 32 ++++++++++++-- app/java/src/SettingsActivity.java | 41 ++++++++++++++++++ app/java/src/WifiListenerReceiver.java | 39 +++++++++++++++++ app/java/src/WifiListenerService.java | 78 ++++++++++++++++++++++++++++++++++ 10 files changed, 273 insertions(+), 7 deletions(-) create mode 100644 app/java/res/layout/settings.xml create mode 100644 app/java/res/menu/settings.xml create mode 100644 app/java/res/values/styles.xml create mode 100644 app/java/src/SettingsActivity.java create mode 100644 app/java/src/WifiListenerReceiver.java create mode 100644 app/java/src/WifiListenerService.java diff --git a/Readme.md b/Readme.md index c1d5934..0d4f515 100644 --- a/Readme.md +++ b/Readme.md @@ -12,3 +12,16 @@ - https://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android - https://www.vogella.com/tutorials/AndroidServices/article.html#communication-with-services - https://github.com/googlecodelabs/bazel-android-intro/blob/master/sample/mediarecorder/java/com/example/android/mediarecorder/MainActivity.java + +### Network +- https://stackoverflow.com/questions/15088507/open-app-when-connect-with-wifi +- https://stackoverflow.com/questions/40545396/connectivity-change-deprecated-in-target-of-android-n +- https://stackoverflow.com/questions/48527171/detect-connectivity-change-in-android-7-and-above-when-app-is-killed-in-backgrou +- https://stackoverflow.com/questions/25678216/android-internet-connectivity-change-listener +- https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges +- https://developer.android.com/guide/background/persistent/getting-started +- https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback + +### Menu +- https://medium.com/@bhavyakaria/step-by-step-guide-to-create-app-settings-using-preferences-in-android-part-1-fa470305b530 +- https://code.tutsplus.com/tutorials/how-to-code-a-settings-screen-in-an-android-app--cms-30433 diff --git a/app/java/AndroidManifest.xml b/app/java/AndroidManifest.xml index 6c1f400..e295ed4 100644 --- a/app/java/AndroidManifest.xml +++ b/app/java/AndroidManifest.xml @@ -7,10 +7,11 @@ android:versionName="1.0" > + @@ -29,6 +30,12 @@ + + + + + + + + + diff --git a/app/java/res/layout/settings.xml b/app/java/res/layout/settings.xml new file mode 100644 index 0000000..5ba8c78 --- /dev/null +++ b/app/java/res/layout/settings.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + diff --git a/app/java/res/menu/settings.xml b/app/java/res/menu/settings.xml new file mode 100644 index 0000000..2d86504 --- /dev/null +++ b/app/java/res/menu/settings.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/java/res/values/strings.xml b/app/java/res/values/strings.xml index 2f5e4e6..bd4f88b 100644 --- a/app/java/res/values/strings.xml +++ b/app/java/res/values/strings.xml @@ -12,9 +12,13 @@ - Start Proxy + Start - Stop Proxy + Stop + + + + Settings diff --git a/app/java/res/values/styles.xml b/app/java/res/values/styles.xml new file mode 100644 index 0000000..d5037d5 --- /dev/null +++ b/app/java/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index 049ee29..bfad05a 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -14,11 +14,15 @@ import android.os.Bundle; import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.TextureView; import android.view.View; import android.widget.Button; import android.widget.Toast; +import android.provider.Settings; public class MainActivity extends Activity { @@ -44,6 +48,9 @@ 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 @@ -61,6 +68,28 @@ public class MainActivity extends Activity { unregisterReceiver(receiver); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.settings, menu); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int itemId = item.getItemId(); + + if (itemId == R.id.menu__settings) { + Intent intent = new Intent(MainActivity.this, SettingsActivity.class); + startActivity(intent); + + return true; + } + + return super.onOptionsItemSelected(item); + } + /** * is called by "startActivityForResult" internally */ @@ -71,9 +100,6 @@ public class MainActivity extends Activity { } } - @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {} - /** * button_start is clicked */ diff --git a/app/java/src/SettingsActivity.java b/app/java/src/SettingsActivity.java new file mode 100644 index 0000000..83e2661 --- /dev/null +++ b/app/java/src/SettingsActivity.java @@ -0,0 +1,41 @@ +package org.pihole.dnsproxy; + +import android.app.Activity; +import android.app.AlertDialog; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +import android.net.VpnService; + +import android.os.Bundle; + +import android.util.Log; + +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.TextureView; +import android.view.View; + +import android.widget.Button; +import android.widget.Toast; +import android.provider.Settings; + +public class SettingsActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.settings); + + // if (setting.get(USE_WIFI_LISTENER)) + // requestPermissions(new String[]{ + // android.Manifest.permission.ACCESS_FINE_LOCATION, + // }, 1234); + // startService(WifiListenerService); + } +} diff --git a/app/java/src/WifiListenerReceiver.java b/app/java/src/WifiListenerReceiver.java new file mode 100644 index 0000000..e8ebedf --- /dev/null +++ b/app/java/src/WifiListenerReceiver.java @@ -0,0 +1,39 @@ +package org.pihole.dnsproxy; + +import android.provider.Settings; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import android.net.NetworkInfo; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiInfo; + +import android.os.Bundle; + +import android.util.Log; + +public class WifiListenerReceiver extends BroadcastReceiver +{ + @Override + public void onReceive(Context context, Intent intent) { + NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); + Intent dnsProxyService = new Intent(context, DNSProxyService.class); + + // start + if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + + // if (wifiInfo.getSSID() == setting.get(LOCAL_HOME_WIFI_SSID)) + + context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_START)); + } + + // stop + else if (networkInfo.getState().equals(NetworkInfo.State.DISCONNECTED) && DNSProxyService.isRunning()) { + context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); + } + } +} diff --git a/app/java/src/WifiListenerService.java b/app/java/src/WifiListenerService.java new file mode 100644 index 0000000..e25c325 --- /dev/null +++ b/app/java/src/WifiListenerService.java @@ -0,0 +1,78 @@ +package org.pihole.dnsproxy; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; + +import android.os.IBinder; + +import android.util.Log; + +public class WifiListenerService extends Service +{ + + public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener"; + + private BroadcastReceiver receiver; + + @Override + public void onCreate () { + super.onCreate(); + + this.receiver = new WifiListenerReceiver(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + this.start(); + + return START_STICKY; + } + + @Override + public void onDestroy() { + this.stop(); + + super.onDestroy(); + } + + @Override + public IBinder onBind(Intent intent) { return null; } + + public void start() { + // registerReceiver(this.receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)); + registerReceiver(this.receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); + + NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + + NotificationChannel channel = new NotificationChannel( + WifiListenerService.NOTIFICATION_CHANNEL_ID, + WifiListenerService.NOTIFICATION_CHANNEL_ID, + NotificationManager.IMPORTANCE_DEFAULT + ); + manager.createNotificationChannel(channel); + + Notification notification = new Notification.Builder(this, WifiListenerService.NOTIFICATION_CHANNEL_ID) + .setSmallIcon(R.drawable.logo) + .setContentTitle("Pihole DNS Proxy - WiFi Listener") + .setContentText("Listening for WiFi connection change") + .build(); + + startForeground(2, notification); + } + + public void stop() { + unregisterReceiver(this.receiver); + stopForeground(true); + } +} -- cgit v1.2.3