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 MainActivity extends Activity { private Button buttonStart; private Intent dnsProxyService; private BroadcastReceiver receiver = new BroadcastReceiver() { /** * Set button_start state on receive from DNSProxyService */ @Override public void onReceive(Context context, Intent intent) { setStateButtonStart(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 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 public void onResume() { super.onResume(); this.setStateButtonStart(); registerReceiver(this.receiver, new IntentFilter(DNSProxyService.NOTIFICATION)); } @Override public void onPause() { super.onPause(); 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 */ @Override public void onActivityResult(int request, int result, Intent data) { if (result == RESULT_OK) { this.start(); } } /** * button_start is clicked */ public void onClickButtonStart(View view) { if (!DNSProxyService.isRunning()) { //start Intent intent = VpnService.prepare(this); if (intent != null) { startActivityForResult(intent, 0); } else { this.onActivityResult(0, RESULT_OK, null); } } else { // stop this.stop(); } } /** * Set button_start text */ public void setStateButtonStart() { if (!DNSProxyService.isRunning()) { this.buttonStart.setText(getString(R.string.button_start__start)); } else { this.buttonStart.setText(getString(R.string.button_start__stop)); } } /** * Start DNSProxyService */ public void start() { startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_START)); Toast .makeText( this, getString(R.string.service_starting), Toast.LENGTH_SHORT ) .show(); } /** * Stop DNSProxyService */ public void stop() { startService(this.dnsProxyService.setAction(DNSProxyService.ACTION_STOP)); Toast .makeText( this, getString(R.string.service_stopping), Toast.LENGTH_SHORT ) .show(); } }