|
|
|
|
|
|
|
|
package de.edotzlaff.schockwelle; |
|
|
package de.edotzlaff.schockwelle; |
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
|
|
import androidx.fragment.app.FragmentActivity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
|
|
import android.content.pm.PackageManager; |
|
|
import android.location.Location; |
|
|
import android.location.Location; |
|
|
import android.os.Bundle; |
|
|
import android.os.Bundle; |
|
|
import android.os.SystemClock; |
|
|
|
|
|
import android.util.Log; |
|
|
import android.util.Log; |
|
|
import android.widget.TextView; |
|
|
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.CameraUpdateFactory; |
|
|
import com.google.android.gms.maps.GoogleMap; |
|
|
import com.google.android.gms.maps.GoogleMap; |
|
|
import com.google.android.gms.maps.OnMapReadyCallback; |
|
|
import com.google.android.gms.maps.OnMapReadyCallback; |
|
|
import com.google.android.gms.maps.SupportMapFragment; |
|
|
import com.google.android.gms.maps.SupportMapFragment; |
|
|
import com.google.android.gms.maps.model.LatLng; |
|
|
import com.google.android.gms.maps.model.LatLng; |
|
|
import com.google.android.gms.maps.model.MarkerOptions; |
|
|
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.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 org.w3c.dom.Text; |
|
|
|
|
|
|
|
|
import java.util.Calendar; |
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
|
public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCallback { |
|
|
public class SensorMapsActivity 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; |
|
|
|
|
|
|
|
|
|
|
|
//vars |
|
|
|
|
|
private Boolean mLocationPermissionsGranted = false; |
|
|
private GoogleMap mMap; |
|
|
private GoogleMap mMap; |
|
|
|
|
|
private FusedLocationProviderClient mFusedLocationProviderClient; |
|
|
|
|
|
|
|
|
|
|
|
Date currentTime; |
|
|
|
|
|
Location currentLocation; |
|
|
|
|
|
|
|
|
|
|
|
private double breitengrad; |
|
|
|
|
|
private double laengengrad; |
|
|
|
|
|
|
|
|
private DatabaseReference mDatenbank; |
|
|
private DatabaseReference mDatenbank; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TextView tv= (TextView) findViewById(R.id.txtSensor); |
|
|
TextView tv= (TextView) findViewById(R.id.txtSensor); |
|
|
|
|
|
|
|
|
initMap(); |
|
|
|
|
|
|
|
|
getLocationPermission(); //Zuerst werden die aktuellen Standortdaten ermittelt |
|
|
|
|
|
|
|
|
|
|
|
//getDataBaseValues(); // Hier werden die Daten von der DB abgefragt //TODO Aron: sobald die Methode aufgerufen wird crashed die APP |
|
|
|
|
|
|
|
|
|
|
|
//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? |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
//initalize or map |
|
|
|
|
|
initMap(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void initMap(){ |
|
|
void initMap(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onMapReady(GoogleMap googleMap) { |
|
|
public void onMapReady(GoogleMap googleMap) { |
|
|
|
|
|
|
|
|
|
|
|
Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show(); |
|
|
mMap = googleMap; |
|
|
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 |
|
|
// Add a marker in Sydney and move the camera |
|
|
LatLng sydney = new LatLng(-34, 151); |
|
|
LatLng sydney = new LatLng(-34, 151); |
|
|
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); |
|
|
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); |
|
|
mMap.moveCamera(CameraUpdateFactory.newLatLng(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().getTime(); |
|
|
|
|
|
Toast.makeText(SensorMapsActivity.this, currentTime.toString(), Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),15f); |
|
|
|
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
Toast.makeText(SensorMapsActivity.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)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//TODO Edward: Mit Methode getDataBaseValues werden die Werte in der DB abgefragt. In Methode processDataBaseValues werden diese beispielhaft ausgelesen. Kann nach Bedarf angepasst werden. |
|
|
//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() |
|
|
public void getDataBaseValues() |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
Log.w("Datenbankfehler", error.toException()); |
|
|
Log.w("Datenbankfehler", error.toException()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//TODO Edward Distanz zwischen zwei Punkten berechnen |
|
|
|
|
|
//TODO Edward Dauer für Timer berechnen bis Smartphone vibriert |
|
|
//TODO Edward Dauer für Timer berechnen bis Smartphone vibriert |
|
|
/* |
|
|
|
|
|
|
|
|
|
|
|
private float distance(double currentlatitude, double currentlongitude, double originLat, double originLon) { |
|
|
private float distance(double currentlatitude, double currentlongitude, double originLat, double originLon) { |
|
|
|
|
|
|
|
|
float[] results = new float[1]; |
|
|
float[] results = new float[1]; |
|
|
Location.distanceBetween(currentlatitude, currentlongitude, -34, 151, results); |
|
|
|
|
|
|
|
|
Location.distanceBetween(currentlatitude, currentlongitude, originLat, originLon, results); |
|
|
float distanceInMeters = results[0]; |
|
|
float distanceInMeters = results[0]; |
|
|
|
|
|
|
|
|
return distanceInMeters; |
|
|
return distanceInMeters; |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |