Browse Source

Added Alarm Recording to project but RecyclerView crashed

DoNotMergeAlarmRecorderTest
Christian Tinz 1 year ago
parent
commit
a9e2571915

+ 5
- 1
app/src/main/AndroidManifest.xml View File

package="com.example.greenwatch"> package="com.example.greenwatch">


<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<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.CAMERA" />


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



+ 6
- 1
app/src/main/java/com/example/greenwatch/AccelerometerActivity.java View File

import android.hardware.SensorEventListener; import android.hardware.SensorEventListener;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.os.Bundle; import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;


private SensorManager accelerometerManager; private SensorManager accelerometerManager;
private Sensor accelerometerSensor; private Sensor accelerometerSensor;
private SurfaceView surfaceView;
private TextView accelerometerStatusMessage; private TextView accelerometerStatusMessage;
private TextView accelerometerDataTV; private TextView accelerometerDataTV;
private TextView accelerometerWarningTV; private TextView accelerometerWarningTV;
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer); setContentView(R.layout.activity_accelerometer);


surfaceView = (SurfaceView) findViewById(R.id.surfaceViewAccelerometer);
accelerometerStatusMessage = (TextView) findViewById(R.id.tvAccelerometerStatusmessage); accelerometerStatusMessage = (TextView) findViewById(R.id.tvAccelerometerStatusmessage);
accelerometerDataTV = (TextView) findViewById(R.id.tvAccelerometerData); accelerometerDataTV = (TextView) findViewById(R.id.tvAccelerometerData);
accelerometerWarningTV = (TextView) findViewById(R.id.tvAccelerometerWarning); accelerometerWarningTV = (TextView) findViewById(R.id.tvAccelerometerWarning);
}); });


mAccelerometerViewModel = new ViewModelProvider(this).get(AccelerometerViewModel.class); mAccelerometerViewModel = new ViewModelProvider(this).get(AccelerometerViewModel.class);
mAccelerometerViewModel.init();
mAccelerometerViewModel.init(surfaceView.getHolder());
mAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { mAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override @Override
public void onChanged(List<Device> devices) { public void onChanged(List<Device> devices) {
@Override @Override
public void onChanged(Boolean aBoolean) { public void onChanged(Boolean aBoolean) {
if (aBoolean) { if (aBoolean) {
mAccelerometerViewModel.startAlarmRecording();
Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); Toast.makeText(AccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
} }
else { else {
mAccelerometerViewModel.stopAlarmRecording(AccelerometerActivity.this);
Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); Toast.makeText(AccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
} }
} }

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



import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
private Button audiodetectionAndAccelerometerButton; private Button audiodetectionAndAccelerometerButton;
private Button videodetectionAndAccelerometerButton; private Button videodetectionAndAccelerometerButton;
private Button connectionButton; private Button connectionButton;
private SurfaceView surfaceView;
private MainActivityViewModel mMainActivityViewModel; private MainActivityViewModel mMainActivityViewModel;


@Override @Override
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);


surfaceView = (SurfaceView) findViewById(R.id.surfaceViewMainActivity);
audiodetectionButton = (Button) findViewById(R.id.audiodetectionButton); audiodetectionButton = (Button) findViewById(R.id.audiodetectionButton);
videodetectionButton = (Button) findViewById(R.id.videodetectionButton); videodetectionButton = (Button) findViewById(R.id.videodetectionButton);
accelerometerButton = (Button) findViewById(R.id.accelerometerButton); accelerometerButton = (Button) findViewById(R.id.accelerometerButton);
alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter); alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);


mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class); mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
mMainActivityViewModel.init();
mMainActivityViewModel.init(surfaceView.getHolder());
mMainActivityViewModel.accessRequestCamera(this); mMainActivityViewModel.accessRequestCamera(this);
mMainActivityViewModel.accessRequestAudioRecording(this);
mMainActivityViewModel.accessRequestWriteExternalStorage(this);
mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { mMainActivityViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override @Override
public void onChanged(List<Device> devices) { public void onChanged(List<Device> devices) {
@Override @Override
public void onChanged(Boolean aBoolean) { public void onChanged(Boolean aBoolean) {
if (aBoolean) { if (aBoolean) {
mMainActivityViewModel.startAlarmRecording();
Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
} }
else { else {
mMainActivityViewModel.stopAlarmRecording(MainActivity.this);
Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
} }
} }
startActivity(intent); startActivity(intent);
} }
public void openVideodetectionAndAccelerometerActivity(){ public void openVideodetectionAndAccelerometerActivity(){
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
startActivity(intent);
//Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
//startActivity(intent);
} }
public void openConnectionActivity(){ public void openConnectionActivity(){
//Intent intent = new Intent(this, ConnectionActivity.class); //Intent intent = new Intent(this, ConnectionActivity.class);

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

import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;


import android.os.Bundle; import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
public class VideodetectionActivity extends AppCompatActivity { public class VideodetectionActivity extends AppCompatActivity {


private Button backToMainActivity; private Button backToMainActivity;
private SurfaceView surfaceView;
private VideodetectionViewModel mVideoDetectionViewModel; private VideodetectionViewModel mVideoDetectionViewModel;


@Override @Override
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videodetection); setContentView(R.layout.activity_videodetection);


surfaceView = (SurfaceView) findViewById(R.id.surfaceViewVideodetection);
backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity); backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity);


RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView); RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
}); });


mVideoDetectionViewModel = new ViewModelProvider(this).get(VideodetectionViewModel.class); mVideoDetectionViewModel = new ViewModelProvider(this).get(VideodetectionViewModel.class);
mVideoDetectionViewModel.init();
mVideoDetectionViewModel.init(surfaceView.getHolder());
mVideoDetectionViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { mVideoDetectionViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
@Override @Override
public void onChanged(List<Device> devices) { public void onChanged(List<Device> devices) {
@Override @Override
public void onChanged(Boolean aBoolean) { public void onChanged(Boolean aBoolean) {
if (aBoolean) { if (aBoolean) {
mVideoDetectionViewModel.startAlarmRecording();
Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
} }
else { else {
mVideoDetectionViewModel.stopAlarmRecording(VideodetectionActivity.this);
Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
} }
} }

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

package com.example.greenwatch;
/*package com.example.greenwatch;


import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.lifecycle.ProcessCameraProvider;
accelerometerManager.unregisterListener(this, accelerometerSensor); accelerometerManager.unregisterListener(this, accelerometerSensor);
} }
} }
}
}*/

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

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 Runnable audioRecorderRunnable;
private Runnable 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();
}

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

audioRecorderRunnable = new AudioRecorder(audioRecorder, audioPath);
videoRecorderRunnable = new VideoRecorder(videoRecorder, videoPath, previewHolder);
audioThread = new Thread(audioRecorderRunnable);
videoThread = new Thread(videoRecorderRunnable);

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

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

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
isRecording = true;
String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
String dcimDirectory = externalStorageDirectory + "/DCIM";
videoPath = dcimDirectory + "/video.mp4";
audioPath = dcimDirectory + "/audio.mp3";
}

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

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

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();
}
}
}

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

package com.example.greenwatch.alarmrecorder.runnables;

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

import com.example.greenwatch.MainActivity;

import java.io.IOException;

public class AudioRecorder implements Runnable {

private final MediaRecorder audioRecorder;
private final String audioPath;
public AudioRecorder(MediaRecorder audioRecorder, String audioPath) {
this.audioRecorder = audioRecorder;
this.audioPath = audioPath;
}
@Override
public void run() {
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();
}
}

}

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

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 final MediaRecorder videoRecorder;
private final String videoPath;
private SurfaceHolder previewHolder;

public VideoRecorder(MediaRecorder videoRecorder, String videoPath, SurfaceHolder previewHolder) {
this.videoRecorder = videoRecorder;
this.videoPath = videoPath;
this.previewHolder = previewHolder;
}
@Override
public void run() {
videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
videoRecorder.setOutputFile(videoPath);
videoRecorder.setOrientationHint(90);
//todo find solution for videoView
videoRecorder.setPreviewDisplay(previewHolder.getSurface());

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

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

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

package com.example.greenwatch.viewmodels; package com.example.greenwatch.viewmodels;


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

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


import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device; import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository; import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.AccelerometerSensor; import com.example.greenwatch.sensors.AccelerometerSensor;
private MutableLiveData<List<Device>> mAlarmHistoryList; private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording; private MutableLiveData<Boolean> mStartAlarmRecording;
private AccelerometerSensor mAccelerometerSensor; private AccelerometerSensor mAccelerometerSensor;
private AlarmRecorder mAlarmRecorder;
private DeviceRepository mDeviceRepository; private DeviceRepository mDeviceRepository;


@Override @Override
public void init() {
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) { if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance(); mDeviceRepository = DeviceRepository.getInstance();
} }
if (mAccelerometerSensor == null) { if (mAccelerometerSensor == null) {
mAccelerometerSensor = AccelerometerSensor.getInstance(); mAccelerometerSensor = AccelerometerSensor.getInstance();
} }
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) { if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList(); mDeviceList = mDeviceRepository.getConnectedDeviceList();
} }
if (mAccelerometerAlarmDetected == null) { if (mAccelerometerAlarmDetected == null) {
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected(); mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected();
} }
setAlarmRecordingPreviewHolder(previewHolder);
} }


public void addValueToGesamtBE(float newValue) { public void addValueToGesamtBE(float newValue) {
public String getSystemTimeStamp() { public String getSystemTimeStamp() {
return mDeviceRepository.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
- 2
app/src/main/java/com/example/greenwatch/viewmodels/MainActivityViewModel.java View File

package com.example.greenwatch.viewmodels; package com.example.greenwatch.viewmodels;


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


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


import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.communication.WiFiCommunication; import com.example.greenwatch.communication.WiFiCommunication;
import com.example.greenwatch.models.Device; import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository; import com.example.greenwatch.repositories.DeviceRepository;
private MutableLiveData<List<Device>> mAlarmHistoryList; private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording; private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository; private DeviceRepository mDeviceRepository;
private AlarmRecorder mAlarmRecorder;
private static final int RIGHTS_REQUEST_CAMERA = 10; private static final int RIGHTS_REQUEST_CAMERA = 10;
private static final int REQUEST_PERMISSION = 200;


@Override @Override
public void init() {
public void init(SurfaceHolder previewHolder) {
WiFiCommunication mWiFiCommunication; WiFiCommunication mWiFiCommunication;
if(mDeviceList != null) { if(mDeviceList != null) {
return; return;
} }
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
//todo: check if WiFi instanz can be hold only by the repository //todo: check if WiFi instanz can be hold only by the repository
mDeviceRepository = DeviceRepository.getInstance(); mDeviceRepository = DeviceRepository.getInstance();
mWiFiCommunication = WiFiCommunication.getInstance(); mWiFiCommunication = WiFiCommunication.getInstance();
mDeviceRepository.setWiFiCommunication(mWiFiCommunication); mDeviceRepository.setWiFiCommunication(mWiFiCommunication);
mWiFiCommunication.setDeviceRepository(mDeviceRepository); mWiFiCommunication.setDeviceRepository(mDeviceRepository);
mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
//mDeviceRepository.createNewDevice(mDeviceRepository.getSystemTimeStamp(), mDeviceRepository.getLocalDeviceUUID(), false, "No Sensor selected", 0);
mDeviceList = mDeviceRepository.getConnectedDeviceList(); mDeviceList = mDeviceRepository.getConnectedDeviceList();
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList();
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording(); mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording();
setAlarmRecordingPreviewHolder(previewHolder);
} }


public boolean isCameraAccessAllowed(Context context) { public boolean isCameraAccessAllowed(Context context) {
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RIGHTS_REQUEST_CAMERA); ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.CAMERA}, RIGHTS_REQUEST_CAMERA);
} }


public boolean isAudioRecordingAccessAllowed(Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}

public void accessRequestAudioRecording(Activity activity) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSION);
}

public boolean isWriteExternalStorageAccessAllowed(Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}

public void accessRequestWriteExternalStorage(Activity activity) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
}

@Override @Override
public LiveData<List<Device>> getConnectedDeviceList() { public LiveData<List<Device>> getConnectedDeviceList() {
return mDeviceList; return mDeviceList;
public String getSystemTimeStamp() { public String getSystemTimeStamp() {
return null; return null;
} }

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

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

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

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



import java.util.List; import java.util.List;


public class VideodetectionAndAccelerometerViewModel extends ViewModel implements ViewModelInterface{
/*public class VideodetectionAndAccelerometerViewModel extends ViewModel implements ViewModelInterface{
private MutableLiveData<List<Device>> mDeviceList; private MutableLiveData<List<Device>> mDeviceList;
private MutableLiveData<Boolean> mAccelerometerAlarmDetected; private MutableLiveData<Boolean> mAccelerometerAlarmDetected;
private MutableLiveData<Boolean> mVideoAlarmDetected; private MutableLiveData<Boolean> mVideoAlarmDetected;
public String getSystemTimeStamp() { public String getSystemTimeStamp() {
return mDeviceRepository.getSystemTimeStamp(); return mDeviceRepository.getSystemTimeStamp();
} }
}
}*/

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

package com.example.greenwatch.viewmodels; package com.example.greenwatch.viewmodels;


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


import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;


import com.example.greenwatch.alarmrecorder.AlarmRecorder;
import com.example.greenwatch.models.Device; import com.example.greenwatch.models.Device;
import com.example.greenwatch.repositories.DeviceRepository; import com.example.greenwatch.repositories.DeviceRepository;
import com.example.greenwatch.sensors.CameraSensor; import com.example.greenwatch.sensors.CameraSensor;
private MutableLiveData<List<Device>> mAlarmHistoryList; private MutableLiveData<List<Device>> mAlarmHistoryList;
private MutableLiveData<Boolean> mStartAlarmRecording; private MutableLiveData<Boolean> mStartAlarmRecording;
private DeviceRepository mDeviceRepository; private DeviceRepository mDeviceRepository;
private AlarmRecorder mAlarmRecorder;
private CameraSensor mCameraSensor; private CameraSensor mCameraSensor;




} }


@Override @Override
public void init() {
public void init(SurfaceHolder previewHolder) {
if (mDeviceRepository == null) { if (mDeviceRepository == null) {
mDeviceRepository = DeviceRepository.getInstance(); mDeviceRepository = DeviceRepository.getInstance();
} }
if (mCameraSensor == null) { if (mCameraSensor == null) {
mCameraSensor = CameraSensor.getInstance(); mCameraSensor = CameraSensor.getInstance();
} }
if (mAlarmRecorder == null) {
mAlarmRecorder = AlarmRecorder.getInstance();
}
if (mDeviceList == null) { if (mDeviceList == null) {
mDeviceList = mDeviceRepository.getConnectedDeviceList(); mDeviceList = mDeviceRepository.getConnectedDeviceList();
} }
if (mVideoAlarmDetected == null) { if (mVideoAlarmDetected == null) {
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue(); mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
} }
setAlarmRecordingPreviewHolder(previewHolder);
} }


public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) { public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) {
public String getSystemTimeStamp() { public String getSystemTimeStamp() {
return mDeviceRepository.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

package com.example.greenwatch.viewmodels; package com.example.greenwatch.viewmodels;


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

import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;


import com.example.greenwatch.models.Device; import com.example.greenwatch.models.Device;
import java.util.List; import java.util.List;


public interface ViewModelInterface { public interface ViewModelInterface {
void init();
void init(SurfaceHolder previewHolder);
LiveData<List<Device>> getConnectedDeviceList(); LiveData<List<Device>> getConnectedDeviceList();
LiveData<List<Device>> getAlarmHistoryList(); LiveData<List<Device>> getAlarmHistoryList();
LiveData<Boolean> getStartAlarmRecording(); LiveData<Boolean> getStartAlarmRecording();
int getSensorMassage(String deviceID); int getSensorMassage(String deviceID);
String getLocalDeviceUUID(); String getLocalDeviceUUID();
String getSystemTimeStamp(); String getSystemTimeStamp();
void startAlarmRecording();
void stopAlarmRecording(Context context);
void setAlarmRecordingPreviewHolder(SurfaceHolder previewHolder);
} }

+ 7
- 0
app/src/main/res/layout/activity_accelerometer.xml View File

android:padding="10dp" android:padding="10dp"
tools:context=".AccelerometerActivity"> tools:context=".AccelerometerActivity">


<SurfaceView
android:id="@+id/surfaceViewAccelerometer"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="visible">
</SurfaceView>

<TextView <TextView
android:id="@+id/tvAccelerometerStatusmessage" android:id="@+id/tvAccelerometerStatusmessage"
android:layout_width="match_parent" android:layout_width="match_parent"

+ 8
- 2
app/src/main/res/layout/activity_main.xml View File

android:id="@+id/alarmHistoryListRecyclerView" android:id="@+id/alarmHistoryListRecyclerView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="3"
android:layout_weight="4"
tools:listitem="@layout/alarm_history_item"> tools:listitem="@layout/alarm_history_item">
</androidx.recyclerview.widget.RecyclerView> </androidx.recyclerview.widget.RecyclerView>


android:id="@+id/deviceListRecyclerView" android:id="@+id/deviceListRecyclerView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="3"
android:layout_weight="4"
tools:listitem="@layout/device_item"> tools:listitem="@layout/device_item">
</androidx.recyclerview.widget.RecyclerView> </androidx.recyclerview.widget.RecyclerView>

<SurfaceView
android:id="@+id/surfaceViewMainActivity"
android:layout_width="1dp"
android:layout_height="1dp">
</SurfaceView>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"

+ 7
- 0
app/src/main/res/layout/activity_videodetection.xml View File

android:padding="10dp" android:padding="10dp"
tools:context=".VideodetectionActivity"> tools:context=".VideodetectionActivity">


<SurfaceView
android:id="@+id/surfaceViewVideodetection"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="visible">
</SurfaceView>

<TextView <TextView
android:id="@+id/tvVideodetectionStatusmessage" android:id="@+id/tvVideodetectionStatusmessage"
android:layout_width="match_parent" android:layout_width="match_parent"

Loading…
Cancel
Save