@ -1,8 +1,11 @@
package com.jens.automation2 ;
import android.app.Activity ;
import android.content.Intent ;
import android.graphics.Color ;
import android.os.Bundle ;
import android.text.InputFilter ;
import android.text.Spanned ;
import android.view.View ;
import android.widget.Button ;
import android.widget.EditText ;
@ -17,57 +20,61 @@ import org.apache.commons.lang3.StringUtils;
public class ActivityManageTriggerDevicePosition extends Activity
{
TextView current OrientationX, currentOrientationY , currentOrientationZ , tvAppliesX , tvAppliesY , tvAppliesZ ;
TextView current Azimuth, currentPitch , currentRoll , tvAppliesAzimuth , tvAppliesPitch , tvAppliesRoll ;
Button bApplyPositionValues , bSavePositionValues ;
EditText etDesired PositionX, etDesiredPositionXTolerance , etDesiredPositionY , etDesiredPositionYTolerance , etDesiredPositionZ , etDesiredPositionZ Tolerance;
EditText etDesired Azimuth, etDesiredAzimuthTolerance , etDesiredPitch , etDesiredPitchTolerance , etDesiredRoll , etDesiredRoll Tolerance;
float desiredX , desiredY , desiredZ , desiredXTolerance , desiredYTolerance , desiredZTolerance ;
public static String vectorFieldName = "deviceVector" ;
public void updateFields ( float x , float y , float z )
boolean editMode = false ;
float desiredAzimuth , desiredPitch , desiredRoll , desiredAzimuthTolerance , desiredPitchTolerance , desiredRollTolerance ;
public void updateFields ( float azimuth , float pitch , float roll )
{
currentOrientationX . setText ( Float . toString ( x ) ) ;
currentOrientationY . setText ( Float . toString ( y ) ) ;
currentOrientationZ . setText ( Float . toString ( z ) ) ;
current Azimuth. setText ( Float . toString ( azimuth ) ) ;
current Pitch. setText ( Float . toString ( pitch ) ) ;
current Roll. setText ( Float . toString ( roll ) ) ;
if ( checkInputs ( ) )
{
desired X = Float . parseFloat ( etDesiredPositionX . getText ( ) . toString ( ) ) ;
desired XTolerance = Float . parseFloat ( etDesiredPositionX Tolerance. getText ( ) . toString ( ) ) ;
if ( x > = desiredX - desiredXTolerance | | x < = desiredX + desiredX Tolerance)
desired Azimuth = Float . parseFloat ( etDesiredAzimuth . getText ( ) . toString ( ) ) ;
desired AzimuthTolerance = Float . parseFloat ( etDesiredAzimuth Tolerance. getText ( ) . toString ( ) ) ;
if ( Math. abs ( azimuth ) < = Math . abs ( desiredAzimuth - desiredAzimuthTolerance ) | | Math . abs ( azimuth ) < = desiredAzimuth + desiredAzimuth Tolerance)
{
tvApplies X . setText ( getResources ( ) . getString ( R . string . yes ) ) ;
tvApplies X . setTextColor ( Color . GREEN ) ;
tvApplies Azimuth . setText ( getResources ( ) . getString ( R . string . yes ) ) ;
tvApplies Azimuth . setTextColor ( Color . GREEN ) ;
}
else
{
tvApplies X . setText ( getResources ( ) . getString ( R . string . no ) ) ;
tvApplies X . setTextColor ( Color . RED ) ;
tvApplies Azimuth . setText ( getResources ( ) . getString ( R . string . no ) ) ;
tvApplies Azimuth . setTextColor ( Color . RED ) ;
}
desired Y = Float . parseFloat ( etDesiredPositionY . getText ( ) . toString ( ) ) ;
desired YTolerance = Float . parseFloat ( etDesiredPositionY Tolerance. getText ( ) . toString ( ) ) ;
if ( y > = desiredY - desiredYTolerance | | y < = desiredY + desiredY Tolerance)
desired Pitch = Float . parseFloat ( etDesiredPitch . getText ( ) . toString ( ) ) ;
desired PitchTolerance = Float . parseFloat ( etDesiredPitch Tolerance. getText ( ) . toString ( ) ) ;
if ( Math. abs ( pitch ) < = Math . abs ( desiredPitch - desiredPitchTolerance ) | | Math . abs ( pitch ) < = desiredPitch + desiredPitch Tolerance)
{
tvApplies Y . setText ( getResources ( ) . getString ( R . string . yes ) ) ;
tvApplies Y . setTextColor ( Color . GREEN ) ;
tvApplies Pitch . setText ( getResources ( ) . getString ( R . string . yes ) ) ;
tvApplies Pitch . setTextColor ( Color . GREEN ) ;
}
else
{
tvApplies Y . setText ( getResources ( ) . getString ( R . string . no ) ) ;
tvApplies Y . setTextColor ( Color . RED ) ;
tvApplies Pitch . setText ( getResources ( ) . getString ( R . string . no ) ) ;
tvApplies Pitch . setTextColor ( Color . RED ) ;
}
desired Z = Float . parseFloat ( etDesiredPositionZ . getText ( ) . toString ( ) ) ;
desired ZTolerance = Float . parseFloat ( etDesiredPositionZ Tolerance. getText ( ) . toString ( ) ) ;
if ( z > = desiredZ - desiredZTolerance | | z < = desiredZ + desiredZ Tolerance)
desired Roll = Float . parseFloat ( etDesiredRoll . getText ( ) . toString ( ) ) ;
desired RollTolerance = Float . parseFloat ( etDesiredRoll Tolerance. getText ( ) . toString ( ) ) ;
if ( Math. abs ( roll ) < = Math . abs ( desiredRoll - desiredRollTolerance ) | | Math . abs ( roll ) < = desiredRoll + desiredRoll Tolerance)
{
tvApplies Z . setText ( getResources ( ) . getString ( R . string . yes ) ) ;
tvApplies Z . setTextColor ( Color . GREEN ) ;
tvApplies Roll . setText ( getResources ( ) . getString ( R . string . yes ) ) ;
tvApplies Roll . setTextColor ( Color . GREEN ) ;
}
else
{
tvApplies Z . setText ( getResources ( ) . getString ( R . string . no ) ) ;
tvApplies Z . setTextColor ( Color . RED ) ;
tvApplies Roll . setText ( getResources ( ) . getString ( R . string . no ) ) ;
tvApplies Roll . setTextColor ( Color . RED ) ;
}
}
}
@ -78,36 +85,55 @@ public class ActivityManageTriggerDevicePosition extends Activity
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_manage_trigger_device_position ) ;
current OrientationX = ( TextView ) findViewById ( R . id . currentOrientationX ) ;
current OrientationY = ( TextView ) findViewById ( R . id . currentOrientationY ) ;
current OrientationZ = ( TextView ) findViewById ( R . id . currentOrientationZ ) ;
tvApplies X = ( TextView ) findViewById ( R . id . tvApplies X ) ;
tvApplies Y = ( TextView ) findViewById ( R . id . tvApplies Y ) ;
tvApplies Z = ( TextView ) findViewById ( R . id . tvApplies Z ) ;
current Azimuth = ( TextView ) findViewById ( R . id . tvCurrentAzimuth ) ;
current Pitch = ( TextView ) findViewById ( R . id . tvCurrentOrientationPitch ) ;
current Roll = ( TextView ) findViewById ( R . id . tvCurrentRoll ) ;
tvApplies Azimuth = ( TextView ) findViewById ( R . id . tvApplies Azimuth ) ;
tvApplies Pitch = ( TextView ) findViewById ( R . id . tvApplies Pitch ) ;
tvApplies Roll = ( TextView ) findViewById ( R . id . tvApplies Roll ) ;
bApplyPositionValues = ( Button ) findViewById ( R . id . bApplyPositionValues ) ;
bSavePositionValues = ( Button ) findViewById ( R . id . bSavePositionValues ) ;
etDesiredPositionX = ( EditText ) findViewById ( R . id . etDesiredPositionX ) ;
etDesiredPositionXTolerance = ( EditText ) findViewById ( R . id . etDesiredPositionXTolerance ) ;
etDesiredPositionY = ( EditText ) findViewById ( R . id . etDesiredPositionY ) ;
etDesiredPositionYTolerance = ( EditText ) findViewById ( R . id . etDesiredPositionYTolerance ) ;
etDesiredPositionZ = ( EditText ) findViewById ( R . id . etDesiredPositionZ ) ;
etDesiredPositionZTolerance = ( EditText ) findViewById ( R . id . etDesiredPositionZTolerance ) ;
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)});
etDesiredAzimuthTolerance . setFilters ( new InputFilter [ ] { new InputFilterMinMax ( 0 , 359 ) } ) ;
etDesiredPitchTolerance . setFilters ( new InputFilter [ ] { new InputFilterMinMax ( 0 , 359 ) } ) ;
etDesiredRollTolerance . setFilters ( new InputFilter [ ] { new InputFilterMinMax ( 0 , 359 ) } ) ;
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 ] ) ;
}
bApplyPositionValues . setOnClickListener ( new View . OnClickListener ( )
{
@Override
public void onClick ( View v )
{
if ( ! StringUtils . isEmpty ( currentOrientationX . getText ( ) ) )
etDesiredPositionX . setText ( currentOrientationX . getText ( ) ) ;
if ( ! StringUtils . isEmpty ( current Azimuth . getText ( ) ) )
etDesired Azimuth. setText ( currentAzimuth . getText ( ) ) ;
if ( ! StringUtils . isEmpty ( currentOrientationY . getText ( ) ) )
etDesiredPositionY . setText ( currentOrientationY . getText ( ) ) ;
if ( ! StringUtils . isEmpty ( current Pitch . getText ( ) ) )
etDesiredP itch. setText ( currentPitch . getText ( ) ) ;
if ( ! StringUtils . isEmpty ( currentOrientationZ . getText ( ) ) )
etDesiredPositionZ . setText ( currentOrientationZ . getText ( ) ) ;
if ( ! StringUtils . isEmpty ( current Roll . getText ( ) ) )
etDesired Roll. setText ( currentRoll . getText ( ) ) ;
}
} ) ;
@ -116,13 +142,24 @@ public class ActivityManageTriggerDevicePosition extends Activity
@Override
public void onClick ( View v )
{
if ( checkInputs ( ) )
if ( ! checkInputs ( ) )
{
Toast . makeText ( ActivityManageTriggerDevicePosition . this , getResources ( ) . getString ( R . string . enterValidNumbersIntoAllFields ) , Toast . LENGTH_LONG ) . show ( ) ;
}
else
{
// Save
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 ( ) ;
}
}
} ) ;
@ -130,19 +167,29 @@ public class ActivityManageTriggerDevicePosition extends Activity
boolean checkInputs ( )
{
return (
! StringUtils . isEmpty ( etDesired PositionX . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesired PositionX . getText ( ) . toString ( ) )
if (
! StringUtils . isEmpty ( etDesired Azimuth . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesired Azimuth . getText ( ) . toString ( ) )
& &
! StringUtils . isEmpty ( etDesired PositionX Tolerance. getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesired PositionX Tolerance. getText ( ) . toString ( ) )
! StringUtils . isEmpty ( etDesired Azimuth Tolerance. getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesired Azimuth Tolerance. getText ( ) . toString ( ) )
& &
! StringUtils . isEmpty ( etDesiredP ositionY . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesiredP ositionY . getText ( ) . toString ( ) )
! StringUtils . isEmpty ( etDesiredP itch . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesiredP itch . getText ( ) . toString ( ) )
& &
! StringUtils . isEmpty ( etDesiredP ositionY Tolerance. getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesiredP ositionY Tolerance. getText ( ) . toString ( ) )
! StringUtils . isEmpty ( etDesiredP itch Tolerance. getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesiredP itch Tolerance. getText ( ) . toString ( ) )
& &
! StringUtils . isEmpty ( etDesired PositionZ . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesired PositionZ . getText ( ) . toString ( ) )
! StringUtils . isEmpty ( etDesired Roll . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesired Roll . getText ( ) . toString ( ) )
& &
! StringUtils . isEmpty ( etDesiredPositionZTolerance . getText ( ) . toString ( ) ) & & Miscellaneous . isNumeric ( etDesiredPositionZTolerance . getText ( ) . toString ( ) )
) ;
! 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 ( ) ) ;
if ( Math . abs ( da ) < = 180 | | Math . abs ( dp ) < = 180 | | Math . abs ( dr ) < = 180 )
return true ;
}
return false ;
}
@Override
@ -158,4 +205,36 @@ public class ActivityManageTriggerDevicePosition extends Activity
super . onPause ( ) ;
DevicePositionListener . getInstance ( ) . stopSensor ( ) ;
}
}
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 "" ;
}
}
}