|
@@ -9,11 +9,14 @@ import android.text.Editable;
|
|
|
import android.text.TextWatcher;
|
|
import android.text.TextWatcher;
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
|
import android.widget.Button;
|
|
import android.widget.Button;
|
|
|
|
|
+import android.widget.CompoundButton;
|
|
|
import android.widget.EditText;
|
|
import android.widget.EditText;
|
|
|
import android.widget.TextView;
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
+import androidx.appcompat.widget.SwitchCompat;
|
|
|
|
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import info.knacki.prometheusandroidexporter.configuration.ConfigurationManager;
|
|
import info.knacki.prometheusandroidexporter.configuration.ConfigurationManager;
|
|
@@ -25,7 +28,9 @@ public class MainActivity extends AppCompatActivity implements NetworkReceiver.A
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_main);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
+ fConfig = ConfigurationManager.GetInstance(this).GetConfiguration();
|
|
|
|
|
|
|
|
|
|
+ // Start / kill service buttons
|
|
|
findViewById(R.id.bt_killservice).setOnClickListener(new View.OnClickListener() {
|
|
findViewById(R.id.bt_killservice).setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
public void onClick(View v) {
|
|
@@ -41,21 +46,23 @@ public class MainActivity extends AppCompatActivity implements NetworkReceiver.A
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- fConfig = ConfigurationManager.GetInstance(this).GetConfiguration();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // Save config button
|
|
|
final Button bt_save = findViewById(R.id.bt_saveconfiguration);
|
|
final Button bt_save = findViewById(R.id.bt_saveconfiguration);
|
|
|
bt_save.setOnClickListener(new View.OnClickListener() {
|
|
bt_save.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
public void onClick(View v) {
|
|
|
ConfigurationManager.Save(MainActivity.this, fConfig);
|
|
ConfigurationManager.Save(MainActivity.this, fConfig);
|
|
|
- MainService.StopService(MainActivity.this);
|
|
|
|
|
- MainService.StartService(MainActivity.this);
|
|
|
|
|
|
|
+ if (IsServiceRunning()) {
|
|
|
|
|
+ MainService.StopService(MainActivity.this);
|
|
|
|
|
+ MainService.StartService(MainActivity.this);
|
|
|
|
|
+ }
|
|
|
bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // Port input
|
|
|
EditText input_port = findViewById(R.id.input_port);
|
|
EditText input_port = findViewById(R.id.input_port);
|
|
|
- input_port.setText(Short.toString(fConfig.GetPort()));
|
|
|
|
|
|
|
+ input_port.setText(("" + fConfig.GetPort()));
|
|
|
input_port.addTextChangedListener(new TextWatcher() {
|
|
input_port.addTextChangedListener(new TextWatcher() {
|
|
|
@Override
|
|
@Override
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
|
@@ -76,13 +83,25 @@ public class MainActivity extends AppCompatActivity implements NetworkReceiver.A
|
|
|
bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
- UpdateUiState();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Start on boot input
|
|
|
|
|
+ // FIXME check app permission
|
|
|
|
|
+ SwitchCompat input_startonboot = findViewById(R.id.input_startonboot);
|
|
|
|
|
+ input_startonboot.setChecked(fConfig.IsStartOnBootEnabled());
|
|
|
|
|
+ input_startonboot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
|
+ fConfig.SetStartOnBootEnabled(isChecked);
|
|
|
|
|
+ bt_save.setEnabled(!fConfig.equals(ConfigurationManager.GetInstance(MainActivity.this).GetConfiguration()));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
protected void onResume() {
|
|
protected void onResume() {
|
|
|
super.onResume();
|
|
super.onResume();
|
|
|
NetworkReceiver.SetAdditionalReceiver(this);
|
|
NetworkReceiver.SetAdditionalReceiver(this);
|
|
|
|
|
+ UpdateUiState();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -93,7 +112,8 @@ public class MainActivity extends AppCompatActivity implements NetworkReceiver.A
|
|
|
|
|
|
|
|
private boolean IsServiceRunning() {
|
|
private boolean IsServiceRunning() {
|
|
|
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
|
|
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
- final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
|
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ final List<ActivityManager.RunningServiceInfo> services = (activityManager == null) ? ((List<ActivityManager.RunningServiceInfo>)Collections.EMPTY_LIST) : activityManager.getRunningServices(Integer.MAX_VALUE);
|
|
|
final String serviceClassName = MainService.class.getName();
|
|
final String serviceClassName = MainService.class.getName();
|
|
|
|
|
|
|
|
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
|
|
for (ActivityManager.RunningServiceInfo runningServiceInfo : services) {
|
|
@@ -122,7 +142,7 @@ public class MainActivity extends AppCompatActivity implements NetworkReceiver.A
|
|
|
public void run() {
|
|
public void run() {
|
|
|
findViewById(R.id.bt_killservice).setEnabled(running);
|
|
findViewById(R.id.bt_killservice).setEnabled(running);
|
|
|
findViewById(R.id.bt_startservice).setEnabled(!running);
|
|
findViewById(R.id.bt_startservice).setEnabled(!running);
|
|
|
- ((TextView) findViewById(R.id.txt_servicestate)).setText(running ? ("running" + ip) : "stopped");
|
|
|
|
|
|
|
+ ((TextView) findViewById(R.id.txt_servicestate)).setText(running ? (" running" + ip) : " stopped");
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|