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.

VideodetectionActivity.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.os.Bundle;
  10. import android.view.SurfaceView;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import com.example.greenwatch.adapters.AlarmHistoryListAdapter;
  16. import com.example.greenwatch.adapters.DeviceListAdapter;
  17. import com.example.greenwatch.models.Device;
  18. import com.example.greenwatch.viewmodels.VideodetectionViewModel;
  19. import com.google.common.util.concurrent.ListenableFuture;
  20. import java.util.List;
  21. import java.util.concurrent.ExecutionException;
  22. public class VideodetectionActivity extends AppCompatActivity {
  23. private Button backToMainActivity;
  24. private SurfaceView surfaceView;
  25. private TextView tvVideodetectionDeviceListRecyclerView;
  26. private TextView tvVideodetectionAlarmHistoryListRecyclerView;
  27. private VideodetectionViewModel mVideoDetectionViewModel;
  28. private Permission permission = new Permission();
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_videodetection);
  33. surfaceView = (SurfaceView) findViewById(R.id.surfaceViewVideodetection);
  34. tvVideodetectionDeviceListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionDeviceListRecyclerView);
  35. tvVideodetectionAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAlarmHistoryListRecyclerView);
  36. backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity);
  37. RecyclerView recyclerView = findViewById(R.id.deviceListRecyclerView);
  38. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  39. recyclerView.setHasFixedSize(true);
  40. final DeviceListAdapter deviceListAdapter = new DeviceListAdapter();
  41. recyclerView.setAdapter(deviceListAdapter);
  42. RecyclerView alarmHistoryListRecyclerView = findViewById(R.id.alarmHistoryListRecyclerView);
  43. alarmHistoryListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  44. alarmHistoryListRecyclerView.setHasFixedSize(true);
  45. final AlarmHistoryListAdapter alarmHistoryListAdapter = new AlarmHistoryListAdapter();
  46. alarmHistoryListRecyclerView.setAdapter(alarmHistoryListAdapter);
  47. backToMainActivity.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View v) {
  50. finish();
  51. }
  52. });
  53. mVideoDetectionViewModel = new ViewModelProvider(this).get(VideodetectionViewModel.class);
  54. mVideoDetectionViewModel.init(surfaceView.getHolder());
  55. mVideoDetectionViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
  56. @Override
  57. public void onChanged(List<Device> devices) {
  58. deviceListAdapter.setDevices(devices);
  59. }
  60. });
  61. mVideoDetectionViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
  62. @Override
  63. public void onChanged(List<Device> devices) {
  64. alarmHistoryListAdapter.setAlarmHistoryList(devices);
  65. }
  66. });
  67. mVideoDetectionViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
  68. @Override
  69. public void onChanged(Boolean aBoolean) {
  70. if (aBoolean) {
  71. if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
  72. Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
  73. mVideoDetectionViewModel.startAlarmRecording();
  74. //todo AlarmHandling einfügen
  75. } else {
  76. permission.alarmRechteAnfordern(VideodetectionActivity.this);
  77. if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
  78. Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
  79. mVideoDetectionViewModel.startAlarmRecording();
  80. //todo Alarmhandling einfügen
  81. } else {
  82. Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
  83. mVideoDetectionViewModel.stopAlarmRecording(VideodetectionActivity.this);
  84. }
  85. }
  86. }
  87. else {
  88. Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
  89. }
  90. }
  91. });
  92. mVideoDetectionViewModel.getVideoAlarmDetectedValue().observe(this, new Observer<Boolean>() {
  93. @Override
  94. public void onChanged(Boolean aBoolean) {
  95. if (aBoolean) {
  96. mVideoDetectionViewModel.updateDevice(mVideoDetectionViewModel.getLocalDeviceUUID(), mVideoDetectionViewModel.getSystemTimeStamp(), true, "Video", 1.0f);
  97. }
  98. else {
  99. mVideoDetectionViewModel.updateDevice(mVideoDetectionViewModel.getLocalDeviceUUID(), mVideoDetectionViewModel.getSystemTimeStamp(), false, "Video", 0.0f);
  100. }
  101. }
  102. });
  103. final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
  104. cameraProviderFuture.addListener(() -> {
  105. try {
  106. mVideoDetectionViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this);
  107. } catch (ExecutionException | InterruptedException e) {
  108. e.printStackTrace();
  109. }
  110. }, ContextCompat.getMainExecutor(this));
  111. }
  112. }