proximity trigger
This commit is contained in:
@@ -43,7 +43,7 @@ import java.util.List;
|
||||
|
||||
public class ActivityManageTriggerProximity extends Activity
|
||||
{
|
||||
TextView tvTriggerProximityHint;
|
||||
TextView tvTriggerProximityHint, tvProximityCurrentValue;
|
||||
Button bTriggerProximitySave;
|
||||
RangeSlider rsProximity;
|
||||
SeekBar sbProximityTest;
|
||||
@@ -68,6 +68,7 @@ public class ActivityManageTriggerProximity extends Activity
|
||||
rsProximity = (RangeSlider) findViewById(R.id.rsProximity);
|
||||
sbProximityTest = (SeekBar) findViewById(R.id.sbProximityTest);
|
||||
tvTriggerProximityHint = (TextView) findViewById(R.id.tvTriggerProximityHint);
|
||||
tvProximityCurrentValue = (TextView) findViewById(R.id.tvProximityCurrentValue);
|
||||
bTriggerProximitySave = (Button) findViewById(R.id.bTriggerProximitySave);
|
||||
|
||||
sbProximityTest.setEnabled(false);
|
||||
@@ -79,14 +80,20 @@ public class ActivityManageTriggerProximity extends Activity
|
||||
public void onClick(View v)
|
||||
{
|
||||
Intent response = new Intent();
|
||||
// response.putExtra("wifiState", rbTriggerWifiConnected.isChecked());
|
||||
// response.putExtra("wifiName", etTriggerWifiName.getText().toString());
|
||||
|
||||
response.putExtra(ActivityManageRule.intentNameTriggerParameter2, String.valueOf(rsProximity.getValues().get(0)) + Trigger.triggerParameter2Split + String.valueOf(rsProximity.getValues().get(1)));
|
||||
setResult(RESULT_OK, response);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
rsProximity.setValues(20f, 80f);
|
||||
if(getIntent().hasExtra(ActivityManageRule.intentNameTriggerParameter2))
|
||||
{
|
||||
String[] values = getIntent().getStringExtra(ActivityManageRule.intentNameTriggerParameter2).split(Trigger.triggerParameter2Split);
|
||||
rsProximity.setValues(Float.parseFloat(values[0]), Float.parseFloat(values[1]));
|
||||
}
|
||||
else
|
||||
rsProximity.setValues(0f, 10f);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -139,7 +146,8 @@ public class ActivityManageTriggerProximity extends Activity
|
||||
// check if the sensor type is proximity sensor.
|
||||
if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY)
|
||||
{
|
||||
sbProximityTest.setProgress((int)sensorEvent.values[0] * 100);
|
||||
sbProximityTest.setProgress((int)sensorEvent.values[0]);
|
||||
tvProximityCurrentValue.setText((String.valueOf(sensorEvent.values[0])));
|
||||
|
||||
if (sensorEvent.values[0] == 0)
|
||||
{
|
||||
@@ -160,5 +168,5 @@ public class ActivityManageTriggerProximity extends Activity
|
||||
{
|
||||
|
||||
}
|
||||
};;;
|
||||
};
|
||||
}
|
||||
@@ -66,6 +66,7 @@ public class ActivityPermissions extends Activity
|
||||
public final static String permissionNameWireguard = "com.wireguard.android.permission.CONTROL_TUNNELS";
|
||||
public final static String permissionNameGoogleActivityDetection = "com.google.android.gms.permission.ACTIVITY_RECOGNITION";
|
||||
public final static String permissionNameSuperuser = "android.permission.ACCESS_SUPERUSER";
|
||||
public final static String permissionNameProximity = "android.hardware.sensor.proximity";
|
||||
|
||||
public static ActivityPermissions getInstance()
|
||||
{
|
||||
@@ -603,6 +604,10 @@ public class ActivityPermissions extends Activity
|
||||
if(Build.VERSION.SDK_INT >= 31 && Miscellaneous.getTargetSDK(Miscellaneous.getAnyContext()) >= 31)
|
||||
addToArrayListUnique(Manifest.permission.SCHEDULE_EXACT_ALARM, requiredPermissions);
|
||||
break;
|
||||
// Proximity permission cannot be requested, but is always granted.
|
||||
case proximity:
|
||||
// addToArrayListUnique(ActivityPermissions.permissionNameProximity, requiredPermissions);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -1013,6 +1018,11 @@ public class ActivityPermissions extends Activity
|
||||
for(String ruleName : getRulesUsing(Trigger.Trigger_Enum.calendarEvent))
|
||||
usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
|
||||
break;
|
||||
// Proximity permission cannot be requested, but is always granted.
|
||||
// case ActivityPermissions.permissionNameProximity:
|
||||
// for(String ruleName : getRulesUsing(Trigger.Trigger_Enum.proximity))
|
||||
// usingElements.add(String.format(getResources().getString(R.string.ruleXrequiresThis), ruleName));
|
||||
// break;
|
||||
}
|
||||
|
||||
return usingElements;
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.jens.automation2.receivers.MediaPlayerListener;
|
||||
import com.jens.automation2.receivers.NoiseListener;
|
||||
import com.jens.automation2.receivers.PhoneStatusListener;
|
||||
import com.jens.automation2.receivers.ProcessListener;
|
||||
import com.jens.automation2.receivers.ProximityReceiver;
|
||||
import com.jens.automation2.receivers.ScreenStateReceiver;
|
||||
import com.jens.automation2.receivers.SubSystemStateReceiver;
|
||||
import com.jens.automation2.receivers.TetheringReceiver;
|
||||
@@ -215,6 +216,9 @@ public class ReceiverCoordinator
|
||||
|
||||
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.calendarEvent) && ActivityPermissions.isPermissionDeclaredInManifest(AutomationService.getInstance(), Manifest.permission.READ_CALENDAR) && ActivityPermissions.havePermission(Manifest.permission.READ_CALENDAR, AutomationService.getInstance()))
|
||||
CalendarReceiver.startCalendarReceiver(AutomationService.getInstance());
|
||||
|
||||
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.proximity))
|
||||
ProximityReceiver.startProximityReceiver(AutomationService.getInstance());
|
||||
}
|
||||
|
||||
public static void stopAllReceivers()
|
||||
@@ -234,6 +238,7 @@ public class ReceiverCoordinator
|
||||
DeviceOrientationListener.getInstance().stopListener(AutomationService.getInstance());
|
||||
TetheringReceiver.getInstance().stopListener(AutomationService.getInstance());
|
||||
SubSystemStateReceiver.getInstance().stopListener(AutomationService.getInstance());
|
||||
ProximityReceiver.stopProximityReceiver();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.jens.automation2.receivers.NoiseListener;
|
||||
import com.jens.automation2.receivers.NotificationListener;
|
||||
import com.jens.automation2.receivers.PhoneStatusListener;
|
||||
import com.jens.automation2.receivers.ProcessListener;
|
||||
import com.jens.automation2.receivers.ProximityReceiver;
|
||||
import com.jens.automation2.receivers.ScreenStateReceiver;
|
||||
import com.jens.automation2.receivers.TetheringReceiver;
|
||||
|
||||
@@ -294,6 +295,10 @@ public class Trigger
|
||||
if(!checkCalendarEvent(false))
|
||||
result = false;
|
||||
break;
|
||||
case proximity:
|
||||
if(!checkProximity())
|
||||
result = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -828,6 +833,27 @@ public class Trigger
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean checkProximity()
|
||||
{
|
||||
try
|
||||
{
|
||||
float currentLevel = ProximityReceiver.getProximityLevel();
|
||||
String[] values = this.getTriggerParameter2().split(Trigger.triggerParameter2Split);
|
||||
float part1 = Float.parseFloat(values[0]);
|
||||
float part2 = Float.parseFloat(values[1]);
|
||||
|
||||
// Are we within the defined scope?
|
||||
if(part1 <= currentLevel && currentLevel <= part2)
|
||||
return true;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("e", "checkProximity()", Log.getStackTraceString(e), 1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean checkBluetooth()
|
||||
{
|
||||
Miscellaneous.logEvent("i", Miscellaneous.getAnyContext().getResources().getString(R.string.ruleCheckOf), String.format("Checking for bluetooth...", this.getParentRule().getName()), 4);
|
||||
@@ -1965,6 +1991,10 @@ public class Trigger
|
||||
returnString.append(")");
|
||||
|
||||
break;
|
||||
case proximity:
|
||||
String[] parts = triggerParameter2.split(triggerParameter2Split);
|
||||
returnString.append(String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.proximityText), parts[0], parts[1]));
|
||||
break;
|
||||
default:
|
||||
returnString.append("error");
|
||||
break;
|
||||
|
||||
@@ -300,7 +300,7 @@ public class BatteryReceiver extends BroadcastReceiver implements AutomationList
|
||||
@Override
|
||||
public Trigger_Enum[] getMonitoredTrigger()
|
||||
{
|
||||
// actually monitores several
|
||||
// actually monitors several
|
||||
return new Trigger_Enum[] { Trigger_Enum.batteryLevel, Trigger_Enum.charging, Trigger_Enum.usb_host_connection };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package com.jens.automation2.receivers;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
import com.jens.automation2.ActivityPermissions;
|
||||
import com.jens.automation2.AutomationService;
|
||||
import com.jens.automation2.Miscellaneous;
|
||||
import com.jens.automation2.Rule;
|
||||
import com.jens.automation2.Trigger.Trigger_Enum;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ProximityReceiver implements AutomationListenerInterface
|
||||
{
|
||||
public static AutomationService automationServiceRef = null;
|
||||
static float proximityLevel = -1; // initialize with a better value than this
|
||||
static boolean proximityReceiverActive = false;
|
||||
|
||||
SensorManager sensorManager;
|
||||
Sensor proximitySensor;
|
||||
|
||||
static ProximityReceiver proximityReceiverInstance = null;
|
||||
|
||||
public static float getProximityLevel()
|
||||
{
|
||||
return proximityLevel;
|
||||
}
|
||||
|
||||
public static void startProximityReceiver(final AutomationService automationServiceRef)
|
||||
{
|
||||
if(!proximityReceiverActive)
|
||||
{
|
||||
ProximityReceiver.automationServiceRef = automationServiceRef;
|
||||
|
||||
if(proximityReceiverInstance == null)
|
||||
proximityReceiverInstance = new ProximityReceiver();
|
||||
|
||||
proximityReceiverInstance.startListener(automationServiceRef);
|
||||
|
||||
proximityReceiverActive = true;
|
||||
}
|
||||
}
|
||||
public static void stopProximityReceiver()
|
||||
{
|
||||
if(proximityReceiverActive)
|
||||
{
|
||||
if(proximityReceiverInstance != null)
|
||||
{
|
||||
proximityReceiverInstance = null;
|
||||
}
|
||||
|
||||
proximityReceiverActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startListener(AutomationService automationService)
|
||||
{
|
||||
if(!proximityReceiverActive)
|
||||
{
|
||||
ProximityReceiver.automationServiceRef = automationServiceRef;
|
||||
|
||||
sensorManager = (SensorManager) automationService.getSystemService(Context.SENSOR_SERVICE);
|
||||
|
||||
// from sensor service we are
|
||||
// calling proximity sensor
|
||||
proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
|
||||
|
||||
// handling the case if the proximity
|
||||
// sensor is not present in users device.
|
||||
if (proximitySensor == null)
|
||||
{
|
||||
Miscellaneous.logEvent("w", "No proximity sensor", "Device doesn\'t seem to have a proximity sensor.", 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
// registering our sensor with sensor manager.
|
||||
sensorManager.registerListener(proximitySensorEventListener, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
}
|
||||
|
||||
proximityReceiverActive = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopListener(AutomationService automationService)
|
||||
{
|
||||
if(proximityReceiverActive)
|
||||
{
|
||||
if(proximityReceiverInstance != null)
|
||||
{
|
||||
if(proximitySensor != null)
|
||||
sensorManager.unregisterListener(proximitySensorEventListener);
|
||||
|
||||
proximityReceiverInstance = null;
|
||||
}
|
||||
|
||||
proximityReceiverActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
SensorEventListener proximitySensorEventListener = new SensorEventListener()
|
||||
{
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent sensorEvent)
|
||||
{
|
||||
// check if the sensor type is proximity sensor.
|
||||
if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY)
|
||||
{
|
||||
proximityLevel = (int)sensorEvent.values[0];
|
||||
|
||||
Miscellaneous.logEvent("i", "Proximity", String.valueOf(proximityLevel), 5);
|
||||
|
||||
// if (sensorEvent.values[0] == 0)
|
||||
// {
|
||||
//// sensorStatusTV.setText("Near");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
//// sensorStatusTV.setText("Away");
|
||||
// }
|
||||
|
||||
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.proximity);
|
||||
for(Rule oneRule : ruleCandidates)
|
||||
{
|
||||
if(oneRule.getsGreenLight(automationServiceRef))
|
||||
oneRule.activate(automationServiceRef, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int i)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
public static boolean haveAllPermission()
|
||||
{
|
||||
return ActivityPermissions.havePermission(ActivityPermissions.permissionNameProximity, Miscellaneous.getAnyContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isListenerRunning()
|
||||
{
|
||||
return proximityReceiverActive;
|
||||
}
|
||||
@Override
|
||||
public Trigger_Enum[] getMonitoredTrigger()
|
||||
{
|
||||
// actually monitors several
|
||||
return new Trigger_Enum[] { Trigger_Enum.proximity };
|
||||
}
|
||||
}
|
||||
@@ -37,8 +37,8 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:valueFrom="0"
|
||||
android:valueTo="100"
|
||||
android:stepSize="1"
|
||||
android:valueTo="10"
|
||||
android:stepSize="0.1"
|
||||
android:theme="@style/AppThemeCompat" />
|
||||
|
||||
<RelativeLayout
|
||||
@@ -97,6 +97,11 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvProximityCurrentValue"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/bTriggerProximitySave"
|
||||
android:layout_marginTop="@dimen/default_margin"
|
||||
|
||||
@@ -966,7 +966,8 @@
|
||||
<string name="testArea">Test area</string>
|
||||
<string name="testAreaProximityComment">Approach your device to test how it reacts.</string>
|
||||
<string name="deviceDoesntHaveProximitySensor">Your device doesn\'t seem to have a proximity sensor.</string>
|
||||
<string name="proximitySensorHint">A proximity sensor works differently on every device model. On some it just reports close or far as binary values, on others it may report more granular values. You may use this tester to see how it behaves on your device.</string>
|
||||
<string name="proximitySensorHint">The proximity sensor works differently on every device model. On some it just reports close or far as binary values, on others it may report more granular values. You can use this tester to see how it behaves on your device.</string>
|
||||
<string name="close">close</string>
|
||||
<string name="far">far</string>
|
||||
<string name="proximityText">proximity is between \"%1$s\" and \"%2$s\"</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user