diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-05-19 12:28:24 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-05-19 12:28:24 +0200 |
commit | f47c908c88875c32fd4c624aeecefbf660c17e85 (patch) | |
tree | 659fd0d6a854d9af06275172e21a8e9e9fc8cc65 /app/java/src/WifiListenerService.java | |
parent | 309993eec6085cfa0e112cd5f45c8c55beeee5db (diff) |
cleanup and comments
Diffstat (limited to 'app/java/src/WifiListenerService.java')
-rw-r--r-- | app/java/src/WifiListenerService.java | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/app/java/src/WifiListenerService.java b/app/java/src/WifiListenerService.java index 1bf89eb..da4c337 100644 --- a/app/java/src/WifiListenerService.java +++ b/app/java/src/WifiListenerService.java @@ -6,21 +6,20 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; -import android.content.SharedPreferences; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.location.LocationManager; -import android.net.ConnectivityManager; +import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.IBinder; -import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.util.Log; @@ -28,33 +27,33 @@ import android.util.Log; public class WifiListenerService extends Service { - public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener"; + public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener.NOTIFICATION"; public static String ACTION_START = "org.pihole.dnsproxy.service.wifiListener.START"; public static String ACTION_STOP = "org.pihole.dnsproxy.service.wifiListener.STOP"; public static String ACTION_STOP_SET_PREFERENCE = "org.pihole.dnsproxy.service.wifiListener.STOP_SET_PREFERENCE"; - private BroadcastReceiver receiver; - - @Override - public void onCreate () { - super.onCreate(); - - this.receiver = new WifiListenerReceiver(); - } + private BroadcastReceiver receiver = new WifiListenerReceiver(); @Override public int onStartCommand(Intent intent, int flags, int startId) { + // start the service if (intent.getAction().equals(WifiListenerService.ACTION_START)) { this.listen(); return Service.START_STICKY; - } else if (intent.getAction().equals(WifiListenerService.ACTION_STOP)) { + } + + // stop the service + else if (intent.getAction().equals(WifiListenerService.ACTION_STOP)) { try { this.deafen(); } catch (Exception exception) {} return Service.START_NOT_STICKY; - } else if (intent.getAction().equals(WifiListenerService.ACTION_STOP_SET_PREFERENCE)) { + } + + // stop and disable + else if (intent.getAction().equals(WifiListenerService.ACTION_STOP_SET_PREFERENCE)) { WifiListenerService.disable(this); return Service.START_NOT_STICKY; @@ -74,19 +73,22 @@ public class WifiListenerService extends Service public IBinder onBind(Intent intent) { return null; } /** - * start this service + * Start the service */ public static void start(Context context) { context.startService((new Intent(context, WifiListenerService.class)).setAction(WifiListenerService.ACTION_START)); } /** - * stop this service + * Stop the service */ public static void stop(Context context) { context.startService((new Intent(context, WifiListenerService.class)).setAction(WifiListenerService.ACTION_STOP)); } + /** + * Start/Stop based on setting + */ public static void toggle(Context context) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); @@ -112,7 +114,21 @@ public class WifiListenerService extends Service } /** - * setup listener + * Get current Wifi SSID + */ + public static String getWifiSSID(Context context) { + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + String ssid = wifiInfo.getSSID(); + + // remove quotes around ssid + ssid = ssid.substring(1, ssid.length() - 1); + + return ssid; + } + + /** + * Setup listener */ private void listen() { registerReceiver(this.receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); @@ -121,7 +137,7 @@ public class WifiListenerService extends Service } /** - * stop listening + * Stop listening */ private void deafen() { unregisterReceiver(this.receiver); @@ -168,7 +184,14 @@ public class WifiListenerService extends Service startForeground(2, notification); } + /** + * SubClass to enable auto-activation + */ public static class OnActivationListener { + + /** + * Disable auto-activation + */ public static void disable(Context context) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); @@ -182,6 +205,9 @@ public class WifiListenerService extends Service } } + /** + * Check if location services are needed + */ public static boolean checkLocationNeeded(Context context) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); @@ -194,6 +220,9 @@ public class WifiListenerService extends Service return !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } + /** + * Ask User to enable location services if they are needed + */ public static void askLocationIfNeeded(Activity context) { if (checkLocationNeeded(context)) { context.requestPermissions(new String[]{ |