package de.edotzlaff.detection; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.app.Dialog; import android.content.Intent; 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.os.Bundle; import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; 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.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import org.w3c.dom.Text; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final int ERROR_DIALOG_REQUEST = 9001; 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 = true; private int indexID = 1; //########################################################################################################################################################################## //################################################################## vvv SensorParameter vvv ############################################################################## private SensorManager mSensorManager; private static final float mUpperThreshold = 1.5f; // für Emulator auf 1.5 setzen private static final float mLowerThreshold = 0.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; //################################################################## ^^^^ SensorParameter ^^^^ ############################################################################ //########################################################################################################################################################################## @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (isServiceOK()){ init(); } //Detection funktion here getLocationPermission(); //DB function here } //################################################################## vvv Detection Code vvv ################################################################################### //########################################################################################################################################################################## 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(1000, 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; earthquakeDetected(); } else { mShakeDetectionIsActive = true; mShakeDetected = false; } } }.start(); } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; private void earthquakeDetected() { Toast.makeText(getApplicationContext(), "!Earthquake detected!", Toast.LENGTH_SHORT).show(); //setDataBaseValues(); } //################################################################## ^^^^ Detection Code ^^^^ ################################################################################### //########################################################################################################################################################################## //########################################################################################################################################################################## //################################################################## vvv Get Location from Device vvv #################################################################### private void getLocationPermission() { String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mLocationPermissionsGranted = true; getDeviceLocation(); } else { ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE); } } 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(); if(useOwnGPS) { breitengrad = currentLocation.getLatitude(); laengengrad = currentLocation.getLongitude(); } currentLocation.setLatitude(breitengrad); currentLocation.setLongitude(laengengrad); setText(); } else{ Toast.makeText(MainActivity.this, "Current Location unavailable", Toast.LENGTH_SHORT).show(); } } }); } }catch (SecurityException e){ Log.e(TAG,"Device Location not found" + e.getMessage()); } } //################################################################## ^^^^ Get Location from Device ^^^^ ################################################################## //########################################################################################################################################################################## //########################################################################################################################################################################## //################################################################## vvv DB Code vvv ##################################################################################### //TODO Aron //################################################################## ^^^^ DB Code ^^^^ ################################################################################### //########################################################################################################################################################################## //########################################################################################################################################################################## //################################################################## vvv Set Text of Device 1/2/3 on MainActivity vvv #################################################### private void setText(){ TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1); TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2); TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3); TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4); txtDevice1.setText("Device 1"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n"); txtDevice2.setText("Device 2"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n"); txtDevice3.setText("Device 3"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n"); txtDevice4.setText("Device 4"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n"); } //################################################################## ^^^^ Set Text of Device 1/2/3 on MainActivity ^^^^ ################################################## //########################################################################################################################################################################## //########################################################################################################################################################################## //################################################################## vvv Maps Code vvv ################################################################################### public boolean isServiceOK(){ Log.d(TAG, "isServicesOK(): checking google services version"); int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this); if (available== ConnectionResult.SUCCESS){ Log.d(TAG,"isServicesOK: Google Play Services is working"); return true; } else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)){ Log.d(TAG, "isServicesOK(): an error occured but we can fix it"); Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST); dialog.show(); } else { Toast.makeText(this, "You can`t make map request", Toast.LENGTH_SHORT).show(); } return false; } private void init() { Button btnEarthquake = (Button) findViewById(R.id.btnEarthquakeLocation); btnEarthquake.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, EarthquakeLocation.class); startActivity(intent); } }); } //################################################################## ^^^^ Maps Code ^^^^ ################################################################## //########################################################################################################################################################### }