diff options
Diffstat (limited to 'app/java/src/MainActivity.java')
-rw-r--r-- | app/java/src/MainActivity.java | 61 |
1 files changed, 49 insertions, 12 deletions
diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index bfad05a..6d232b9 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -7,11 +7,17 @@ import android.content.BroadcastReceiver; import android.content.Context; 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; @@ -29,7 +35,7 @@ public class MainActivity extends Activity { private Button buttonStart; private Intent dnsProxyService; - private BroadcastReceiver receiver = new BroadcastReceiver() { + private BroadcastReceiver receiveDNSProxyService = new BroadcastReceiver() { /** * Set button_start state on receive from DNSProxyService @@ -40,6 +46,17 @@ public class MainActivity extends Activity { } }; + private BroadcastReceiver receiveNetworkChange = new BroadcastReceiver() { + + /** + * Set button_start state on receive of network change + */ + @Override + public void onReceive(Context context, Intent intent) { + setStateButtonStart(); + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -48,9 +65,6 @@ 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 @@ -58,14 +72,17 @@ public class MainActivity extends Activity { super.onResume(); this.setStateButtonStart(); - registerReceiver(this.receiver, new IntentFilter(DNSProxyService.NOTIFICATION)); + this.toggleWifiListenerService(); + registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION)); + registerReceiver(this.receiveNetworkChange, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); } @Override public void onPause() { super.onPause(); - unregisterReceiver(receiver); + unregisterReceiver(this.receiveDNSProxyService); + unregisterReceiver(this.receiveNetworkChange); } @Override @@ -96,7 +113,7 @@ public class MainActivity extends Activity { @Override public void onActivityResult(int request, int result, Intent data) { if (result == RESULT_OK) { - this.start(); + this.startDNSProxyService(); } } @@ -114,25 +131,35 @@ public class MainActivity extends Activity { } } else { // stop - this.stop(); + this.stopDNSProxyService(); } } /** - * Set button_start text + * Set button_start state */ - public void setStateButtonStart() { + private void setStateButtonStart() { + // change text if (!DNSProxyService.isRunning()) { this.buttonStart.setText(getString(R.string.button_start__start)); } else { this.buttonStart.setText(getString(R.string.button_start__stop)); } + + // toggle 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); + } } /** * Start DNSProxyService */ - public void start() { + private void startDNSProxyService() { startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_START)); Toast @@ -147,7 +174,7 @@ public class MainActivity extends Activity { /** * Stop DNSProxyService */ - public void stop() { + private void stopDNSProxyService() { startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); Toast @@ -158,4 +185,14 @@ public class MainActivity extends Activity { ) .show(); } + + private void toggleWifiListenerService() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + + if (sharedPreferences.getBoolean("use_wifi_listener", false)) { + startService(new Intent(this, WifiListenerService.class)); + } else { + stopService(new Intent(this, WifiListenerService.class)); + } + } } |