diff options
Diffstat (limited to 'app/java/src/MainActivity.java')
-rw-r--r-- | app/java/src/MainActivity.java | 118 |
1 files changed, 93 insertions, 25 deletions
diff --git a/app/java/src/MainActivity.java b/app/java/src/MainActivity.java index 9fd99f6..f437c9d 100644 --- a/app/java/src/MainActivity.java +++ b/app/java/src/MainActivity.java @@ -3,7 +3,10 @@ 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; @@ -18,51 +21,116 @@ import android.widget.Toast; 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) { + Bundle bundle = intent.getExtras(); + // if (bundle != null) { + setStateButtonStart(); + // } + } + }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.main); this.buttonStart = (Button) findViewById(R.id.button_start); - this.buttonStart.setText(DNSService.isRunning() ? "Stop" : "Start"); + this.dnsProxyService = new Intent(this, DNSProxyService.class); } @Override public void onResume() { super.onResume(); - this.buttonStart.setText("on RESSSSUME"); + + this.setStateButtonStart(); + registerReceiver(this.receiver, new IntentFilter(DNSProxyService.NOTIFICATION)); } @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {} + public void onPause() { + super.onPause(); - public void onStartClick(View view) { - Intent intent = VpnService.prepare(this); - if (intent != null) { - startActivityForResult(intent, 0); + unregisterReceiver(receiver); + } + + /** + * is called by "startActivityForResult" internally + */ + @Override + public void onActivityResult(int request, int result, Intent data) { + if (result == RESULT_OK) { + this.start(); } - //Intent intent = new Intent(this, DNSService.class); + } - if (!DNSService.isRunning()) { - startService(intent); + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {} - new AlertDialog.Builder(this) - .setTitle("PROXY START") - .setMessage("BRRR") - .show(); - Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); - this.buttonStart.setText("Stop"); + /** + * button_start is clicked + */ + public void onClickButtonStart(View view) { + if (!DNSProxyService.isRunning()) { //start + Intent intent = DNSProxyService.prepare(this); + + if (intent != null) { + startActivityForResult(intent, 0); + } else { + this.onActivityResult(0, RESULT_OK, null); + } + } + else { // stop + this.stop(); } - else { - stopService(intent); - - new AlertDialog.Builder(this) - .setTitle("PROXY STOP") - .setMessage("RRRB") - .show(); - Toast.makeText(this, "service stopping", Toast.LENGTH_SHORT).show(); - this.buttonStart.setText("Start"); + } + + /** + * 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); + + Toast + .makeText( + this, + getString(R.string.service_starting), + Toast.LENGTH_SHORT + ) + .show(); + } + + /** + * Stop DNSProxyService + */ + public void stop() { + stopService(this.dnsProxyService); + + Toast + .makeText( + this, + getString(R.string.service_stopping), + Toast.LENGTH_SHORT + ) + .show(); + } } |