From 81d6ab7b5fc50a3aaeafde26f1efdc8ecd1df4b3 Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 27 Nov 2021 20:22:13 +0100 Subject: [PATCH] Position trigger --- app/src/apkFlavor/AndroidManifest.xml | 1 + .../jens/automation2/ActivityManageRule.java | 12 + .../ActivityManageTriggerDevicePosition.java | 161 +++++++++++++ .../java/com/jens/automation2/Trigger.java | 4 +- .../receivers/DevicePositionListener.java | 90 ++++--- app/src/main/res/drawable-hdpi/smartphone.png | Bin 0 -> 1296 bytes ...ctivity_manage_trigger_device_position.xml | 221 ++++++++++++++++++ app/src/main/res/values/strings.xml | 8 + 8 files changed, 468 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/com/jens/automation2/ActivityManageTriggerDevicePosition.java create mode 100644 app/src/main/res/drawable-hdpi/smartphone.png create mode 100644 app/src/main/res/layout/activity_manage_trigger_device_position.xml diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index 73643316..728d9674 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -145,6 +145,7 @@ + = desiredX - desiredXTolerance || x <= desiredX + desiredXTolerance) + { + tvAppliesX.setText(getResources().getString(R.string.yes)); + tvAppliesX.setTextColor(Color.GREEN); + } + else + { + tvAppliesX.setText(getResources().getString(R.string.no)); + tvAppliesX.setTextColor(Color.RED); + } + + desiredY = Float.parseFloat(etDesiredPositionY.getText().toString()); + desiredYTolerance = Float.parseFloat(etDesiredPositionYTolerance.getText().toString()); + if(y >= desiredY - desiredYTolerance || y <= desiredY + desiredYTolerance) + { + tvAppliesY.setText(getResources().getString(R.string.yes)); + tvAppliesY.setTextColor(Color.GREEN); + } + else + { + tvAppliesY.setText(getResources().getString(R.string.no)); + tvAppliesY.setTextColor(Color.RED); + } + + desiredZ = Float.parseFloat(etDesiredPositionZ.getText().toString()); + desiredZTolerance = Float.parseFloat(etDesiredPositionZTolerance.getText().toString()); + if(z >= desiredZ - desiredZTolerance || z <= desiredZ + desiredZTolerance) + { + tvAppliesZ.setText(getResources().getString(R.string.yes)); + tvAppliesZ.setTextColor(Color.GREEN); + } + else + { + tvAppliesZ.setText(getResources().getString(R.string.no)); + tvAppliesZ.setTextColor(Color.RED); + } + } + } + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_manage_trigger_device_position); + + currentOrientationX = (TextView) findViewById(R.id.currentOrientationX); + currentOrientationY = (TextView) findViewById(R.id.currentOrientationY); + currentOrientationZ = (TextView) findViewById(R.id.currentOrientationZ); + tvAppliesX = (TextView) findViewById(R.id.tvAppliesX); + tvAppliesY = (TextView) findViewById(R.id.tvAppliesY); + tvAppliesZ = (TextView) findViewById(R.id.tvAppliesZ); + + 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); + + bApplyPositionValues.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + if(!StringUtils.isEmpty(currentOrientationX.getText())) + etDesiredPositionX.setText(currentOrientationX.getText()); + + if(!StringUtils.isEmpty(currentOrientationY.getText())) + etDesiredPositionY.setText(currentOrientationY.getText()); + + if(!StringUtils.isEmpty(currentOrientationZ.getText())) + etDesiredPositionZ.setText(currentOrientationZ.getText()); + } + }); + + bSavePositionValues.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + if(checkInputs()) + { + Toast.makeText(ActivityManageTriggerDevicePosition.this, getResources().getString(R.string.enterValidNumbersIntoAllFields), Toast.LENGTH_LONG).show(); + } + else + { + // Save + } + } + }); + } + + boolean checkInputs() + { + return( + !StringUtils.isEmpty(etDesiredPositionX.getText().toString()) && Miscellaneous.isNumeric(etDesiredPositionX.getText().toString()) + && + !StringUtils.isEmpty(etDesiredPositionXTolerance.getText().toString()) && Miscellaneous.isNumeric(etDesiredPositionXTolerance.getText().toString()) + && + !StringUtils.isEmpty(etDesiredPositionY.getText().toString()) && Miscellaneous.isNumeric(etDesiredPositionY.getText().toString()) + && + !StringUtils.isEmpty(etDesiredPositionYTolerance.getText().toString()) && Miscellaneous.isNumeric(etDesiredPositionYTolerance.getText().toString()) + && + !StringUtils.isEmpty(etDesiredPositionZ.getText().toString()) && Miscellaneous.isNumeric(etDesiredPositionZ.getText().toString()) + && + !StringUtils.isEmpty(etDesiredPositionZTolerance.getText().toString()) && Miscellaneous.isNumeric(etDesiredPositionZTolerance.getText().toString()) + ); + } + + @Override + protected void onResume() + { + super.onResume(); + DevicePositionListener.getInstance().startSensor(ActivityManageTriggerDevicePosition.this, this); + } + + @Override + protected void onPause() + { + super.onPause(); + DevicePositionListener.getInstance().stopSensor(); + } +} diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 3164a04b..ed0c49f4 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -323,7 +323,7 @@ public class Trigger */ public enum Trigger_Enum { - pointOfInterest, timeFrame, charging, batteryLevel, usb_host_connection, speed, noiseLevel, wifiConnection, process_started_stopped, airplaneMode, roaming, nfcTag, activityDetection, bluetoothConnection, headsetPlugged, notification, phoneCall; //phoneCall always needs to be at the very end because of Google's shitty so called privacy + pointOfInterest, timeFrame, charging, batteryLevel, usb_host_connection, speed, noiseLevel, wifiConnection, process_started_stopped, airplaneMode, roaming, nfcTag, activityDetection, bluetoothConnection, headsetPlugged, notification, devicePosition, phoneCall; //phoneCall always needs to be at the very end because of Google's shitty so called privacy public String getFullName(Context context) { @@ -363,6 +363,8 @@ public class Trigger return context.getResources().getString(R.string.triggerHeadsetPlugged); case notification: return context.getResources().getString(R.string.notification); + case devicePosition: + return context.getResources().getString(R.string.devicePosition); default: return "Unknown"; } diff --git a/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java b/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java index e2355b89..8c7a81e9 100644 --- a/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/DevicePositionListener.java @@ -1,57 +1,91 @@ package com.jens.automation2.receivers; +import static android.content.Context.SENSOR_SERVICE; + import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.widget.TextView; + +import com.jens.automation2.ActivityManageTriggerDevicePosition; +import com.jens.automation2.Miscellaneous; public class DevicePositionListener implements SensorEventListener { // https://developer.android.com/guide/topics/sensors/sensors_position#java - private SensorManager sensorManager; - private final float[] accelerometerReading = new float[3]; - private final float[] magnetometerReading = new float[3]; + ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance = null; - public DevicePositionListener(Context context) + //the Sensor Manager + private SensorManager sManager; + static DevicePositionListener instance = null; + + public static DevicePositionListener getInstance() { - sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); + if(instance == null) + instance = new DevicePositionListener(); + + return instance; } - /* - 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 ground—causes 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 direction—moving 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. - */ - void getDeviceOrientation() + public void startSensor(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_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST); + } + public void stopSensor() + { + //unregister the sensor listener + sManager.unregisterListener(this); + } - // Rotation matrix based on current readings from accelerometer and magnetometer. - final float[] rotationMatrix = new float[9]; - SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading); - - // Express the updated rotation matrix as three orientation angles. - final float[] orientationAngles = new float[3]; - SensorManager.getOrientation(rotationMatrix, orientationAngles); - + @Override + public void onAccuracyChanged(Sensor arg0, int arg1) + { + //Do nothing. } @Override public void onSensorChanged(SensorEvent event) { + //if sensor is unreliable, return void + if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) + { + return; + } + //else it will output the Roll, Pitch and Yawn values + activityManageTriggerDevicePositionInstance.updateFields(event.values[2], event.values[1], event.values[0]); + +// 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])); } - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) - { + /* + 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 ground—causes + 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 direction—moving 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. + */ } diff --git a/app/src/main/res/drawable-hdpi/smartphone.png b/app/src/main/res/drawable-hdpi/smartphone.png new file mode 100644 index 0000000000000000000000000000000000000000..c992725c2c2e7fac1e95be60cf72bafaa445ca42 GIT binary patch literal 1296 zcmeAS@N?(olHy`uVBq!ia0vp^Za}QT!3HFqFZ8SfQY`6?zK#qG8~eHcB(ehe3dtTp zz6=aiY77hwEes65fIKPgsd?{K8v^Bxg#W5tq`R%O8 zZkbex*3B6{{^b)+?9g{v5EXF0mG*6(9kv~BG&_j}v*R4N`Ud}SRUdj4lz*3VVTuJN`XJ{PgF zZhtw?zSjFue>N?;`|S6%U*!c_by>bUZA911T&l~qmG@smV)y(9!9SSn@)kZ?`QQ78 z<+Y>n8k&zS)E+N1-l>m|Jd5lx8wICgrI`x zv%?1$Y**y|y}$65nA$AOjsIUS4^5OfbMwQ4|E-)7Efq3+2TosbO`Mj<*>~vLi&>LS z2Opdj$zjL;TVzUV`0`7)3v^%TZCBs5?8&E)4QXOcubV2J93#sDXDolkJ zm!3ascG^Ua{mgxb>sE-RX->0#s1}i9)dO~_kr1=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