123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package com.example.greenwatch;
-
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.camera.lifecycle.ProcessCameraProvider;
- import androidx.core.content.ContextCompat;
- import androidx.lifecycle.Observer;
- import androidx.lifecycle.ViewModelProvider;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
-
- import android.hardware.Sensor;
- 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;
- 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.VideodetectionAndAccelerometerViewModel;
- import com.example.greenwatch.viewmodels.VideodetectionViewModel;
- import com.google.common.util.concurrent.ListenableFuture;
-
- import java.util.List;
- 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;
- private TextView tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView;
- private Button backToMainActivity;
- private VideodetectionAndAccelerometerViewModel mVideodetectionAndAccelerometerViewModel;
- private Permission permission = new Permission();
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- 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);
- tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView);
- backToMainActivity = (Button) findViewById(R.id.videodetectionAndAccelerometerBackToMainActivity);
-
- RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- recyclerView.setHasFixedSize(true);
-
- final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
- recyclerView.setAdapter(deviceListAdapter);
-
- RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
- alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
- alarmHistoryListRecyclerView.setHasFixedSize(true);
-
- final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
- alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
-
- backToMainActivity.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
-
- mVideodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(VideodetectionAndAccelerometerViewModel.class);
- mVideodetectionAndAccelerometerViewModel.init(surfaceView.getHolder());
- mVideodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
- @Override
- public void onChanged(List<Device> devices) {
- deviceListAdapter.setDevices(devices);
- }
- });
-
- mVideodetectionAndAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
- @Override
- public void onChanged(List<Device> devices) {
- alarmHistoryListAdapter.setAlarmHistoryList(devices);
- }
- });
-
- mVideodetectionAndAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
- @Override
- public void onChanged(Boolean aBoolean) {
- 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);
- }
- }
- });
-
- mVideodetectionAndAccelerometerViewModel.getVideodetectionAndAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() {
- @Override
- public void onChanged(Boolean aBoolean) {
- if (aBoolean) {
- if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) {
- mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", mVideodetectionAndAccelerometerViewModel.getAccelerometerSensorMeanValue());
- }
- else if (mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) {
- mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Video", 1.0f);
- }
- else if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) {
- mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "VideoAndAccelerometer", mVideodetectionAndAccelerometerViewModel.getAccelerometerSensorMeanValue());
- }
- }
- else {
- mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "VideoAndAccelerometer", 0.0f);
- }
- }
- });
-
- final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
- cameraProviderFuture.addListener(() -> {
- try {
- mVideodetectionAndAccelerometerViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this);
- } catch (ExecutionException | InterruptedException e) {
- e.printStackTrace();
- }
- }, ContextCompat.getMainExecutor(this));
-
- accelerometerManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- if (accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) {
- accelerometerSensor = null;
- Toast.makeText(VideodetectionAndAccelerometerActivity.this, "No accelerometer sensor available", Toast.LENGTH_LONG).show();
- }
- else {
- accelerometerSensor = accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
- }
- }
-
- @Override
- public void onSensorChanged(SensorEvent event) {
- StringBuilder sb = new StringBuilder();
- sb.append("x=")
- .append(event.values[0])
- .append("\ny=")
- .append(event.values[1])
- .append("\nz=")
- .append(event.values[2]);
- videodetectionAndAccelerometerDataTV.setText(sb.toString());
- mVideodetectionAndAccelerometerViewModel.addValueToGesamtBE(event.values[0] + event.values[1] + event.values[2]);
- mVideodetectionAndAccelerometerViewModel.meanValueCalculation();
- mVideodetectionAndAccelerometerViewModel.calibrateAccelerometerSensor();
- mVideodetectionAndAccelerometerViewModel.checkAlarmCondition();
- }
-
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
-
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (accelerometerSensor != null) {
- if (accelerometerManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME)) {
- Toast.makeText(VideodetectionAndAccelerometerActivity.this, "We registered to the sensor", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Registration did not work", Toast.LENGTH_LONG).show();
- }
- }
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- if (accelerometerSensor != null) {
- accelerometerManager.unregisterListener(this, accelerometerSensor);
- }
- }
- }
|