|
|
@@ -3,14 +3,24 @@ package info.knacki.prometheusandroidexporter;
|
|
|
import android.app.ActivityManager;
|
|
|
import android.content.Context;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Looper;
|
|
|
+import android.text.Editable;
|
|
|
+import android.text.TextWatcher;
|
|
|
import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.EditText;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
-public class MainActivity extends AppCompatActivity {
|
|
|
+import info.knacki.prometheusandroidexporter.configuration.ConfigurationManager;
|
|
|
+import info.knacki.prometheusandroidexporter.receiver.NetworkReceiver;
|
|
|
+
|
|
|
+public class MainActivity extends AppCompatActivity implements NetworkReceiver.AdditionalReceiver {
|
|
|
+ private ConfigurationManager.Configuration fConfig;
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
@@ -30,9 +40,57 @@ public class MainActivity extends AppCompatActivity {
|
|
|
UpdateUiState();
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ fConfig = ConfigurationManager.GetInstance(this).GetConfiguration();
|
|
|
+
|
|
|
+ final Button bt_save = findViewById(R.id.bt_saveconfiguration);
|
|
|
+ bt_save.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ ConfigurationManager.Save(MainActivity.this, fConfig);
|
|
|
+ MainService.StopService(MainActivity.this);
|
|
|
+ MainService.StartService(MainActivity.this);
|
|
|
+ bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ EditText input_port = findViewById(R.id.input_port);
|
|
|
+ input_port.setText(Short.toString(fConfig.GetPort()));
|
|
|
+ input_port.addTextChangedListener(new TextWatcher() {
|
|
|
+ @Override
|
|
|
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterTextChanged(Editable s) {
|
|
|
+ short port;
|
|
|
+ try {
|
|
|
+ port = Short.parseShort(s.toString());
|
|
|
+ }
|
|
|
+ catch (NumberFormatException e) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fConfig.SetPort(port);
|
|
|
+ bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
|
+ }
|
|
|
+ });
|
|
|
UpdateUiState();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ NetworkReceiver.SetAdditionalReceiver(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPause() {
|
|
|
+ super.onPause();
|
|
|
+ NetworkReceiver.SetAdditionalReceiver(null);
|
|
|
+ }
|
|
|
+
|
|
|
private boolean IsServiceRunning() {
|
|
|
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
|
|
|
@@ -45,10 +103,34 @@ public class MainActivity extends AppCompatActivity {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ private String GetVerboseIp() {
|
|
|
+ String localIP = HttpService.GetBoundAddr(getApplicationContext());
|
|
|
+ if (localIP == null)
|
|
|
+ return " - waiting for network";
|
|
|
+ return " on " + localIP;
|
|
|
+ }
|
|
|
+
|
|
|
private void UpdateUiState() {
|
|
|
- boolean running = IsServiceRunning();
|
|
|
- findViewById(R.id.bt_killservice).setEnabled(running);
|
|
|
- findViewById(R.id.bt_startservice).setEnabled(!running);
|
|
|
- ((TextView) findViewById(R.id.txt_servicestate)).setText(running ? "running" : "stopped");
|
|
|
+ (new Thread() {
|
|
|
+ @Override
|
|
|
+ public synchronized void run() {
|
|
|
+ final boolean running = IsServiceRunning();
|
|
|
+ final String ip = GetVerboseIp();
|
|
|
+
|
|
|
+ (new Handler(Looper.getMainLooper())).post(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ findViewById(R.id.bt_killservice).setEnabled(running);
|
|
|
+ findViewById(R.id.bt_startservice).setEnabled(!running);
|
|
|
+ ((TextView) findViewById(R.id.txt_servicestate)).setText(running ? ("running" + ip) : "stopped");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void Update() {
|
|
|
+ UpdateUiState();
|
|
|
}
|
|
|
}
|