Browse Source

Shake Detection added.

SubWorker^2
Patrick Halboth 3 years ago
parent
commit
29e1070b73

+ 1
- 1
.idea/compiler.xml View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="1.8" />
<bytecodeTargetLevel target="11" />
</component>
</project>

+ 1
- 1
.idea/misc.xml View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

+ 10
- 0
.idea/runConfigurations.xml View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

+ 105
- 0
app/src/main/java/de/edotzlaff/schockwelle/EarthquakeMapsActivity.java View File

@@ -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() {

+ 2
- 2
app/src/main/res/layout/activity_earthquake_maps.xml View File

@@ -7,11 +7,11 @@
tools:context=".EarthquakeMapsActivity">

<TextView
android:id="@+id/txtEarthquake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shake your Smartphone for a Earthquake"
android:layout_centerHorizontal="true"
android:id="@+id/txtEarthquake"/>
android:text="Shake your Smartphone for an Earthquake" />


<fragment xmlns:android="http://schemas.android.com/apk/res/android"

+ 1
- 1
build.gradle View File

@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.google.gms:google-services:4.3.8'

// NOTE: Do not place your application dependencies here; they belong

+ 1
- 1
gradle/wrapper/gradle-wrapper.properties View File

@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip

Loading…
Cancel
Save