vvv Detection Code vvv

This commit is contained in:
Patrick Halboth 2021-06-25 11:56:47 +02:00
parent 5b78abe6ca
commit 2b5b997192

View File

@ -7,6 +7,7 @@ import androidx.core.content.ContextCompat;
import android.Manifest; import android.Manifest;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.hardware.Sensor; import android.hardware.Sensor;
@ -36,10 +37,12 @@ import com.google.android.gms.tasks.Task;
import org.w3c.dom.Text; import org.w3c.dom.Text;
import java.util.Calendar; import java.util.Calendar;
import java.util.Objects;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity"; private static final String TAG = "MainActivity";
private static final int ERROR_DIALOG_REQUEST = 9001; private static final int ERROR_DIALOG_REQUEST = 9001;
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
@ -58,15 +61,16 @@ public class MainActivity extends AppCompatActivity {
//########################################################################################################################################################################## //##########################################################################################################################################################################
//################################################################## vvv SensorParameter vvv ############################################################################## //################################################################## vvv SensorParameter vvv ##############################################################################
private SensorManager mSensorManager; private SensorManager mSensorManager;
private static final float mUpperThreshold = 1.5f; // für Emulator auf 1.5 setzen private static final float mUpperThreshold = 1.0f; // für Emulator auf 1.5 setzen
private static final float mLowerThreshold = 0.5f; // für Emulator auf 0.5 setzen private static final float mLowerThreshold = 0.5f; // für Emulator auf 0.5 setzen
private static final long mShakeDetectionLockTimeMicroSeconds = 10000; private static final long mShakeDetectionLockTimeMicroSeconds = 1000;
private float mAccel; private float mAccel;
private float mAccelCurrent; private float mAccelCurrent;
private float mAccelLast; private float mAccelLast;
private boolean mShakeDetectionIsActive = false; private boolean mShakeDetectionIsActive = false;
private boolean mShakeDetected = false; private boolean mShakeDetected = false;
private boolean allowShakeEvent = true; private boolean allowShakeEvent = true;
//################################################################## ^^^^ SensorParameter ^^^^ ############################################################################ //################################################################## ^^^^ SensorParameter ^^^^ ############################################################################
//########################################################################################################################################################################## //##########################################################################################################################################################################
@ -80,7 +84,7 @@ public class MainActivity extends AppCompatActivity {
} }
//Detection funktion here //Detection funktion here
sensorManagementInit();
getLocationPermission(); getLocationPermission();
//DB function here //DB function here
@ -90,7 +94,17 @@ public class MainActivity extends AppCompatActivity {
//################################################################## vvv Detection Code vvv ################################################################################### //################################################################## vvv Detection Code vvv ###################################################################################
//########################################################################################################################################################################## //##########################################################################################################################################################################
private void sensorManagementInit()
{
//TextView txtEarthquake = (TextView) findViewById(R.id.txtEarthquake);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Objects.requireNonNull(mSensorManager).registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mAccel = 1f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;
mShakeDetectionIsActive = true;
mShakeDetected = false;
}
private final SensorEventListener mSensorListener = new SensorEventListener() { private final SensorEventListener mSensorListener = new SensorEventListener() {
@Override @Override
public void onSensorChanged(SensorEvent event) { public void onSensorChanged(SensorEvent event) {
@ -101,20 +115,23 @@ public class MainActivity extends AppCompatActivity {
mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z)); mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
float delta = mAccelCurrent - mAccelLast; float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta; mAccel = mAccel * 0.9f + delta;
// Log.d(TAG,"mAccel: "+ mAccel); Log.d(TAG,"mAccel: "+ mAccel);
if (mShakeDetectionIsActive) { if (mShakeDetectionIsActive && !mShakeDetected) {
if(Math.abs(mAccel) > mUpperThreshold) { if(Math.abs(mAccel) > mUpperThreshold) {
new CountDownTimer(1000, 10) { new CountDownTimer(500, 10) {
public void onTick(long millisUntilFinished) { public void onTick(long millisUntilFinished) {
if (Math.abs(mAccel) > mUpperThreshold) { mShakeDetectionIsActive = false;
/* if (Math.abs(mAccel) > mUpperThreshold) {
mShakeDetectionIsActive = false; mShakeDetectionIsActive = false;
} else if (Math.abs(mAccel) < mLowerThreshold) { } else if (Math.abs(mAccel) < mLowerThreshold) {
mShakeDetectionIsActive = true; mShakeDetectionIsActive = true;
this.cancel(); this.cancel();
} }
*/
} }
public void onFinish() { public void onFinish() {
if (Math.abs(mAccel) > mUpperThreshold) { if (Math.abs(mAccel) > mUpperThreshold) {
mShakeDetectionIsActive = false; mShakeDetectionIsActive = false;
@ -139,7 +156,25 @@ public class MainActivity extends AppCompatActivity {
private void earthquakeDetected() private void earthquakeDetected()
{ {
Toast.makeText(getApplicationContext(), "!Earthquake detected!", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "!Earthquake detected!", Toast.LENGTH_SHORT).show();
Log.d(TAG,"!Earthquake detected!");
//setDataBaseValues(); //setDataBaseValues();
new CountDownTimer(mShakeDetectionLockTimeMicroSeconds, 100) {
public void onTick(long millisUntilFinished) {
Log.d(TAG,"Earthquake: "+ millisUntilFinished + " ms");
}
public void onFinish() {
Log.d(TAG,"Earthquake Detection is Unlocked: ");
mShakeDetectionIsActive = true;
mShakeDetected = false;
}
}.start();
} }
//################################################################## ^^^^ Detection Code ^^^^ ################################################################################### //################################################################## ^^^^ Detection Code ^^^^ ###################################################################################