You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideodetectionAndAccelerometerActivity.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.example.greenwatch;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.camera.lifecycle.ProcessCameraProvider;
  4. import androidx.core.content.ContextCompat;
  5. import androidx.lifecycle.Observer;
  6. import androidx.lifecycle.ViewModelProvider;
  7. import androidx.recyclerview.widget.LinearLayoutManager;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import android.hardware.Sensor;
  10. import android.hardware.SensorEvent;
  11. import android.hardware.SensorEventListener;
  12. import android.hardware.SensorManager;
  13. import android.os.Bundle;
  14. import android.view.SurfaceView;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19. import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
  20. import com.example.greenwatch.adapters.DeviceListAdapter;
  21. import com.example.greenwatch.models.Device;
  22. import com.example.greenwatch.viewmodels.AccelerometerViewModel;
  23. import com.example.greenwatch.viewmodels.VideodetectionAndAccelerometerViewModel;
  24. import com.example.greenwatch.viewmodels.VideodetectionViewModel;
  25. import com.google.common.util.concurrent.ListenableFuture;
  26. import java.util.List;
  27. import java.util.concurrent.ExecutionException;
  28. public class VideodetectionAndAccelerometerActivity extends AppCompatActivity implements SensorEventListener {
  29. private SensorManager accelerometerManager;
  30. private Sensor accelerometerSensor;
  31. private SurfaceView surfaceView;
  32. private TextView videodetectionAndAccelerometerStatusMessage;
  33. private TextView videodetectionAndAccelerometerDataTV;
  34. private TextView tvVideodetectionAndAccelerometerDeviceListRecyclerView;
  35. private TextView tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView;
  36. private Button backToMainActivity;
  37. private VideodetectionAndAccelerometerViewModel mVideodetectionAndAccelerometerViewModel;
  38. private Permission permission = new Permission();
  39. @Override
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.activity_videodetection_and_accelerometer);
  43. surfaceView = (SurfaceView) findViewById(R.id.surfaceViewVideodetectionAndAccelerometer);
  44. videodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerStatusmessage);
  45. videodetectionAndAccelerometerDataTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerData);
  46. tvVideodetectionAndAccelerometerDeviceListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAndAccelerometerDeviceListRecyclerView);
  47. tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView);
  48. backToMainActivity = (Button) findViewById(R.id.videodetectionAndAccelerometerBackToMainActivity);
  49. RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
  50. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  51. recyclerView.setHasFixedSize(true);
  52. final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
  53. recyclerView.setAdapter(deviceListAdapter);
  54. RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
  55. alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  56. alarmHistoryListRecyclerView.setHasFixedSize(true);
  57. final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
  58. alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
  59. backToMainActivity.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. finish();
  63. }
  64. });
  65. mVideodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(VideodetectionAndAccelerometerViewModel.class);
  66. mVideodetectionAndAccelerometerViewModel.init(surfaceView.getHolder());
  67. mVideodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
  68. @Override
  69. public void onChanged(List<Device> devices) {
  70. deviceListAdapter.setDevices(devices);
  71. }
  72. });
  73. mVideodetectionAndAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
  74. @Override
  75. public void onChanged(List<Device> devices) {
  76. alarmHistoryListAdapter.setAlarmHistoryList(devices);
  77. }
  78. });
  79. mVideodetectionAndAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
  80. @Override
  81. public void onChanged(Boolean aBoolean) {
  82. if (aBoolean) {
  83. if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){
  84. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
  85. mVideodetectionAndAccelerometerViewModel.startAlarmRecording();
  86. //todo AlarmHandling einfügen
  87. } else {
  88. permission.alarmRechteAnfordern(VideodetectionAndAccelerometerActivity.this);
  89. if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){
  90. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
  91. mVideodetectionAndAccelerometerViewModel.startAlarmRecording();
  92. //todo Alarmhandling einfügen
  93. } else {
  94. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
  95. mVideodetectionAndAccelerometerViewModel.stopAlarmRecording(VideodetectionAndAccelerometerActivity.this);
  96. }
  97. }
  98. }
  99. else {
  100. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
  101. mVideodetectionAndAccelerometerViewModel.stopAlarmRecording(VideodetectionAndAccelerometerActivity.this);
  102. }
  103. }
  104. });
  105. mVideodetectionAndAccelerometerViewModel.getVideodetectionAndAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() {
  106. @Override
  107. public void onChanged(Boolean aBoolean) {
  108. if (aBoolean) {
  109. if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) {
  110. mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", mVideodetectionAndAccelerometerViewModel.getAccelerometerSensorMeanValue());
  111. }
  112. else if (mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) {
  113. mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Video", 1.0f);
  114. }
  115. else if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) {
  116. mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "VideoAndAccelerometer", mVideodetectionAndAccelerometerViewModel.getAccelerometerSensorMeanValue());
  117. }
  118. }
  119. else {
  120. mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "VideoAndAccelerometer", 0.0f);
  121. }
  122. }
  123. });
  124. final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
  125. cameraProviderFuture.addListener(() -> {
  126. try {
  127. mVideodetectionAndAccelerometerViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this);
  128. } catch (ExecutionException | InterruptedException e) {
  129. e.printStackTrace();
  130. }
  131. }, ContextCompat.getMainExecutor(this));
  132. accelerometerManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  133. if (accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) {
  134. accelerometerSensor = null;
  135. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "No accelerometer sensor available", Toast.LENGTH_LONG).show();
  136. }
  137. else {
  138. accelerometerSensor = accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);
  139. }
  140. }
  141. @Override
  142. public void onSensorChanged(SensorEvent event) {
  143. StringBuilder sb = new StringBuilder();
  144. sb.append("x=")
  145. .append(event.values[0])
  146. .append("\ny=")
  147. .append(event.values[1])
  148. .append("\nz=")
  149. .append(event.values[2]);
  150. videodetectionAndAccelerometerDataTV.setText(sb.toString());
  151. mVideodetectionAndAccelerometerViewModel.addValueToGesamtBE(event.values[0] + event.values[1] + event.values[2]);
  152. mVideodetectionAndAccelerometerViewModel.meanValueCalculation();
  153. mVideodetectionAndAccelerometerViewModel.calibrateAccelerometerSensor();
  154. mVideodetectionAndAccelerometerViewModel.checkAlarmCondition();
  155. }
  156. @Override
  157. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  158. }
  159. @Override
  160. protected void onResume() {
  161. super.onResume();
  162. if (accelerometerSensor != null) {
  163. if (accelerometerManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME)) {
  164. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "We registered to the sensor", Toast.LENGTH_LONG).show();
  165. } else {
  166. Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Registration did not work", Toast.LENGTH_LONG).show();
  167. }
  168. }
  169. }
  170. @Override
  171. protected void onPause() {
  172. super.onPause();
  173. if (accelerometerSensor != null) {
  174. accelerometerManager.unregisterListener(this, accelerometerSensor);
  175. }
  176. }
  177. }