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 10KB

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