From 29e1070b73528284742d097f48661911091187ef Mon Sep 17 00:00:00 2001 From: Patrick Halboth Date: Fri, 18 Jun 2021 15:33:10 +0200 Subject: [PATCH 1/3] Shake Detection added. --- .idea/compiler.xml | 2 +- .idea/misc.xml | 2 +- .idea/runConfigurations.xml | 10 ++ .../schockwelle/EarthquakeMapsActivity.java | 105 ++++++++++++++++++ .../res/layout/activity_earthquake_maps.xml | 4 +- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 7 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 .idea/runConfigurations.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 61a9130..fb7f4a8 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index d5d35ec..860da66 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/edotzlaff/schockwelle/EarthquakeMapsActivity.java b/app/src/main/java/de/edotzlaff/schockwelle/EarthquakeMapsActivity.java index fc31e17..c52a6ab 100644 --- a/app/src/main/java/de/edotzlaff/schockwelle/EarthquakeMapsActivity.java +++ b/app/src/main/java/de/edotzlaff/schockwelle/EarthquakeMapsActivity.java @@ -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() { diff --git a/app/src/main/res/layout/activity_earthquake_maps.xml b/app/src/main/res/layout/activity_earthquake_maps.xml index 671a642..13d990b 100644 --- a/app/src/main/res/layout/activity_earthquake_maps.xml +++ b/app/src/main/res/layout/activity_earthquake_maps.xml @@ -7,11 +7,11 @@ tools:context=".EarthquakeMapsActivity"> + android:text="Shake your Smartphone for an Earthquake" /> Date: Fri, 18 Jun 2021 18:52:54 +0200 Subject: [PATCH 2/3] Vibration added. TODO: Write / Fetch Database Values & Calculate Time Difference --- app/src/main/AndroidManifest.xml | 2 +- .../schockwelle/SensorMapsActivity.java | 86 ++++++++++++++++++- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3013079..2fdfb71 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,7 @@ - + = 26) { + if(duration == 0) + { + v.cancel(); //stop vibration if still running + Toast.makeText(this, "Vibration has been stopped", Toast.LENGTH_SHORT).show(); + return; + } + + Toast.makeText(this, "Ampl: " + amplitude + ", Dur: " + duration, Toast.LENGTH_SHORT).show(); + + v.vibrate(VibrationEffect.createOneShot(duration,amplitude)); + + + } else { + + if(duration == 0) + { + v.cancel(); //stop vibration if still running + + return; + } + + v.vibrate(duration); + } + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); From 9932f3fc248511daaaa72dc51d688df1c8cfb6ce Mon Sep 17 00:00:00 2001 From: Patrick Halboth Date: Sun, 20 Jun 2021 18:47:14 +0200 Subject: [PATCH 3/3] Probleme mit Database Beschreiben und Lesen. Bitte einheitliches DB Format festelegen und alle Commits mal Pusheb --- app/src/main/AndroidManifest.xml | 1 + .../schockwelle/EarthquakeMapsActivity.java | 22 +++++++++++++------ .../schockwelle/SensorMapsActivity.java | 20 +++++++++++++---- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2fdfb71..8755d0e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,6 +10,7 @@ + > 8 & 0xff),(ip >> 16 & 0xff), (ip >> 24 & 0xff)); mDatenbank = FirebaseDatabase.getInstance().getReference(); - mDatenbank.child("overview").child("IDG1").child("ip").setValue("10.00.00.001"); - mDatenbank.child("overview").child("IDG1").child("vibration").setValue(true); + mDatenbank.child("overview").child("IDG1").child("ip").setValue(ipAddress); + mDatenbank.child("overview").child("IDG1").child("vibration").setValue(vibration); mDatenbank.child("overview").child("IDG1").child("timestamp").setValue(currentTime); //aktueller Zeitstempel wird in Datenbank eingetragen mDatenbank.child("overview").child("IDG1").child("breitengrad").setValue(currentLocation.getLatitude()); //aktueller Breitengrad mDatenbank.child("overview").child("IDG1").child("laengengrad").setValue(currentLocation.getLongitude()); //aktueller Längergrad - mDatenbank.child("overview").child("IDG1").child("amplitude").setValue(1001); + mDatenbank.child("overview").child("IDG1").child("amplitude").setValue(255); } } \ No newline at end of file diff --git a/app/src/main/java/de/edotzlaff/schockwelle/SensorMapsActivity.java b/app/src/main/java/de/edotzlaff/schockwelle/SensorMapsActivity.java index 819060d..67342b2 100644 --- a/app/src/main/java/de/edotzlaff/schockwelle/SensorMapsActivity.java +++ b/app/src/main/java/de/edotzlaff/schockwelle/SensorMapsActivity.java @@ -44,7 +44,7 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa private static final String TAG = "MainActivity"; private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; - + private static final double EARTHQUAKE_VELOCITY = 1; // 1 Meter pro Sekunde Erdbebengeschwindigkeit //vars private Boolean mLocationPermissionsGranted = false; private GoogleMap mMap; @@ -75,7 +75,7 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa getVibrationAbility(); //distance(currentLocation.getLatitude(), currentLocation.getLongitude(),breitengrad,laengengrad); //Übergabe zur Berechnung der Distanz zwischen Auslöser und aktuellem Standort //TODO Patrick: die Funktion distance gibt Meter zurück, könntest Du das mit dem Zeitstempel und der Berechnung bis zur Vibration machen? - setVibrationTimer(10000,1000,255); + // setVibrationTimer(10000,1000,255); } private void getLocationPermission() { @@ -108,10 +108,16 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa } } - private long getTimeStampDifference(Date vibrationTime) + private long getTimeStampDifference(float distance) { long diff= 0; //TODO Zeitdifferenz in Millisekunden zwischen aktuellen Uhrzeit und Vibratonszeitstempel berechnen + + + if (distance>0) + { + diff = (long)Math.round(1/(EARTHQUAKE_VELOCITY/distance)); + } return diff; } private void setVibrationTimer(long msDelay, int duration, int amplitude) @@ -286,7 +292,13 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa //####### Auslesen für ing-Werte #######: String amplitudeString = data.child("overview").child("IDG" + i).child("amplitude").getValue().toString(); int amplitude = Integer.parseInt(amplitudeString); - + //TODO Workaround beseiteigen: hier wird immer davon ausgegangen, dass auslösendes Gerät die ID 1 besitzt + if(vibration == true && i == 1) + { + float distance = distance(currentLocation.getLatitude(), currentLocation.getLongitude(),breitengrad,laengengrad); + long delay = getTimeStampDifference(distance); + setVibrationTimer(delay,1000,amplitude); + } //####### Optional zur Kontrolle #######: /* System.out.println("IDG" + i + " - IP:" + data.child("overview").child("IDG" + i).child("ip").getValue().toString());