diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-05-14 13:20:43 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-05-14 13:24:30 +0200 |
commit | 6f4dc54bd119203da8dd1f91e65c4e4b99ab2ff0 (patch) | |
tree | fa7fe2459283fe81dba17f95eccdf5eded883e11 | |
parent | 6aae8398d489de5f33e8824a252793306d570d3c (diff) |
menu
-rw-r--r-- | Readme.md | 13 | ||||
-rw-r--r-- | app/java/AndroidManifest.xml | 22 | ||||
-rw-r--r-- | app/java/res/layout/settings.xml | 26 | ||||
-rw-r--r-- | app/java/res/menu/settings.xml | 10 | ||||
-rw-r--r-- | app/java/res/values/strings.xml | 8 | ||||
-rw-r--r-- | app/java/res/values/styles.xml | 11 | ||||
-rw-r--r-- | app/java/src/MainActivity.java | 32 | ||||
-rw-r--r-- | app/java/src/SettingsActivity.java | 41 | ||||
-rw-r--r-- | app/java/src/WifiListenerReceiver.java | 39 | ||||
-rw-r--r-- | app/java/src/WifiListenerService.java | 78 |
10 files changed, 273 insertions, 7 deletions
@@ -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" > <uses-sdk - android:minSdkVersion="16" - android:targetSdkVersion="30" + android:minSdkVersion="28" + android:targetSdkVersion="31" /> + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> @@ -29,6 +30,12 @@ </intent-filter> </service> + <service + android:name=".WifiListenerService" + > + </service> + + <activity android:name=".MainActivity" android:label="@string/app_label" @@ -39,5 +46,16 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> + + <activity + android:name=".SettingsActivity" + android:label="@string/menu__settings" + android:parentActivityName=".MainActivity"> + + <meta-data + android:name="android.support.PARENT_ACTIVITY" + android:value=".MainActivity" + /> + </activity> </application> </manifest> 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 @@ +<?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/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 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu + xmlns:android="http://schemas.android.com/apk/res/android" +> + <item + android:id="@+id/menu__settings" + android:title="@string/menu__settings" + android:orderInCategory="100" + /> +</menu> 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 @@ </string> <string name="button_start__start"> - Start Proxy + Start </string> <string name="button_start__stop"> - Stop Proxy + Stop + </string> + + <string name="menu__settings"> + Settings </string> </resources> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <style name="button_start__start" parent="@android:style/Widget.Button"> + <item name="android:background">#96060C</item> + <item name="android:textColor">@android:color/white</item> + </style> + <style name="button_start__stop" parent="@android:style/Widget.Button"> + <item name="android:background">#22B225</item> + <item name="android:textColor">@android:color/white</item> + </style> +</resources> 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); + } +} |