Browse Source

added AlarmRecording

Benutzeroberflaeche
Christian Tinz 1 year ago
parent
commit
f56973547b
19 changed files with 702 additions and 119 deletions
  1. 6
    2
      app/src/main/AndroidManifest.xml
  2. 12
    3
      app/src/main/java/com/example/greenwatch/AccelerometerActivity.java
  3. 8
    3
      app/src/main/java/com/example/greenwatch/AudiodetectionActivity.java
  4. 8
    2
      app/src/main/java/com/example/greenwatch/AudiodetectionAndAccelerometerActivity.java
  5. 12
    3
      app/src/main/java/com/example/greenwatch/MainActivity.java
  6. 6
    2
      app/src/main/java/com/example/greenwatch/VideodetectionActivity.java
  7. 8
    1
      app/src/main/java/com/example/greenwatch/VideodetectionAndAccelerometerActivity.java
  8. 256
    0
      app/src/main/java/com/example/greenwatch/alarmrecorder/AlarmRecorder.java
  9. 46
    0
      app/src/main/java/com/example/greenwatch/alarmrecorder/runnables/AudioRecorder.java
  10. 54
    0
      app/src/main/java/com/example/greenwatch/alarmrecorder/runnables/VideoRecorder.java
  11. 10
    3
      app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java
  12. 17
    5
      app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java
  13. 30
    1
      app/src/main/java/com/example/greenwatch/viewmodels/AccelerometerViewModel.java
  14. 57
    29
      app/src/main/java/com/example/greenwatch/viewmodels/AudiodetectionAndAccelerometerViewModel.java
  15. 42
    10
      app/src/main/java/com/example/greenwatch/viewmodels/MainActivityViewModel.java
  16. 46
    21
      app/src/main/java/com/example/greenwatch/viewmodels/MicrofonViewModel.java
  17. 54
    32
      app/src/main/java/com/example/greenwatch/viewmodels/VideodetectionAndAccelerometerViewModel.java
  18. 23
    1
      app/src/main/java/com/example/greenwatch/viewmodels/VideodetectionViewModel.java
  19. 7
    1
      app/src/main/java/com/example/greenwatch/viewmodels/ViewModelInterface.java

+ 6
- 2
app/src/main/AndroidManifest.xml View File

@@ -3,10 +3,14 @@
package="com.example.greenwatch">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

<uses-feature android:name="android.hardware.camera"/>


+ 12
- 3
app/src/main/java/com/example/greenwatch/AccelerometerActivity.java View File

@@ -11,6 +11,7 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@@ -27,6 +28,7 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv

private SensorManager accelerometerManager;
private Sensor accelerometerSensor;
private SurfaceView surfaceView;
private TextView accelerometerStatusMessage;
private TextView accelerometerDataTV;
private TextView tvAccelerometerdeviceListRecyclerView;
@@ -40,6 +42,7 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);

surfaceView = (SurfaceView) findViewById(R.id.surfaceViewAccelerometer);
accelerometerStatusMessage = (TextView) findViewById(R.id.tvAccelerometerStatusmessage);
accelerometerDataTV = (TextView) findViewById(R.id.tvAccelerometerData);
tvAccelerometerdeviceListRecyclerView = (TextView) findViewById(R.id.tvAccelerometerdeviceListRecyclerView);
@@ -68,7 +71,7 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
});

mAccelerometerViewModel = new ViewModelProvider(this).get(AccelerometerViewModel.class);
mAccelerometerViewModel.init();
mAccelerometerViewModel.init(surfaceView.getHolder());
mAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -89,18 +92,24 @@ public class AccelerometerActivity extends AppCompatActivity implements SensorEv
if (aBoolean) {
if(permission.alarmRechtePruefen(AccelerometerActivity.this, AccelerometerActivity.this)){
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mAccelerometerViewModel.startAlarmRecording();
//todo AlarmHandling einfügen
} else {
}
else {
permission.alarmRechteAnfordern(AccelerometerActivity.this);
if(permission.alarmRechtePruefen(AccelerometerActivity.this, AccelerometerActivity.this)){
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mAccelerometerViewModel.startAlarmRecording();
//todo Alarmhandling einfügen
} else {
}
else {
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mAccelerometerViewModel.stopAlarmRecording(AccelerometerActivity.this);
}
}
}
else {
mAccelerometerViewModel.stopAlarmRecording(AccelerometerActivity.this);
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
}
}

+ 8
- 3
app/src/main/java/com/example/greenwatch/AudiodetectionActivity.java View File

@@ -6,8 +6,8 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@@ -16,7 +16,6 @@ import android.widget.Toast;
import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
import com.example.greenwatch.adapters.DeviceListAdapter;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.viewmodels.AccelerometerViewModel;
import com.example.greenwatch.viewmodels.MicrofonViewModel;

import java.util.List;
@@ -26,6 +25,7 @@ public class AudiodetectionActivity extends AppCompatActivity {
private TextView tvAAudiodetectionAlarmHistoryListRecyclerView;
private TextView tvAudiodetectionDeviceListRecyclerView;
private Button backToMainActivityButton;
private SurfaceView surfaceView;
private Permission permission = new Permission();
private MicrofonViewModel mMicrofonViewModel;

@@ -34,6 +34,7 @@ public class AudiodetectionActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audiodetection);

surfaceView = (SurfaceView) findViewById(R.id.surfaceViewAudio);
microfonStatusMessage = (TextView) findViewById(R.id.tvAudiodetectionStatusmessage);
tvAAudiodetectionAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvAudiodetectionAlarmHistoryListRecyclerView);
tvAudiodetectionDeviceListRecyclerView = (TextView) findViewById(R.id.tvAudiodetectionDeviceListRecyclerView);
@@ -61,7 +62,7 @@ public class AudiodetectionActivity extends AppCompatActivity {
});

mMicrofonViewModel = new ViewModelProvider(this).get(MicrofonViewModel.class);
mMicrofonViewModel.init();
mMicrofonViewModel.init(surfaceView.getHolder());
mMicrofonViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -82,19 +83,23 @@ public class AudiodetectionActivity extends AppCompatActivity {
if (aBoolean) {
if(permission.alarmRechtePruefen(AudiodetectionActivity.this, AudiodetectionActivity.this)){
Toast.makeText(AudiodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mMicrofonViewModel.startAlarmRecording();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(AudiodetectionActivity.this);
if(permission.alarmRechtePruefen(AudiodetectionActivity.this, AudiodetectionActivity.this)){
Toast.makeText(AudiodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mMicrofonViewModel.startAlarmRecording();
//todo Alarmhandling einfügen
} else {
Toast.makeText(AudiodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mMicrofonViewModel.stopAlarmRecording(AudiodetectionActivity.this);
}
}
}
else {
Toast.makeText(AudiodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mMicrofonViewModel.stopAlarmRecording(AudiodetectionActivity.this);
}
}
});

+ 8
- 2
app/src/main/java/com/example/greenwatch/AudiodetectionAndAccelerometerActivity.java View File

@@ -11,6 +11,7 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@@ -20,13 +21,13 @@ import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
import com.example.greenwatch.adapters.DeviceListAdapter;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.viewmodels.AudiodetectionAndAccelerometerViewModel;
import com.example.greenwatch.viewmodels.VideodetectionAndAccelerometerViewModel;

import java.util.List;

public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager accelerometerManager;
private Sensor accelerometerSensor;
private SurfaceView surfaceView;
private TextView audiodetectionAndAccelerometerStatusMessage;
private TextView tvAudiodetectionAndAccelerometerDeviceListRecyclerView;
private TextView tvAudiodetectionAndAccelerometerAlarmHistoryListRecyclerView;
@@ -40,6 +41,7 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity im
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audiodetection_and_accelerometer);

surfaceView = (SurfaceView) findViewById(R.id.surfaceViewAudiodetectionAndAccelerometer);
audiodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvAudiodetectionAndAccelerometerStatusmessage);
tvAudiodetectionAndAccelerometerDeviceListRecyclerView = (TextView) findViewById(R.id.tvAudiodetectionAndAccelerometerDeviceListRecyclerView);
tvAudiodetectionAndAccelerometerAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvAudiodetectionAndAccelerometerAlarmHistoryListRecyclerView);
@@ -67,7 +69,7 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity im
});

mAudiodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(AudiodetectionAndAccelerometerViewModel.class);
mAudiodetectionAndAccelerometerViewModel.init();
mAudiodetectionAndAccelerometerViewModel.init(surfaceView.getHolder());
mAudiodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -88,19 +90,23 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity im
if (aBoolean) {
if(permission.alarmRechtePruefen(AudiodetectionAndAccelerometerActivity.this, AudiodetectionAndAccelerometerActivity.this)){
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mAudiodetectionAndAccelerometerViewModel.startAlarmRecording();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(AudiodetectionAndAccelerometerActivity.this);
if(permission.alarmRechtePruefen(AudiodetectionAndAccelerometerActivity.this, AudiodetectionAndAccelerometerActivity.this)){
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mAudiodetectionAndAccelerometerViewModel.startAlarmRecording();
//todo Alarmhandling einfügen
} else {
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mAudiodetectionAndAccelerometerViewModel.stopAlarmRecording(AudiodetectionAndAccelerometerActivity.this);
}
}
}
else {
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mAudiodetectionAndAccelerometerViewModel.stopAlarmRecording(AudiodetectionAndAccelerometerActivity.this);
}
}
});

+ 12
- 3
app/src/main/java/com/example/greenwatch/MainActivity.java View File

@@ -9,10 +9,11 @@ import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
@@ -20,7 +21,6 @@ import com.example.greenwatch.adapters.DeviceListAdapter;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.viewmodels.MainActivityViewModel;


import java.util.List;

public class MainActivity extends AppCompatActivity {
@@ -73,7 +73,8 @@ public class MainActivity extends AppCompatActivity {
permission.startRechtePruefen(this, this);

mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
mMainActivityViewModel.init();
mMainActivityViewModel.init(surfaceView.getHolder());

mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -94,14 +95,17 @@ public class MainActivity extends AppCompatActivity {
if (aBoolean) {
if(permission.alarmRechtePruefen(context, activity)){
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mMainActivityViewModel.startAlarmRecording();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(activity);
if(permission.alarmRechtePruefen(context, activity)){
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mMainActivityViewModel.startAlarmRecording();
//todo Alarmhandling einfügen
} else {
Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mMainActivityViewModel.stopAlarmRecording(MainActivity.this);
}
}
}
@@ -196,4 +200,9 @@ public class MainActivity extends AppCompatActivity {
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
startActivity(intent);
}

@Override
protected void onPause() {
super.onPause();
}
}

+ 6
- 2
app/src/main/java/com/example/greenwatch/VideodetectionActivity.java View File

@@ -9,14 +9,15 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
import com.example.greenwatch.adapters.DeviceListAdapter;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.viewmodels.AccelerometerViewModel;
import com.example.greenwatch.viewmodels.VideodetectionViewModel;
import com.google.common.util.concurrent.ListenableFuture;

@@ -64,7 +65,7 @@ public class VideodetectionActivity extends AppCompatActivity {
});

mVideoDetectionViewModel = new ViewModelProvider(this).get(VideodetectionViewModel.class);
mVideoDetectionViewModel.init();
mVideoDetectionViewModel.init(surfaceView.getHolder());
mVideoDetectionViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -85,14 +86,17 @@ public class VideodetectionActivity extends AppCompatActivity {
if (aBoolean) {
if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mVideoDetectionViewModel.startAlarmRecording();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(VideodetectionActivity.this);
if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mVideoDetectionViewModel.startAlarmRecording();
//todo Alarmhandling einfügen
} else {
Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mVideoDetectionViewModel.stopAlarmRecording(VideodetectionActivity.this);
}
}
}

+ 8
- 1
app/src/main/java/com/example/greenwatch/VideodetectionAndAccelerometerActivity.java View File

@@ -13,6 +13,7 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@@ -32,6 +33,7 @@ import java.util.concurrent.ExecutionException;
public class VideodetectionAndAccelerometerActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager accelerometerManager;
private Sensor accelerometerSensor;
private SurfaceView surfaceView;
private TextView videodetectionAndAccelerometerStatusMessage;
private TextView videodetectionAndAccelerometerDataTV;
private TextView tvVideodetectionAndAccelerometerDeviceListRecyclerView;
@@ -45,6 +47,7 @@ public class VideodetectionAndAccelerometerActivity extends AppCompatActivity im
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videodetection_and_accelerometer);

surfaceView = (SurfaceView) findViewById(R.id.surfaceViewVideodetectionAndAccelerometer);
videodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerStatusmessage);
videodetectionAndAccelerometerDataTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerData);
tvVideodetectionAndAccelerometerDeviceListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAndAccelerometerDeviceListRecyclerView);
@@ -73,7 +76,7 @@ public class VideodetectionAndAccelerometerActivity extends AppCompatActivity im
});

mVideodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(VideodetectionAndAccelerometerViewModel.class);
mVideodetectionAndAccelerometerViewModel.init();
mVideodetectionAndAccelerometerViewModel.init(surfaceView.getHolder());
mVideodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override
public void onChanged(List<Device> devices) {
@@ -94,19 +97,23 @@ public class VideodetectionAndAccelerometerActivity extends AppCompatActivity im
if (aBoolean) {
if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mVideodetectionAndAccelerometerViewModel.startAlarmRecording();
//todo AlarmHandling einfügen
} else {
permission.alarmRechteAnfordern(VideodetectionAndAccelerometerActivity.this);
if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
mVideodetectionAndAccelerometerViewModel.startAlarmRecording();
//todo Alarmhandling einfügen
} else {
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mVideodetectionAndAccelerometerViewModel.stopAlarmRecording(VideodetectionAndAccelerometerActivity.this);
}
}
}
else {
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
mVideodetectionAndAccelerometerViewModel.stopAlarmRecording(VideodetectionAndAccelerometerActivity.this);
}
}
});

+ 256
- 0
app/src/main/java/com/example/greenwatch/alarmrecorder/AlarmRecorder.java View File

@@ -0,0 +1,256 @@
package com.example.greenwatch.alarmrecorder;

import android.app.Activity;
import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.widget.Toast;

import com.example.greenwatch.MainActivity;
import com.example.greenwatch.alarmrecorder.runnables.AudioRecorder;
import com.example.greenwatch.alarmrecorder.runnables.VideoRecorder;
import com.example.greenwatch.sensors.AccelerometerSensor;
import com.example.greenwatch.sensors.CameraSensor;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

public class AlarmRecorder {
private static AlarmRecorder alarmRecorderInstance;
private boolean isRecording;
private String videoPath;
private String audioPath;
private MediaFormat videoFormat;
private MediaFormat audioFormat;
private MediaExtractor videoExtractor;
private MediaExtractor audioExtractor;
private AudioRecorder audioRecorderRunnable;
private VideoRecorder videoRecorderRunnable;
private SurfaceHolder previewHolder;
private MediaRecorder videoRecorder;
//private MediaRecorder audioRecorder;
private Thread videoThread; // Video-Thread als Instanzvariable
private Thread audioThread; // Audio-Thread als Instanzvariable

private AlarmRecorder() {
//audioRecorder = new MediaRecorder();
//videoRecorder = new MediaRecorder();
videoExtractor = new MediaExtractor();
audioExtractor = new MediaExtractor();
audioRecorderRunnable = new AudioRecorder();
videoRecorderRunnable = new VideoRecorder();
}

public static synchronized AlarmRecorder getInstance() {
if (alarmRecorderInstance == null){
alarmRecorderInstance = new AlarmRecorder();
}
return alarmRecorderInstance;
}

public void setPreviewHolder(SurfaceHolder previewHolder) {
this.previewHolder = previewHolder;
}

public void startRecording() {
createStoragePaths(); //Speicherort und -namen für Audio- und Video-Datei

setAudioPath(audioPath);
setVideoPath(videoPath);
setVideoRecorderPreviewHolder(previewHolder);

audioThread = new Thread(audioRecorderRunnable);
videoThread = new Thread(videoRecorderRunnable);

//Threads starten
videoThread.start();
audioThread.start();
}

public void stopRecording(Context context) {
try {
stopAudioRecording();
stopVideoRecording();

Toast.makeText(context, "Video- und Audioaufzeichnung beendet", Toast.LENGTH_SHORT).show();

waitTillThreadsStopped();
File videoFile = new File(videoPath); //Speichere das aufgenommene Video
File audioFile = new File(audioPath); //Speichere die aufgenommene Audio

if (videoFile.exists() && audioFile.exists()) {
//Wenn Video- und Audioaufzeichnung gestoppt und abgespeichert sind, beginne mit dem Mergeprozess der beiden
mergeVideoWithAudio();
Toast.makeText(context, "Video und Audio erfolgreich zusammengeführt", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Dateien wurden nicht gefunden!", Toast.LENGTH_SHORT).show();
}
} catch (RuntimeException stopException) {
stopException.printStackTrace();
}
}

private void createStoragePaths(){
//Pfade zum Zwischenspeichern der aufgenommenen Audio und Video-Datei
String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
String dcimDirectory = externalStorageDirectory + "/DCIM";
videoPath = dcimDirectory + "/video.mp4";
audioPath = dcimDirectory + "/audio.mp3";
}

private void setVideoPath(String videoPath) {
videoRecorderRunnable.setVideoPath(videoPath);
}

private void setVideoRecorderPreviewHolder(SurfaceHolder previewHolder) {
videoRecorderRunnable.setPreviewHolder(previewHolder);
}
private void stopVideoRecording(){
videoRecorderRunnable.stopVideoRecording();
}

private void setAudioPath(String audioPath) {
audioRecorderRunnable.setAudioPath(audioPath);
}

private void stopAudioRecording(){
audioRecorderRunnable.stopAudioRecording();
}

private void waitTillThreadsStopped(){
try {
videoThread.join();
audioThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void mergeVideoWithAudio() {
try {
setVideoExtractorDataSource(); //extrahieren der Video Datei, die zuvor zwischengespeichert wurde
setAudioExtractorDataSource(); //extrahieren der Audio Datei, die zuvor zwischengespeichert wurde

//Speicherort der später zusammengeführten Datei
String outputFilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath() + "/merged_video.mp4";
//MediaMuxer zum Zusammenführen einer Audio- und einer Videodatei
MediaMuxer muxer = new MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
int videoTrack = muxer.addTrack(videoFormat);
int audioTrack = muxer.addTrack(audioFormat);
muxer.start();

ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

videoExtraction(buffer, videoTrack, bufferInfo, muxer);
audioExtraction(buffer, audioTrack, bufferInfo, muxer);

muxer.stop();
muxer.release();

// Löschen der separaten Video- und Audio-Dateien
deleteVideoFile();
deleteAudioFile();
} catch (IOException e) {
e.printStackTrace();
}
}

private void setVideoExtractorDataSource() {
try {
videoExtractor.setDataSource(videoPath);
int videoTrackIndex = getTrackIndex(videoExtractor, "video/");
if (videoTrackIndex < 0) {
// Video-Track nicht gefunden
return;
}
videoExtractor.selectTrack(videoTrackIndex);
videoFormat = videoExtractor.getTrackFormat(videoTrackIndex);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void setAudioExtractorDataSource() {
try {
audioExtractor.setDataSource(audioPath);
int audioTrackIndex = getTrackIndex(audioExtractor, "audio/");
if (audioTrackIndex < 0) {
// Audio-Track nicht gefunden
return;
}
audioExtractor.selectTrack(audioTrackIndex);
audioFormat = audioExtractor.getTrackFormat(audioTrackIndex);

} catch (IOException e) {
throw new RuntimeException(e);
}
}

private int getTrackIndex(MediaExtractor extractor, String mimeType) {
int trackCount = extractor.getTrackCount();
for (int i = 0; i < trackCount; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String trackMimeType = format.getString(MediaFormat.KEY_MIME);
if (trackMimeType.startsWith(mimeType)) {
return i;
}
}
return -1;
}

private void videoExtraction(ByteBuffer buffer, int videoTrack, MediaCodec.BufferInfo bufferInfo, MediaMuxer muxer) {
videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
while (true) {
int sampleSize = videoExtractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
break;
}
long presentationTimeUs = videoExtractor.getSampleTime();
bufferInfo.offset = 0;
bufferInfo.size = sampleSize;
bufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
bufferInfo.presentationTimeUs = presentationTimeUs;
muxer.writeSampleData(videoTrack, buffer, bufferInfo);
videoExtractor.advance();
}
}

private void audioExtraction(ByteBuffer buffer, int audioTrack, MediaCodec.BufferInfo bufferInfo, MediaMuxer muxer) {
audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
while (true) {
int sampleSize = audioExtractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
break;
}
long presentationTimeUs = audioExtractor.getSampleTime();
bufferInfo.offset = 0;
bufferInfo.size = sampleSize;
bufferInfo.flags = 0; // or MediaCodec.BUFFER_FLAG_KEY_FRAME
bufferInfo.presentationTimeUs = presentationTimeUs;
muxer.writeSampleData(audioTrack, buffer, bufferInfo);
audioExtractor.advance();
}
}


private void deleteVideoFile(){
File videoFile = new File(videoPath);
if (videoFile.exists()) {
videoFile.delete();
}
}

private void deleteAudioFile(){
File audioFile = new File(audioPath);
if (audioFile.exists()) {
audioFile.delete();
}
}
}

+ 46
- 0
app/src/main/java/com/example/greenwatch/alarmrecorder/runnables/AudioRecorder.java View File

@@ -0,0 +1,46 @@
package com.example.greenwatch.alarmrecorder.runnables;

import android.media.MediaRecorder;
import android.view.SurfaceHolder;
import android.widget.Toast;

import com.example.greenwatch.MainActivity;

import java.io.IOException;

public class AudioRecorder implements Runnable {

private MediaRecorder audioRecorder = null;
private String audioPath;
public AudioRecorder() {
}

public void setAudioPath(String audioPath) {
this.audioPath = audioPath;
}

public void stopAudioRecording(){
if (audioRecorder != null) {
audioRecorder.stop();
audioRecorder.reset();
audioRecorder.release();
audioRecorder = null;
}
}

@Override
public void run() {
audioRecorder = new MediaRecorder();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
audioRecorder.setOutputFile(audioPath);

try {
audioRecorder.prepare();
audioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

+ 54
- 0
app/src/main/java/com/example/greenwatch/alarmrecorder/runnables/VideoRecorder.java View File

@@ -0,0 +1,54 @@
package com.example.greenwatch.alarmrecorder.runnables;

import android.media.MediaRecorder;
import android.view.SurfaceHolder;

import com.example.greenwatch.viewmodels.ViewModelInterface;

import java.io.IOException;

public class VideoRecorder implements Runnable{
private MediaRecorder videoRecorder;
private String videoPath;
private SurfaceHolder previewHolder;

public VideoRecorder() {
videoRecorder = new MediaRecorder();
}

public void setPreviewHolder(SurfaceHolder previewHolder) {
this.previewHolder = previewHolder;
}

public void setVideoPath(String audioPath) {
this.videoPath = audioPath;
}

public void stopVideoRecording(){
if (videoRecorder != null) {
videoRecorder.stop();
videoRecorder.reset();
videoRecorder.release();
videoRecorder = null;
}
}

@Override
public void run() {
videoRecorder = new MediaRecorder();
videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
videoRecorder.setOutputFile(videoPath);
videoRecorder.setOrientationHint(90);
videoRecorder.setPreviewDisplay(previewHolder.getSurface());

try {
videoRecorder.prepare();
videoRecorder.start();

} catch (IOException e) {
e.printStackTrace();
}
}
}

+ 10
- 3
app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java View File

@@ -17,7 +17,7 @@ public class WiFiCommunication {
private final InetAddress address;
private final DatagramSocket socket;
private final int port;
private String rxString;
private volatile String rxString;
private String sendMessage;
//private String sendMsg = "default";
private boolean isNewMessage;
@@ -48,7 +48,14 @@ public class WiFiCommunication {
}

public void setDeviceRepository(DeviceRepository deviceRepository) {
this.mDeviceRepository = deviceRepository;
if (mDeviceRepository == null) {
this.mDeviceRepository = deviceRepository;
}

}
public void setRecievedSting(String rxString) {
this.rxString = rxString;
mDeviceRepository.getNewReceivedMessage(this.rxString);
}

public void setNewMessage(String newMessage, boolean isNewMessage){
@@ -117,7 +124,7 @@ public class WiFiCommunication {
rxString = new String(packet.getData(), 0, packet.getLength());

if(!previousRxString.equals(rxString)){
mDeviceRepository.getNewReceivedMessage(rxString);
setRecievedSting(rxString);

}
previousRxString = rxString;

+ 17
- 5
app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java View File

@@ -42,7 +42,10 @@ public class DeviceRepository {
}

public void setWiFiCommunication(WiFiCommunication wiFiCommunication) {
this.mWiFiCommunication = wiFiCommunication;
if (mWiFiCommunication == null) {
this.mWiFiCommunication = wiFiCommunication;
}

}

public MutableLiveData<List<Device>> getConnectedDeviceList() {
@@ -76,10 +79,10 @@ public class DeviceRepository {

public void getNewReceivedMessage(String newMessage) {
String[] messageString = messageStringSplitter(newMessage);
if(messageString[0]=="1" && messageString.length == 7) {
if(messageString[0].equals("1") && messageString.length == 7) {
String timeStamp = messageString[1];
String deviceID = messageString[3];
boolean sensorStatus = convertSensorStatus(messageString[4]);
boolean sensorStatus = convertRecievedSensorStatus(messageString[4]);
String sensorType = messageString[5];
float sensorMassage = Float.valueOf(messageString[6]);

@@ -107,7 +110,7 @@ public class DeviceRepository {
.append(delimiter)
.append(device.getDeviceID())
.append(delimiter)
.append(device.getSensorStatus())
.append(convertSendSensorStatus(device.getSensorStatus()))
.append(delimiter)
.append(device.getSensorType())
.append(delimiter)
@@ -307,10 +310,19 @@ public class DeviceRepository {
return message.split(delimiter);
}

private boolean convertSensorStatus(String status) {
private boolean convertRecievedSensorStatus(String status) {
return status.equals(sensorStatusKey);
}

private String convertSendSensorStatus(boolean status) {
if (status){
return "An";
}
else {
return "Aus";
}
}

private void setAlarmHistoryDeviceList(Device device) {
if (alarmHistoryDeviceList.size() == maxAlarmHistoryListSize) {
alarmHistoryDeviceList.remove(alarmHistoryDeviceList.size() -1);

+ 30
- 1
app/src/main/java/com/example/greenwatch/viewmodels/AccelerometerViewModel.java View File

@@ -1,9 +1,18 @@
package com.example.greenwatch.viewmodels;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.view.SurfaceHolder;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor;
@@ -16,16 +25,20 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private AccelerometerSensor mAccelerometerSensor;
private AlarmRecorder mAlarmRecorder;
private DeviceRepository mDeviceRepository;

@Override
public void init() {
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
@@ -38,6 +51,7 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
}
setAlarmRecordingPreviewHolder(previewHolder);
}

public void addValueToGesamtBE(float newValue) {
@@ -143,4 +157,19 @@ public class AccelerometerViewModel extends ViewModel implements ViewModelInterf
public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp();
}

@Override
public void startAlarmRecording() {
mAlarmRecorder.startRecording();
}

@Override
public void stopAlarmRecording(Context context) {
mAlarmRecorder.stopRecording(context);
}

@Override
public void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder) {
mAlarmRecorder.setPreviewHolder(previewHolder);
}
}

+ 57
- 29
app/src/main/java/com/example/greenwatch/viewmodels/AudiodetectionAndAccelerometerViewModel.java View File

@@ -1,10 +1,14 @@
package com.example.greenwatch.viewmodels;

import android.content.Context;
import android.view.SurfaceHolder;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor;
@@ -21,41 +25,14 @@ public class AudiodetectionAndAccelerometerViewModel extends ViewModel implement
private MutableLiveData<Boolean> mStartAlarmRecording;
private AccelerometerSensor mAccelerometerSensor;
private MicrofonSensor mMicrofonSensor;
private AlarmRecorder mAlarmRecorder;
private DeviceRepository mDeviceRepository;
private boolean microfonAlarmDetected;
private boolean accelerometerAlarmDetected;
private boolean microfondetectionAndAccelerometerAlarmDetected;

@Override
public void init() {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mMicrofonSensor == null) {
mMicrofonSensor = MicrofonSensor.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
}
if (mMicrofonAlarmDetected == null) {
mMicrofonAlarmDetected = mMicrofonSensor.getAccelerometerAlarmDetected();
}
microfondetectionAndAccelerometerAlarmDetected = false;
accelerometerAlarmDetected = false;
microfonAlarmDetected = false;
registerAlarmObserver();

}

public void addValueToGesamtBE(float newValue) {
@@ -148,6 +125,42 @@ public class AudiodetectionAndAccelerometerViewModel extends ViewModel implement
mAccelerometerAlarmDetected.removeObserver(observer);
}

@Override
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mMicrofonSensor == null) {
mMicrofonSensor = MicrofonSensor.getInstance();
}
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
}
if (mMicrofonAlarmDetected == null) {
mMicrofonAlarmDetected = mMicrofonSensor.getAccelerometerAlarmDetected();
}
microfondetectionAndAccelerometerAlarmDetected = false;
accelerometerAlarmDetected = false;
microfonAlarmDetected = false;
registerAlarmObserver();
setAlarmRecordingPreviewHolder(previewHolder);
}

@Override
public LiveData<List<Device>> getConnectedDeviceList() {
return mDeviceList;
@@ -227,4 +240,19 @@ public class AudiodetectionAndAccelerometerViewModel extends ViewModel implement
public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp();
}

@Override
public void startAlarmRecording() {
mAlarmRecorder.startRecording();
}

@Override
public void stopAlarmRecording(Context context) {
mAlarmRecorder.stopRecording(context);
}

@Override
public void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder) {
mAlarmRecorder.setPreviewHolder(previewHolder);
}
}

+ 42
- 10
app/src/main/java/com/example/greenwatch/viewmodels/MainActivityViewModel.java View File

@@ -4,6 +4,7 @@ import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.view.SurfaceHolder;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@@ -11,9 +12,11 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.communication.WiFiCommunication;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor;

import java.util.List;

@@ -23,22 +26,36 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository;
private WiFiCommunication mWiFiCommunication;
private AlarmRecorder mAlarmRecorder;

@Override
public void init() {
WiFiCommunication mWiFiCommunication;
if(mDeviceList != null) {
return;
public void init(SurfaceHolder previewHolder) {

if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mWiFiCommunication == null) {
mWiFiCommunication = WiFiCommunication.getInstance();
}
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
mDeviceRepository = DeviceRepository.getInstance();
mWiFiCommunication = WiFiCommunication.getInstance();
mDeviceRepository.setWiFiCommunication(mWiFiCommunication);
mWiFiCommunication.setDeviceRepository(mDeviceRepository);
mDeviceRepository.setWiFiCommunication(mWiFiCommunication);
mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0.0f);
mDeviceRepository.setDeviceID(mDeviceRepository.getLocalDeviceUUID(), WiFiCommunication.getLocalIpAddress());
mDeviceList = mDeviceRepository.getConnectedDeviceList();
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
setAlarmRecordingPreviewHolder(previewHolder);
mWiFiCommunication = null;
}

@Override
@@ -120,4 +137,19 @@ public class MainActivityViewModel extends ViewModel implements ViewModelInterfa
public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp();
}

@Override
public void startAlarmRecording() {
mAlarmRecorder.startRecording();
}

@Override
public void stopAlarmRecording(Context context) {
mAlarmRecorder.stopRecording(context);
}

@Override
public void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder) {
mAlarmRecorder.setPreviewHolder(previewHolder);
}
}

+ 46
- 21
app/src/main/java/com/example/greenwatch/viewmodels/MicrofonViewModel.java View File

@@ -1,11 +1,16 @@
package com.example.greenwatch.viewmodels;

import android.content.Context;
import android.view.SurfaceHolder;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor;
import com.example.greenwatch.sensors.MicrofonSensor;

import java.util.List;
@@ -17,15 +22,39 @@ public class MicrofonViewModel extends ViewModel implements ViewModelInterface {
private MutableLiveData<Boolean> mStartAlarmRecording;
private MicrofonSensor mMicrofonSensor;
private DeviceRepository mDeviceRepository;
private AlarmRecorder mAlarmRecorder;

public void startMicrofonSensor() {
mMicrofonSensor.start();
}

public void stopMicrofonSensor() {
mMicrofonSensor.stop();
}

public void recalibrationMicrofonSensor() {
mMicrofonSensor.doRecalibration();
}

public float getAmplitudeInDB() {
return mMicrofonSensor.getAmplitudeInDB();
}

public LiveData<Boolean> getMicrofonAlarmDetected() {
return mMicrofonAlarmDetected;
}

@Override
public void init() {
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mMicrofonSensor == null) {
mMicrofonSensor = MicrofonSensor.getInstance();
}
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
@@ -38,26 +67,7 @@ public class MicrofonViewModel extends ViewModel implements ViewModelInterface {
if (mMicrofonAlarmDetected == null) {
mMicrofonAlarmDetected = mMicrofonSensor.getAccelerometerAlarmDetected();
}
}

public void startMicrofonSensor() {
mMicrofonSensor.start();
}

public void stopMicrofonSensor() {
mMicrofonSensor.stop();
}

public void recalibrationMicrofonSensor() {
mMicrofonSensor.doRecalibration();
}

public float getAmplitudeInDB() {
return mMicrofonSensor.getAmplitudeInDB();
}

public LiveData<Boolean> getMicrofonAlarmDetected() {
return mMicrofonAlarmDetected;
setAlarmRecordingPreviewHolder(previewHolder);
}

@Override
@@ -139,4 +149,19 @@ public class MicrofonViewModel extends ViewModel implements ViewModelInterface {
public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp();
}

@Override
public void startAlarmRecording() {
mAlarmRecorder.startRecording();
}

@Override
public void stopAlarmRecording(Context context) {
mAlarmRecorder.stopRecording(context);
}

@Override
public void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder) {
mAlarmRecorder.setPreviewHolder(previewHolder);
}
}

+ 54
- 32
app/src/main/java/com/example/greenwatch/viewmodels/VideodetectionAndAccelerometerViewModel.java View File

@@ -1,6 +1,7 @@
package com.example.greenwatch.viewmodels;

import android.content.Context;
import android.view.SurfaceHolder;

import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.lifecycle.LifecycleOwner;
@@ -9,6 +10,7 @@ import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;

import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor;
@@ -26,42 +28,11 @@ public class VideodetectionAndAccelerometerViewModel extends ViewModel implement
private AccelerometerSensor mAccelerometerSensor;
private CameraSensor mCameraSensor;
private DeviceRepository mDeviceRepository;
private AlarmRecorder mAlarmRecorder;
private boolean videoAlarmDetected;
private boolean accelerometerAlarmDetected;
private boolean videodetectionAndAccelerometerAlarmDetected;

@Override
public void init() {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mCameraSensor == null) {
mCameraSensor = CameraSensor.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
}
if (mVideoAlarmDetected == null) {
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
}
videodetectionAndAccelerometerAlarmDetected = false;
videoAlarmDetected = false;
accelerometerAlarmDetected = false;
registerAlarmObserver();
}

public void addValueToGesamtBE(float newValue) {
mAccelerometerSensor.addValueToGesamtBE(newValue);
}
@@ -140,6 +111,42 @@ public class VideodetectionAndAccelerometerViewModel extends ViewModel implement
mAccelerometerAlarmDetected.removeObserver(observer);
}

@Override
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance();
}
if (mCameraSensor == null) {
mCameraSensor = CameraSensor.getInstance();
}
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
if (mAlarmHistoryList == null) {
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
}
if (mStartAlarmRecording == null) {
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
}
if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
}
if (mVideoAlarmDetected == null) {
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
}
videodetectionAndAccelerometerAlarmDetected = false;
videoAlarmDetected = false;
accelerometerAlarmDetected = false;
registerAlarmObserver();
setAlarmRecordingPreviewHolder(previewHolder);
}

@Override
public LiveData<List<Device>> getConnectedDeviceList() {
return mDeviceList;
@@ -219,4 +226,19 @@ public class VideodetectionAndAccelerometerViewModel extends ViewModel implement
public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp();
}

@Override
public void startAlarmRecording() {
mAlarmRecorder.startRecording();
}

@Override
public void stopAlarmRecording(Context context) {
mAlarmRecorder.stopRecording(context);
}

@Override
public void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder) {
mAlarmRecorder.setPreviewHolder(previewHolder);
}
}

+ 23
- 1
app/src/main/java/com/example/greenwatch/viewmodels/VideodetectionViewModel.java View File

@@ -1,6 +1,7 @@
package com.example.greenwatch.viewmodels;

import android.content.Context;
import android.view.SurfaceHolder;

import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.lifecycle.LifecycleOwner;
@@ -8,6 +9,7 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.CameraSensor;
@@ -21,6 +23,7 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository;
private AlarmRecorder mAlarmRecorder;
private CameraSensor mCameraSensor;


@@ -29,13 +32,16 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
}

@Override
public void init() {
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance();
}
if (mCameraSensor == null) {
mCameraSensor = CameraSensor.getInstance();
}
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList();
}
@@ -48,6 +54,7 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
if (mVideoAlarmDetected == null) {
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
}
setAlarmRecordingPreviewHolder(previewHolder);
}

public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) {
@@ -137,4 +144,19 @@ public class VideodetectionViewModel extends ViewModel implements ViewModelInter
public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp();
}

@Override
public void startAlarmRecording() {
mAlarmRecorder.startRecording();
}

@Override
public void stopAlarmRecording(Context context) {
mAlarmRecorder.stopRecording(context);
}

@Override
public void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder) {
mAlarmRecorder.setPreviewHolder(previewHolder);
}
}

+ 7
- 1
app/src/main/java/com/example/greenwatch/viewmodels/ViewModelInterface.java View File

@@ -1,5 +1,8 @@
package com.example.greenwatch.viewmodels;

import android.content.Context;
import android.view.SurfaceHolder;

import androidx.lifecycle.LiveData;

import com.example.greenwatch.models.Device;
@@ -7,7 +10,7 @@ import com.example.greenwatch.models.Device;
import java.util.List;

public interface ViewModelInterface {
void init();
void init(SurfaceHolder previewHolder);
LiveData<List<Device>> getConnectedDeviceList();
LiveData<List<Device>> getAlarmHistoryList();
LiveData<Boolean> getStartAlarmRecording();
@@ -24,4 +27,7 @@ public interface ViewModelInterface {
float getSensorMassage(String deviceID);
String getLocalDeviceUUID();
String getSystemTimeStamp();
void startAlarmRecording();
void stopAlarmRecording(Context context);
void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder);
}

Loading…
Cancel
Save