From 5b78abe6ca8d61d8ad174c9c3f4c155063298e8b Mon Sep 17 00:00:00 2001 From: Patrick Halboth Date: Fri, 25 Jun 2021 11:10:39 +0200 Subject: [PATCH] vvv Detection Code vvv --- .../detection/EarthquakeLocation.java | 70 ---------------- .../de/edotzlaff/detection/MainActivity.java | 79 +++++++++++++++++-- 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/de/edotzlaff/detection/EarthquakeLocation.java b/app/src/main/java/de/edotzlaff/detection/EarthquakeLocation.java index 4e4923d..d3dc3aa 100644 --- a/app/src/main/java/de/edotzlaff/detection/EarthquakeLocation.java +++ b/app/src/main/java/de/edotzlaff/detection/EarthquakeLocation.java @@ -31,20 +31,6 @@ 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) { @@ -63,62 +49,6 @@ 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 ######################################################################### diff --git a/app/src/main/java/de/edotzlaff/detection/MainActivity.java b/app/src/main/java/de/edotzlaff/detection/MainActivity.java index 6e33c72..5f57d72 100644 --- a/app/src/main/java/de/edotzlaff/detection/MainActivity.java +++ b/app/src/main/java/de/edotzlaff/detection/MainActivity.java @@ -9,8 +9,13 @@ import android.Manifest; import android.app.Dialog; import android.content.Intent; import android.content.pm.PackageManager; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; import android.location.Location; import android.os.Bundle; +import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.widget.Button; @@ -50,6 +55,20 @@ public class MainActivity extends AppCompatActivity { private double laengengrad; private boolean useOwnGPS = true; private int indexID = 1; + //########################################################################################################################################################################## + //################################################################## 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) { @@ -69,14 +88,62 @@ public class MainActivity extends AppCompatActivity { } - //########################################################################################################################################################################## - //################################################################## vvv Detect Vibration vvv ############################################################################ - - //TODO Böbbi - - //################################################################## ^^^^ Detect Vibration ^^^^ ########################################################################## + //################################################################## 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(1000, 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 Get Location from Device vvv ####################################################################