summaryrefslogtreecommitdiff
path: root/app/java/src/WifiListenerService.java
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-05-14 13:20:43 +0200
committerDaniel Weipert <code@drogueronin.de>2023-05-14 13:24:30 +0200
commit6f4dc54bd119203da8dd1f91e65c4e4b99ab2ff0 (patch)
treefa7fe2459283fe81dba17f95eccdf5eded883e11 /app/java/src/WifiListenerService.java
parent6aae8398d489de5f33e8824a252793306d570d3c (diff)
menu
Diffstat (limited to 'app/java/src/WifiListenerService.java')
-rw-r--r--app/java/src/WifiListenerService.java78
1 files changed, 78 insertions, 0 deletions
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);
+ }
+}