summaryrefslogtreecommitdiff
path: root/app/java/src/WifiListenerService.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/java/src/WifiListenerService.java')
-rw-r--r--app/java/src/WifiListenerService.java145
1 files changed, 137 insertions, 8 deletions
diff --git a/app/java/src/WifiListenerService.java b/app/java/src/WifiListenerService.java
index e25c325..1bf89eb 100644
--- a/app/java/src/WifiListenerService.java
+++ b/app/java/src/WifiListenerService.java
@@ -1,27 +1,37 @@
package org.pihole.dnsproxy;
+import android.app.Activity;
import android.app.Notification;
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.location.LocationManager;
+
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.IBinder;
+import android.preference.PreferenceFragment;
+import android.preference.PreferenceManager;
+
import android.util.Log;
public class WifiListenerService extends Service
{
public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener";
+ 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;
@@ -34,14 +44,28 @@ public class WifiListenerService extends Service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- this.start();
+ if (intent.getAction().equals(WifiListenerService.ACTION_START)) {
+ this.listen();
+
+ return Service.START_STICKY;
+ } 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)) {
+ WifiListenerService.disable(this);
+
+ return Service.START_NOT_STICKY;
+ }
- return START_STICKY;
+ return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
- this.stop();
+ this.deafen();
super.onDestroy();
}
@@ -49,10 +73,66 @@ public class WifiListenerService extends Service
@Override
public IBinder onBind(Intent intent) { return null; }
- public void start() {
- // registerReceiver(this.receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
+ /**
+ * start this service
+ */
+ public static void start(Context context) {
+ context.startService((new Intent(context, WifiListenerService.class)).setAction(WifiListenerService.ACTION_START));
+ }
+
+ /**
+ * stop this service
+ */
+ public static void stop(Context context) {
+ context.startService((new Intent(context, WifiListenerService.class)).setAction(WifiListenerService.ACTION_STOP));
+ }
+
+ public static void toggle(Context context) {
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+
+ if (sharedPreferences.getBoolean("use_wifi_listener", false)) {
+ WifiListenerService.start(context);
+ } else {
+ WifiListenerService.stop(context);
+ }
+ }
+
+ /**
+ * Stop Wifi Listener and disable completely
+ */
+ public static void disable(Context context) {
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+ SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
+
+ WifiListenerService.stop(context);
+ sharedPreferencesEditor.putBoolean("use_wifi_listener", false);
+ sharedPreferencesEditor.commit();
+
+ WifiListenerService.OnActivationListener.disable(context);
+ }
+
+ /**
+ * setup listener
+ */
+ private void listen() {
registerReceiver(this.receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
+ this.startForeground();
+ }
+
+ /**
+ * stop listening
+ */
+ private void deafen() {
+ unregisterReceiver(this.receiver);
+
+ stopForeground(true);
+ }
+
+ /**
+ * Start the Foreground notification process
+ */
+ private void startForeground() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(
@@ -66,13 +146,62 @@ public class WifiListenerService extends Service
.setSmallIcon(R.drawable.logo)
.setContentTitle("Pihole DNS Proxy - WiFi Listener")
.setContentText("Listening for WiFi connection change")
+ .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, SettingsActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK))
+ .addAction(
+ R.drawable.logo,
+ "Stop Listener",
+ PendingIntent.getService(this, 0,
+ (new Intent(this, WifiListenerService.class)).setAction(WifiListenerService.ACTION_STOP_SET_PREFERENCE),
+ PendingIntent.FLAG_IMMUTABLE
+ )
+ )
+ .addAction(
+ R.drawable.logo,
+ "Start Proxy",
+ PendingIntent.getService(this, 0,
+ (new Intent(this, DNSProxyService.class)).setAction(DNSProxyService.ACTION_START),
+ PendingIntent.FLAG_IMMUTABLE
+ )
+ )
.build();
startForeground(2, notification);
}
- public void stop() {
- unregisterReceiver(this.receiver);
- stopForeground(true);
+ public static class OnActivationListener {
+ public static void disable(Context context) {
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+ SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
+ LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+
+ sharedPreferencesEditor.putBoolean("use_wifi_listener_for_activation", false);
+ sharedPreferencesEditor.commit();
+
+ if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
+ context.startActivity((new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
+ }
+ }
+
+ public static boolean checkLocationNeeded(Context context) {
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+
+ if (!sharedPreferences.getBoolean("use_wifi_listener_for_activation", false)) {
+ return false;
+ }
+
+ LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+
+ return !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
+ }
+
+ public static void askLocationIfNeeded(Activity context) {
+ if (checkLocationNeeded(context)) {
+ context.requestPermissions(new String[]{
+ android.Manifest.permission.ACCESS_FINE_LOCATION,
+ }, 3765);
+
+ context.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
+ }
+ }
}
}