Skip to content

Commit

Permalink
improvements to visual feedback of sensor event
Browse files Browse the repository at this point in the history
- this is a replacement for the removal of the bitmap overall, which was causing OOMs and ANRs
- this is much more functional, useful, though not quite as sexy :(
  • Loading branch information
n8fr8 committed Feb 8, 2019
1 parent e8f3774 commit 6fb03a4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/havenapp/main/sensors/BumpMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void onTrigger(TriggerEvent event) {
*/
Message message = new Message();
message.what = EventTrigger.BUMP;
message.getData().putString("path","BUMPED!");

try {
if (serviceMessenger != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ public void onSignalReceived(short[] signal) {
if (averageDB > mNoiseThreshold) {

if (!MicrophoneTaskFactory.isRecording()) {
Message message = new Message();
message.what = EventTrigger.MICROPHONE;
try {
if (serviceMessenger != null)
serviceMessenger.send(message);
} catch (RemoteException e) {
// Cannot happen
}

try {
AudioRecorderTask audioRecorderTask = MicrophoneTaskFactory.makeRecorder(context);
audioRecorderTask.setAudioRecorderListener(new AudioRecorderTask.AudioRecorderListener() {
Expand Down
14 changes: 12 additions & 2 deletions 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.Service;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
Expand All @@ -26,6 +27,7 @@

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.havenapp.main.HavenApp;
import org.havenapp.main.MonitorActivity;
Expand Down Expand Up @@ -292,11 +294,19 @@ private void stopSensors ()
/**
* Sends an alert according to type of connectivity
*/
public synchronized void alert(int alertType, String path) {
public void alert(int alertType, String value) {

Date now = new Date();
boolean doNotification = false;

//for the UI visual
Intent iEvent = new Intent("event");
iEvent.putExtra("type",alertType);
LocalBroadcastManager.getInstance(this).sendBroadcast(iEvent);

if (TextUtils.isEmpty(value))
return;

if (mLastEvent == null) {
mLastEvent = new Event();
long eventId = HavenEventDB.getDatabase(getApplicationContext())
Expand All @@ -323,7 +333,7 @@ else if (mPrefs.getNotificationTimeMs() > 0 && mLastNotification != null)

EventTrigger eventTrigger = new EventTrigger();
eventTrigger.setType(alertType);
eventTrigger.setPath(path);
eventTrigger.setPath(value);

mLastEvent.addEventTrigger(eventTrigger);

Expand Down
61 changes: 43 additions & 18 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
*/
package org.havenapp.main.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.hardware.SensorEvent;
import android.os.Bundle;
Expand All @@ -24,16 +28,17 @@

import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;
import org.havenapp.main.model.EventTrigger;

import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public final class CameraFragment extends Fragment {

private CameraViewHolder cameraViewHolder;
private ImageView newImage;
private PreferenceManager prefs;
private TextView txtCameraStatus;
private Bitmap lastBitmap;

/**
* Handler used to update back the UI after motion detection
Expand All @@ -47,28 +52,45 @@ public void handleMessage(Message 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 (msg.what == EventTrigger.CAMERA) {
if (cameraViewHolder.doingVideoProcessing()) {
txtCameraStatus.setText(getString(R.string.motion_detected)
+ "\n" + getString(R.string.status_recording_video));
} else {
txtCameraStatus.setText(getString(R.string.motion_detected));
}
}
else if (msg.what == EventTrigger.POWER) {
txtCameraStatus.setText(getString(R.string.power_detected));
}
else if (msg.what == EventTrigger.MICROPHONE) {
txtCameraStatus.setText(getString(R.string.sound_detected));
}
else if (msg.what == EventTrigger.ACCELEROMETER || msg.what == EventTrigger.BUMP) {
txtCameraStatus.setText(getString(R.string.device_move_detected));
}
else if (msg.what == EventTrigger.LIGHT) {
txtCameraStatus.setText(getString(R.string.status_light));
}


/**
if (cameraViewHolder.doingVideoProcessing()) {
txtCameraStatus.setText("Recording...");
} else {
txtCameraStatus.setText("");
}**/
}
}
}
};

BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

int eventType = intent.getIntExtra("type",-1);

//String path = intent.getData().getPath();

handler.sendEmptyMessage(eventType);
}
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down Expand Up @@ -96,6 +118,7 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(receiver);
}

@Override
Expand All @@ -104,6 +127,10 @@ public void onResume() {
initCamera();

cameraViewHolder.setMotionSensitivity(prefs.getCameraSensitivity());

IntentFilter filter = new IntentFilter();
filter.addAction("event");
LocalBroadcastManager.getInstance(getContext()).registerReceiver(receiver,filter );
}

public void updateCamera ()
Expand Down Expand Up @@ -137,9 +164,7 @@ public void initCamera ()

cameraViewHolder.addListener((newBitmap, rawBitmap, motionDetected) -> {

lastBitmap = rawBitmap;

handler.sendEmptyMessage(motionDetected?1:0);
handler.sendEmptyMessage(motionDetected?EventTrigger.CAMERA:-1);


});
Expand Down
22 changes: 3 additions & 19 deletions src/main/res/layout/camera_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:foreground="@color/translucentOverlay25">

<!--
<com.google.android.cameraview.CameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:keepScreenOn="true"
android:adjustViewBounds="true"
app:autoFocus="true"
app:cameraAspectRatio="4:3"
app:facing="front"
app:flash="off"
app:maximumWidth="1080"
app:maximumPreviewWidth="1080"
app:useHighResPicture="false"/>-->

<!-- Camera -->
<com.otaliastudios.cameraview.CameraView
xmlns:app="http://schemas.android.com/apk/res-auto"
Expand Down Expand Up @@ -56,10 +40,10 @@
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:textColor="@color/Yellow"
android:textColor="?colorPrimary"
android:textSize="28sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal|center_vertical"

android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="80dp"
/>
</FrameLayout>
7 changes: 7 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@
<!-- 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>
<string name="sound_detected">SOUND DETECTED</string>
<string name="device_move_detected">DEVICE MOVEMENT DETECTED</string>
<string name="power_detected">POWER CHANGE DETECTED</string>

<string name="status_recording_video">Recording...</string>
<string name="status_light">LIGHT DETECTED</string>

</resources>

0 comments on commit 6fb03a4

Please sign in to comment.