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-12-08 17:22:18 +01:00
import android.util.Log ;
2021-11-27 20:22:13 +01:00
import android.view.View ;
import android.widget.Button ;
2021-12-09 18:03:00 +01:00
import android.widget.CheckBox ;
2021-11-27 20:22:13 +01:00
import android.widget.EditText ;
import android.widget.TextView ;
import android.widget.Toast ;
import androidx.annotation.Nullable ;
2021-12-11 14:15:05 +01:00
import com.jens.automation2.receivers.DeviceOrientationListener ;
2021-11-27 20:22:13 +01:00
import org.apache.commons.lang3.StringUtils ;
2021-12-11 14:15:05 +01:00
public class ActivityManageTriggerDeviceOrientation extends Activity
2021-11-27 20:22:13 +01:00
{
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-12-09 18:03:00 +01:00
CheckBox chkDevicePositionApplies ;
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-09 18:03:00 +01:00
try
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 ( ) ) ;
2022-01-05 18:06:26 +01:00
if ( desiredAzimuthTolerance = = 180 | | ( desiredAzimuth - desiredAzimuthTolerance < = azimuth & & 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-12-09 18:03:00 +01:00
}
catch ( Exception e )
{
tvAppliesAzimuth . setText ( " " ) ;
}
2021-11-27 20:22:13 +01:00
2021-12-09 18:03:00 +01:00
try
{
2021-11-28 20:01:58 +01:00
desiredPitch = Float . parseFloat ( etDesiredPitch . getText ( ) . toString ( ) ) ;
desiredPitchTolerance = Float . parseFloat ( etDesiredPitchTolerance . getText ( ) . toString ( ) ) ;
2022-01-05 18:06:26 +01:00
if ( desiredPitchTolerance = = 180 | | ( desiredPitch - desiredPitchTolerance < = pitch & & 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-12-09 18:03:00 +01:00
}
catch ( Exception e )
{
tvAppliesPitch . setText ( " " ) ;
}
2021-11-27 20:22:13 +01:00
2021-12-09 18:03:00 +01:00
try
{
2021-11-28 20:01:58 +01:00
desiredRoll = Float . parseFloat ( etDesiredRoll . getText ( ) . toString ( ) ) ;
desiredRollTolerance = Float . parseFloat ( etDesiredRollTolerance . getText ( ) . toString ( ) ) ;
2022-01-05 18:06:26 +01:00
if ( desiredRollTolerance = = 180 | | ( desiredRoll - desiredRollTolerance < = roll & & 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
}
}
2021-12-09 18:03:00 +01:00
catch ( Exception e )
{
tvAppliesRoll . setText ( " " ) ;
}
2021-11-27 20:22:13 +01:00
}
@Override
protected void onCreate ( @Nullable Bundle savedInstanceState )
{
super . onCreate ( savedInstanceState ) ;
2021-12-11 14:15:05 +01:00
setContentView ( R . layout . activity_manage_trigger_device_orientation ) ;
2021-11-27 20:22:13 +01:00
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 ) ;
2021-12-09 18:03:00 +01:00
chkDevicePositionApplies = ( CheckBox ) findViewById ( R . id . chkDevicePositionApplies ) ;
2021-11-28 20:01:58 +01:00
// 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 ;
2021-12-08 17:22:18 +01:00
try
{
2021-12-09 18:03:00 +01:00
boolean chkValue = getIntent ( ) . getBooleanExtra ( ActivityManageRule . intentNameTriggerParameter1 , true ) ;
chkDevicePositionApplies . setChecked ( chkValue ) ;
2021-12-08 17:22:18 +01:00
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 ] ) ;
}
catch ( Exception e )
{
2021-12-11 14:15:05 +01:00
Toast . makeText ( ActivityManageTriggerDeviceOrientation . this , getResources ( ) . getString ( R . string . triggerWrong ) , Toast . LENGTH_SHORT ) . show ( ) ;
2021-12-08 17:22:18 +01:00
Miscellaneous . logEvent ( " e " , " DevicePositionTrigger " , " There \ 's something wrong with a device position trigger. Content: " + getIntent ( ) . getStringExtra ( vectorFieldName ) + " , " + Log . getStackTraceString ( e ) , 1 ) ;
}
2021-11-28 20:01:58 +01:00
}
2021-11-27 20:22:13 +01:00
bApplyPositionValues . setOnClickListener ( new View . OnClickListener ( )
{
@Override
public void onClick ( View v )
{
2022-01-05 18:06:26 +01:00
// Round the values. Too long decimals will destroy the layout
2021-11-28 20:01:58 +01:00
if ( ! StringUtils . isEmpty ( currentAzimuth . getText ( ) ) )
2022-01-05 18:06:26 +01:00
etDesiredAzimuth . setText ( String . valueOf ( Math . round ( Float . parseFloat ( currentAzimuth . getText ( ) . toString ( ) ) ) ) ) ;
2021-11-27 20:22:13 +01:00
2021-11-28 20:01:58 +01:00
if ( ! StringUtils . isEmpty ( currentPitch . getText ( ) ) )
2022-01-05 18:06:26 +01:00
etDesiredPitch . setText ( String . valueOf ( Math . round ( Float . parseFloat ( currentPitch . getText ( ) . toString ( ) ) ) ) ) ;
2021-11-27 20:22:13 +01:00
2021-11-28 20:01:58 +01:00
if ( ! StringUtils . isEmpty ( currentRoll . getText ( ) ) )
2022-01-05 18:06:26 +01:00
etDesiredRoll . setText ( String . valueOf ( Math . round ( Float . parseFloat ( currentRoll . getText ( ) . toString ( ) ) ) ) ) ;
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
{
2021-12-11 14:15:05 +01:00
Toast . makeText ( ActivityManageTriggerDeviceOrientation . this , getResources ( ) . getString ( R . string . enterValidNumbersIntoAllFields ) , Toast . LENGTH_LONG ) . show ( ) ;
2021-11-27 20:22:13 +01:00
}
else
{
// Save
2021-11-28 20:01:58 +01:00
Intent returnData = new Intent ( ) ;
2021-12-09 18:03:00 +01:00
returnData . putExtra ( ActivityManageRule . intentNameTriggerParameter1 , chkDevicePositionApplies . isChecked ( ) ) ;
2021-11-28 20:01:58 +01:00
returnData . putExtra ( vectorFieldName ,
2021-12-09 18:03:00 +01:00
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 ( ) ) ;
2021-11-28 20:01:58 +01:00
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 )
{
2021-12-11 14:15:05 +01:00
Miscellaneous . messageBox ( getResources ( ) . getString ( R . string . warning ) , getResources ( ) . getString ( R . string . toleranceOf180OnlyAllowedIn2Fields ) , ActivityManageTriggerDeviceOrientation . this ) . show ( ) ;
2021-12-05 14:51:00 +01:00
return false ;
}
}
2021-12-08 17:22:18 +01:00
return true ;
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-12-11 14:15:05 +01:00
DeviceOrientationListener . getInstance ( ) . startSensorFromConfigActivity ( ActivityManageTriggerDeviceOrientation . this , this ) ;
2021-11-27 20:22:13 +01:00
}
@Override
protected void onPause ( )
{
super . onPause ( ) ;
2021-12-11 14:15:05 +01:00
DeviceOrientationListener . 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 " " ;
}
}
}