Position trigger

This commit is contained in:
jens 2021-11-27 20:22:13 +01:00
parent 034c76fe30
commit 81d6ab7b5f
8 changed files with 468 additions and 29 deletions

View File

@ -145,6 +145,7 @@
<activity android:name=".ActivityMaintenance" /> <activity android:name=".ActivityMaintenance" />
<activity android:name=".ActivityManageTriggerPhoneCall" /> <activity android:name=".ActivityManageTriggerPhoneCall" />
<activity android:name=".ActivityManageActionBrightnessSetting" /> <activity android:name=".ActivityManageActionBrightnessSetting" />
<activity android:name=".ActivityManageTriggerDevicePosition" />
<activity android:name=".ActivityHelp" /> <activity android:name=".ActivityHelp" />
<activity android:name=".ActivityManageActionVibrate" /> <activity android:name=".ActivityManageActionVibrate" />
<activity <activity

View File

@ -95,6 +95,8 @@ public class ActivityManageRule extends Activity
final static int requestCodeTriggerBluetoothEdit = 6001; final static int requestCodeTriggerBluetoothEdit = 6001;
final static int requestCodeActionScreenBrightnessAdd = 401; final static int requestCodeActionScreenBrightnessAdd = 401;
final static int requestCodeActionScreenBrightnessEdit = 402; final static int requestCodeActionScreenBrightnessEdit = 402;
final static int requestCodeTriggerDevicePositionAdd = 301;
final static int requestCodeTriggerDevicePositionEdit = 302;
final static int requestCodeTriggerNotificationAdd = 8000; final static int requestCodeTriggerNotificationAdd = 8000;
final static int requestCodeTriggerNfcNotificationEdit = 8001; final static int requestCodeTriggerNfcNotificationEdit = 8001;
final static int requestCodeActionPlaySoundAdd = 501; final static int requestCodeActionPlaySoundAdd = 501;
@ -470,6 +472,8 @@ public class ActivityManageRule extends Activity
items.add(new Item(typesLong[i].toString(), R.drawable.headphone)); items.add(new Item(typesLong[i].toString(), R.drawable.headphone));
else if(types[i].toString().equals(Trigger_Enum.notification.toString())) else if(types[i].toString().equals(Trigger_Enum.notification.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.notification)); items.add(new Item(typesLong[i].toString(), R.drawable.notification));
else if(types[i].toString().equals(Trigger_Enum.devicePosition.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.smartphone));
else else
items.add(new Item(typesLong[i].toString(), R.drawable.placeholder)); items.add(new Item(typesLong[i].toString(), R.drawable.placeholder));
} }
@ -542,6 +546,14 @@ public class ActivityManageRule extends Activity
Intent wifiTriggerEditor = new Intent(myContext, ActivityManageTriggerWifi.class); Intent wifiTriggerEditor = new Intent(myContext, ActivityManageTriggerWifi.class);
startActivityForResult(wifiTriggerEditor, requestCodeTriggerWifiAdd); startActivityForResult(wifiTriggerEditor, requestCodeTriggerWifiAdd);
return; return;
// booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)};
}
else if(triggerType == Trigger_Enum.devicePosition)
{
newTrigger.setTriggerType(Trigger_Enum.devicePosition);
Intent devicePositionTriggerEditor = new Intent(myContext, ActivityManageTriggerDevicePosition.class);
startActivityForResult(devicePositionTriggerEditor, requestCodeTriggerDevicePositionAdd);
return;
// booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)}; // booleanChoices = new String[]{getResources().getString(R.string.started), getResources().getString(R.string.stopped)};
} }
// else if(triggerType == Trigger_Enum.wifiConnection) // else if(triggerType == Trigger_Enum.wifiConnection)

View File

@ -0,0 +1,161 @@
package com.jens.automation2;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
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
{
TextView currentOrientationX, currentOrientationY, currentOrientationZ, tvAppliesX, tvAppliesY, tvAppliesZ;
Button bApplyPositionValues, bSavePositionValues;
EditText etDesiredPositionX, etDesiredPositionXTolerance, etDesiredPositionY, etDesiredPositionYTolerance, etDesiredPositionZ, etDesiredPositionZTolerance;
float desiredX, desiredY, desiredZ, desiredXTolerance, desiredYTolerance, desiredZTolerance;
public void updateFields(float x, float y, float z)
{
currentOrientationX.setText(Float.toString(x));
currentOrientationY.setText(Float.toString(y));
currentOrientationZ.setText(Float.toString(z));
if(checkInputs())
{
desiredX = Float.parseFloat(etDesiredPositionX.getText().toString());
desiredXTolerance = Float.parseFloat(etDesiredPositionXTolerance.getText().toString());
if(x >= 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();
}
}

View File

@ -323,7 +323,7 @@ public class Trigger
*/ */
public enum Trigger_Enum { 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) public String getFullName(Context context)
{ {
@ -363,6 +363,8 @@ public class Trigger
return context.getResources().getString(R.string.triggerHeadsetPlugged); return context.getResources().getString(R.string.triggerHeadsetPlugged);
case notification: case notification:
return context.getResources().getString(R.string.notification); return context.getResources().getString(R.string.notification);
case devicePosition:
return context.getResources().getString(R.string.devicePosition);
default: default:
return "Unknown"; return "Unknown";
} }

View File

@ -1,57 +1,91 @@
package com.jens.automation2.receivers; package com.jens.automation2.receivers;
import static android.content.Context.SENSOR_SERVICE;
import android.content.Context; import android.content.Context;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
import android.hardware.SensorEventListener; import android.hardware.SensorEventListener;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.widget.TextView;
import com.jens.automation2.ActivityManageTriggerDevicePosition;
import com.jens.automation2.Miscellaneous;
public class DevicePositionListener implements SensorEventListener public class DevicePositionListener implements SensorEventListener
{ {
// https://developer.android.com/guide/topics/sensors/sensors_position#java // https://developer.android.com/guide/topics/sensors/sensors_position#java
private SensorManager sensorManager; ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance = null;
private final float[] accelerometerReading = new float[3];
private final float[] magnetometerReading = new float[3];
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;
} }
/* public void startSensor(Context context, ActivityManageTriggerDevicePosition activityManageTriggerDevicePositionInstance)
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.
*/
void getDeviceOrientation()
{ {
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. @Override
final float[] rotationMatrix = new float[9]; public void onAccuracyChanged(Sensor arg0, int arg1)
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading); {
//Do nothing.
// Express the updated rotation matrix as three orientation angles.
final float[] orientationAngles = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationAngles);
} }
@Override @Override
public void onSensorChanged(SensorEvent event) 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 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.
*/
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,221 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="@dimen/default_margin" >
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/devicePositionExplanation"
android:layout_marginBottom="@dimen/default_margin" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="@string/orientationX"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/currentOrientationX"
android:layout_marginLeft="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="@string/orientationY"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/currentOrientationY"
android:layout_marginLeft="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="@string/orientationZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/currentOrientationZ"
android:layout_marginLeft="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/bApplyPositionValues"
android:text="@string/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/default_margin" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text=""
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Position"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/tolerance"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/wouldCurrentlyApply"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/default_margin" />
<EditText
android:id="@+id/etDesiredPositionX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:minWidth="100dp"/>
<EditText
android:id="@+id/etDesiredPositionXTolerance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:minWidth="100dp"/>
<TextView
android:id="@+id/tvAppliesX"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:text="y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/default_margin" />
<EditText
android:id="@+id/etDesiredPositionY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:minWidth="100dp"/>
<EditText
android:id="@+id/etDesiredPositionYTolerance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:minWidth="100dp"/>
<TextView
android:id="@+id/tvAppliesY"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:text="z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/default_margin" />
<EditText
android:id="@+id/etDesiredPositionZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:minWidth="100dp"/>
<EditText
android:id="@+id/etDesiredPositionZTolerance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:minWidth="100dp"/>
<TextView
android:id="@+id/tvAppliesZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/bSavePositionValues"
android:text="@string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin" />
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>

View File

@ -713,4 +713,12 @@
<string name="donate">Donate</string> <string name="donate">Donate</string>
<string name="btTetheringNotice">This feature is confirmed to work up until Android 8.0. From some higher version upwards it ceases to work, but due to a lack of physical devices I cannot tell which one that is. On Android 11 it definitely ain\'t working anymore. If you have a version in between please let me know if it\'s working or not.</string> <string name="btTetheringNotice">This feature is confirmed to work up until Android 8.0. From some higher version upwards it ceases to work, but due to a lack of physical devices I cannot tell which one that is. On Android 11 it definitely ain\'t working anymore. If you have a version in between please let me know if it\'s working or not.</string>
<string name="notice">Notice</string> <string name="notice">Notice</string>
<string name="devicePosition">Device position (Gyroscope)</string>
<string name="tolerance">Tolerance</string>
<string name="orientationX">Orientation X (Roll):</string>
<string name="orientationY">Orientation Y (Pitch):</string>
<string name="orientationZ">Orientation Z (Yaw):</string>
<string name="enterValidNumbersIntoAllFields">Enter valid numbers in all fields.</string>
<string name="devicePositionExplanation">When you move your device the below numbers should update. What you can see there, is the current \"position\" of your device. If it is in the desired position, click the apply button to copy the current values to the desired fields.\nBecause you will probably not be able to reach this exact position ever again, enter a tolerance. The is amount to which the position can deviate in one direction or the other.</string>
<string name="wouldCurrentlyApply">Would currently apply?</string>
</resources> </resources>