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 14KB

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