package de.edotzlaff.schockwelle; import android.Manifest; import android.app.ActionBar; import android.content.Context; 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.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.SystemClock; import android.provider.ContactsContract; import android.provider.Settings; import android.util.Log; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; 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.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.time.LocalDateTime; import java.util.Calendar; import java.util.Date; import java.util.Objects; public class EarthquakeMapsActivity extends FragmentActivity implements OnMapReadyCallback { 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 Boolean mLocationPermissionsGranted = false; private GoogleMap mMap; private FusedLocationProviderClient mFusedLocationProviderClient; Long currentTime; Location currentLocation; private DatabaseReference mDatenbank; private double breitengrad; 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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_earthquake_maps); getDataBaseValuesNoListener(); sensorManagementInit(); 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() { String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationPermissionsGranted = true; initMap(); } else { ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); mLocationPermissionsGranted = false; switch (requestCode) { case LOCATION_PERMISSION_REQUEST_CODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mLocationPermissionsGranted = true; initMap(); //initalize or map } } } } private void initMap() { SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show(); mMap = googleMap; if (mLocationPermissionsGranted) { getDeviceLocation(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } mMap.setMyLocationEnabled(true); } // 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)); } private void getDeviceLocation(){ mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); try { if (mLocationPermissionsGranted){ final Task location = mFusedLocationProviderClient.getLastLocation(); location.addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()){ currentLocation = (Location) task.getResult(); //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.setLongitude(laengengrad); moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); //setDataBaseValues(); //Wurde verschoben zu Methode writeEarthquakeToDatabase } else{ Toast.makeText(EarthquakeMapsActivity.this, "Current Location unavailable", Toast.LENGTH_SHORT).show(); } } }); } }catch (SecurityException e){ Log.e(TAG,"Device Location not found" + e.getMessage()); } } private void moveCamera(LatLng latlng, float zoom){ Log.d(TAG,"Latitude: "+latlng.latitude+"Longitude: "+latlng.longitude); 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() { @Override public void onComplete(@NonNull Task 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() { mDatenbank = FirebaseDatabase.getInstance().getReference(); mDatenbank.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { processDataBaseValues(snapshot); //Daten Snapshot, Übergabe an processDataBaseValues } @Override public void onCancelled(@NonNull DatabaseError error) { getDataBaseFailure(error); } }); } public void getDataBaseFailure (DatabaseError error) { System.out.println("Datenbankfehler in gerDataBaseFailure"); Log.w("Datenbankfehler", error.toException()); } */