diff --git a/app/src/main/java/de/edotzlaff/detection/MainActivity.java b/app/src/main/java/de/edotzlaff/detection/MainActivity.java index 5f57d72..81bd4cf 100644 --- a/app/src/main/java/de/edotzlaff/detection/MainActivity.java +++ b/app/src/main/java/de/edotzlaff/detection/MainActivity.java @@ -7,6 +7,7 @@ import androidx.core.content.ContextCompat; import android.Manifest; import android.app.Dialog; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.hardware.Sensor; @@ -36,10 +37,12 @@ import com.google.android.gms.tasks.Task; import org.w3c.dom.Text; import java.util.Calendar; +import java.util.Objects; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; + private static final int ERROR_DIALOG_REQUEST = 9001; private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; @@ -58,15 +61,16 @@ public class MainActivity extends AppCompatActivity { //########################################################################################################################################################################## //################################################################## vvv SensorParameter vvv ############################################################################## 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 long mShakeDetectionLockTimeMicroSeconds = 10000; + private static final long mShakeDetectionLockTimeMicroSeconds = 1000; private float mAccel; private float mAccelCurrent; private float mAccelLast; private boolean mShakeDetectionIsActive = false; private boolean mShakeDetected = false; private boolean allowShakeEvent = true; + //################################################################## ^^^^ SensorParameter ^^^^ ############################################################################ //########################################################################################################################################################################## @@ -80,7 +84,7 @@ public class MainActivity extends AppCompatActivity { } //Detection funktion here - + sensorManagementInit(); getLocationPermission(); //DB function here @@ -90,7 +94,17 @@ public class MainActivity extends AppCompatActivity { //################################################################## 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() { @Override 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)); float delta = mAccelCurrent - mAccelLast; mAccel = mAccel * 0.9f + delta; - // Log.d(TAG,"mAccel: "+ mAccel); - if (mShakeDetectionIsActive) { + Log.d(TAG,"mAccel: "+ mAccel); + if (mShakeDetectionIsActive && !mShakeDetected) { if(Math.abs(mAccel) > mUpperThreshold) { - new CountDownTimer(1000, 10) { + new CountDownTimer(500, 10) { public void onTick(long millisUntilFinished) { - if (Math.abs(mAccel) > mUpperThreshold) { + mShakeDetectionIsActive = false; + /* 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; @@ -139,7 +156,25 @@ public class MainActivity extends AppCompatActivity { private void earthquakeDetected() { Toast.makeText(getApplicationContext(), "!Earthquake detected!", Toast.LENGTH_SHORT).show(); + Log.d(TAG,"!Earthquake detected!"); //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 ^^^^ ###################################################################################