@@ -1,13 +1,34 @@ | |||
package com.example.greenwatch; | |||
import androidx.appcompat.app.AppCompatActivity; | |||
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; | |||
public class AudiodetectionAndAccelerometerActivity 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.AudiodetectionAndAccelerometerViewModel; | |||
import com.example.greenwatch.viewmodels.VideodetectionAndAccelerometerViewModel; | |||
import java.util.List; | |||
public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity implements SensorEventListener { | |||
private SensorManager accelerometerManager; | |||
private Sensor accelerometerSensor; | |||
private TextView audiodetectionAndAccelerometerStatusMessage; | |||
private AudiodetectionAndAccelerometerViewModel mAudiodetectionAndAccelerometerViewModel; | |||
private Button backToMainActivity; | |||
@Override | |||
@@ -15,13 +36,126 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity { | |||
super.onCreate(savedInstanceState); | |||
setContentView(R.layout.activity_audiodetection_and_accelerometer); | |||
audiodetectionAndAccelerometerStatusMessage = (TextView) findViewById(R.id.tvAudiodetectionAndAccelerometerStatusmessage); | |||
backToMainActivity = (Button) findViewById(R.id.audiodetectionAndAccelerometerBackToMainActivity); | |||
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(); | |||
} | |||
}); | |||
mAudiodetectionAndAccelerometerViewModel = new ViewModelProvider(this).get(AudiodetectionAndAccelerometerViewModel.class); | |||
mAudiodetectionAndAccelerometerViewModel.init(); | |||
mAudiodetectionAndAccelerometerViewModel.getConnectedDeviceList().observe(this, new Observer<List<Device>>() { | |||
@Override | |||
public void onChanged(List<Device> devices) { | |||
deviceListAdapter.setDevices(devices); | |||
} | |||
}); | |||
mAudiodetectionAndAccelerometerViewModel.getAlarmHistoryList().observe(this, new Observer<List<Device>>() { | |||
@Override | |||
public void onChanged(List<Device> devices) { | |||
alarmHistoryListAdapter.setAlarmHistoryList(devices); | |||
} | |||
}); | |||
mAudiodetectionAndAccelerometerViewModel.getStartAlarmRecording().observe(this, new Observer<Boolean>() { | |||
@Override | |||
public void onChanged(Boolean aBoolean) { | |||
if (aBoolean) { | |||
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Start Alarm Recording", Toast.LENGTH_LONG).show(); | |||
} | |||
else { | |||
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Stop Alarm Recording", Toast.LENGTH_LONG).show(); | |||
} | |||
} | |||
}); | |||
mAudiodetectionAndAccelerometerViewModel.getAudiodetectionAndAccelerometerAlarmDetected().observe(this, new Observer<Boolean>() { | |||
@Override | |||
public void onChanged(Boolean aBoolean) { | |||
if (aBoolean) { | |||
if (mAudiodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) { | |||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10); | |||
} | |||
else if (mAudiodetectionAndAccelerometerViewModel.getMicrofonAlarmDetected()) { | |||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Microfon", (int) mAudiodetectionAndAccelerometerViewModel.getAmplitudeInDB()); | |||
} | |||
} | |||
else { | |||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "AudioAndAccelerometer", 0); | |||
} | |||
} | |||
}); | |||
accelerometerManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |||
if (accelerometerManager.getSensorList(Sensor.TYPE_GYROSCOPE).size() == 0) { | |||
accelerometerSensor = null; | |||
Toast.makeText(AudiodetectionAndAccelerometerActivity.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]); | |||
audiodetectionAndAccelerometerStatusMessage.setText(sb.toString()); | |||
mAudiodetectionAndAccelerometerViewModel.addValueToGesamtBE(event.values[0] + event.values[1] + event.values[2]); | |||
mAudiodetectionAndAccelerometerViewModel.meanValueCalculation(); | |||
mAudiodetectionAndAccelerometerViewModel.calibrateAccelerometerSensor(); | |||
mAudiodetectionAndAccelerometerViewModel.checkAlarmCondition(); | |||
} | |||
@Override | |||
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |||
} | |||
@Override | |||
protected void onResume() { | |||
super.onResume(); | |||
mAudiodetectionAndAccelerometerViewModel.startMicrofonSensor(); | |||
if (accelerometerSensor != null) { | |||
if (accelerometerManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME)) { | |||
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "We registered to the sensor", Toast.LENGTH_LONG).show(); | |||
} else { | |||
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "Registration did not work", Toast.LENGTH_LONG).show(); | |||
} | |||
} | |||
} | |||
@Override | |||
protected void onPause() { | |||
super.onPause(); | |||
mAudiodetectionAndAccelerometerViewModel.stopMicrofonSensor(); | |||
if (accelerometerSensor != null) { | |||
accelerometerManager.unregisterListener(this, accelerometerSensor); | |||
} | |||
} | |||
} |
@@ -117,10 +117,12 @@ public class MainActivity extends AppCompatActivity { | |||
audiodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() { | |||
@Override | |||
public void onClick(View v) { | |||
//openAudiodetectionAndAccelerometerActivity(); | |||
mMainActivityViewModel.setSensorType(mMainActivityViewModel.getLocalDeviceUUID(), "AudiodetectionAndAccelerometer"); | |||
mMainActivityViewModel.setTimeStamp(mMainActivityViewModel.getLocalDeviceUUID(), "00:00"); | |||
mMainActivityViewModel.setDeviceID(mMainActivityViewModel.getLocalDeviceUUID(), "6789"); | |||
if (mMainActivityViewModel.isMicrofonAccessAllowed(MainActivity.this)) { | |||
openAudiodetectionAndAccelerometerActivity(); | |||
} | |||
else { | |||
mMainActivityViewModel.accessRequestMicrofon(MainActivity.this); | |||
} | |||
} | |||
}); | |||
videodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() { |
@@ -0,0 +1,226 @@ | |||
package com.example.greenwatch.viewmodels; | |||
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.MicrofonSensor; | |||
import java.util.List; | |||
public class AudiodetectionAndAccelerometerViewModel extends ViewModel implements ViewModelInterface { | |||
private MutableLiveData<List<Device>> mDeviceList; | |||
private MutableLiveData<Boolean> mAccelerometerAlarmDetected; | |||
private MutableLiveData<Boolean> mMicrofonAlarmDetected; | |||
private MutableLiveData<Boolean> mAudiodetectionAndAccelerometerAlarmDetected = new MutableLiveData<>(); | |||
private MutableLiveData<List<Device>> mAlarmHistoryList; | |||
private MutableLiveData<Boolean> mStartAlarmRecording; | |||
private AccelerometerSensor mAccelerometerSensor; | |||
private MicrofonSensor mMicrofonSensor; | |||
private DeviceRepository mDeviceRepository; | |||
private boolean microfonAlarmDetected; | |||
private boolean accelerometerAlarmDetected; | |||
private boolean microfondetectionAndAccelerometerAlarmDetected; | |||
@Override | |||
public void init() { | |||
if (mDeviceRepository == null) { | |||
mDeviceRepository = DeviceRepository.getInstance(); | |||
} | |||
if (mAccelerometerSensor == null) { | |||
mAccelerometerSensor = AccelerometerSensor.getInstance(); | |||
} | |||
if (mMicrofonSensor == null) { | |||
mMicrofonSensor = MicrofonSensor.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 (mMicrofonAlarmDetected == null) { | |||
mMicrofonAlarmDetected = mMicrofonSensor.getAccelerometerAlarmDetected(); | |||
} | |||
microfondetectionAndAccelerometerAlarmDetected = false; | |||
accelerometerAlarmDetected = false; | |||
microfonAlarmDetected = 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 LiveData<Boolean> getAudiodetectionAndAccelerometerAlarmDetected() { | |||
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected(); | |||
return mAudiodetectionAndAccelerometerAlarmDetected; | |||
} | |||
public void startMicrofonSensor() { | |||
mMicrofonSensor.start(); | |||
} | |||
public void stopMicrofonSensor() { | |||
mMicrofonSensor.stop(); | |||
} | |||
public void recalibrationMicrofonSensor() { | |||
mMicrofonSensor.doRecalibration(); | |||
} | |||
public float getAmplitudeInDB() { | |||
return mMicrofonSensor.getAmplitudeInDB(); | |||
} | |||
private void setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected() { | |||
mAudiodetectionAndAccelerometerAlarmDetected.setValue(microfondetectionAndAccelerometerAlarmDetected); | |||
} | |||
private void setAccelerometerAlarmDetected(boolean accelerometerAlarmDetected) { | |||
this.accelerometerAlarmDetected = accelerometerAlarmDetected; | |||
} | |||
public boolean getAccelerometerAlarmDetected() { | |||
return accelerometerAlarmDetected; | |||
} | |||
private void setMicrofonAlarmDetected(boolean microfonAlarmDetected) { | |||
this.microfonAlarmDetected = microfonAlarmDetected; | |||
} | |||
public boolean getMicrofonAlarmDetected() { | |||
return microfonAlarmDetected; | |||
} | |||
Observer<Boolean> observer = new Observer<Boolean>() { | |||
@Override | |||
public void onChanged(Boolean aBoolean) { | |||
setAccelerometerAlarmDetected(mAccelerometerAlarmDetected.getValue()); | |||
setMicrofonAlarmDetected(mMicrofonAlarmDetected.getValue()); | |||
if (microfonAlarmDetected || accelerometerAlarmDetected && !microfondetectionAndAccelerometerAlarmDetected) { | |||
microfondetectionAndAccelerometerAlarmDetected = true; | |||
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected(); | |||
} | |||
else if (!microfonAlarmDetected && !accelerometerAlarmDetected && microfondetectionAndAccelerometerAlarmDetected) { | |||
microfondetectionAndAccelerometerAlarmDetected = false; | |||
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected(); | |||
} | |||
} | |||
}; | |||
private void registerAlarmObserver() { | |||
mMicrofonAlarmDetected.observeForever(observer); | |||
mAccelerometerAlarmDetected.observeForever(observer); | |||
} | |||
@Override | |||
protected void onCleared() { | |||
super.onCleared(); | |||
mMicrofonAlarmDetected.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(); | |||
} | |||
} |
@@ -22,4 +22,27 @@ | |||
android:text="Back to MainActivity"> | |||
</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> |