Automation/app/src/main/java/com/jens/automation2/ActivityManageTriggerDevicePosition.java

259 lines
11 KiB
Java
Raw Normal View History

2021-11-27 20:22:13 +01:00
package com.jens.automation2;
import android.app.Activity;
2021-11-28 20:01:58 +01:00
import android.content.Intent;
2021-11-27 20:22:13 +01:00
import android.graphics.Color;
import android.os.Bundle;
2021-11-28 20:01:58 +01:00
import android.text.InputFilter;
import android.text.Spanned;
2021-11-27 20:22:13 +01:00
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.jens.automation2.receivers.DevicePositionListener;
import org.apache.commons.lang3.StringUtils;
public class ActivityManageTriggerDevicePosition extends Activity
{
2021-11-28 20:01:58 +01:00
TextView currentAzimuth, currentPitch, currentRoll, tvAppliesAzimuth, tvAppliesPitch, tvAppliesRoll;
2021-11-27 20:22:13 +01:00
Button bApplyPositionValues, bSavePositionValues;
2021-11-28 20:01:58 +01:00
EditText etDesiredAzimuth, etDesiredAzimuthTolerance, etDesiredPitch, etDesiredPitchTolerance, etDesiredRoll, etDesiredRollTolerance;
2021-11-27 20:22:13 +01:00
2021-11-28 20:01:58 +01:00
public static String vectorFieldName = "deviceVector";
2021-11-27 20:22:13 +01:00
2021-11-28 20:01:58 +01:00
boolean editMode = false;
float desiredAzimuth, desiredPitch, desiredRoll, desiredAzimuthTolerance, desiredPitchTolerance, desiredRollTolerance;
public void updateFields(float azimuth, float pitch, float roll)
2021-11-27 20:22:13 +01:00
{
2021-11-28 20:01:58 +01:00
currentAzimuth.setText(Float.toString(azimuth));
currentPitch.setText(Float.toString(pitch));
currentRoll.setText(Float.toString(roll));
2021-11-27 20:22:13 +01:00
2021-12-05 17:24:47 +01:00
if(checkInputs(false))
2021-11-27 20:22:13 +01:00
{
2021-11-28 20:01:58 +01:00
desiredAzimuth = Float.parseFloat(etDesiredAzimuth.getText().toString());
desiredAzimuthTolerance = Float.parseFloat(etDesiredAzimuthTolerance.getText().toString());
if(Math.abs(azimuth) <= Math.abs(desiredAzimuth - desiredAzimuthTolerance) || Math.abs(azimuth) <= desiredAzimuth + desiredAzimuthTolerance)
2021-11-27 20:22:13 +01:00
{
2021-11-28 20:01:58 +01:00
tvAppliesAzimuth.setText(getResources().getString(R.string.yes));
tvAppliesAzimuth.setTextColor(Color.GREEN);
2021-11-27 20:22:13 +01:00
}
else
{
2021-11-28 20:01:58 +01:00
tvAppliesAzimuth.setText(getResources().getString(R.string.no));
tvAppliesAzimuth.setTextColor(Color.RED);
2021-11-27 20:22:13 +01:00
}
2021-11-28 20:01:58 +01:00
desiredPitch = Float.parseFloat(etDesiredPitch.getText().toString());
desiredPitchTolerance = Float.parseFloat(etDesiredPitchTolerance.getText().toString());
if(Math.abs(pitch) <= Math.abs(desiredPitch - desiredPitchTolerance) || Math.abs(pitch) <= desiredPitch + desiredPitchTolerance)
2021-11-27 20:22:13 +01:00
{
2021-11-28 20:01:58 +01:00
tvAppliesPitch.setText(getResources().getString(R.string.yes));
tvAppliesPitch.setTextColor(Color.GREEN);
2021-11-27 20:22:13 +01:00
}
else
{
2021-11-28 20:01:58 +01:00
tvAppliesPitch.setText(getResources().getString(R.string.no));
tvAppliesPitch.setTextColor(Color.RED);
2021-11-27 20:22:13 +01:00
}
2021-11-28 20:01:58 +01:00
desiredRoll = Float.parseFloat(etDesiredRoll.getText().toString());
desiredRollTolerance = Float.parseFloat(etDesiredRollTolerance.getText().toString());
if(Math.abs(roll) <= Math.abs(desiredRoll - desiredRollTolerance) || Math.abs(roll) <= desiredRoll + desiredRollTolerance)
2021-11-27 20:22:13 +01:00
{
2021-11-28 20:01:58 +01:00
tvAppliesRoll.setText(getResources().getString(R.string.yes));
tvAppliesRoll.setTextColor(Color.GREEN);
2021-11-27 20:22:13 +01:00
}
else
{
2021-11-28 20:01:58 +01:00
tvAppliesRoll.setText(getResources().getString(R.string.no));
tvAppliesRoll.setTextColor(Color.RED);
2021-11-27 20:22:13 +01:00
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_trigger_device_position);
2021-11-28 20:01:58 +01:00
currentAzimuth = (TextView) findViewById(R.id.tvCurrentAzimuth);
currentPitch = (TextView) findViewById(R.id.tvCurrentOrientationPitch);
currentRoll = (TextView) findViewById(R.id.tvCurrentRoll);
tvAppliesAzimuth = (TextView) findViewById(R.id.tvAppliesAzimuth);
tvAppliesPitch = (TextView) findViewById(R.id.tvAppliesPitch);
tvAppliesRoll = (TextView) findViewById(R.id.tvAppliesRoll);
2021-11-27 20:22:13 +01:00
bApplyPositionValues = (Button) findViewById(R.id.bApplyPositionValues);
bSavePositionValues = (Button) findViewById(R.id.bSavePositionValues);
2021-11-28 20:01:58 +01:00
etDesiredAzimuth = (EditText) findViewById(R.id.etDesiredAzimuth);
etDesiredAzimuthTolerance = (EditText) findViewById(R.id.etDesiredAzimuthTolerance);
etDesiredPitch = (EditText) findViewById(R.id.etDesiredPitch);
etDesiredPitchTolerance = (EditText) findViewById(R.id.etDesiredPitchTolerance);
etDesiredRoll = (EditText) findViewById(R.id.etDesiredRoll);
etDesiredRollTolerance = (EditText) findViewById(R.id.etDesiredRollTolerance);
// etDesiredAzimuth.setFilters(new InputFilter[]{new InputFilterMinMax(-180, 180)});
// etDesiredPitch.setFilters(new InputFilter[]{new InputFilterMinMax(-180, 180)});
// etDesiredRoll.setFilters(new InputFilter[]{new InputFilterMinMax(-180, 180)});
2021-12-05 17:24:47 +01:00
etDesiredAzimuthTolerance.setFilters(new InputFilter[]{new InputFilterMinMax(0, 180)});
etDesiredPitchTolerance.setFilters(new InputFilter[]{new InputFilterMinMax(0, 180)});
etDesiredRollTolerance.setFilters(new InputFilter[]{new InputFilterMinMax(0, 180)});
2021-11-28 20:01:58 +01:00
if(getIntent().hasExtra(vectorFieldName))
{
editMode = true;
String values[] = getIntent().getStringExtra(vectorFieldName).split(Trigger.triggerParameter2Split);
etDesiredAzimuth.setText(values[0]);
etDesiredAzimuthTolerance.setText(values[1]);
etDesiredPitch.setText(values[2]);
etDesiredPitchTolerance.setText(values[3]);
etDesiredRoll.setText(values[4]);
etDesiredRollTolerance.setText(values[5]);
}
2021-11-27 20:22:13 +01:00
bApplyPositionValues.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
2021-11-28 20:01:58 +01:00
if(!StringUtils.isEmpty(currentAzimuth.getText()))
etDesiredAzimuth.setText(currentAzimuth.getText());
2021-11-27 20:22:13 +01:00
2021-11-28 20:01:58 +01:00
if(!StringUtils.isEmpty(currentPitch.getText()))
etDesiredPitch.setText(currentPitch.getText());
2021-11-27 20:22:13 +01:00
2021-11-28 20:01:58 +01:00
if(!StringUtils.isEmpty(currentRoll.getText()))
etDesiredRoll.setText(currentRoll.getText());
2021-11-27 20:22:13 +01:00
}
});
bSavePositionValues.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
2021-12-05 17:24:47 +01:00
if(!checkInputs(true))
2021-11-27 20:22:13 +01:00
{
Toast.makeText(ActivityManageTriggerDevicePosition.this, getResources().getString(R.string.enterValidNumbersIntoAllFields), Toast.LENGTH_LONG).show();
}
else
{
// Save
2021-11-28 20:01:58 +01:00
Intent returnData = new Intent();
returnData.putExtra(vectorFieldName,
etDesiredAzimuth.getText().toString() + Trigger.triggerParameter2Split +
etDesiredAzimuthTolerance.getText().toString() + Trigger.triggerParameter2Split +
etDesiredPitch.getText().toString() + Trigger.triggerParameter2Split +
etDesiredPitchTolerance.getText().toString() + Trigger.triggerParameter2Split +
etDesiredRoll.getText().toString() + Trigger.triggerParameter2Split +
etDesiredRollTolerance.getText().toString());
setResult(RESULT_OK, returnData);
finish();
2021-11-27 20:22:13 +01:00
}
}
});
}
2021-12-05 17:24:47 +01:00
boolean checkInputs(boolean showMessages)
2021-11-27 20:22:13 +01:00
{
2021-11-28 20:01:58 +01:00
if(
!StringUtils.isEmpty(etDesiredAzimuth.getText().toString()) && Miscellaneous.isNumeric(etDesiredAzimuth.getText().toString())
2021-11-27 20:22:13 +01:00
&&
2021-11-28 20:01:58 +01:00
!StringUtils.isEmpty(etDesiredAzimuthTolerance.getText().toString()) && Miscellaneous.isNumeric(etDesiredAzimuthTolerance.getText().toString())
2021-11-27 20:22:13 +01:00
&&
2021-11-28 20:01:58 +01:00
!StringUtils.isEmpty(etDesiredPitch.getText().toString()) && Miscellaneous.isNumeric(etDesiredPitch.getText().toString())
2021-11-27 20:22:13 +01:00
&&
2021-11-28 20:01:58 +01:00
!StringUtils.isEmpty(etDesiredPitchTolerance.getText().toString()) && Miscellaneous.isNumeric(etDesiredPitchTolerance.getText().toString())
2021-11-27 20:22:13 +01:00
&&
2021-11-28 20:01:58 +01:00
!StringUtils.isEmpty(etDesiredRoll.getText().toString()) && Miscellaneous.isNumeric(etDesiredRoll.getText().toString())
2021-11-27 20:22:13 +01:00
&&
2021-11-28 20:01:58 +01:00
!StringUtils.isEmpty(etDesiredRollTolerance.getText().toString()) && Miscellaneous.isNumeric(etDesiredRollTolerance.getText().toString())
)
{
float da = Float.parseFloat(etDesiredAzimuth.getText().toString());
float dp = Float.parseFloat(etDesiredPitch.getText().toString());
float dr = Float.parseFloat(etDesiredRoll.getText().toString());
2021-12-05 14:51:00 +01:00
if(Math.abs(da) > 180 || Math.abs(dp) > 180 || Math.abs(dr) > 180)
{
return false;
}
2021-12-05 17:24:47 +01:00
if(showMessages)
2021-12-05 14:51:00 +01:00
{
float dat = Float.parseFloat(etDesiredAzimuthTolerance.getText().toString());
float dpt = Float.parseFloat(etDesiredPitchTolerance.getText().toString());
float drt = Float.parseFloat(etDesiredRollTolerance.getText().toString());
/*
The user may enter a tolerance of 180° for two directions, but not all three.
Otherwise this trigger would always apply.
*/
if (Math.abs(dat) >= 180 && Math.abs(dpt) >= 180 && Math.abs(drt) >= 180)
{
Miscellaneous.messageBox(getResources().getString(R.string.warning), getResources().getString(R.string.toleranceOf180OnlyAllowedIn2Fields), ActivityManageTriggerDevicePosition.this).show();
return false;
}
}
2021-11-28 20:01:58 +01:00
}
return false;
2021-11-27 20:22:13 +01:00
}
@Override
protected void onResume()
{
super.onResume();
2021-11-29 20:14:09 +01:00
DevicePositionListener.getInstance().startSensorFromConfigActivity(ActivityManageTriggerDevicePosition.this, this);
2021-11-27 20:22:13 +01:00
}
@Override
protected void onPause()
{
super.onPause();
2021-11-29 20:14:09 +01:00
DevicePositionListener.getInstance().stopSensorFromConfigActivity();
2021-11-27 20:22:13 +01:00
}
2021-11-28 20:01:58 +01:00
public class InputFilterMinMax implements InputFilter
{
private float minimumValue;
private float maximumValue;
public InputFilterMinMax(float minimumValue, float maximumValue)
{
this.minimumValue = minimumValue;
this.maximumValue = maximumValue;
}
private boolean isInRange(float a, float b, float c)
{
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
{
try
{
int input = Integer.parseInt(dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length()));
if (isInRange(minimumValue, maximumValue, input))
return null;
}
catch (NumberFormatException nfe)
{
}
return "";
}
}
}