<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||
<project version="4"> | <project version="4"> | ||||
<component name="CompilerConfiguration"> | <component name="CompilerConfiguration"> | ||||
<bytecodeTargetLevel target="1.8" /> | |||||
<bytecodeTargetLevel target="11" /> | |||||
</component> | </component> | ||||
</project> | </project> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||
<project version="4"> | <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" /> | <output url="file://$PROJECT_DIR$/build/classes" /> | ||||
</component> | </component> | ||||
<component name="ProjectType"> | <component name="ProjectType"> |
<?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> |
<uses-permission android:name="android.permission.INTERNET" /> | <uses-permission android:name="android.permission.INTERNET" /> | ||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||||
<uses-permission android:name="android.permission.VIBRATE" /> | |||||
<application | <application | ||||
android:allowBackup="true" | android:allowBackup="true" | ||||
android:icon="@mipmap/schockicon" | android:icon="@mipmap/schockicon" | ||||
android:label="@string/title_activity_sensor_maps"></activity> | android:label="@string/title_activity_sensor_maps"></activity> | ||||
<activity | <activity | ||||
android:name=".EarthquakeMapsActivity" | android:name=".EarthquakeMapsActivity" | ||||
android:label="@string/title_activity_earthquake_maps" /> | |||||
android:label="@string/title_activity_earthquake_maps"/> | |||||
<activity android:name=".MainActivity"> | <activity android:name=".MainActivity"> | ||||
<intent-filter> | <intent-filter> | ||||
<action android:name="android.intent.action.MAIN" /> | <action android:name="android.intent.action.MAIN" /> | ||||
</activity> | </activity> | ||||
</application> | </application> | ||||
</manifest> | |||||
</manifest> | |||||
<!--android:parentActivityName=".MainActivity"--> |
package de.edotzlaff.schockwelle; | package de.edotzlaff.schockwelle; | ||||
import java.time.LocalDateTime; | |||||
public class Devices { | public class Devices { | ||||
private String ip; | private String ip; | ||||
private boolean vibration; | private boolean vibration; | ||||
private double breitengrad; | private double breitengrad; | ||||
private double laengengrad; | private double laengengrad; | ||||
private int amplitude; | private int amplitude; | ||||
private String localdatetime; | |||||
private boolean idvergeben; | |||||
private String androidid; | |||||
public Devices() { | public Devices() { | ||||
} | } | ||||
public Devices(String ip, boolean vibration, long timestamp, double breitengrad, double laengengrad, int amplitude) { | |||||
public Devices(String ip, boolean vibration, long timestamp, double breitengrad, double laengengrad, int amplitude, String localdatetime, String androidid) { | |||||
this.ip = ip; | this.ip = ip; | ||||
this.vibration = vibration; | this.vibration = vibration; | ||||
this.timestamp = timestamp; | this.timestamp = timestamp; | ||||
this.breitengrad = breitengrad; | this.breitengrad = breitengrad; | ||||
this.laengengrad = laengengrad; | this.laengengrad = laengengrad; | ||||
this.amplitude = amplitude; | this.amplitude = amplitude; | ||||
this.localdatetime = localdatetime; | |||||
this.androidid = androidid; | |||||
} | } | ||||
public String getIp() { | public String getIp() { | ||||
this.ip = ip; | this.ip = ip; | ||||
} | } | ||||
public boolean isVibration() { | public boolean isVibration() { | ||||
return vibration; | return vibration; | ||||
} | } | ||||
this.vibration = vibration; | this.vibration = vibration; | ||||
} | } | ||||
public long getTimestamp() { | public long getTimestamp() { | ||||
return timestamp; | return timestamp; | ||||
} | } | ||||
this.timestamp = timestamp; | this.timestamp = timestamp; | ||||
} | } | ||||
public double getBreitengrad() { | public double getBreitengrad() { | ||||
return breitengrad; | return breitengrad; | ||||
} | } | ||||
this.breitengrad = breitengrad; | this.breitengrad = breitengrad; | ||||
} | } | ||||
public double getLaengengrad() { | public double getLaengengrad() { | ||||
return laengengrad; | return laengengrad; | ||||
} | } | ||||
this.laengengrad = laengengrad; | this.laengengrad = laengengrad; | ||||
} | } | ||||
public int getAmplitude() { | public int getAmplitude() { | ||||
return amplitude; | return amplitude; | ||||
} | } | ||||
public void setAmplitude(int amplitude) { | public void setAmplitude(int amplitude) { | ||||
this.amplitude = amplitude; | this.amplitude = amplitude; | ||||
} | } | ||||
public String getLocaldatetime() { | |||||
return localdatetime; | |||||
} | |||||
public void setLocaldatetime(String localdatetime) { | |||||
this.localdatetime = localdatetime; | |||||
} | |||||
public String getAndroidid() { | |||||
return androidid; | |||||
} | |||||
public void setAndroidid(String androidid) { | |||||
this.androidid = androidid; | |||||
} | |||||
} | } |
package de.edotzlaff.schockwelle; | package de.edotzlaff.schockwelle; | ||||
import android.Manifest; | import android.Manifest; | ||||
import android.app.ActionBar; | |||||
import android.content.Context; | |||||
import android.content.pm.PackageManager; | 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.location.Location; | ||||
import android.net.wifi.WifiManager; | |||||
import android.os.Build; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.CountDownTimer; | |||||
import android.os.SystemClock; | import android.os.SystemClock; | ||||
import android.provider.ContactsContract; | |||||
import android.provider.Settings; | |||||
import android.util.Log; | import android.util.Log; | ||||
import android.widget.TextView; | |||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import com.google.firebase.database.DatabaseReference; | import com.google.firebase.database.DatabaseReference; | ||||
import com.google.firebase.database.FirebaseDatabase; | import com.google.firebase.database.FirebaseDatabase; | ||||
import com.google.firebase.database.ValueEventListener; | import com.google.firebase.database.ValueEventListener; | ||||
import java.time.LocalDateTime; | |||||
import java.util.Calendar; | import java.util.Calendar; | ||||
import java.util.Date; | import java.util.Date; | ||||
import java.util.Objects; | |||||
public class EarthquakeMapsActivity extends FragmentActivity implements OnMapReadyCallback { | public class EarthquakeMapsActivity extends FragmentActivity implements OnMapReadyCallback { | ||||
private static final String TAG = "MainActivity"; | private static final String TAG = "MainActivity"; | ||||
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; | private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; | ||||
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; | private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; | ||||
//vars | |||||
private Boolean mLocationPermissionsGranted = false; | private Boolean mLocationPermissionsGranted = false; | ||||
private GoogleMap mMap; | private GoogleMap mMap; | ||||
private FusedLocationProviderClient mFusedLocationProviderClient; | private FusedLocationProviderClient mFusedLocationProviderClient; | ||||
private DatabaseReference mDatenbank; | |||||
//Date currentTime; //Workaround für Crash: currentTime nicht als Datum sondern als Long zeitstempel | |||||
Long currentTime; | Long currentTime; | ||||
Location currentLocation; | Location currentLocation; | ||||
private DatabaseReference mDatenbank; | |||||
private double breitengrad; | private double breitengrad; | ||||
private double laengengrad; | private double laengengrad; | ||||
private boolean useOwnGPS; | |||||
private int indexID = 1; | |||||
//########################################################################################################################################################################## | |||||
//################################################################## vvv ShakeParameter vvv ############################################################################## | |||||
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 boolean allowShakeEvent = true; | |||||
//################################################################## ^^^^ ShakeParameter ^^^^ ############################################################################ | |||||
//########################################################################################################################################################################## | |||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
setContentView(R.layout.activity_earthquake_maps); | setContentView(R.layout.activity_earthquake_maps); | ||||
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 | |||||
getDataBaseValuesNoListener(); | |||||
sensorManagementInit(); | |||||
getLocationPermission(); | getLocationPermission(); | ||||
} | } | ||||
//##################################################################################################################################################################### | |||||
//################################################################## vvv ShakeCode 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 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"); | |||||
} | |||||
}; | |||||
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() | |||||
{ | |||||
setDataBaseValues(); | |||||
} | |||||
@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(); | |||||
} | |||||
//################################################################## ^^^^ ShakeCode ^^^^ ############################################################################ | |||||
//##################################################################################################################################################################### | |||||
//##################################################################################################################################################################### | |||||
//################################################################## vvv GPS Code vvv ############################################################################### | |||||
private void getLocationPermission() { | private void getLocationPermission() { | ||||
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; | String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; | ||||
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | ||||
case LOCATION_PERMISSION_REQUEST_CODE: { | case LOCATION_PERMISSION_REQUEST_CODE: { | ||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||||
mLocationPermissionsGranted = true; | mLocationPermissionsGranted = true; | ||||
//initalize or map | |||||
initMap(); | |||||
initMap(); //initalize or map | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private void initMap() { | private void initMap() { | ||||
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); | SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); | ||||
mapFragment.getMapAsync(this); | mapFragment.getMapAsync(this); | ||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | ||||
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, | != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, | ||||
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | ||||
return; | return; | ||||
} | } | ||||
mMap.setMyLocationEnabled(true); | mMap.setMyLocationEnabled(true); | ||||
public void onComplete(@NonNull Task task) { | public void onComplete(@NonNull Task task) { | ||||
if (task.isSuccessful()){ | if (task.isSuccessful()){ | ||||
currentLocation = (Location) task.getResult(); | currentLocation = (Location) task.getResult(); | ||||
//currentTime = Calendar.getInstance().getTime(); //Workaround für Crash: currentTime nicht als Datum sondern als Long zeitstempel | |||||
currentTime = Calendar.getInstance().getTimeInMillis(); | |||||
Toast.makeText(EarthquakeMapsActivity.this, currentTime.toString(), Toast.LENGTH_SHORT).show(); | |||||
//currentTime = Calendar.getInstance().getTimeInMillis(); //verschoben in setDataBaseValues | |||||
//Toast.makeText(EarthquakeMapsActivity.this, currentTime.toString(), Toast.LENGTH_SHORT).show(); //verschoben in setDataBaseValues | |||||
if(useOwnGPS) | |||||
{ | |||||
breitengrad = currentLocation.getLatitude(); | |||||
laengengrad = currentLocation.getLongitude(); | |||||
} | |||||
currentLocation.setLatitude(breitengrad); | currentLocation.setLatitude(breitengrad); | ||||
currentLocation.setLongitude(laengengrad); | currentLocation.setLongitude(laengengrad); | ||||
setDataBaseValues(); //TODO Edward: Die Funktion geht jetzt. Statt Datum wird Unix Zeitstempel als Long Typ verwendet | |||||
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); | moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); | ||||
//setDataBaseValues(); //Wurde verschoben zu Methode writeEarthquakeToDatabase | |||||
} | } | ||||
else{ | else{ | ||||
Toast.makeText(EarthquakeMapsActivity.this, "Current Location unavailable", Toast.LENGTH_SHORT).show(); | Toast.makeText(EarthquakeMapsActivity.this, "Current Location unavailable", Toast.LENGTH_SHORT).show(); | ||||
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, zoom)); | mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, zoom)); | ||||
} | } | ||||
//################################################################## ^^^^ GPS Code ^^^^ ############################################################################# | |||||
//##################################################################################################################################################################### | |||||
//##################################################################################################################################################################### | |||||
//################################################################## vvv Datenbank Code vvv ######################################################################### | |||||
public void getDataBaseValuesNoListener() | |||||
{ | |||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() { | |||||
@Override | |||||
public void onComplete(@NonNull Task<DataSnapshot> task) { | |||||
if (!task.isSuccessful()) { | |||||
System.out.println("Datenbankfehler in getDataBaseValuesNoListener"); | |||||
System.out.println("Error getting data: " + task.getException()); | |||||
} | |||||
else { | |||||
processDataBaseValues(task.getResult()); | |||||
} | |||||
} | |||||
}); | |||||
} | |||||
public void processDataBaseValues (DataSnapshot data) | |||||
{ | |||||
processDeviceIndex(data); | |||||
processLocation(data); | |||||
//processMessageDisplay(data); | |||||
} | |||||
public void processDeviceIndex(DataSnapshot data) | |||||
{ | |||||
for (int i = 1; i<=4; i++) | |||||
{ | |||||
String androidid = data.child("IDG" + i).child("androidid").getValue().toString(); | |||||
if(androidid.isEmpty()) | |||||
{ | |||||
indexID = i; | |||||
break; | |||||
}else | |||||
{ | |||||
continue; | |||||
} | |||||
} | |||||
} | |||||
public void processLocation(DataSnapshot data) | |||||
{ | |||||
String breitengradString = data.child("IDG" + indexID).child("breitengrad").getValue().toString(); | |||||
String laengengradString = data.child("IDG" + indexID).child("laengengrad").getValue().toString(); | |||||
if(breitengradString.isEmpty() || laengengradString.isEmpty()) | |||||
{ | |||||
useOwnGPS = true; | |||||
}else{ | |||||
useOwnGPS = false; | |||||
breitengrad = Double.parseDouble(breitengradString); | |||||
laengengrad = Double.parseDouble(laengengradString); | |||||
} | |||||
} | |||||
/* | |||||
public void processMessageDisplay(DataSnapshot data) | |||||
{ | |||||
String vibrationString; | |||||
String androidid; | |||||
for (int i = 1; i<=4; i++) | |||||
{ | |||||
androidid = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("androidid").getValue().toString(); | |||||
vibrationString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("vibration").getValue().toString(); | |||||
if((!androidid.isEmpty()) && vibrationString.equals("true")) | |||||
{ | |||||
allowShakeEvent = false; | |||||
} | |||||
} | |||||
} | |||||
*/ | |||||
public String getandroidid () | |||||
{ | |||||
return Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID); | |||||
} | |||||
public String getDeviceIpAdress () | |||||
{ | |||||
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); | |||||
int ip = wm.getConnectionInfo().getIpAddress(); | |||||
String ipAddress = String.format("%d.%d.%d.%d",(ip & 0xff),(ip >> 8 & 0xff),(ip >> 16 & 0xff), (ip >> 24 & 0xff)); | |||||
return ipAddress; | |||||
} | |||||
public void setDataBaseValues() | |||||
{ | |||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("ip").setValue(getDeviceIpAdress()); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("vibration").setValue(true); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("timestamp").setValue(Calendar.getInstance().getTimeInMillis()); | |||||
currentTime = Calendar.getInstance().getTimeInMillis(); | |||||
Toast.makeText(EarthquakeMapsActivity.this, currentTime.toString(), Toast.LENGTH_SHORT).show(); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("breitengrad").setValue(breitengrad); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("laengengrad").setValue(laengengrad); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("amplitude").setValue(1001); | |||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("localdatetime").setValue(LocalDateTime.now().toString()); | |||||
} | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + indexID).child("androidid").setValue(getandroidid()); | |||||
} | |||||
//################################################################## ^^^^ Datenbank Code ^^^^ ############################################################################# | |||||
//########################################################################################################################################################################### | |||||
} | |||||
//Evtl. Redundanter Code | |||||
/* | |||||
public void getDataBaseValues() | public void getDataBaseValues() | ||||
{ | { | ||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | mDatenbank = FirebaseDatabase.getInstance().getReference(); | ||||
}); | }); | ||||
} | } | ||||
public void processDataBaseValues (DataSnapshot data) | |||||
{ | |||||
//####### Hier wird beispielweise mit IDG1 gearbeitet, sollte aber klüger ausgearbeitet werden #######: | |||||
String breitengradString = data.child("overview").child("IDG1").child("breitengrad").getValue().toString(); //TODO Aron/Team: Idee für passende ID je nach zugreifendem Gerät ==> Hardcoded oder flexibel? | |||||
String laengengradString = data.child("overview").child("IDG1").child("laengengrad").getValue().toString(); | |||||
breitengrad = Double.parseDouble(breitengradString); | |||||
laengengrad = Double.parseDouble(laengengradString); | |||||
} | |||||
public void getDataBaseFailure (DatabaseError error) | public void getDataBaseFailure (DatabaseError error) | ||||
{ | { | ||||
System.out.println("Fehler"); | |||||
System.out.println("Datenbankfehler in gerDataBaseFailure"); | |||||
Log.w("Datenbankfehler", error.toException()); | Log.w("Datenbankfehler", error.toException()); | ||||
} | } | ||||
//TODO Edward: Hier ist eine beispielhafte Methode mit der du Werte in der Datenbank setzen kannst | |||||
public void setDataBaseValues() | |||||
{ | |||||
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("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); | |||||
} | |||||
} | |||||
*/ |
package de.edotzlaff.schockwelle; | package de.edotzlaff.schockwelle; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.RequiresApi; | |||||
import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||
import androidx.fragment.app.FragmentActivity; | import androidx.fragment.app.FragmentActivity; | ||||
import android.app.ActionBar; | |||||
import android.app.Dialog; | import android.app.Dialog; | ||||
import android.content.Intent; | import android.content.Intent; | ||||
import android.hardware.Sensor; | import android.hardware.Sensor; | ||||
import android.os.Build; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.provider.Settings; | |||||
import android.util.Log; | import android.util.Log; | ||||
import android.view.View; | import android.view.View; | ||||
import android.widget.Button; | import android.widget.Button; | ||||
import com.google.android.gms.common.ConnectionResult; | import com.google.android.gms.common.ConnectionResult; | ||||
import com.google.android.gms.common.GoogleApiAvailability; | import com.google.android.gms.common.GoogleApiAvailability; | ||||
import com.google.android.gms.tasks.OnCompleteListener; | |||||
import com.google.android.gms.tasks.Task; | |||||
import com.google.firebase.database.DataSnapshot; | import com.google.firebase.database.DataSnapshot; | ||||
import com.google.firebase.database.DatabaseError; | import com.google.firebase.database.DatabaseError; | ||||
import com.google.firebase.database.DatabaseReference; | import com.google.firebase.database.DatabaseReference; | ||||
import com.google.firebase.database.FirebaseDatabase; | import com.google.firebase.database.FirebaseDatabase; | ||||
import com.google.firebase.database.ValueEventListener; | import com.google.firebase.database.ValueEventListener; | ||||
import java.time.LocalDateTime; | |||||
import java.time.format.DateTimeFormatter; | |||||
import java.util.Calendar; | |||||
public class MainActivity extends FragmentActivity { | public class MainActivity extends FragmentActivity { | ||||
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 DatabaseReference mDatenbank; | private DatabaseReference mDatenbank; | ||||
String android_id; | |||||
@Override | @Override | ||||
if (isServiceOK()){ | if (isServiceOK()){ | ||||
init(); | init(); | ||||
} | } | ||||
deviceInitDataBase(); //TODO Edward: Mit dieser Methode wird beim Starten der App die DB mit, vorerst festen, Werten für alle IDG 1-4 initialiert | |||||
} | } | ||||
private void init(){ | private void init(){ | ||||
Button btnEarthquake = (Button) findViewById(R.id.btnEarthquake); | Button btnEarthquake = (Button) findViewById(R.id.btnEarthquake); | ||||
Button btnSensor = (Button) findViewById(R.id.btnSensor); | |||||
Button btnMuster = (Button) findViewById(R.id.buttonDatenBankMuster); | |||||
Button btnLeer = (Button) findViewById(R.id.buttonDatenBankLeeren); | |||||
Button btnReset = (Button) findViewById(R.id.buttonReset); | |||||
btnEarthquake.setEnabled(false); | |||||
btnEarthquake.setOnClickListener(new View.OnClickListener() { | btnEarthquake.setOnClickListener(new View.OnClickListener() { | ||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
Intent intent = new Intent(MainActivity.this, EarthquakeMapsActivity.class); | Intent intent = new Intent(MainActivity.this, EarthquakeMapsActivity.class); | ||||
btnEarthquake.setEnabled(false); | |||||
btnSensor.setEnabled(false); | |||||
startActivity(intent); | startActivity(intent); | ||||
} | } | ||||
}); | }); | ||||
Button btnSensor = (Button) findViewById(R.id.btnSensor); | |||||
btnSensor.setEnabled(false); | |||||
btnSensor.setOnClickListener(new View.OnClickListener() { | btnSensor.setOnClickListener(new View.OnClickListener() { | ||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
Intent intent = new Intent(MainActivity.this, SensorMapsActivity.class); | Intent intent = new Intent(MainActivity.this, SensorMapsActivity.class); | ||||
btnSensor.setEnabled(false); | |||||
btnEarthquake.setEnabled(false); | |||||
startActivity(intent); | startActivity(intent); | ||||
} | } | ||||
}); | }); | ||||
btnMuster.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
btnEarthquake.setEnabled(true); | |||||
btnSensor.setEnabled(true); | |||||
btnLeer.setEnabled(false); | |||||
btnMuster.setEnabled(false); | |||||
btnReset.setEnabled(true); | |||||
deviceInitDataBase(); | |||||
} | |||||
}); | |||||
btnLeer.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
btnEarthquake.setEnabled(true); | |||||
btnSensor.setEnabled(true); | |||||
btnMuster.setEnabled(false); | |||||
btnReset.setEnabled(true); | |||||
btnLeer.setEnabled(false); | |||||
datenBankLeeren(); | |||||
} | |||||
}); | |||||
btnReset.setEnabled(false); | |||||
btnReset.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
getDataBaseValuesNoListener(); | |||||
btnEarthquake.setEnabled(true); | |||||
btnSensor.setEnabled(true); | |||||
btnMuster.setEnabled(true); | |||||
btnLeer.setEnabled(true); | |||||
} | |||||
}); | |||||
} | } | ||||
public boolean isServiceOK(){ | public boolean isServiceOK(){ | ||||
public void deviceInitDataBase() | public void deviceInitDataBase() | ||||
{ | { | ||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | mDatenbank = FirebaseDatabase.getInstance().getReference(); | ||||
Devices d1 = new Devices("10.00.00.000", false, 1623926431100L, 49.45470362290784, 11.151032510281498, 1000); | |||||
Devices d2 = new Devices("20.00.00.000", false, 1623926431200L, 40.70064147511923, -73.993767997977787, 200); | |||||
Devices d3 = new Devices("30.00.00.000", false, 1623926431300L, -33.93157129193122, 18.39666975233824, 30); | |||||
Devices d4 = new Devices("40.00.00.000", false, 1623926431400L, -33.40763257191139, -70.61870273653996, 4); | |||||
mDatenbank.child("overview").child("IDG1").setValue(d1); | |||||
mDatenbank.child("overview").child("IDG2").setValue(d2); | |||||
mDatenbank.child("overview").child("IDG3").setValue(d3); | |||||
mDatenbank.child("overview").child("IDG4").setValue(d4); | |||||
getDataBaseValues(); | |||||
Devices d1 = new Devices("10.00.00.000", false, Calendar.getInstance().getTimeInMillis(), 52.51624237821577, 13.37683380067744, 1000, LocalDateTime.now().toString(), ""); //Berlin 52.51624237821577, 13.37683380067744 | |||||
Devices d2 = new Devices("20.00.00.000", false, Calendar.getInstance().getTimeInMillis(), 48.7560280895443, 11.425157702952347, 200, LocalDateTime.now().toString(),""); //Ingolstadt 48.7560280895443, 11.425157702952347 | |||||
Devices d3 = new Devices("30.00.00.000", false, Calendar.getInstance().getTimeInMillis(), 48.16958655466396, 11.617418429524394, 30, LocalDateTime.now().toString(),""); //München 48.16958655466396, 11.617418429524394 | |||||
Devices d4 = new Devices("40.00.00.000", false, Calendar.getInstance().getTimeInMillis(), 45.55620112204013, 12.342516140205387, 4, LocalDateTime.now().toString(),""); //Venedig 45.55620112204013, 12.342516140205387 | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG1").setValue(d1); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG2").setValue(d2); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG3").setValue(d3); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG4").setValue(d4); | |||||
//Nürnberg HBF: 49.4474136331757, 11.082661293035727 | |||||
} | |||||
//Datenbank auslesen OHNE Listener. D.h. es werden Daten (snapshot) ausgelesen und kein Listener hinterlegt. | |||||
//Sollten sich danach Daten zu einem beliebigen Zeitpunkt in der DB ändern, wird die Funktion "onDataChange" NICHT erneut ausgelöst. | |||||
public void getDataBaseValuesNoListener() | |||||
{ | |||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() { | |||||
@Override | |||||
public void onComplete(@NonNull Task<DataSnapshot> task) { | |||||
if (!task.isSuccessful()) { | |||||
System.out.println("Error getting data: " + task.getException()); | |||||
} | |||||
else { | |||||
testProcess(task.getResult()); | |||||
} | |||||
} | |||||
}); | |||||
} | |||||
public void datenBankLeeren() | |||||
{ | |||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | |||||
for (int i = 1; i<=4; i++) | |||||
{ | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("ip").setValue(""); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("vibration").setValue(false); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("timestamp").setValue(""); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("breitengrad").setValue(""); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("laengengrad").setValue(""); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("amplitude").setValue(""); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("localdatetime").setValue(""); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("androidid").setValue(""); | |||||
} | |||||
} | |||||
public void testProcess(DataSnapshot data) | |||||
{ | |||||
mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewAronTestetInDiesemAbschnitt"); | |||||
} | } | ||||
} | |||||
public void getDataBaseValues() | |||||
/* | |||||
//Datenbank auslesen MIT Listener. D.h. es werden Daten (snapshot) ausgelesen und gleichzeitig ein Listener hinterlegt. | |||||
//Sollten sich danach Daten zu einem beliebigen Zeitpunkt in der DB ändern, wird die Funktion "onDataChange" erneut ausgelöst und wieder Daten (snapshot) ausgelesen. | |||||
public void getDataBaseValuesListener() | |||||
{ | { | ||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | |||||
mDatenbank.addValueEventListener(new ValueEventListener() { | mDatenbank.addValueEventListener(new ValueEventListener() { | ||||
@Override | @Override | ||||
public void onDataChange(@NonNull DataSnapshot snapshot) { | public void onDataChange(@NonNull DataSnapshot snapshot) { | ||||
processDataBaseValues(snapshot); //Daten - Snapshot, Übergabe an Methode processDataBaseValues | |||||
//testProcess(snapshot); //Daten - Snapshot, Übergabe an Methode processDataBaseValues | |||||
} | } | ||||
@Override | @Override | ||||
public void onCancelled(@NonNull DatabaseError error) { | public void onCancelled(@NonNull DatabaseError error) { | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
*/ | |||||
/* | |||||
public void getDataBaseFailure (DatabaseError error) | |||||
{ | |||||
System.out.println("Fehler"); | |||||
Log.w("Datenbankfehler", error.toException()); | |||||
} | |||||
*/ | |||||
/* | |||||
@RequiresApi(api = Build.VERSION_CODES.O) | |||||
public void processDataBaseValues (DataSnapshot data) | public void processDataBaseValues (DataSnapshot data) | ||||
{ | { | ||||
for (int i = 1; i<=4; i++) //Für alle IDG 1-4 werden Werte ausgegeben | for (int i = 1; i<=4; i++) //Für alle IDG 1-4 werden Werte ausgegeben | ||||
{ | { | ||||
//####### Auslesen für String-Werte #######: | //####### Auslesen für String-Werte #######: | ||||
String ipAdresse = data.child("overview").child("IDG" + i).child("ip").getValue().toString(); | |||||
String ipAdresse = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("ip").getValue().toString(); | |||||
//####### Auslesen für boolean-Werte #######: | //####### Auslesen für boolean-Werte #######: | ||||
String vibrationString = data.child("overview").child("IDG" + i).child("vibration").getValue().toString(); | |||||
String vibrationString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("vibration").getValue().toString(); | |||||
boolean vibration; | boolean vibration; | ||||
if(vibrationString == "true"){ | if(vibrationString == "true"){ | ||||
vibration = true; | vibration = true; | ||||
} | } | ||||
//####### Auslesen für double-Werte #######: | //####### Auslesen für double-Werte #######: | ||||
String breitengradString = data.child("overview").child("IDG" + i).child("breitengrad").getValue().toString(); | |||||
String laengengradString = data.child("overview").child("IDG" + i).child("laengengrad").getValue().toString(); | |||||
String breitengradString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("breitengrad").getValue().toString(); | |||||
String laengengradString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("laengengrad").getValue().toString(); | |||||
double breitengrad = Double.parseDouble(breitengradString); | double breitengrad = Double.parseDouble(breitengradString); | ||||
double laengengrad = Double.parseDouble(laengengradString); | double laengengrad = Double.parseDouble(laengengradString); | ||||
//####### Auslesen für long-Werte #######: | //####### Auslesen für long-Werte #######: | ||||
String timestampString = data.child("overview").child("IDG" + i).child("timestamp").getValue().toString(); | |||||
String timestampString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("timestamp").getValue().toString(); | |||||
long timestamp = Long.parseLong(timestampString); | long timestamp = Long.parseLong(timestampString); | ||||
//####### Auslesen für ing-Werte #######: | //####### Auslesen für ing-Werte #######: | ||||
String amplitudeString = data.child("overview").child("IDG" + i).child("amplitude").getValue().toString(); | |||||
String amplitudeString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("amplitude").getValue().toString(); | |||||
int amplitude = Integer.parseInt(amplitudeString); | int amplitude = Integer.parseInt(amplitudeString); | ||||
//####### Optional zur Kontrolle #######: | |||||
/* | |||||
System.out.println("IDG" + i + " - IP:" + data.child("overview").child("IDG" + i).child("ip").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Vibration:" + data.child("overview").child("IDG" + i).child("vibration").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Zeitstempel:" + data.child("overview").child("IDG" + i).child("timestamp").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Breitengrad:" + data.child("overview").child("IDG" + i).child("breitengrad").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Laengengrad:" + data.child("overview").child("IDG" + i).child("laengengrad").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Amplitude:" + data.child("overview").child("IDG" + i).child("amplitude").getValue().toString()); | |||||
*/ | |||||
//####### Auslesen für LocalDateTime#######: | |||||
String localdatetimeString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("localdatetime").getValue().toString(); | |||||
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; | |||||
LocalDateTime localdatetime = LocalDateTime.parse(localdatetimeString, formatter); | |||||
} | } | ||||
} | } | ||||
public void getDataBaseFailure (DatabaseError error) | |||||
{ | |||||
System.out.println("Fehler"); | |||||
Log.w("Datenbankfehler", error.toException()); | |||||
} | |||||
} | |||||
*/ |
package de.edotzlaff.schockwelle; | package de.edotzlaff.schockwelle; | ||||
import android.Manifest; | import android.Manifest; | ||||
import android.content.Context; | |||||
import android.content.pm.PackageManager; | import android.content.pm.PackageManager; | ||||
import android.location.Location; | import android.location.Location; | ||||
import android.os.Build; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.CountDownTimer; | |||||
import android.os.VibrationEffect; | |||||
import android.os.Vibrator; | |||||
import android.provider.Settings; | |||||
import android.util.Log; | import android.util.Log; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import com.google.firebase.database.FirebaseDatabase; | import com.google.firebase.database.FirebaseDatabase; | ||||
import com.google.firebase.database.ValueEventListener; | import com.google.firebase.database.ValueEventListener; | ||||
import java.time.LocalDateTime; | |||||
import java.util.Calendar; | import java.util.Calendar; | ||||
import java.util.Date; | import java.util.Date; | ||||
private static final String TAG = "MainActivity"; | private static final String TAG = "MainActivity"; | ||||
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; | private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; | ||||
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; | private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; | ||||
private static final double EARTHQUAKE_VELOCITY = 1; // 1 Meter pro Sekunde Erdbebengeschwindigkeit | |||||
//vars | //vars | ||||
private Boolean mLocationPermissionsGranted = false; | private Boolean mLocationPermissionsGranted = false; | ||||
private GoogleMap mMap; | private GoogleMap mMap; | ||||
private FusedLocationProviderClient mFusedLocationProviderClient; | private FusedLocationProviderClient mFusedLocationProviderClient; | ||||
Date currentTime; | |||||
//Date currentTime; | |||||
Location currentLocation; | Location currentLocation; | ||||
Long currentTime; | |||||
private double breitengrad; | private double breitengrad; | ||||
private double laengengrad; | private double laengengrad; | ||||
private double sensorGPSbreitengrad; | |||||
private double sensorGPSlaengengrad; | |||||
private boolean useOwnGPS; | |||||
private boolean takeGPSfromDB = true; | |||||
private boolean tookOwnGPS = false; | |||||
boolean vibrationTrigger = true; | |||||
private DatabaseReference mDatenbank; | private DatabaseReference mDatenbank; | ||||
private String breitengradQuellVibration; | |||||
private String laengengradQuellVibration; | |||||
private Boolean mDeviceCanVibrate = false; | |||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
setContentView(R.layout.activity_sensor_maps); | setContentView(R.layout.activity_sensor_maps); | ||||
TextView tv= (TextView) findViewById(R.id.txtSensor); | |||||
getDataBaseValuesWithListener(); | |||||
TextView tv= (TextView) findViewById(R.id.txtSensor); | |||||
getLocationPermission(); //Zuerst werden die aktuellen Standortdaten ermittelt | getLocationPermission(); //Zuerst werden die aktuellen Standortdaten ermittelt | ||||
getDataBaseValues(); // Hier werden die Daten von der DB abgefragt //TODO Edward: Geht wieder :D | |||||
getVibrationAbility(); | |||||
} | |||||
//##################################################################################################################################################################### | |||||
//################################################################## vvv ShakeCode vvv ############################################################################## | |||||
private void getVibrationAbility() | |||||
{ | |||||
// Get instance of Vibrator from current Context | |||||
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); | |||||
// Output yes if can vibrate, no otherwise | |||||
if (v.hasVibrator()) { | |||||
Log.v("Can Vibrate", "YES"); | |||||
// Log.v("Can Control Amplitude", v.hasAmplitudeControl() ? "YES" : "NO"); | |||||
mDeviceCanVibrate = true; | |||||
} | |||||
else | |||||
{ | |||||
Log.v("Can Vibrate", "NO"); | |||||
mDeviceCanVibrate = false; | |||||
} | |||||
} | |||||
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, int index) | |||||
{ | |||||
new CountDownTimer(msDelay, 1000) { | |||||
public void onTick(long millisUntilFinished) { | |||||
((TextView) findViewById(R.id.txtSensor)).setText("Earthquake hits in " + millisUntilFinished / 1000 + " s"); | |||||
} | |||||
public void onFinish() { | |||||
Toast.makeText(getApplicationContext(), "The Ground is shaking!", Toast.LENGTH_SHORT).show(); | |||||
performVibration(duration, amplitude); | |||||
( (TextView) findViewById(R.id.txtSensor)).setText("No Earthquake upcoming"); | |||||
//In DB schreiben! | |||||
setVibrationInDataBase(index); | |||||
} | |||||
}.start(); | |||||
} | |||||
public void performVibration(int duration, int amplitude) { | |||||
if(!mDeviceCanVibrate) | |||||
return; | |||||
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); | |||||
if (Build.VERSION.SDK_INT >= 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)); | |||||
//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? | |||||
} else { | |||||
if(duration == 0) | |||||
{ | |||||
v.cancel(); //stop vibration if still running | |||||
return; | |||||
} | |||||
v.vibrate(duration); | |||||
} | |||||
} | } | ||||
//################################################################## ^^^^ ShakeCode ^^^^ ############################################################################ | |||||
//##################################################################################################################################################################### | |||||
//##################################################################################################################################################################### | |||||
//################################################################## vvv GPS Code vvv ############################################################################### | |||||
private void getLocationPermission() { | private void getLocationPermission() { | ||||
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; | String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; | ||||
} | } | ||||
// Add a marker in Sydney and move the camera | // Add a marker in Sydney and move the camera | ||||
LatLng sydney = new LatLng(-34, 151); | |||||
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); | |||||
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); | |||||
//LatLng sydney = new LatLng(-34, 151); | |||||
//mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); | |||||
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); | |||||
} | } | ||||
private void getDeviceLocation(){ | private void getDeviceLocation(){ | ||||
public void onComplete(@NonNull Task task) { | public void onComplete(@NonNull Task task) { | ||||
if (task.isSuccessful()){ | if (task.isSuccessful()){ | ||||
currentLocation = (Location) task.getResult(); | currentLocation = (Location) task.getResult(); | ||||
currentTime = Calendar.getInstance().getTime(); | |||||
currentTime = Calendar.getInstance().getTimeInMillis(); | |||||
if (!useOwnGPS) | |||||
{ | |||||
currentLocation.setLatitude(breitengrad); | |||||
currentLocation.setLongitude(laengengrad); | |||||
} | |||||
Toast.makeText(SensorMapsActivity.this, currentTime.toString(), Toast.LENGTH_SHORT).show(); | Toast.makeText(SensorMapsActivity.this, currentTime.toString(), Toast.LENGTH_SHORT).show(); | ||||
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); | moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); | ||||
} | } | ||||
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, zoom)); | mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, zoom)); | ||||
} | } | ||||
//TODO Edward: Mit Methode getDataBaseValues werden die Werte in der DB abgefragt. In Methode processDataBaseValues werden diese beispielhaft ausgelesen. Kann nach Bedarf angepasst werden. | |||||
public void getDataBaseValues() | |||||
//################################################################## ^^^^ GPS Code ^^^^ ############################################################################# | |||||
//##################################################################################################################################################################### | |||||
//Datenbank auslesen mit Listener. D.h. es werden Daten (snapshot) ausgelesen und gleichzeitig ein Listener hinterlegt. | |||||
//Sollten sich danach Daten zu einem beliebigen Zeitpunkt in der DB ändern, wird die Funktion "onDataChange" erneut ausgelöst und wieder Daten (snapshot) ausgelesen. | |||||
public void getDataBaseValuesWithListener() | |||||
{ | { | ||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | mDatenbank = FirebaseDatabase.getInstance().getReference(); | ||||
mDatenbank.addValueEventListener(new ValueEventListener() { | mDatenbank.addValueEventListener(new ValueEventListener() { | ||||
@Override | @Override | ||||
public void onDataChange(@NonNull DataSnapshot snapshot) { | public void onDataChange(@NonNull DataSnapshot snapshot) { | ||||
processDataBaseValues(snapshot); | |||||
processDataBaseUpdate(snapshot); | |||||
} | } | ||||
@Override | @Override | ||||
public void onCancelled(@NonNull DatabaseError error) { | public void onCancelled(@NonNull DatabaseError error) { | ||||
}); | }); | ||||
} | } | ||||
public void processDataBaseValues (DataSnapshot data) | |||||
public void processDataBaseUpdate (DataSnapshot data) | |||||
{ | { | ||||
for (int i = 1; i<=4; i++) //Für alle IDG 1-4 werden Werte ausgegeben | |||||
String breitengradString; | |||||
String laengengradString; | |||||
String vibrationString; | |||||
String androidid; | |||||
int vibratingDevices = 0; | |||||
for (int i = 1; i<=4; i++) | |||||
{ | { | ||||
//####### Auslesen für String-Werte #######: | |||||
String ipAdresse = data.child("overview").child("IDG" + i).child("ip").getValue().toString(); | |||||
//####### Auslesen für boolean-Werte #######: | |||||
String vibrationString = data.child("overview").child("IDG" + i).child("vibration").getValue().toString(); | |||||
boolean vibration; | |||||
if(vibrationString == "true"){ | |||||
vibration = true; | |||||
}else{ | |||||
vibration = false; | |||||
breitengradString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("breitengrad").getValue().toString(); | |||||
laengengradString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("laengengrad").getValue().toString(); | |||||
androidid = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("androidid").getValue().toString(); | |||||
vibrationString = data.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + i).child("vibration").getValue().toString(); | |||||
if(!androidid.isEmpty()) | |||||
{ | |||||
//System.out.println("Index " + i + " ist nicht leer"); | |||||
if(vibrationString == "false") | |||||
{ | |||||
//System.out.println("Index " + i + " Vibration ist false"); | |||||
continue; | |||||
} | |||||
else | |||||
{ | |||||
//System.out.println("Index " + i + " Vibration ist true"); | |||||
if(androidid.equals(getandroidid())) | |||||
{ | |||||
//System.out.println("Index " + i + " Vibration ist von mir. Falscher Alarm"); | |||||
vibrationTrigger = false; | |||||
break; | |||||
} | |||||
else | |||||
{ | |||||
//System.out.println("Index " + i + " Vibration ist nicht von mir. Breitengrad und Laegengrad von Vibrationsquelle bestimmen."); | |||||
breitengradQuellVibration = breitengradString; | |||||
laengengradQuellVibration = laengengradString; | |||||
vibratingDevices++; | |||||
continue; | |||||
} | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
//System.out.println("Index " + i + " ist leer"); | |||||
if(androidid.isEmpty() && !breitengradString.isEmpty() && !laengengradString.isEmpty() && vibrationString.equals("false") && takeGPSfromDB) | |||||
{ | |||||
System.out.println("Index " + i +" -> Leere AdnroidID. Breitengrad und Laengengrad sind belegt. Vibration ist false. Kann als eigenes Gerät beansprucht werden."); | |||||
System.out.println("Index " + i +" -> Übernehme Breitengrad: " + breitengradString + " und Laengengrad: " + laengengradString + " als eigene Position."); | |||||
breitengrad = Double.parseDouble(breitengradString); | |||||
laengengrad = Double.parseDouble(laengengradString); | |||||
useOwnGPS = false; | |||||
takeGPSfromDB = false; | |||||
}else | |||||
{ | |||||
if(!tookOwnGPS && takeGPSfromDB) | |||||
{ | |||||
//System.out.println("Nutze eigene DB. Bin bei Index " + i + " ++++++++++++++++++++++++++++++++++++++++++++"); | |||||
tookOwnGPS = true; | |||||
useOwnGPS = true; | |||||
} | |||||
} | |||||
if(vibrationTrigger == true && (vibratingDevices == 1)) | |||||
{ | |||||
//System.out.println("#########Freigabe zum Schreiben in DB mit Index " + i + " liegt vor. Schreibe nun... !!!!!#####################"); | |||||
/* | |||||
System.out.println("currentLocation.getLatitude(): " + currentLocation.getLatitude()); | |||||
System.out.println("currentLocation.getLongitude(): " + currentLocation.getLongitude()); | |||||
System.out.println("Double.parseDouble(breitengradQuellVibration): " + Double.parseDouble(breitengradQuellVibration)); | |||||
System.out.println("Double.parseDouble(laengengradQuellVibration): " + Double.parseDouble(laengengradQuellVibration)); | |||||
System.out.println("######################################################################################################################################################################"); | |||||
*/ | |||||
vibrationTrigger = false; | |||||
float distanceToEarthquake; | |||||
distanceToEarthquake = distance(currentLocation.getLatitude(), currentLocation.getLongitude(), Double.parseDouble(breitengradQuellVibration), Double.parseDouble(laengengradQuellVibration)); | |||||
System.out.println("Distance to Earthquake: " + distanceToEarthquake); | |||||
long wellenAusbreitungsGeschwindigkeit = 4500; //Meter die Sekunde | |||||
long delayInSeconds = (long) distanceToEarthquake/wellenAusbreitungsGeschwindigkeit; //s | |||||
long delayInMilliSeconds = delayInSeconds*1000; | |||||
setVibrationTimer(delayInMilliSeconds,1500,255,i); | |||||
//setVibrationInDataBase(i); | |||||
break; | |||||
} | |||||
} | } | ||||
} | |||||
//####### Auslesen für double-Werte #######: | |||||
String breitengradString = data.child("overview").child("IDG" + i).child("breitengrad").getValue().toString(); | |||||
String laengengradString = data.child("overview").child("IDG" + i).child("laengengrad").getValue().toString(); | |||||
double breitengrad = Double.parseDouble(breitengradString); | |||||
double laengengrad = Double.parseDouble(laengengradString); | |||||
//####### Auslesen für long-Werte #######: | |||||
String timestampString = data.child("overview").child("IDG" + i).child("timestamp").getValue().toString(); | |||||
long timestamp = Long.parseLong(timestampString); | |||||
//####### Auslesen für ing-Werte #######: | |||||
String amplitudeString = data.child("overview").child("IDG" + i).child("amplitude").getValue().toString(); | |||||
int amplitude = Integer.parseInt(amplitudeString); | |||||
//####### Optional zur Kontrolle #######: | |||||
/* | |||||
System.out.println("IDG" + i + " - IP:" + data.child("overview").child("IDG" + i).child("ip").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Vibration:" + data.child("overview").child("IDG" + i).child("vibration").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Zeitstempel:" + data.child("overview").child("IDG" + i).child("timestamp").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Breitengrad:" + data.child("overview").child("IDG" + i).child("breitengrad").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Laengengrad:" + data.child("overview").child("IDG" + i).child("laengengrad").getValue().toString()); | |||||
System.out.println("IDG" + i + " - Amplitude:" + data.child("overview").child("IDG" + i).child("amplitude").getValue().toString()); | |||||
*/ | |||||
//TODO Weiterer Code von Patrick. @Aron Anschauen ob dieser noch verwendet werden muss da paar Sachen schon in dieser Version umgesetzt worde sind. | |||||
/* | |||||
//####### Auslesen für boolean-Werte #######: | |||||
int i = 1; | |||||
String vibrationString = data.child("overview").child("IDG").child("vibration").getValue().toString(); | |||||
String amplitudeString = data.child("overview").child("IDG").child("amplitude").getValue().toString(); | |||||
boolean vibration; | |||||
if(vibrationString.equals("true")){ | |||||
vibration = true; | |||||
}else{ | |||||
vibration = false; | |||||
} | |||||
int amplitude = Integer.parseInt(amplitudeString); | |||||
// 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); | |||||
} | } | ||||
*/ | |||||
} | |||||
public void setVibrationInDataBase(int k) | |||||
{ | |||||
mDatenbank = FirebaseDatabase.getInstance().getReference(); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("ip").setValue(k + "0.00.00.000"); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("vibration").setValue(true); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("timestamp").setValue(Calendar.getInstance().getTimeInMillis()); //aktueller Zeitstempel wird in Datenbank eingetragen | |||||
if (useOwnGPS) | |||||
{ | |||||
//System.out.println("YYYcurrentLocation.getLatitude(): " + currentLocation.getLatitude()); | |||||
//System.out.println("YYYcurrentLocation.getLongitude(): " + currentLocation.getLongitude()); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("breitengrad").setValue(currentLocation.getLatitude()); //aktueller Breitengrad | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("laengengrad").setValue(currentLocation.getLongitude()); //aktueller Längergrad | |||||
}else{ | |||||
//System.out.println("YYYbreitengrad: " + breitengrad); | |||||
//System.out.println("YYYlaengengrad: " + laengengrad); | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("breitengrad").setValue(breitengrad); //aktueller Breitengrad | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("laengengrad").setValue(laengengrad); //aktueller Längergrad | |||||
} | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("amplitude").setValue(1001); | |||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("localdatetime").setValue(LocalDateTime.now().toString()); | |||||
} | |||||
mDatenbank.child("overviewAronTestetInDiesemAbschnitt").child("IDG" + k).child("androidid").setValue(getandroidid()); | |||||
} | |||||
public String getandroidid () | |||||
{ | |||||
return Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID); | |||||
} | } | ||||
public void getDataBaseFailure (DatabaseError error) | public void getDataBaseFailure (DatabaseError error) | ||||
return distanceInMeters; | return distanceInMeters; | ||||
} | } | ||||
} | |||||
} |
xmlns:tools="http://schemas.android.com/tools" | xmlns:tools="http://schemas.android.com/tools" | ||||
android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
android:layout_height="match_parent" | android:layout_height="match_parent" | ||||
tools:context=".EarthquakeMapsActivity"> | |||||
tools:context=".EarthquakeMapsActivity" | |||||
android:paddingTop="?attr/actionBarSize"> | |||||
<TextView | <TextView | ||||
android:layout_width="wrap_content" | android:layout_width="wrap_content" | ||||
android:layout_height="wrap_content" | android:layout_height="wrap_content" | ||||
android:text="Shake your Smartphone for a Earthquake" | |||||
android:text="@string/shake_your_smartphone_for_a_earthquake" | |||||
android:layout_centerHorizontal="true" | android:layout_centerHorizontal="true" | ||||
android:id="@+id/txtEarthquake"/> | android:id="@+id/txtEarthquake"/> | ||||
<fragment xmlns:android="http://schemas.android.com/apk/res/android" | |||||
xmlns:map="http://schemas.android.com/apk/res-auto" | |||||
xmlns:tools="http://schemas.android.com/tools" | |||||
<androidx.fragment.app.FragmentContainerView xmlns:map="http://schemas.android.com/apk/res-auto" | |||||
android:layout_below="@+id/txtEarthquake" | android:layout_below="@+id/txtEarthquake" | ||||
android:id="@+id/map" | android:id="@+id/map" | ||||
android:name="com.google.android.gms.maps.SupportMapFragment" | android:name="com.google.android.gms.maps.SupportMapFragment" | ||||
android:layout_width="match_parent" | android:layout_width="match_parent" | ||||
android:layout_height="match_parent" | android:layout_height="match_parent" | ||||
android:layout_marginTop="50dp" | |||||
tools:context=".EarthquakeMapsActivity" /> | tools:context=".EarthquakeMapsActivity" /> | ||||
android:layout_centerHorizontal="true" | android:layout_centerHorizontal="true" | ||||
android:layout_below="@id/btnEarthquake"/> | android:layout_below="@id/btnEarthquake"/> | ||||
<Button | |||||
android:id="@+id/buttonDatenBankMuster" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_below="@+id/btnSensor" | |||||
android:layout_centerHorizontal="true" | |||||
android:layout_marginTop="50dp" | |||||
android:text="@string/start_mit_vordefinierter_db" /> | |||||
<Button | |||||
android:id="@+id/buttonDatenBankLeeren" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_below="@+id/buttonDatenBankMuster" | |||||
android:layout_centerHorizontal="true" | |||||
android:text="@string/start_mit_leeren_db" /> | |||||
<Button | |||||
android:id="@+id/buttonReset" | |||||
android:layout_width="wrap_content" | |||||
android:layout_height="wrap_content" | |||||
android:layout_below="@+id/buttonDatenBankLeeren" | |||||
android:layout_centerHorizontal="true" | |||||
android:text="@string/weitermachen_buttonreset" /> | |||||
</RelativeLayout> | </RelativeLayout> |
<string name="title_activity_sensor_maps">Map</string> | <string name="title_activity_sensor_maps">Map</string> | ||||
<string name="be_a_sensor">Be a Sensor</string> | <string name="be_a_sensor">Be a Sensor</string> | ||||
<string name="generate_earthquake">Generate Earthquake</string> | <string name="generate_earthquake">Generate Earthquake</string> | ||||
<string name="buttontest">ButtonTest</string> | |||||
<string name="mit_leeren_db_starten">Mit leeren DB starten</string> | |||||
<string name="mit_voller_db_starten_beispielwerte">Mit voller DB starten (Beispielwerte)</string> | |||||
<string name="mit_voller_db_starten">Mit voller DB starten</string> | |||||
<string name="universalertestbutton">UniversalerTestButton</string> | |||||
<string name="weitermachen">Weitermachen</string> | |||||
<string name="shake_your_smartphone_for_a_earthquake">Shake your Smartphone for a Earthquake</string> | |||||
<string name="start_mit_vordefinierter_db">Start mit vordefinierter DB</string> | |||||
<string name="start_mit_leeren_db">Start mit Leeren DB</string> | |||||
<string name="weitermachen_buttonreset">Weitermachen - Buttonreset</string> | |||||
</resources> | </resources> |
jcenter() | jcenter() | ||||
} | } | ||||
dependencies { | 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' | classpath 'com.google.gms:google-services:4.3.8' | ||||
// NOTE: Do not place your application dependencies here; they belong | // NOTE: Do not place your application dependencies here; they belong |
distributionPath=wrapper/dists | distributionPath=wrapper/dists | ||||
zipStoreBase=GRADLE_USER_HOME | zipStoreBase=GRADLE_USER_HOME | ||||
zipStorePath=wrapper/dists | 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 |