Skip to content

Commit

Permalink
add support for light sensor
Browse files Browse the repository at this point in the history
- can detect flash lights in the room!
  • Loading branch information
n8fr8 committed Aug 9, 2017
1 parent 3133763 commit 01ccae0
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public class EventTrigger extends SugarRecord {
*/
public static final int PRESSURE = 2;

/**
* Pressure change detected message
*/
public static final int LIGHT = 3;

public EventTrigger ()
{
Expand Down Expand Up @@ -78,6 +82,9 @@ public String getStringType ()
case EventTrigger.ACCELEROMETER:
sType = "ACCELEROMETER";
break;
case EventTrigger.LIGHT:
sType = "LIGHT";
break;
case EventTrigger.CAMERA:
sType = "CAMERA MOTION";
break;
Expand All @@ -97,9 +104,6 @@ public String getMimeType ()
String sType = "";

switch (getType()) {
case EventTrigger.ACCELEROMETER:
sType = null;
break;
case EventTrigger.CAMERA:
sType = "image/*";
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package info.guardianproject.phoneypot.sensors;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import info.guardianproject.phoneypot.PreferenceManager;
import info.guardianproject.phoneypot.model.EventTrigger;
import info.guardianproject.phoneypot.service.MonitorService;

/**
* Created by n8fr8 on 3/10/17.
*/
public class AmbientLightMonitor implements SensorEventListener {

// For shake motion detection.
private SensorManager sensorMgr;

/**
* Accelerometer sensor
*/
private Sensor sensor;

/**
* Last update of the accelerometer
*/
private long lastUpdate = -1;

/**
* Current accelerometer values
*/
private float current_values[];

/**
* Last accelerometer values
*/
private float last_values[];

/**
* Data field used to retrieve application prefences
*/
private PreferenceManager prefs;

private final static float LIGHT_CHANGE_THRESHOLD = 10f;

private int maxAlertPeriod = 30;
private int remainingAlertPeriod = 0;
private boolean alert = false;
private final static int CHECK_INTERVAL = 1000;

public AmbientLightMonitor(Context context) {
prefs = new PreferenceManager(context);

context.bindService(new Intent(context,
MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);

sensorMgr = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
sensor = (Sensor) sensorMgr.getDefaultSensor(Sensor.TYPE_LIGHT);

if (sensor == null) {
Log.i("AccelerometerFrament", "Warning: no accelerometer");
} else {
sensorMgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Safe not to implement

}

public void onSensorChanged(SensorEvent event) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
if ((curTime - lastUpdate) > CHECK_INTERVAL) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;

current_values = event.values.clone();

if (alert && remainingAlertPeriod > 0) {
remainingAlertPeriod = remainingAlertPeriod - 1;
} else {
alert = false;
}

if (last_values != null) {

boolean isChanged = false;

float lightChangedValue = Math.abs(last_values[0]-current_values[0]);

Log.d("LightSensor","Light changed: " + lightChangedValue);

//see if light value changed more than 10 values
isChanged = lightChangedValue >LIGHT_CHANGE_THRESHOLD;


if (isChanged) {
/*
* Send Alert
*/

alert = true;
remainingAlertPeriod = maxAlertPeriod;

Message message = new Message();
message.what = EventTrigger.LIGHT;
message.getData().putString("path",lightChangedValue+"");

try {
if (serviceMessenger != null) {
serviceMessenger.send(message);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
last_values = current_values.clone();
}
}
}

public void stop(Context context) {
sensorMgr.unregisterListener(this);
context.unbindService(mConnection);
}

private Messenger serviceMessenger = null;

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
IBinder service) {
Log.i("AccelerometerFragment", "SERVICE CONNECTED");
// We've bound to LocalService, cast the IBinder and get LocalService instance
serviceMessenger = new Messenger(service);
}

public void onServiceDisconnected(ComponentName arg0) {
Log.i("AccelerometerFragment", "SERVICE DISCONNECTED");
serviceMessenger = null;
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import info.guardianproject.phoneypot.model.Event;
import info.guardianproject.phoneypot.model.EventTrigger;
import info.guardianproject.phoneypot.sensors.AccelerometerMonitor;
import info.guardianproject.phoneypot.sensors.AmbientLightMonitor;
import info.guardianproject.phoneypot.sensors.BarometerMonitor;
import info.guardianproject.phoneypot.sensors.MicrophoneMonitor;

Expand Down Expand Up @@ -66,6 +67,7 @@ public class MonitorService extends Service {
AccelerometerMonitor mAccelManager = null;
MicrophoneMonitor mMicMonitor = null;
BarometerMonitor mBaroMonitor = null;
AmbientLightMonitor mLightMonitor = null;

/**
* Last Event instances
Expand Down Expand Up @@ -188,6 +190,7 @@ private void startSensors ()
if (prefs.getAccelerometerSensitivity() != PreferenceManager.OFF) {
mAccelManager = new AccelerometerMonitor(this);
mBaroMonitor = new BarometerMonitor(this);
mLightMonitor = new AmbientLightMonitor(this);
}

if (prefs.getMicrophoneSensitivity() != PreferenceManager.OFF)
Expand All @@ -201,6 +204,7 @@ private void stopSensors ()
if (prefs.getAccelerometerSensitivity() != PreferenceManager.OFF) {
mAccelManager.stop(this);
mBaroMonitor.stop(this);
mLightMonitor.stop(this);
}

if (prefs.getMicrophoneSensitivity() != PreferenceManager.OFF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ private String getMimeType (EventTrigger eventTrigger)
String sType = "";

switch (eventTrigger.getType()) {
case EventTrigger.ACCELEROMETER:
sType = null;
break;
case EventTrigger.CAMERA:
sType = "image/jpeg";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,7 @@ private String generateLog () {

mEventLog.append("Event Triggered @ " + eventTrigger.getTriggerTime().toLocaleString()).append("\n");

String sType = "";

switch (eventTrigger.getType()) {
case EventTrigger.ACCELEROMETER:
sType = "ACCELEROMETER";
break;
case EventTrigger.CAMERA:
sType = "CAMERA MOTION";
break;
case EventTrigger.MICROPHONE:
sType = "SOUND";
break;
default:
sType = "UNKNOWN";
}
String sType = eventTrigger.getStringType();

mEventLog.append("Event Type: " + sType);
mEventLog.append("\n==========================\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ else if (eventTrigger.getType() == EventTrigger.MICROPHONE)
audioWife.init(context, Uri.fromFile(new File(eventTrigger.getPath())))
.useDefaultUi(holder.extra, inflater);


}
else if (eventTrigger.getType() == EventTrigger.ACCELEROMETER)
{
desc += "\nSPEED: " + eventTrigger.getPath();

}
else if (eventTrigger.getType() == EventTrigger.LIGHT)
{
desc += "\nLIGHT: " + eventTrigger.getPath();

}
else if (eventTrigger.getType() == EventTrigger.PRESSURE)
{
Expand All @@ -127,6 +133,13 @@ else if (eventTrigger.getType() == EventTrigger.PRESSURE)

}

@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);

AudioWife.getInstance().release();
}

private void shareMedia (EventTrigger eventTrigger)
{

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/layout/activity_event.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_dialog_email" />
app:srcCompat="@android:drawable/ic_menu_share" />

</android.support.design.widget.CoordinatorLayout>

0 comments on commit 01ccae0

Please sign in to comment.