From 5192b52b6af22b987e1c40810b83d39e06532fd0 Mon Sep 17 00:00:00 2001 From: Stokesbook Date: Wed, 27 Dec 2017 17:45:13 +1100 Subject: [PATCH] Monitor significant motion with BumpMonitor --- .../org/havenapp/main/model/EventTrigger.java | 7 + .../havenapp/main/sensors/BumpMonitor.java | 120 ++++++++++++++++++ .../havenapp/main/service/MonitorService.java | 13 +- src/main/res/values/strings.xml | 1 + 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/havenapp/main/sensors/BumpMonitor.java diff --git a/src/main/java/org/havenapp/main/model/EventTrigger.java b/src/main/java/org/havenapp/main/model/EventTrigger.java index 1b77dba3..ea1654c5 100644 --- a/src/main/java/org/havenapp/main/model/EventTrigger.java +++ b/src/main/java/org/havenapp/main/model/EventTrigger.java @@ -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 () @@ -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); } diff --git a/src/main/java/org/havenapp/main/sensors/BumpMonitor.java b/src/main/java/org/havenapp/main/sensors/BumpMonitor.java new file mode 100644 index 00000000..8938eb8e --- /dev/null +++ b/src/main/java/org/havenapp/main/sensors/BumpMonitor.java @@ -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; + } + }; + +} diff --git a/src/main/java/org/havenapp/main/service/MonitorService.java b/src/main/java/org/havenapp/main/service/MonitorService.java index 7b8ace57..438b71b5 100644 --- a/src/main/java/org/havenapp/main/service/MonitorService.java +++ b/src/main/java/org/havenapp/main/service/MonitorService.java @@ -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; @@ -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") @@ -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; @@ -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); } @@ -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); } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index a71dbcf8..d2e6db2d 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -150,5 +150,6 @@ Motion (Camera) Microphone USB Power + Bump (Accelerometer) Unknown