diff options
Diffstat (limited to 'app/java/src/WifiListenerService.java')
-rw-r--r-- | app/java/src/WifiListenerService.java | 78 |
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); + } +} |