diff options
Diffstat (limited to 'app/java/src')
-rw-r--r-- | app/java/src/MainActivity.java | 32 | ||||
-rw-r--r-- | app/java/src/SettingsActivity.java | 41 | ||||
-rw-r--r-- | app/java/src/WifiListenerReceiver.java | 39 | ||||
-rw-r--r-- | app/java/src/WifiListenerService.java | 78 |
4 files changed, 187 insertions, 3 deletions
diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index 049ee29..bfad05a 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -14,11 +14,15 @@ import android.os.Bundle; 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 { @@ -44,6 +48,9 @@ public class MainActivity extends Activity { this.buttonStart = (Button) findViewById(R.id.button_start); this.dnsProxyService = new Intent(this, DNSProxyService.class); + + // if setting.get(USER_WIFI_LISTENER) + // startService(new Intent(this, WifiListenerService.class)); } @Override @@ -61,6 +68,28 @@ public class MainActivity extends Activity { unregisterReceiver(receiver); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.settings, menu); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int itemId = item.getItemId(); + + if (itemId == R.id.menu__settings) { + Intent intent = new Intent(MainActivity.this, SettingsActivity.class); + startActivity(intent); + + return true; + } + + return super.onOptionsItemSelected(item); + } + /** * is called by "startActivityForResult" internally */ @@ -71,9 +100,6 @@ public class MainActivity extends Activity { } } - @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {} - /** * button_start is clicked */ diff --git a/app/java/src/SettingsActivity.java b/app/java/src/SettingsActivity.java new file mode 100644 index 0000000..83e2661 --- /dev/null +++ b/app/java/src/SettingsActivity.java @@ -0,0 +1,41 @@ +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.net.VpnService; + +import android.os.Bundle; + +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 SettingsActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.settings); + + // if (setting.get(USE_WIFI_LISTENER)) + // requestPermissions(new String[]{ + // android.Manifest.permission.ACCESS_FINE_LOCATION, + // }, 1234); + // startService(WifiListenerService); + } +} diff --git a/app/java/src/WifiListenerReceiver.java b/app/java/src/WifiListenerReceiver.java new file mode 100644 index 0000000..e8ebedf --- /dev/null +++ b/app/java/src/WifiListenerReceiver.java @@ -0,0 +1,39 @@ +package org.pihole.dnsproxy; + +import android.provider.Settings; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +import android.net.NetworkInfo; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiInfo; + +import android.os.Bundle; + +import android.util.Log; + +public class WifiListenerReceiver extends BroadcastReceiver +{ + @Override + public void onReceive(Context context, Intent intent) { + NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); + Intent dnsProxyService = new Intent(context, DNSProxyService.class); + + // start + if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) { + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + + // if (wifiInfo.getSSID() == setting.get(LOCAL_HOME_WIFI_SSID)) + + context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_START)); + } + + // stop + else if (networkInfo.getState().equals(NetworkInfo.State.DISCONNECTED) && DNSProxyService.isRunning()) { + context.startService(dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); + } + } +} 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); + } +} |