Position trigger

This commit is contained in:
jens 2021-11-29 20:14:09 +01:00
parent a9bd7b9561
commit 6f0dbc9555
3 changed files with 137 additions and 49 deletions

View File

@ -196,14 +196,14 @@ public class ActivityManageTriggerDevicePosition extends Activity
protected void onResume()
{
super.onResume();
DevicePositionListener.getInstance().startSensor(ActivityManageTriggerDevicePosition.this, this);
DevicePositionListener.getInstance().startSensorFromConfigActivity(ActivityManageTriggerDevicePosition.this, this);
}
@Override
protected void onPause()
{
super.onPause();
DevicePositionListener.getInstance().stopSensor();
DevicePositionListener.getInstance().stopSensorFromConfigActivity();
}
public class InputFilterMinMax implements InputFilter

View File

@ -10,6 +10,7 @@ import com.jens.automation2.receivers.AutomationListenerInterface;
import com.jens.automation2.receivers.BatteryReceiver;
import com.jens.automation2.receivers.BluetoothReceiver;
import com.jens.automation2.receivers.ConnectivityReceiver;
import com.jens.automation2.receivers.DevicePositionListener;
import com.jens.automation2.receivers.HeadphoneJackListener;
import com.jens.automation2.receivers.NoiseListener;
import com.jens.automation2.receivers.PhoneStatusListener;
@ -216,6 +217,7 @@ public class ReceiverCoordinator
BluetoothReceiver.stopBluetoothReceiver();
HeadphoneJackListener.getInstance().stopListener(AutomationService.getInstance());
DevicePositionListener.getInstance().stopListener(AutomationService.getInstance());
}
catch(Exception e)
{
@ -350,6 +352,24 @@ public class ReceiverCoordinator
}
}
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.devicePosition))
{
if(!DevicePositionListener.getInstance().isListenerRunning())
{
Miscellaneous.logEvent("i", "DevicePositionListener", "Starting DevicePositionListener because used in a new/changed rule.", 4);
if(HeadphoneJackListener.getInstance().haveAllPermission())
DevicePositionListener.getInstance().startListener(AutomationService.getInstance());
}
}
else
{
if(DevicePositionListener.getInstance().isListenerRunning())
{
Miscellaneous.logEvent("i", "DevicePositionListener", "Shutting down DevicePositionListener because not used in any rule.", 4);
DevicePositionListener.getInstance().stopListener(AutomationService.getInstance());
}
}
AutomationService.updateNotification();
}
}

View File

@ -10,9 +10,14 @@ import android.hardware.SensorManager;
import android.widget.TextView;
import com.jens.automation2.ActivityManageTriggerDevicePosition;
import com.jens.automation2.AutomationService;
import com.jens.automation2.Miscellaneous;
import com.jens.automation2.Rule;
import com.jens.automation2.Trigger;
public class DevicePositionListener implements SensorEventListener
import java.util.ArrayList;
public class DevicePositionListener implements SensorEventListener, AutomationListenerInterface
{
// https://developer.android.com/guide/topics/sensors/sensors_position#java
@ -21,6 +26,7 @@ public class DevicePositionListener implements SensorEventListener
//the Sensor Manager
private SensorManager sManager;
static DevicePositionListener instance = null;
boolean isRunning = false;
// Gravity rotational data
private float gravity[];
@ -43,34 +49,38 @@ public class DevicePositionListener implements SensorEventListener
return instance;
}
/*
Computes the device's orientation based on the rotation matrix.
When it returns, the array values are as follows:
values[0]: Azimuth, angle of rotation about the -z axis. This value represents the angle between the device's y axis and the magnetic north pole. When facing north, this angle is 0, when facing south, this angle is π. Likewise, when facing east, this angle is π/2, and when facing west, this angle is -π/2. The range of values is -π to π.
values[1]: Pitch, angle of rotation about the x axis. This value represents the angle between a plane parallel to the device's screen and a plane parallel to the ground. Assuming that the bottom edge of the device faces the user and that the screen is face-up, tilting the top edge of the device toward the ground creates a positive pitch angle. The range of values is -π to π.
values[2]: Roll, angle of rotation about the y axis. This value represents the angle between a plane perpendicular to the device's screen and a plane perpendicular to the ground. Assuming that the bottom edge of the device faces the user and that the screen is face-up, tilting the left edge of the device toward the ground creates a positive roll angle. The range of values is -π/2 to π/2.
Applying these three rotations in the azimuth, pitch, roll order transforms an identity matrix to the rotation matrix passed into this method. Also, note that all three orientation angles are expressed in radians.
*/
public void startSensor(Context context, ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance)
public void startSensorFromConfigActivity(Context context, ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance)
{
this.activityManageTriggerDevicePositionInstance = activityManageTriggerDevicePositionInstance;
sManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
if(!isRunning)
{
sManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
// SensorManager.getOrientation()
/*
register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible
*/
isRunning = true;
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
}
}
public void stopSensor()
public void stopSensorFromConfigActivity()
{
//unregister the sensor listener
sManager.unregisterListener(this);
if(isRunning)
{
if(!Rule.isAnyRuleUsing(Trigger.Trigger_Enum.devicePosition))
{
//unregister the sensor listener
sManager.unregisterListener(this);
isRunning = true;
}
}
}
@Override
@ -109,31 +119,89 @@ Applying these three rotations in the azimuth, pitch, roll order transforms an i
}
//else it will output the Roll, Pitch and Yawn values
activityManageTriggerDevicePositionInstance.updateFields(azimuth, pitch, roll);
if(activityManageTriggerDevicePositionInstance != null)
activityManageTriggerDevicePositionInstance.updateFields(azimuth, pitch, roll);
// tvToUpdate.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
// "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
// "Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
/*
Azimuth (degrees of rotation about the -z axis).
This is the angle between the device's current compass direction and magnetic north. If the top edge of the
device faces magnetic north, the azimuth is 0 degrees; if the top edge faces south, the azimuth is 180 degrees.
Similarly, if the top edge faces east, the azimuth is 90 degrees, and if the top edge faces west, the azimuth is 270 degrees.
Pitch (degrees of rotation about the x axis).
This is the angle between a plane parallel to the device's screen and a plane parallel to the ground. If you hold the device
parallel to the ground with the bottom edge closest to you and tilt the top edge of the device toward the ground, the pitch
angle becomes positive. Tilting in the opposite direction moving the top edge of the device away from the groundcauses
the pitch angle to become negative. The range of values is -180 degrees to 180 degrees.
Roll (degrees of rotation about the y axis).
This is the angle between a plane perpendicular to the device's screen and a plane perpendicular to the ground.
If you hold the device parallel to the ground with the bottom edge closest to you and tilt the left edge of the
device toward the ground, the roll angle becomes positive. Tilting in the opposite directionmoving the right
edge of the device toward the ground causes the roll angle to become negative. The range of values is -90 degrees
to 90 degrees.
*/
if(AutomationService.isMyServiceRunning(Miscellaneous.getAnyContext()))
{
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.devicePosition);
for (int i = 0; i < ruleCandidates.size(); i++)
{
if (ruleCandidates.get(i).applies(Miscellaneous.getAnyContext()))
ruleCandidates.get(i).activate(AutomationService.getInstance(), false);
}
}
}
@Override
public void startListener(AutomationService automationService)
{
if(!isRunning)
{
sManager = (SensorManager) Miscellaneous.getAnyContext().getSystemService(SENSOR_SERVICE);
/*
register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible
*/
isRunning = true;
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
public void stopListener(AutomationService automationService)
{
this.activityManageTriggerDevicePositionInstance = null;
if(isRunning)
{
//unregister the sensor listener
sManager.unregisterListener(this);
isRunning = true;
}
}
@Override
public boolean isListenerRunning()
{
return isRunning;
}
@Override
public Trigger.Trigger_Enum[] getMonitoredTrigger()
{
return new Trigger.Trigger_Enum[] { Trigger.Trigger_Enum.devicePosition };
}
/*
Azimuth (degrees of rotation about the -z axis).
This is the angle between the device's current compass direction and magnetic north. If the top edge of the
device faces magnetic north, the azimuth is 0 degrees; if the top edge faces south, the azimuth is 180 degrees.
Similarly, if the top edge faces east, the azimuth is 90 degrees, and if the top edge faces west, the azimuth is 270 degrees.
Pitch (degrees of rotation about the x axis).
This is the angle between a plane parallel to the device's screen and a plane parallel to the ground. If you hold the device
parallel to the ground with the bottom edge closest to you and tilt the top edge of the device toward the ground, the pitch
angle becomes positive. Tilting in the opposite direction moving the top edge of the device away from the groundcauses
the pitch angle to become negative. The range of values is -180 degrees to 180 degrees.
Roll (degrees of rotation about the y axis).
This is the angle between a plane perpendicular to the device's screen and a plane perpendicular to the ground.
If you hold the device parallel to the ground with the bottom edge closest to you and tilt the left edge of the
device toward the ground, the roll angle becomes positive. Tilting in the opposite directionmoving the right
edge of the device toward the ground causes the roll angle to become negative. The range of values is -90 degrees
to 90 degrees.
Computes the device's orientation based on the rotation matrix.
When it returns, the array values are as follows:
values[0]: Azimuth, angle of rotation about the -z axis. This value represents the angle between the device's y axis and the magnetic north pole. When facing north, this angle is 0, when facing south, this angle is π. Likewise, when facing east, this angle is π/2, and when facing west, this angle is -π/2. The range of values is -π to π.
values[1]: Pitch, angle of rotation about the x axis. This value represents the angle between a plane parallel to the device's screen and a plane parallel to the ground. Assuming that the bottom edge of the device faces the user and that the screen is face-up, tilting the top edge of the device toward the ground creates a positive pitch angle. The range of values is -π to π.
values[2]: Roll, angle of rotation about the y axis. This value represents the angle between a plane perpendicular to the device's screen and a plane perpendicular to the ground. Assuming that the bottom edge of the device faces the user and that the screen is face-up, tilting the left edge of the device toward the ground creates a positive roll angle. The range of values is -π/2 to π/2.
Applying these three rotations in the azimuth, pitch, roll order transforms an identity matrix to the rotation matrix passed into this method. Also, note that all three orientation angles are expressed in radians.
*/
}