Projektteil 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainActivity.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package de.edotzlaff.detection;
  2. import androidx.annotation.NonNull;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.core.app.ActivityCompat;
  5. import androidx.core.content.ContextCompat;
  6. import android.Manifest;
  7. import android.app.Dialog;
  8. import android.content.Intent;
  9. import android.content.pm.PackageManager;
  10. import android.hardware.Sensor;
  11. import android.hardware.SensorEvent;
  12. import android.hardware.SensorEventListener;
  13. import android.hardware.SensorManager;
  14. import android.location.Location;
  15. import android.os.Bundle;
  16. import android.os.CountDownTimer;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22. import com.google.android.gms.common.ConnectionResult;
  23. import com.google.android.gms.common.GoogleApiAvailability;
  24. import com.google.android.gms.location.FusedLocationProviderClient;
  25. import com.google.android.gms.location.LocationServices;
  26. import com.google.android.gms.maps.GoogleMap;
  27. import com.google.android.gms.maps.OnMapReadyCallback;
  28. import com.google.android.gms.maps.SupportMapFragment;
  29. import com.google.android.gms.maps.model.LatLng;
  30. import com.google.android.gms.tasks.OnCompleteListener;
  31. import com.google.android.gms.tasks.Task;
  32. import org.w3c.dom.Text;
  33. import java.util.Calendar;
  34. public class MainActivity extends AppCompatActivity {
  35. private static final String TAG = "MainActivity";
  36. private static final int ERROR_DIALOG_REQUEST = 9001;
  37. private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
  38. private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
  39. private Boolean mLocationPermissionsGranted = false;
  40. private GoogleMap mMap;
  41. private FusedLocationProviderClient mFusedLocationProviderClient;
  42. Long currentTime;
  43. Location currentLocation;
  44. // private DatabaseReference mDatenbank;
  45. private double breitengrad;
  46. private double laengengrad;
  47. private boolean useOwnGPS = true;
  48. private int indexID = 1;
  49. //##########################################################################################################################################################################
  50. //################################################################## vvv SensorParameter vvv ##############################################################################
  51. private SensorManager mSensorManager;
  52. private static final float mUpperThreshold = 1.5f; // für Emulator auf 1.5 setzen
  53. private static final float mLowerThreshold = 0.5f; // für Emulator auf 0.5 setzen
  54. private static final long mShakeDetectionLockTimeMicroSeconds = 10000;
  55. private float mAccel;
  56. private float mAccelCurrent;
  57. private float mAccelLast;
  58. private boolean mShakeDetectionIsActive = false;
  59. private boolean mShakeDetected = false;
  60. private boolean allowShakeEvent = true;
  61. //################################################################## ^^^^ SensorParameter ^^^^ ############################################################################
  62. //##########################################################################################################################################################################
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.activity_main);
  67. if (isServiceOK()){
  68. init();
  69. }
  70. //Detection funktion here
  71. getLocationPermission();
  72. //DB function here
  73. }
  74. //################################################################## vvv Detection Code vvv ###################################################################################
  75. //##########################################################################################################################################################################
  76. private final SensorEventListener mSensorListener = new SensorEventListener() {
  77. @Override
  78. public void onSensorChanged(SensorEvent event) {
  79. float x = event.values[0];
  80. float y = event.values[1];
  81. float z = event.values[2];
  82. mAccelLast = mAccelCurrent;
  83. mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
  84. float delta = mAccelCurrent - mAccelLast;
  85. mAccel = mAccel * 0.9f + delta;
  86. // Log.d(TAG,"mAccel: "+ mAccel);
  87. if (mShakeDetectionIsActive) {
  88. if(Math.abs(mAccel) > mUpperThreshold) {
  89. new CountDownTimer(1000, 10) {
  90. public void onTick(long millisUntilFinished) {
  91. if (Math.abs(mAccel) > mUpperThreshold) {
  92. mShakeDetectionIsActive = false;
  93. } else if (Math.abs(mAccel) < mLowerThreshold) {
  94. mShakeDetectionIsActive = true;
  95. this.cancel();
  96. }
  97. }
  98. public void onFinish() {
  99. if (Math.abs(mAccel) > mUpperThreshold) {
  100. mShakeDetectionIsActive = false;
  101. mShakeDetected = true;
  102. earthquakeDetected();
  103. } else {
  104. mShakeDetectionIsActive = true;
  105. mShakeDetected = false;
  106. }
  107. }
  108. }.start();
  109. }
  110. }
  111. }
  112. @Override
  113. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  114. }
  115. };
  116. private void earthquakeDetected()
  117. {
  118. Toast.makeText(getApplicationContext(), "!Earthquake detected!", Toast.LENGTH_SHORT).show();
  119. //setDataBaseValues();
  120. }
  121. //################################################################## ^^^^ Detection Code ^^^^ ###################################################################################
  122. //##########################################################################################################################################################################
  123. //##########################################################################################################################################################################
  124. //################################################################## vvv Get Location from Device vvv ####################################################################
  125. private void getLocationPermission() {
  126. String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
  127. if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  128. mLocationPermissionsGranted = true;
  129. getDeviceLocation();
  130. } else {
  131. ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE);
  132. }
  133. }
  134. private void getDeviceLocation(){
  135. mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
  136. try {
  137. if (mLocationPermissionsGranted){
  138. final Task location = mFusedLocationProviderClient.getLastLocation();
  139. location.addOnCompleteListener(new OnCompleteListener() {
  140. @Override
  141. public void onComplete(@NonNull Task task) {
  142. if (task.isSuccessful()){
  143. currentLocation = (Location) task.getResult();
  144. if(useOwnGPS)
  145. {
  146. breitengrad = currentLocation.getLatitude();
  147. laengengrad = currentLocation.getLongitude();
  148. }
  149. currentLocation.setLatitude(breitengrad);
  150. currentLocation.setLongitude(laengengrad);
  151. setText();
  152. }
  153. else{
  154. Toast.makeText(MainActivity.this, "Current Location unavailable", Toast.LENGTH_SHORT).show();
  155. }
  156. }
  157. });
  158. }
  159. }catch (SecurityException e){
  160. Log.e(TAG,"Device Location not found" + e.getMessage());
  161. }
  162. }
  163. //################################################################## ^^^^ Get Location from Device ^^^^ ##################################################################
  164. //##########################################################################################################################################################################
  165. //##########################################################################################################################################################################
  166. //################################################################## vvv DB Code vvv #####################################################################################
  167. //TODO Aron
  168. //################################################################## ^^^^ DB Code ^^^^ ###################################################################################
  169. //##########################################################################################################################################################################
  170. //##########################################################################################################################################################################
  171. //################################################################## vvv Set Text of Device 1/2/3 on MainActivity vvv ####################################################
  172. private void setText(){
  173. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  174. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  175. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  176. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  177. txtDevice1.setText("Device 1"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  178. txtDevice2.setText("Device 2"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  179. txtDevice3.setText("Device 3"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  180. txtDevice4.setText("Device 4"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  181. }
  182. //################################################################## ^^^^ Set Text of Device 1/2/3 on MainActivity ^^^^ ##################################################
  183. //##########################################################################################################################################################################
  184. //##########################################################################################################################################################################
  185. //################################################################## vvv Maps Code vvv ###################################################################################
  186. public boolean isServiceOK(){
  187. Log.d(TAG, "isServicesOK(): checking google services version");
  188. int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
  189. if (available== ConnectionResult.SUCCESS){
  190. Log.d(TAG,"isServicesOK: Google Play Services is working");
  191. return true;
  192. }
  193. else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)){
  194. Log.d(TAG, "isServicesOK(): an error occured but we can fix it");
  195. Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST);
  196. dialog.show();
  197. }
  198. else {
  199. Toast.makeText(this, "You can`t make map request", Toast.LENGTH_SHORT).show();
  200. }
  201. return false;
  202. }
  203. private void init() {
  204. Button btnEarthquake = (Button) findViewById(R.id.btnEarthquakeLocation);
  205. btnEarthquake.setOnClickListener(new View.OnClickListener() {
  206. @Override
  207. public void onClick(View v) {
  208. Intent intent = new Intent(MainActivity.this, EarthquakeLocation.class);
  209. startActivity(intent);
  210. }
  211. });
  212. }
  213. //################################################################## ^^^^ Maps Code ^^^^ ##################################################################
  214. //###########################################################################################################################################################
  215. }