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.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; 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.AccelerometerViewModel; import com.example.greenwatch.viewmodels.VideodetectionAndAccelerometerViewModel; import com.example.greenwatch.viewmodels.VideodetectionViewModel; import com.google.common.util.concurrent.ListenableFuture; import java.util.List; import java.util.concurrent.ExecutionException; public class VideodetectionAndAccelerometerActivity extends AppCompatActivity implements SensorEventListener { private SensorManager accelerometerManager; private Sensor accelerometerSensor; private TextView videodetectionAndAccelerometerStatusMessage; private TextView videodetectionAndAccelerometerDataTV; private TextView tvVideodetectionAndAccelerometerDeviceListRecyclerView; private TextView tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView; private Button backToMainActivity; private VideodetectionAndAccelerometerViewModel mVideodetectionAndAccelerometerViewModel; private Permission permission = new Permission(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_videodetection_and_accelerometer); videodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerStatusmessage); videodetectionAndAccelerometerDataTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerData); tvVideodetectionAndAccelerometerDeviceListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAndAccelerometerDeviceListRecyclerView); tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView = (TextView) findViewById(R.id.tvVideodetectionAndAccelerometerAlarmHistoryListRecyclerView); backToMainActivity = (Button) findViewById(R.id.videodetectionAndAccelerometerBackToMainActivity); 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(); } }); mVideodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(VideodetectionAndAccelerometerViewModel.class); mVideodetectionAndAccelerometerViewModel.init(); mVideodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer>() { @Override public void onChanged(List devices) { deviceListAdapter.setDevices(devices); } }); mVideodetectionAndAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer>() { @Override public void onChanged(List devices) { alarmHistoryListAdapter.setAlarmHistoryList(devices); } }); mVideodetectionAndAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer() { @Override public void onChanged(Boolean aBoolean) { if (aBoolean) { if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){ Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); //todo AlarmHandling einfügen } else { permission.alarmRechteAnfordern(VideodetectionAndAccelerometerActivity.this); if(permission.alarmRechtePruefen(VideodetectionAndAccelerometerActivity.this, VideodetectionAndAccelerometerActivity.this)){ Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); //todo Alarmhandling einfügen } else { Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); } } } else { Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); } } }); mVideodetectionAndAccelerometerViewModel.getVideodetectionAndAccelerometerAlarmDetected().observe(this, new Observer() { @Override public void onChanged(Boolean aBoolean) { if (aBoolean) { if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) { mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", mVideodetectionAndAccelerometerViewModel.getAccelerometerSensorMeanValue()); } else if (mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) { mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Video", 1.0f); } else if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) { mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "VideoAndAccelerometer", mVideodetectionAndAccelerometerViewModel.getAccelerometerSensorMeanValue()); } } else { mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "VideoAndAccelerometer", 0.0f); } } }); final ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { mVideodetectionAndAccelerometerViewModel.bindImageAnalysis(cameraProviderFuture.get(), this, this); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } }, ContextCompat.getMainExecutor(this)); accelerometerManager = (SensorManager) getSystemService(SENSOR_SERVICE); if (accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) { accelerometerSensor = null; Toast.makeText(VideodetectionAndAccelerometerActivity.this, "No accelerometer sensor available", Toast.LENGTH_LONG).show(); } else { accelerometerSensor = accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0); } } @Override public void onSensorChanged(SensorEvent event) { StringBuilder sb = new StringBuilder(); sb.append("x=") .append(event.values[0]) .append("\ny=") .append(event.values[1]) .append("\nz=") .append(event.values[2]); videodetectionAndAccelerometerDataTV.setText(sb.toString()); mVideodetectionAndAccelerometerViewModel.addValueToGesamtBE(event.values[0] + event.values[1] + event.values[2]); mVideodetectionAndAccelerometerViewModel.meanValueCalculation(); mVideodetectionAndAccelerometerViewModel.calibrateAccelerometerSensor(); mVideodetectionAndAccelerometerViewModel.checkAlarmCondition(); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override protected void onResume() { super.onResume(); if (accelerometerSensor != null) { if (accelerometerManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME)) { Toast.makeText(VideodetectionAndAccelerometerActivity.this, "We registered to the sensor", Toast.LENGTH_LONG).show(); } else { Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Registration did not work", Toast.LENGTH_LONG).show(); } } } @Override protected void onPause() { super.onPause(); if (accelerometerSensor != null) { accelerometerManager.unregisterListener(this, accelerometerSensor); } } }