startActivity(intent); | startActivity(intent); | ||||
} | } | ||||
public void openVideodetectionAndAccelerometerActivity(){ | public void openVideodetectionAndAccelerometerActivity(){ | ||||
//Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class); | |||||
//startActivity(intent); | |||||
mMainActivityViewModel.updateDevice(mMainActivityViewModel.getLocalDeviceUUID(), "24:00", false, "Video", 0); | |||||
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class); | |||||
startActivity(intent); | |||||
} | } | ||||
public void openConnectionActivity(){ | public void openConnectionActivity(){ | ||||
//Intent intent = new Intent(this, ConnectionActivity.class); | //Intent intent = new Intent(this, ConnectionActivity.class); |
package com.example.greenwatch; | package com.example.greenwatch; | ||||
import androidx.appcompat.app.AppCompatActivity; | 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.os.Bundle; | ||||
import android.view.View; | import android.view.View; | ||||
import android.widget.Button; | import android.widget.Button; | ||||
import android.widget.TextView; | |||||
import android.widget.Toast; | |||||
public class VideodetectionAndAccelerometerActivity extends AppCompatActivity { | |||||
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 videodetectionAndAccelerometerWarningTV; | |||||
private Button backToMainActivity; | private Button backToMainActivity; | ||||
private VideodetectionAndAccelerometerViewModel mVideodetectionAndAccelerometerViewModel; | |||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
setContentView(R.layout.activity_videodetection_and_accelerometer); | setContentView(R.layout.activity_videodetection_and_accelerometer); | ||||
videodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerStatusmessage); | |||||
videodetectionAndAccelerometerDataTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerData); | |||||
videodetectionAndAccelerometerWarningTV = (TextView) findViewById(R.id.tvvideodetectionAndAccelerometerWarning); | |||||
backToMainActivity = (Button) findViewById(R.id.videodetectionAndAccelerometerBackToMainActivity); | 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() { | backToMainActivity.setOnClickListener(new View.OnClickListener() { | ||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
finish(); | finish(); | ||||
} | } | ||||
}); | }); | ||||
mVideodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(VideodetectionAndAccelerometerViewModel.class); | |||||
mVideodetectionAndAccelerometerViewModel.init(); | |||||
mVideodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { | |||||
@Override | |||||
public void onChanged(List<Device> devices) { | |||||
deviceListAdapter.setDevices(devices); | |||||
} | |||||
}); | |||||
mVideodetectionAndAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() { | |||||
@Override | |||||
public void onChanged(List<Device> devices) { | |||||
alarmHistoryListAdapter.setAlarmHistoryList(devices); | |||||
} | |||||
}); | |||||
mVideodetectionAndAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() { | |||||
@Override | |||||
public void onChanged(Boolean aBoolean) { | |||||
if (aBoolean) { | |||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); | |||||
} | |||||
else { | |||||
Toast.makeText(VideodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); | |||||
} | |||||
} | |||||
}); | |||||
mVideodetectionAndAccelerometerViewModel.getVideodetectionAndAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() { | |||||
@Override | |||||
public void onChanged(Boolean aBoolean) { | |||||
if (aBoolean) { | |||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10); | |||||
} | |||||
else { | |||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "Accelerometer", 0); | |||||
} | |||||
} | |||||
}); | |||||
final ListenableFuture<ProcessCameraProvider> 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); | |||||
} | |||||
} | } | ||||
} | } |
package com.example.greenwatch.viewmodels; | |||||
import android.content.Context; | |||||
import androidx.camera.lifecycle.ProcessCameraProvider; | |||||
import androidx.lifecycle.LifecycleOwner; | |||||
import androidx.lifecycle.LiveData; | |||||
import androidx.lifecycle.MutableLiveData; | |||||
import androidx.lifecycle.Observer; | |||||
import androidx.lifecycle.ViewModel; | |||||
import com.example.greenwatch.models.Device; | |||||
import com.example.greenwatch.repositories.DeviceRepository; | |||||
import com.example.greenwatch.sensors.AccelerometerSensor; | |||||
import com.example.greenwatch.sensors.CameraSensor; | |||||
import java.util.List; | |||||
public class VideodetectionAndAccelerometerViewModel extends ViewModel implements ViewModelInterface{ | |||||
private MutableLiveData<List<Device>> mDeviceList; | |||||
private MutableLiveData<Boolean> mAccelerometerAlarmDetected; | |||||
private MutableLiveData<Boolean> mVideoAlarmDetected; | |||||
private MutableLiveData<Boolean> mVideodetectionAndAccelerometerAlarmDetected = new MutableLiveData<>(); | |||||
private MutableLiveData<List<Device>> mAlarmHistoryList; | |||||
private MutableLiveData<Boolean> mStartAlarmRecording; | |||||
private AccelerometerSensor mAccelerometerSensor; | |||||
private CameraSensor mCameraSensor; | |||||
private DeviceRepository mDeviceRepository; | |||||
private boolean VideodetectionAndAccelerometerAlarmDetected; | |||||
@Override | |||||
public void init() { | |||||
if (mDeviceRepository == null) { | |||||
mDeviceRepository = DeviceRepository.getInstance(); | |||||
} | |||||
if (mAccelerometerSensor == null) { | |||||
mAccelerometerSensor = AccelerometerSensor.getInstance(); | |||||
} | |||||
if (mCameraSensor == null) { | |||||
mCameraSensor = CameraSensor.getInstance(); | |||||
} | |||||
if (mDeviceList == null) { | |||||
mDeviceList = mDeviceRepository.getConnectedDeviceList(); | |||||
} | |||||
if (mAlarmHistoryList == null) { | |||||
mAlarmHistoryList = mDeviceRepository.getAlarmHistoryDeviceList(); | |||||
} | |||||
if (mStartAlarmRecording == null) { | |||||
mStartAlarmRecording = mDeviceRepository.getStartAlarmRecording(); | |||||
} | |||||
if (mAccelerometerAlarmDetected == null) { | |||||
mAccelerometerAlarmDetected = mAccelerometerSensor.getAccelerometerAlarmDetected(); | |||||
} | |||||
if (mVideoAlarmDetected == null) { | |||||
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue(); | |||||
} | |||||
VideodetectionAndAccelerometerAlarmDetected = false; | |||||
registerAlarmObserver(); | |||||
} | |||||
public void addValueToGesamtBE(float newValue) { | |||||
mAccelerometerSensor.addValueToGesamtBE(newValue); | |||||
} | |||||
public void meanValueCalculation() { | |||||
mAccelerometerSensor.meanValueCalculation(); | |||||
} | |||||
public void calibrateAccelerometerSensor() { | |||||
mAccelerometerSensor.calibrateAccelerometerSensor(); | |||||
} | |||||
public void checkAlarmCondition() { | |||||
mAccelerometerSensor.checkAlarmCondition(); | |||||
} | |||||
public void bindImageAnalysis(ProcessCameraProvider cameraProvider, LifecycleOwner lifecycleOwner, Context context) { | |||||
mCameraSensor.bindImageAnalysis(cameraProvider, lifecycleOwner, context); | |||||
} | |||||
public LiveData<Boolean> getVideodetectionAndAccelerometerAlarmDetected() { | |||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected(); | |||||
return mVideodetectionAndAccelerometerAlarmDetected; | |||||
} | |||||
private void setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected() { | |||||
mVideodetectionAndAccelerometerAlarmDetected.setValue(VideodetectionAndAccelerometerAlarmDetected); | |||||
} | |||||
Observer<Boolean> observer = new Observer<Boolean>() { | |||||
@Override | |||||
public void onChanged(Boolean aBoolean) { | |||||
if (mVideoAlarmDetected.getValue() || mAccelerometerAlarmDetected.getValue() && !VideodetectionAndAccelerometerAlarmDetected) { | |||||
VideodetectionAndAccelerometerAlarmDetected = true; | |||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected(); | |||||
} | |||||
else if (!mVideoAlarmDetected.getValue() && !mAccelerometerAlarmDetected.getValue() && VideodetectionAndAccelerometerAlarmDetected) { | |||||
VideodetectionAndAccelerometerAlarmDetected = false; | |||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected(); | |||||
} | |||||
} | |||||
}; | |||||
private void registerAlarmObserver() { | |||||
mVideoAlarmDetected.observeForever(observer); | |||||
mAccelerometerAlarmDetected.observeForever(observer); | |||||
} | |||||
@Override | |||||
protected void onCleared() { | |||||
super.onCleared(); | |||||
mVideoAlarmDetected.removeObserver(observer); | |||||
mAccelerometerAlarmDetected.removeObserver(observer); | |||||
} | |||||
@Override | |||||
public LiveData<List<Device>> getConnectedDeviceList() { | |||||
return mDeviceList; | |||||
} | |||||
@Override | |||||
public LiveData<List<Device>> getAlarmHistoryList() { | |||||
return mAlarmHistoryList; | |||||
} | |||||
@Override | |||||
public LiveData<Boolean> getStartAlarmRecording() { | |||||
return mStartAlarmRecording; | |||||
} | |||||
@Override | |||||
public void updateDevice(String deviceID, String timeStamp, boolean sensorStatus, String sensorType, int sensorMassage) { | |||||
mDeviceRepository.updateDevice(deviceID, timeStamp, sensorStatus, sensorType, sensorMassage); | |||||
} | |||||
@Override | |||||
public void setTimeStamp(String deviceID, String timeStamp) { | |||||
mDeviceRepository.setTimeStamp(deviceID, timeStamp); | |||||
} | |||||
@Override | |||||
public String getTimeStamp(String deviceID) { | |||||
return mDeviceRepository.getTimeStamp(deviceID); | |||||
} | |||||
@Override | |||||
public void setDeviceID(String deviceID, String newDeviceID) { | |||||
mDeviceRepository.setDeviceID(deviceID, newDeviceID); | |||||
} | |||||
@Override | |||||
public String getDeviceID(String deviceID) { | |||||
return mDeviceRepository.getDeviceID(deviceID); | |||||
} | |||||
@Override | |||||
public void setSensorStatus(String deviceID, boolean sensorStatus) { | |||||
mDeviceRepository.setSensorStatus(deviceID, sensorStatus); | |||||
} | |||||
@Override | |||||
public boolean getSensorStatus(String deviceID) { | |||||
return mDeviceRepository.getSensorStatus(deviceID); | |||||
} | |||||
@Override | |||||
public void setSensorType(String deviceID, String sensorType) { | |||||
mDeviceRepository.setSensorType(deviceID, sensorType); | |||||
} | |||||
@Override | |||||
public String getSensorType(String deviceID) { | |||||
return mDeviceRepository.getSensorType(deviceID); | |||||
} | |||||
@Override | |||||
public void setSensorMassage(String deviceID, int sensorMessage) { | |||||
mDeviceRepository.setSensorMassage(deviceID, sensorMessage); | |||||
} | |||||
@Override | |||||
public int getSensorMassage(String deviceID) { | |||||
return mDeviceRepository.getSensorMassage(deviceID); | |||||
} | |||||
@Override | |||||
public String getLocalDeviceUUID() { | |||||
return mDeviceRepository.getLocalDeviceUUID(); | |||||
} | |||||
@Override | |||||
public String getSystemTimeStamp() { | |||||
return mDeviceRepository.getSystemTimeStamp(); | |||||
} | |||||
} |
tools:context=".VideodetectionAndAccelerometerActivity"> | tools:context=".VideodetectionAndAccelerometerActivity"> | ||||
<TextView | <TextView | ||||
android:id="@+id/tvVideodetectionAndAccelerometerStatusmessage" | |||||
android:id="@+id/tvvideodetectionAndAccelerometerStatusmessage" | |||||
android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
android:text="Videodetection + Accelerometer Activity"> | |||||
android:text=""> | |||||
</TextView> | |||||
<TextView | |||||
android:id="@+id/tvvideodetectionAndAccelerometerData" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | |||||
android:text=""> | |||||
</TextView> | |||||
<TextView | |||||
android:id="@+id/tvvideodetectionAndAccelerometerWarning" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="wrap_content" | |||||
android:text=""> | |||||
</TextView> | </TextView> | ||||
<Button | <Button | ||||
android:text="Back to MainActivity"> | android:text="Back to MainActivity"> | ||||
</Button> | </Button> | ||||
<LinearLayout | |||||
android:layout_width="match_parent" | |||||
android:layout_height="match_parent" | |||||
android:orientation="vertical"> | |||||
<androidx.recyclerview.widget.RecyclerView | |||||
android:id="@+id/deviceListRecyclerView" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="match_parent" | |||||
tools:listitem="@layout/device_item" | |||||
android:layout_weight="1"> | |||||
</androidx.recyclerview.widget.RecyclerView> | |||||
<androidx.recyclerview.widget.RecyclerView | |||||
android:id="@+id/alarmHistoryListRecyclerView" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="match_parent" | |||||
tools:listitem="@layout/alarm_history_item" | |||||
android:layout_weight="1"> | |||||
</androidx.recyclerview.widget.RecyclerView> | |||||
</LinearLayout> | |||||
</LinearLayout> | </LinearLayout> |