|
|
@@ -1,13 +1,22 @@ |
|
|
|
package de.edotzlaff.schockwelle; |
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.pm.PackageManager; |
|
|
|
import android.location.Location; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.CountDownTimer; |
|
|
|
import android.os.SystemClock; |
|
|
|
import android.util.Log; |
|
|
|
import android.widget.Button; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import android.hardware.Sensor; |
|
|
|
import android.hardware.SensorEvent; |
|
|
|
import android.hardware.SensorEventListener; |
|
|
|
import android.hardware.SensorManager; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.core.app.ActivityCompat; |
|
|
|
import androidx.core.content.ContextCompat; |
|
|
@@ -30,6 +39,7 @@ import com.google.firebase.database.FirebaseDatabase; |
|
|
|
import com.google.firebase.database.ValueEventListener; |
|
|
|
import java.util.Calendar; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
public class EarthquakeMapsActivity extends FragmentActivity implements OnMapReadyCallback { |
|
|
|
|
|
|
@@ -51,14 +61,109 @@ public class EarthquakeMapsActivity extends FragmentActivity implements OnMapRea |
|
|
|
private double breitengrad; |
|
|
|
private double laengengrad; |
|
|
|
|
|
|
|
//Shake Sensor |
|
|
|
private SensorManager mSensorManager; |
|
|
|
private static final float mUpperThreshold = 10.5f; // für Emulator auf 1.5 setzen |
|
|
|
private static final float mLowerThreshold = 5.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 CountDownTimer mLockTimer = new CountDownTimer(mShakeDetectionLockTimeMicroSeconds, 1000) { |
|
|
|
public void onTick(long millisUntilFinished) { |
|
|
|
((TextView) findViewById(R.id.txtEarthquake)).setText("Earthquake started! Detection locked for " + millisUntilFinished / 1000 + " s"); |
|
|
|
} |
|
|
|
|
|
|
|
public void onFinish() { |
|
|
|
mShakeDetectionIsActive = true; |
|
|
|
mShakeDetected = false; |
|
|
|
Toast.makeText(getApplicationContext(), "Shake Detection unlocked", Toast.LENGTH_SHORT).show(); |
|
|
|
( (TextView) findViewById(R.id.txtEarthquake)).setText("Shake your Smartphone for an Earthquake"); |
|
|
|
} |
|
|
|
}; |
|
|
|
@Override |
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
setContentView(R.layout.activity_earthquake_maps); |
|
|
|
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; |
|
|
|
|
|
|
|
getDataBaseValues(); //TODO Edward: Nur als Anmerkung, diese Methode erfolgt damit deine Methode getDeviceLocation rechtzeitig Koordinaten aus der DB bekommt |
|
|
|
//TODO Hast schon echt viel erledigt :D Ich habe gedacht das die Class EarthquakeMapsActivity Daten an die DB schickt und die SensorMapsActivity die Daten bekommt, ich glaub die Funktion muss in die andere Class |
|
|
|
getLocationPermission(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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(50, 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; |
|
|
|
Toast.makeText(getApplicationContext(), "Shake event detected", Toast.LENGTH_SHORT).show(); |
|
|
|
writeEarthquakeToDatabase(); |
|
|
|
mLockTimer.start(); |
|
|
|
} else { |
|
|
|
mShakeDetectionIsActive = true; |
|
|
|
mShakeDetected = false; |
|
|
|
} |
|
|
|
} |
|
|
|
}.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
private void writeEarthquakeToDatabase() |
|
|
|
{ |
|
|
|
//TODO Erdbeben + Erzeugerparameter in Datenbank schreiben |
|
|
|
} |
|
|
|
@Override |
|
|
|
protected void onResume() { |
|
|
|
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), |
|
|
|
SensorManager.SENSOR_DELAY_NORMAL); |
|
|
|
super.onResume(); |
|
|
|
} |
|
|
|
@Override |
|
|
|
protected void onPause() { |
|
|
|
mSensorManager.unregisterListener(mSensorListener); |
|
|
|
super.onPause(); |
|
|
|
} |
|
|
|
|
|
|
|
private void getLocationPermission() { |