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.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 { private Button buttonStart; private Button buttonStop; private BroadcastReceiver receiveDNSProxyService = new BroadcastReceiver() { /** * Set button_start state on receive from DNSProxyService */ @Override public void onReceive(Context context, Intent intent) { setStateButtonStartStop(); } }; private BroadcastReceiver receiveNetworkChange = new BroadcastReceiver() { /** * Set button_start state on receive of network change */ @Override public void onReceive(Context context, Intent intent) { setStateButtonStartStop(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.buttonStart = (Button) findViewById(R.id.button_start); this.buttonStop = (Button) findViewById(R.id.button_stop); } @Override public void onResume() { super.onResume(); this.setStateButtonStartStop(); registerReceiver(this.receiveDNSProxyService, new IntentFilter(DNSProxyService.NOTIFICATION)); registerReceiver(this.receiveNetworkChange, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); WifiListenerService.toggle(this); WifiListenerService.OnActivationListener.askLocationIfNeeded(this); } @Override public void onPause() { super.onPause(); unregisterReceiver(this.receiveDNSProxyService); unregisterReceiver(this.receiveNetworkChange); } @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) { startActivity(new Intent(this, SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } /** * is called by "startActivityForResult" internally */ @Override public void onActivityResult(int request, int result, Intent data) { if (result == RESULT_OK) { this.startDNSProxyService(); } } /** * button_start is clicked */ public void onClickButtonStart(View view) { this.prepareVpnService(); } /** * button_stop is clicked */ public void onClickButtonStop(View view) { this.stopDNSProxyService(); } /** * prepare VpnService properly * and ask for confirmation */ private void prepareVpnService() { Intent intent = VpnService.prepare(this); if (intent != null) { startActivityForResult(intent, 0); } else { this.onActivityResult(0, RESULT_OK, null); } } /** * Set button_start state */ private void setStateButtonStartStop() { if (!DNSProxyService.isRunning()) { this.buttonStart.setVisibility(View.VISIBLE); this.buttonStop.setVisibility(View.GONE); } else { 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); } */ } /** * Start DNSProxyService */ private void startDNSProxyService() { DNSProxyService.start(this); Toast .makeText( this, getString(R.string.service_starting), Toast.LENGTH_SHORT ) .show(); } /** * Stop DNSProxyService */ private void stopDNSProxyService() { DNSProxyService.stop(this); Toast .makeText( this, getString(R.string.service_stopping), Toast.LENGTH_SHORT ) .show(); } }