diff options
| -rw-r--r-- | app/java/AndroidManifest.xml | 4 | ||||
| -rw-r--r-- | app/java/res/menu/settings.xml | 4 | ||||
| -rw-r--r-- | app/java/res/values/strings.xml | 2 | ||||
| -rw-r--r-- | app/java/src/DNSProxyConnection.java | 8 | ||||
| -rw-r--r-- | app/java/src/DNSProxyRunner.java | 12 | ||||
| -rw-r--r-- | app/java/src/DNSProxyService.java | 18 | ||||
| -rw-r--r-- | app/java/src/MainActivity.java | 22 | ||||
| -rw-r--r-- | app/java/src/SettingsActivity.java | 33 | ||||
| -rw-r--r-- | app/java/src/WifiListenerReceiver.java | 11 | ||||
| -rw-r--r-- | app/java/src/WifiListenerService.java | 65 | 
10 files changed, 79 insertions, 100 deletions
diff --git a/app/java/AndroidManifest.xml b/app/java/AndroidManifest.xml index 8312571..6734e59 100644 --- a/app/java/AndroidManifest.xml +++ b/app/java/AndroidManifest.xml @@ -4,7 +4,7 @@      xmlns:tools="http://schemas.android.com/tools"      package="org.pihole.dnsproxy"      android:versionCode="1" -    android:versionName="1.0" +    android:versionName="1.0.0"  >      <uses-sdk          android:minSdkVersion="28" @@ -48,7 +48,7 @@          <activity              android:name=".SettingsActivity" -            android:label="@string/menu__settings" +            android:label="@string/menu_settings"              android:parentActivityName=".MainActivity">              <meta-data diff --git a/app/java/res/menu/settings.xml b/app/java/res/menu/settings.xml index 3af94ef..3e3bcbb 100644 --- a/app/java/res/menu/settings.xml +++ b/app/java/res/menu/settings.xml @@ -3,9 +3,9 @@      xmlns:android="http://schemas.android.com/apk/res/android"  >      <item -        android:id="@+id/menu__settings" +        android:id="@+id/menu_settings"          android:icon="@android:drawable/ic_menu_preferences" -        android:title="@string/menu__settings" +        android:title="@string/menu_settings"          android:showAsAction="always"      />  </menu> diff --git a/app/java/res/values/strings.xml b/app/java/res/values/strings.xml index b34abae..7848d3e 100644 --- a/app/java/res/values/strings.xml +++ b/app/java/res/values/strings.xml @@ -18,7 +18,7 @@          Stop      </string> -    <string name="menu__settings"> +    <string name="menu_settings">          Settings      </string>  </resources> diff --git a/app/java/src/DNSProxyConnection.java b/app/java/src/DNSProxyConnection.java index 0573515..420b5fc 100644 --- a/app/java/src/DNSProxyConnection.java +++ b/app/java/src/DNSProxyConnection.java @@ -6,7 +6,7 @@ import android.util.Log;  public class DNSProxyConnection { -    public static String THREAD_NAME = "org.pihole.dnsproxy.service.thread"; +    public static String THREAD_NAME = "org.pihole.dnsproxy.service.dnsproxy.thread";      private DNSProxyService service;      private Thread thread; @@ -16,6 +16,9 @@ public class DNSProxyConnection {          this.service = service;      } +    /** +     * Setup and start the connection +     */      public void start() {          DNSProxyRunner runner = new DNSProxyRunner(this.service);          runner.setOnEstablishListener(tunInterface -> { @@ -26,6 +29,9 @@ public class DNSProxyConnection {          this.thread.start();      } +    /** +     * Stop and close the connection +     */      public void stop() {          try {              this.thread.interrupt(); diff --git a/app/java/src/DNSProxyRunner.java b/app/java/src/DNSProxyRunner.java index 6a45413..dd0e3e5 100644 --- a/app/java/src/DNSProxyRunner.java +++ b/app/java/src/DNSProxyRunner.java @@ -6,15 +6,6 @@ import android.os.ParcelFileDescriptor;  import android.util.Log; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -import java.net.InetSocketAddress; -import java.net.SocketAddress; - -import java.nio.channels.DatagramChannel; -  public class DNSProxyRunner implements Runnable {      public interface OnEstablishListener { @@ -43,6 +34,9 @@ public class DNSProxyRunner implements Runnable {          }      } +    /** +     * Callback when proxy connection is established +     */      public void setOnEstablishListener(OnEstablishListener listener) {          this.onEstablishListener = listener;      } diff --git a/app/java/src/DNSProxyService.java b/app/java/src/DNSProxyService.java index 2b2147f..5a3d70f 100644 --- a/app/java/src/DNSProxyService.java +++ b/app/java/src/DNSProxyService.java @@ -23,10 +23,10 @@ import java.net.InetAddress;  public class DNSProxyService extends VpnService {      public static String LOG_TAG = "org.pihole.dnsproxy.log"; -    public static String NOTIFICATION = "org.pihole.dnsproxy.service"; -    public static String NOTIFICATION_CHANNEL_ID = "PiholeDNSProxy"; -    public static String ACTION_START = "org.pihole.dnsproxy.service.START"; -    public static String ACTION_STOP = "org.pihole.dnsproxy.service.STOP"; +    public static String NOTIFICATION_NOTIFY = "org.pihole.dnsproxy.service.dnsproxy.notification.NOTIFY"; +    public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.dnsproxy.NOTIFICATION"; +    public static String ACTION_START = "org.pihole.dnsproxy.service.dnsproxy.START"; +    public static String ACTION_STOP = "org.pihole.dnsproxy.service.dnsproxy.STOP";      public static String PIHOLE_ADDRESS = ""; @@ -52,10 +52,16 @@ public class DNSProxyService extends VpnService {          super.onDestroy();      } +    /** +     * Builder has to be created by service +     */      public VpnService.Builder newBuilder() {          return new VpnService.Builder();      } +    /** +     * Whether the proxy is setup running +     */      public static boolean isRunning()      {          return DNSProxyService.connection != null; @@ -87,7 +93,7 @@ public class DNSProxyService extends VpnService {          this.startForeground();          // send notification when started -        Intent notification = new Intent(DNSProxyService.NOTIFICATION); +        Intent notification = new Intent(DNSProxyService.NOTIFICATION_NOTIFY);          sendBroadcast(notification);      } @@ -101,7 +107,7 @@ public class DNSProxyService extends VpnService {          stopForeground(true);          // send notification when stopped -        Intent notification = new Intent(DNSProxyService.NOTIFICATION); +        Intent notification = new Intent(DNSProxyService.NOTIFICATION_NOTIFY);          sendBroadcast(notification);      } diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index f30f91c..3d2c197 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -1,7 +1,6 @@  package org.pihole.dnsproxy;  import android.app.Activity; -import android.app.AlertDialog;  import android.content.BroadcastReceiver;  import android.content.Context; @@ -9,26 +8,20 @@ import android.content.Intent;  import android.content.IntentFilter;  import android.content.SharedPreferences; -import android.net.ConnectivityManager; -import android.net.NetworkInfo;  import android.net.VpnService;  import android.net.wifi.WifiManager;  import android.os.Bundle; -import android.preference.PreferenceManager; -  import android.util.Log;  import android.view.Menu;  import android.view.MenuInflater;  import android.view.MenuItem; -import android.view.TextureView;  import android.view.View;  import android.widget.Button;  import android.widget.Toast; -import android.provider.Settings;  public class MainActivity extends Activity { @@ -72,7 +65,7 @@ public class MainActivity extends Activity {          super.onResume();          this.setStateButtonStartStop(); -        registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION)); +        registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION_NOTIFY));          registerReceiver(this.receiveNetworkChange, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));          WifiListenerService.toggle(this); @@ -99,7 +92,7 @@ public class MainActivity extends Activity {      public boolean onOptionsItemSelected(MenuItem item) {          int itemId = item.getItemId(); -        if (itemId == R.id.menu__settings) { +        if (itemId == R.id.menu_settings) {              startActivity(new Intent(this, SettingsActivity.class));              return true; @@ -133,7 +126,7 @@ public class MainActivity extends Activity {      }      /** -     * prepare VpnService properly +     * Prepare VpnService properly       * and ask for confirmation       */      private void prepareVpnService() { @@ -157,15 +150,6 @@ public class MainActivity extends Activity {              this.buttonStart.setVisibility(View.GONE);              this.buttonStop.setVisibility(View.VISIBLE);          } - -        // toggle whether enabled -        /* ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); -        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); -        if (networkInfo.isConnected() || DNSProxyService.isRunning()) { -            this.buttonStart.setEnabled(true); -        } else if (!networkInfo.isConnected() && !DNSProxyService.isRunning()) { -            this.buttonStart.setEnabled(false); -        } */      }      /** diff --git a/app/java/src/SettingsActivity.java b/app/java/src/SettingsActivity.java index d04e2ba..0b3c7f2 100644 --- a/app/java/src/SettingsActivity.java +++ b/app/java/src/SettingsActivity.java @@ -1,43 +1,17 @@  package org.pihole.dnsproxy; -import android.app.Activity; -import android.app.AlertDialog; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter;  import android.content.SharedPreferences;  import android.content.SharedPreferences.OnSharedPreferenceChangeListener; -import android.net.VpnService; -import android.net.wifi.WifiManager; -import android.net.wifi.WifiInfo; -  import android.os.Bundle; -import android.preference.MultiSelectListPreference;  import android.preference.PreferenceActivity;  import android.preference.PreferenceFragment;  import android.util.Log; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.TextureView; -import android.view.View; - -import android.widget.Button; -import android.widget.Toast; -import android.provider.Settings; - -import java.util.List; -  public class SettingsActivity extends PreferenceActivity { -    private MultiSelectListPreference knownSsids; -      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState); @@ -135,12 +109,7 @@ public class SettingsActivity extends PreferenceActivity {                      WifiListenerService.OnActivationListener.askLocationIfNeeded(context.getActivity());                      if (sharedPreferences.getString("wifi_listener_ssid", "").equals("")) { -                        WifiManager wifiManager = (WifiManager) context.getActivity().getSystemService(Context.WIFI_SERVICE); -                        WifiInfo wifiInfo = wifiManager.getConnectionInfo(); -                        String ssid = wifiInfo.getSSID(); - -                        // remove quotes around ssid -                        ssid = ssid.substring(1, ssid.length() - 1); +                        String ssid = WifiListenerService.getWifiSSID(context.getActivity());                          if (!ssid.equals("unknown ssid")) {                              SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); diff --git a/app/java/src/WifiListenerReceiver.java b/app/java/src/WifiListenerReceiver.java index 6bf93bd..5e05654 100644 --- a/app/java/src/WifiListenerReceiver.java +++ b/app/java/src/WifiListenerReceiver.java @@ -1,7 +1,5 @@  package org.pihole.dnsproxy; -import android.provider.Settings; -  import android.content.BroadcastReceiver;  import android.content.Context;  import android.content.Intent; @@ -9,9 +7,7 @@ import android.content.SharedPreferences;  import android.net.NetworkInfo;  import android.net.wifi.WifiManager; -import android.net.wifi.WifiInfo; -import android.os.Bundle;  import android.os.Handler;  import android.preference.PreferenceManager; @@ -37,12 +33,7 @@ public class WifiListenerReceiver extends BroadcastReceiver              (new Handler()).postDelayed(new Runnable() {                  @Override                  public void run() { -                    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); +                    String ssid = WifiListenerService.getWifiSSID(context);                      if (ssid.equals(sharedPreferences.getString("wifi_listener_ssid", ""))) {                          DNSProxyService.start(context); 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[]{  | 
