123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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.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.VideodetectionViewModel;
- import com.google.common.util.concurrent.ListenableFuture;
-
- import java.util.List;
- import java.util.concurrent.ExecutionException;
-
- public class VideodetectionActivity extends AppCompatActivity {
-
- private Button backToMainActivity;
- private SurfaceView surfaceView;
- private TextView tvVideodetectionDeviceListRecyclerView;
- private TextView tvVideodetectionAlarmHistoryListRecyclerView;
- private VideodetectionViewModel mVideoDetectionViewModel;
- private Permission permission = new Permission();
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_videodetection);
-
- surfaceView = (SurfaceView) findViewById(R.id.surfaceViewVideodetection);
- tvVideodetectionDeviceListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionDeviceListRecyclerView);
- tvVideodetectionAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAlarmHistoryListRecyclerView);
- backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity);
-
- 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();
- }
- });
-
- mVideoDetectionViewModel = new ViewModelProvider(this).get(VideodetectionViewModel.class);
- mVideoDetectionViewModel.init(surfaceView.getHolder());
- mVideoDetectionViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() {
- @Override
- public void onChanged(List<Device> devices) {
- deviceListAdapter.setDevices(devices);
- }
- });
-
- mVideoDetectionViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() {
- @Override
- public void onChanged(List<Device> devices) {
- alarmHistoryListAdapter.setAlarmHistoryList(devices);
- }
- });
-
- mVideoDetectionViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() {
- @Override
- public void onChanged(Boolean aBoolean) {
- if (aBoolean) {
- if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
- Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
- mVideoDetectionViewModel.startAlarmRecording();
- //todo AlarmHandling einfügen
- } else {
- permission.alarmRechteAnfordern(VideodetectionActivity.this);
- if(permission.alarmRechtePruefen(VideodetectionActivity.this, VideodetectionActivity.this)){
- Toast.makeText(VideodetectionActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show();
- mVideoDetectionViewModel.startAlarmRecording();
- //todo Alarmhandling einfügen
- } else {
- Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
- mVideoDetectionViewModel.stopAlarmRecording(VideodetectionActivity.this);
- }
- }
- }
- else {
- Toast.makeText(VideodetectionActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show();
- }
- }
- });
-
- mVideoDetectionViewModel.getVideoAlarmDetectedValue().observe(this, new Observer<Boolean>() {
- @Override
- public void onChanged(Boolean aBoolean) {
- if (aBoolean) {
- mVideoDetectionViewModel.updateDevice(mVideoDetectionViewModel.getLocalDeviceUUID(), mVideoDetectionViewModel.getSystemTimeStamp(), true, "Video", 1.0f);
- }
- else {
- mVideoDetectionViewModel.updateDevice(mVideoDetectionViewModel.getLocalDeviceUUID(), mVideoDetectionViewModel.getSystemTimeStamp(), false, "Video", 0.0f);
- }
-
- }
- });
-
- final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
- cameraProviderFuture.addListener(() -> {
- try {
- mVideoDetectionViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this);
- } catch (ExecutionException | InterruptedException e) {
- e.printStackTrace();
- }
- }, ContextCompat.getMainExecutor(this));
- }
- }
|