Skip to content

Commit

Permalink
Monitor significant motion with BumpMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
rockgecko-development committed Dec 27, 2017
1 parent 17ebbbd commit 5192b52
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/org/havenapp/main/model/EventTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class EventTrigger extends SugarRecord {
* Power change detected message
*/
public static final int POWER = 4;
/**
* Significant motion detected message
*/
public static final int BUMP = 5;


public EventTrigger ()
Expand Down Expand Up @@ -105,6 +109,9 @@ public String getStringType (Context context)
case EventTrigger.POWER:
sType = context.getString(R.string.sensor_power);
break;
case EventTrigger.BUMP:
sType = context.getString(R.string.sensor_bump);
break;
default:
sType = context.getString(R.string.sensor_unknown);
}
Expand Down
120 changes: 120 additions & 0 deletions src/main/java/org/havenapp/main/sensors/BumpMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.havenapp.main.sensors;

import android.annotation.TargetApi;
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.SensorManager;
import android.hardware.TriggerEvent;
import android.hardware.TriggerEventListener;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import org.havenapp.main.model.EventTrigger;
import org.havenapp.main.service.MonitorService;

/**
* Use the Significant Motion trigger sensor on API 18+
*
* Created by rockgecko on 27/12/17.
*/
@TargetApi(18)
public class BumpMonitor {

// For shake motion detection.
private SensorManager sensorMgr;

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

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


private final static int CHECK_INTERVAL = 1000;

public BumpMonitor(Context context) {


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

sensorMgr = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
bumpSensor = sensorMgr.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);

if (bumpSensor == null) {
Log.i("BumpMonitor", "Warning: no significant motion sensor");
} else {
boolean registered = sensorMgr.requestTriggerSensor(sensorListener, bumpSensor);
Log.i("BumpMonitor", "Significant motion sensor registered: "+registered);
}

}


public void stop(Context context) {
sensorMgr.cancelTriggerSensor(sensorListener, bumpSensor);
context.unbindService(mConnection);
}
private TriggerEventListener sensorListener = new TriggerEventListener() {
@Override
public void onTrigger(TriggerEvent event) {
Log.i("BumpMonitor", "Sensor triggered");
//value[0] = 1.0 when the sensor triggers. 1.0 is the only allowed value.
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if (event.sensor.getType() == Sensor.TYPE_SIGNIFICANT_MOTION) {
if ((curTime - lastUpdate) > CHECK_INTERVAL) {
lastUpdate = curTime;

/*
* Send Alert
*/
Message message = new Message();
message.what = EventTrigger.BUMP;

try {
if (serviceMessenger != null) {
serviceMessenger.send(message);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//re-register the listener (it finishes after each event)
boolean registered = sensorMgr.requestTriggerSensor(sensorListener, bumpSensor);
Log.i("BumpMonitor", "Significant motion sensor re-registered: "+registered);

}
};

private Messenger serviceMessenger = null;

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
IBinder service) {
Log.i("BumpMonitor", "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("BumpMonitor", "SERVICE DISCONNECTED");
serviceMessenger = null;
}
};

}
13 changes: 12 additions & 1 deletion src/main/java/org/havenapp/main/service/MonitorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
Expand All @@ -38,6 +39,7 @@
import org.havenapp.main.sensors.AccelerometerMonitor;
import org.havenapp.main.sensors.AmbientLightMonitor;
import org.havenapp.main.sensors.BarometerMonitor;
import org.havenapp.main.sensors.BumpMonitor;
import org.havenapp.main.sensors.MicrophoneMonitor;

@SuppressLint("HandlerLeak")
Expand Down Expand Up @@ -73,6 +75,7 @@ public class MonitorService extends Service {
* Sensor Monitors
*/
AccelerometerMonitor mAccelManager = null;
BumpMonitor mBumpMonitor = null;
MicrophoneMonitor mMicMonitor = null;
BarometerMonitor mBaroMonitor = null;
AmbientLightMonitor mLightMonitor = null;
Expand Down Expand Up @@ -209,6 +212,9 @@ private void startSensors ()

if (mPrefs.getAccelerometerSensitivity() != PreferenceManager.OFF) {
mAccelManager = new AccelerometerMonitor(this);
if(Build.VERSION.SDK_INT>=18) {
mBumpMonitor = new BumpMonitor(this);
}
mBaroMonitor = new BarometerMonitor(this);
mLightMonitor = new AmbientLightMonitor(this);
}
Expand All @@ -222,9 +228,14 @@ private void startSensors ()
private void stopSensors ()
{
mIsRunning = false;

//this will never be false:
// -you can't use ==, != for string comparisons, use equals() instead
// -Value is never set to OFF in the first place
if (mPrefs.getAccelerometerSensitivity() != PreferenceManager.OFF) {
mAccelManager.stop(this);
if(Build.VERSION.SDK_INT>=18) {
mBumpMonitor.stop(this);
}
mBaroMonitor.stop(this);
mLightMonitor.stop(this);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,6 @@
<string name="sensor_camera">Motion (Camera)</string>
<string name="sensor_sound">Microphone</string>
<string name="sensor_power">USB Power</string>
<string name="sensor_bump">Bump (Accelerometer)</string>
<string name="sensor_unknown">Unknown</string>
</resources>

0 comments on commit 5192b52

Please sign in to comment.