Haven is for people who need a way to protect their personal spaces and possessions without compromising their own privacy, through an Android app and on-device sensors
more work on #365 - simplify display of motion detection - we are creating too many in memory bitmaps just for fancy UI that isn't that usable - this now displays a simple "motion detected" string instead of the change detection overlay
n8fr8e8f37748ddaa
diff --git a/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java b/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.javaindex 04be1a3..8c988d1 100644--- a/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java+++ b/src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java@@ -36,7 +36,6 @@ public class MotionDetector {// Input dataprivate List<MotionListener> listeners = new ArrayList<>();- private Handler handler;private int motionSensitivity;// Output data@@ -58,14 +57,13 @@ public class MotionDetector {}public MotionDetector(-- Handler updateHandler,int motionSensitivity) {// this.renderScript = renderScript;- this.handler = updateHandler;this.motionSensitivity = motionSensitivity;detector = new LuminanceMotionDetector();++}public void setDetectColor (int detectColor)@@ -87,8 +85,8 @@ public class MotionDetector {boolean facingFront) {int[] newPicLuma = ImageCodec.N21toLuma(rawNewPic, width, height);- if (rawOldPic != null) {+ if (rawOldPic != null) {detector.setThreshold(motionSensitivity);List<Integer> changedPixels =@@ -96,10 +94,10 @@ public class MotionDetector {if (changedPixels != null) {+ /*int[] newPic = ImageCodec.lumaToGreyscale(newPicLuma, width, height);newPicLuma = null;-System.gc();for (int i = 0; i < newPic.length; i++)@@ -109,8 +107,6 @@ public class MotionDetector {newPic[changedPixel] = detectColor;}--Matrix mtx = new Matrix();if (facingFront) {@@ -124,31 +120,26 @@ public class MotionDetector {= Bitmap.createBitmap(Bitmap.createBitmap(newPic, width, height, Bitmap.Config.ARGB_4444), 0, 0, width, height, mtx, true);newPic = null;+ */Bitmap rawBitmap = convertImage(rawNewPic,width,height);- handler.post(() -> {- for (MotionListener listener : listeners) {- listener.onProcess(- newBitmap,- rawBitmap,- true);- }-- });+ for (MotionListener listener : listeners) {+ listener.onProcess(+ null,+ rawBitmap,+ true);+ }}else{- //nothing changed- handler.post(() -> {- for (MotionListener listener : listeners) {- listener.onProcess(- null,- null,- false);- }-- });+ for (MotionListener listener : listeners) {+ listener.onProcess(+ null,+ null,+ false);+ }+}}diff --git a/src/main/java/org/havenapp/main/ui/CameraFragment.java b/src/main/java/org/havenapp/main/ui/CameraFragment.javaindex cbfa444..b2e94e3 100644--- a/src/main/java/org/havenapp/main/ui/CameraFragment.java+++ b/src/main/java/org/havenapp/main/ui/CameraFragment.java@@ -8,8 +8,11 @@*/package org.havenapp.main.ui;+import android.graphics.Bitmap;import android.hardware.SensorEvent;import android.os.Bundle;+import android.os.Handler;+import android.os.Message;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@@ -30,6 +33,41 @@ public final class CameraFragment extends Fragment {private ImageView newImage;private PreferenceManager prefs;private TextView txtCameraStatus;+ private Bitmap lastBitmap;++ /**+ * Handler used to update back the UI after motion detection+ */+ private final Handler handler = new Handler()+ {+ @Override+ public void handleMessage(Message msg) {+ super.handleMessage(msg);++ if (!isDetached()) {+ if (txtCameraStatus != null) {++ if (msg.what == 0) {+ // newImage.setImageResource(R.drawable.blankimage);+ txtCameraStatus.setText("");++ } else if (msg.what == 1) {+ // newImage.setImageBitmap(lastBitmap);+ txtCameraStatus.setText(getString(R.string.motion_detected));++ }+++ /**+ if (cameraViewHolder.doingVideoProcessing()) {+ txtCameraStatus.setText("Recording...");+ } else {+ txtCameraStatus.setText("");+ }**/+ }+ }+ }+ };@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,@@ -98,18 +136,11 @@ public final class CameraFragment extends Fragment {cameraViewHolder = new CameraViewHolder(getActivity(), cameraView);cameraViewHolder.addListener((newBitmap, rawBitmap, motionDetected) -> {- if (motionDetected)- newImage.setImageBitmap(newBitmap);- else- newImage.setImageResource(R.drawable.blankimage);-- if (txtCameraStatus != null) {- if (cameraViewHolder.doingVideoProcessing()) {- txtCameraStatus.setText("Recording...");- } else {- txtCameraStatus.setText("");- }- }++ lastBitmap = rawBitmap;++ handler.sendEmptyMessage(motionDetected?1:0);+});}diff --git a/src/main/java/org/havenapp/main/ui/CameraViewHolder.java b/src/main/java/org/havenapp/main/ui/CameraViewHolder.javaindex eb7ac3e..ee27793 100644--- a/src/main/java/org/havenapp/main/ui/CameraViewHolder.java+++ b/src/main/java/org/havenapp/main/ui/CameraViewHolder.java@@ -134,7 +134,6 @@ public class CameraViewHolder {prefs = new PreferenceManager(context);task = new MotionDetector(- updateHandler,motionSensitivity);task.addListener((detectedImage, rawBitmap, motionDetected) -> {@@ -142,51 +141,53 @@ public class CameraViewHolder {for (MotionDetector.MotionListener listener : listeners)listener.onProcess(detectedImage,rawBitmap,motionDetected);- if (motionDetected) {+ if (motionDetected)+ mEncodeVideoThreadPool.execute(() -> saveDetectedImage(rawBitmap));- if (serviceMessenger != null) {- Message message = new Message();- message.what = EventTrigger.CAMERA;+ });+ /*+ * We bind to the alert service+ */+ this.context.bindService(new Intent(context,+ MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);+ }- try {+ private void saveDetectedImage (Bitmap rawBitmap)+ {+ if (serviceMessenger != null) {+ Message message = new Message();+ message.what = EventTrigger.CAMERA;- File fileImageDir = new File(Environment.getExternalStorageDirectory(), prefs.getDefaultMediaStoragePath());- fileImageDir.mkdirs();+ try {- String ts = new SimpleDateFormat(Utils.DATE_TIME_PATTERN,- Locale.getDefault()).format(new Date());+ File fileImageDir = new File(Environment.getExternalStorageDirectory(), prefs.getDefaultMediaStoragePath());+ fileImageDir.mkdirs();- File fileImage = new File(fileImageDir, ts.concat(".detected.original.jpg"));- FileOutputStream stream = new FileOutputStream(fileImage);- rawBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);+ String ts = new SimpleDateFormat(Utils.DATE_TIME_PATTERN,+ Locale.getDefault()).format(new Date());- stream.flush();- stream.close();- message.getData().putString("path", fileImage.getAbsolutePath());+ File fileImage = new File(fileImageDir, ts.concat(".detected.original.jpg"));+ FileOutputStream stream = new FileOutputStream(fileImage);+ rawBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);- //store the still match frame, even if doing video- serviceMessenger.send(message);+ stream.flush();+ stream.close();+ message.getData().putString("path", fileImage.getAbsolutePath());- if (prefs.getVideoMonitoringActive() && (!doingVideoProcessing)) {- recordVideo();+ //store the still match frame, even if doing video+ serviceMessenger.send(message);- }+ if (prefs.getVideoMonitoringActive() && (!doingVideoProcessing)) {+ recordVideo();- } catch (Exception e) {- // Cannot happen- Log.e("CameraViewHolder", "error creating image", e);- }}- }-- });- /*- * We bind to the alert service- */- this.context.bindService(new Intent(context,- MonitorService.class), mConnection, Context.BIND_ABOVE_CLIENT);- }+ } catch (Exception e) {+ // Cannot happen+ Log.e("CameraViewHolder", "error creating image", e);+ }+ }+ }public void setMotionSensitivity (intmotionSensitivity )@@ -242,16 +243,10 @@ public class CameraViewHolder {if (frame.getData() != null && frame.getSize() != null) {- byte[] data = frame.getData();- Size size = frame.getSize();- int width = size.getWidth();- int height = size.getHeight();- int rot = getCorrectCameraOrientation(cameraView.getFacing(),frame.getRotation());-if (!doingVideoProcessing) {- mDecodeThreadPool.execute(() -> processNewFrame(data, width, height, rot));+ mDecodeThreadPool.execute(() -> processNewFrame(frame));} else {- mEncodeVideoThreadPool.execute(() -> recordNewFrame(data, width, height, rot));+ mEncodeVideoThreadPool.execute(() -> recordNewFrame(frame));}}}@@ -300,25 +295,32 @@ public class CameraViewHolder {private Matrix mtxVideoRotate;- private void recordNewFrame (byte[] data, int width, int height, int rotationDegrees)+ private void recordNewFrame (Frame frame){- Bitmap bitmap = MotionDetector.convertImage(data, width, height);- //Nv21Image.nv21ToBitmap(renderScript, data, width, height);+ byte[] data = frame.getData();+ Size size = frame.getSize();- bitmap = Bitmap.createBitmap(bitmap,0,0,width,height,mtxVideoRotate,true);+ if (data != null && size != null) {+ int width = size.getWidth();+ int height = size.getHeight();+ int rotationDegrees = getCorrectCameraOrientation(cameraView.getFacing(), frame.getRotation());- try {- if (encoder != null)- encoder.encodeImage(bitmap);+ Bitmap bitmap = MotionDetector.convertImage(data, width, height);- bitmap.recycle();+ bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, mtxVideoRotate, true);- } catch (Exception e) {- e.printStackTrace();- }+ try {+ if (encoder != null)+ encoder.encodeImage(bitmap);++ bitmap.recycle();+ } catch (Exception e) {+ e.printStackTrace();+ }+ }}@@ -343,18 +345,27 @@ public class CameraViewHolder {}- private synchronized void processNewFrame (byte[] data, int width, int height, int rotationDegrees)+ private void processNewFrame (Frame frame){- task.detect(- lastPic,- data,- width,- height,- rotationDegrees,- cameraView.getFacing()==Facing.FRONT);- lastPic = data;+ byte[] data = frame.getData();+ Size size = frame.getSize();+ if (data != null && size != null) {+ int width = size.getWidth();+ int height = size.getHeight();+ int rotationDegrees = getCorrectCameraOrientation(cameraView.getFacing(), frame.getRotation());++ task.detect(+ lastPic,+ data,+ width,+ height,+ rotationDegrees,+ cameraView.getFacing() == Facing.FRONT);++ lastPic = data;+ }}diff --git a/src/main/res/layout/camera_fragment.xml b/src/main/res/layout/camera_fragment.xmlindex 12a6711..5286218 100644--- a/src/main/res/layout/camera_fragment.xml+++ b/src/main/res/layout/camera_fragment.xml@@ -56,7 +56,7 @@android:layout_height="wrap_content"android:gravity="center"android:text=""- android:textColor="@color/DarkRed"+ android:textColor="@color/Yellow"android:textSize="28sp"android:textStyle="bold"android:layout_gravity="center_horizontal|center_vertical"diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xmlindex c66f1ba..1f3a3ef 100644--- a/src/main/res/values/strings.xml+++ b/src/main/res/values/strings.xml@@ -165,5 +165,6 @@<!-- accepted digits for directory name - may vary for locale/platform/etc.Skipping periods, spaces, and other special characters to keep it simple --><string name="path_chars">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_/</string>+ <string name="motion_detected">MOTION DETECTED</string></resources>