vvv Detection Code vvv

This commit is contained in:
2021-06-25 11:00:13 +02:00
parent 812c1d7dfa
commit cfb0b0c612
4 changed files with 87 additions and 5 deletions
@@ -1,11 +1,17 @@
package de.edotzlaff.detection;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.Log;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
@@ -25,6 +31,20 @@ public class EarthquakeLocation extends FragmentActivity implements OnMapReadyCa
private GoogleMap mMap;
private static final String TAGEPIZENTRUM = "Epizentrum";
//##########################################################################################################################################################################
//################################################################## vvv SensorParameter vvv ##############################################################################
private SensorManager mSensorManager;
private static final float mUpperThreshold = 1.5f; // für Emulator auf 1.5 setzen
private static final float mLowerThreshold = 0.5f; // für Emulator auf 0.5 setzen
private static final long mShakeDetectionLockTimeMicroSeconds = 10000;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast;
private boolean mShakeDetectionIsActive = false;
private boolean mShakeDetected = false;
private boolean allowShakeEvent = true;
//################################################################## ^^^^ SensorParameter ^^^^ ############################################################################
//##########################################################################################################################################################################
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -43,8 +63,62 @@ public class EarthquakeLocation extends FragmentActivity implements OnMapReadyCa
//################################################################## ^^^^ DB Code ^^^^ ###################################################################################
//##########################################################################################################################################################################
//################################################################## vvv Detection Code vvv ###################################################################################
//##########################################################################################################################################################################
private final SensorEventListener mSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// Log.d(TAG,"mAccel: "+ mAccel);
if (mShakeDetectionIsActive) {
if(Math.abs(mAccel) > mUpperThreshold) {
new CountDownTimer(1500, 10) {
public void onTick(long millisUntilFinished) {
if (Math.abs(mAccel) > mUpperThreshold) {
mShakeDetectionIsActive = false;
} else if (Math.abs(mAccel) < mLowerThreshold) {
mShakeDetectionIsActive = true;
this.cancel();
}
}
public void onFinish() {
if (Math.abs(mAccel) > mUpperThreshold) {
mShakeDetectionIsActive = false;
mShakeDetected = true;
earthquakeDetected();
} else {
mShakeDetectionIsActive = true;
mShakeDetected = false;
}
}
}.start();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private void earthquakeDetected()
{
Toast.makeText(getApplicationContext(), "!Earthquake detected!", Toast.LENGTH_SHORT).show();
//setDataBaseValues();
}
//################################################################## ^^^^ Detection Code ^^^^ ###################################################################################
//##########################################################################################################################################################################
//##########################################################################################################################################################################
//################################################################## vvv Calculate Epicenter vvv #########################################################################
@@ -52,7 +126,7 @@ public class EarthquakeLocation extends FragmentActivity implements OnMapReadyCa
//TODO Jan
@RequiresApi(api = Build.VERSION_CODES.O)
public Epizentrum ermittleEpizentrum(DataSnapshot data)
/* public Epizentrum ermittleEpizentrum(DataSnapshot data)
{
Erdbeben erdbeben = new Erdbeben();
erdbeben.addDetektionssignalList(mapToDetektionssignale(data));
@@ -71,8 +145,8 @@ public class EarthquakeLocation extends FragmentActivity implements OnMapReadyCa
return epizentrum;
}
public ArrayList<Detektionssignal> mapToDetektionssignale(DataSnapshot data)
*/
/* public ArrayList<Detektionssignal> mapToDetektionssignale(DataSnapshot data)
{
ArrayList<Detektionssignal> detektionssignale = new ArrayList<>();
for(data.child("overviewnodes").getChildernKey)
@@ -90,6 +164,8 @@ public class EarthquakeLocation extends FragmentActivity implements OnMapReadyCa
return detektionssignale;
}
*/
//################################################################## ^^^^ Calculate Epicenter ^^^^ #######################################################################
//##########################################################################################################################################################################