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

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