Browse Source

vvv Detection Code vvv

EpiTest
Patrick Halboth 2 years ago
parent
commit
2b5b997192
1 changed files with 43 additions and 8 deletions
  1. 43
    8
      app/src/main/java/de/edotzlaff/detection/MainActivity.java

+ 43
- 8
app/src/main/java/de/edotzlaff/detection/MainActivity.java View File



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;
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;
//########################################################################################################################################################################## //##########################################################################################################################################################################
//################################################################## 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 ^^^^ ############################################################################
//########################################################################################################################################################################## //##########################################################################################################################################################################


} }


//Detection funktion here //Detection funktion here
sensorManagementInit();
getLocationPermission(); getLocationPermission();


//DB function here //DB function here


//################################################################## 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) {
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);
if (mShakeDetectionIsActive) {
Log.d(TAG,"mAccel: "+ mAccel);
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;
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 ^^^^ ###################################################################################

Loading…
Cancel
Save