diff options
Diffstat (limited to 'app/java/src/DNSProxyService.java')
-rw-r--r-- | app/java/src/DNSProxyService.java | 59 |
1 files changed, 52 insertions, 7 deletions
diff --git a/app/java/src/DNSProxyService.java b/app/java/src/DNSProxyService.java index 8911036..2b2147f 100644 --- a/app/java/src/DNSProxyService.java +++ b/app/java/src/DNSProxyService.java @@ -5,6 +5,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; +import android.content.SharedPreferences; import android.content.Context; import android.content.Intent; @@ -13,6 +14,8 @@ import android.net.DhcpInfo; import android.net.VpnService; import android.net.wifi.WifiManager; +import android.preference.PreferenceManager; + import android.util.Log; import java.net.InetAddress; @@ -32,11 +35,11 @@ public class DNSProxyService extends VpnService { @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent.getAction().equals(DNSProxyService.ACTION_START)) { - this.start(); + this.connect(); return Service.START_STICKY; } else { - this.stop(); + this.disconnect(); return Service.START_NOT_STICKY; } @@ -44,7 +47,7 @@ public class DNSProxyService extends VpnService { @Override public void onDestroy() { - this.stop(); + this.disconnect(); super.onDestroy(); } @@ -58,7 +61,24 @@ public class DNSProxyService extends VpnService { return DNSProxyService.connection != null; } - public void start() { + /** + * Start this service + */ + public static void start(Context context) { + context.startService((new Intent(context, DNSProxyService.class)).setAction(DNSProxyService.ACTION_START)); + } + + /** + * Stop this service + */ + public static void stop(Context context) { + context.startService((new Intent(context, DNSProxyService.class)).setAction(DNSProxyService.ACTION_STOP)); + } + + /** + * Setup connection + */ + private void connect() { this.setPiholeAddress(); DNSProxyService.connection = new DNSProxyConnection(this); @@ -71,7 +91,10 @@ public class DNSProxyService extends VpnService { sendBroadcast(notification); } - public void stop() { + /** + * Disconnect from connection + */ + private void disconnect() { DNSProxyService.connection.stop(); DNSProxyService.connection = null; @@ -82,7 +105,16 @@ public class DNSProxyService extends VpnService { sendBroadcast(notification); } + /** + * Get and set IP address of Pihole DNS server + */ private void setPiholeAddress() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + if (!sharedPreferences.getBoolean("use_automatic_dns_server_discovery", true)) { + DNSProxyService.PIHOLE_ADDRESS = sharedPreferences.getString("dns_server_address", ""); + return; + } + WifiManager manager = (WifiManager) getSystemService(WIFI_SERVICE); DhcpInfo info = manager.getDhcpInfo(); @@ -100,11 +132,16 @@ public class DNSProxyService extends VpnService { } } + /** + * Start the Foreground notification process + */ private void startForeground() { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel( - DNSProxyService.NOTIFICATION_CHANNEL_ID, DNSProxyService.NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT + DNSProxyService.NOTIFICATION_CHANNEL_ID, + DNSProxyService.NOTIFICATION_CHANNEL_ID, + NotificationManager.IMPORTANCE_DEFAULT ); manager.createNotificationChannel(channel); @@ -112,7 +149,15 @@ public class DNSProxyService extends VpnService { .setSmallIcon(R.drawable.logo) .setContentTitle("Pihole DNS Proxy") .setContentText("Running with " + DNSProxyService.PIHOLE_ADDRESS) - // .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(MainActivity.class), PendingIntent.FLAG_IMMUTABLE)) + .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK)) + .addAction( + R.drawable.logo, + "Stop", + PendingIntent.getService(this, 0, + (new Intent(this, DNSProxyService.class)).setAction(DNSProxyService.ACTION_STOP), + PendingIntent.FLAG_IMMUTABLE + ) + ) .build(); startForeground(1, notification); |