From 6f4dc54bd119203da8dd1f91e65c4e4b99ab2ff0 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 14 May 2023 13:20:43 +0200 Subject: menu --- app/java/src/WifiListenerService.java | 78 +++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/java/src/WifiListenerService.java (limited to 'app/java/src/WifiListenerService.java') 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