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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package de.edotzlaff.detection;
  2. import androidx.annotation.NonNull;
  3. import androidx.annotation.RequiresApi;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.core.app.ActivityCompat;
  6. import androidx.core.content.ContextCompat;
  7. import android.Manifest;
  8. import android.app.Dialog;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.pm.PackageManager;
  12. import android.hardware.Sensor;
  13. import android.hardware.SensorEvent;
  14. import android.hardware.SensorEventListener;
  15. import android.hardware.SensorManager;
  16. import android.location.Location;
  17. import android.os.Build;
  18. import android.os.Bundle;
  19. import android.os.CountDownTimer;
  20. import android.provider.Settings;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. import com.google.android.gms.common.ConnectionResult;
  27. import com.google.android.gms.common.GoogleApiAvailability;
  28. import com.google.android.gms.location.FusedLocationProviderClient;
  29. import com.google.android.gms.location.LocationServices;
  30. import com.google.android.gms.maps.GoogleMap;
  31. import com.google.android.gms.tasks.OnCompleteListener;
  32. import com.google.android.gms.tasks.Task;
  33. import com.google.firebase.database.DataSnapshot;
  34. import com.google.firebase.database.DatabaseError;
  35. import com.google.firebase.database.DatabaseReference;
  36. import com.google.firebase.database.FirebaseDatabase;
  37. import com.google.firebase.database.ValueEventListener;
  38. import java.time.LocalDateTime;
  39. import java.time.ZoneOffset;
  40. import java.util.Calendar;
  41. import java.util.Objects;
  42. public class MainActivity extends AppCompatActivity {
  43. private static final String TAG = "MainActivity";
  44. private static final int ERROR_DIALOG_REQUEST = 9001;
  45. private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
  46. private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
  47. private Boolean mLocationPermissionsGranted = false;
  48. private GoogleMap mMap;
  49. private FusedLocationProviderClient mFusedLocationProviderClient;
  50. Long currentTime;
  51. Location currentLocation;
  52. private DatabaseReference mDatenbank;
  53. private double breitengrad;
  54. private double laengengrad;
  55. private boolean useOwnGPS = true;
  56. private int indexID = 1;
  57. private int writtenDevices = 1;
  58. private boolean allowCalculation = true;
  59. private boolean enableDisplayReset = true;
  60. //##########################################################################################################################################################################
  61. //################################################################## vvv SensorParameter vvv ##############################################################################
  62. private SensorManager mSensorManager;
  63. private static final float mUpperThreshold = 1.0f; // für Emulator auf 1.5 setzen
  64. private static final float mLowerThreshold = 0.5f; // für Emulator auf 0.5 setzen
  65. private static final long mShakeDetectionLockTimeMicroSeconds = 3500;
  66. private float mAccel;
  67. private float mAccelCurrent;
  68. private float mAccelLast;
  69. private boolean mShakeDetectionIsActive = false;
  70. private boolean mShakeDetected = false;
  71. private boolean allowShakeEvent = true;
  72. //################################################################## ^^^^ SensorParameter ^^^^ ############################################################################
  73. //##########################################################################################################################################################################
  74. @Override
  75. protected void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. setContentView(R.layout.activity_main);
  78. if (isServiceOK()){
  79. init();
  80. }
  81. sensorManagementInit();
  82. initDataBase();
  83. getLocationPermission();
  84. getDataBaseValuesWithListener();
  85. }
  86. //################################################################## vvv Detection Code vvv ###################################################################################
  87. //##########################################################################################################################################################################
  88. private void sensorManagementInit()
  89. {
  90. //TextView txtEarthquake = (TextView) findViewById(R.id.txtEarthquake);
  91. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  92. Objects.requireNonNull(mSensorManager).registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  93. mAccel = 1f;
  94. mAccelCurrent = SensorManager.GRAVITY_EARTH;
  95. mAccelLast = SensorManager.GRAVITY_EARTH;
  96. mShakeDetectionIsActive = false;
  97. mShakeDetected = false;
  98. }
  99. private final SensorEventListener mSensorListener = new SensorEventListener() {
  100. @Override
  101. public void onSensorChanged(SensorEvent event) {
  102. float x = event.values[0];
  103. float y = event.values[1];
  104. float z = event.values[2];
  105. mAccelLast = mAccelCurrent;
  106. mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
  107. float delta = mAccelCurrent - mAccelLast;
  108. mAccel = mAccel * 0.9f + delta;
  109. // Log.d(TAG,"mAccel: "+ mAccel);
  110. if (mShakeDetectionIsActive && !mShakeDetected) {
  111. if(Math.abs(mAccel) > mUpperThreshold) {
  112. new CountDownTimer(1000, 10) {
  113. public void onTick(long millisUntilFinished) {
  114. mShakeDetectionIsActive = false;
  115. /* if (Math.abs(mAccel) > mUpperThreshold) {
  116. mShakeDetectionIsActive = false;
  117. } else if (Math.abs(mAccel) < mLowerThreshold) {
  118. mShakeDetectionIsActive = true;
  119. this.cancel();
  120. }
  121. */
  122. }
  123. public void onFinish() {
  124. if (Math.abs(mAccel) > mUpperThreshold) {
  125. mShakeDetectionIsActive = false;
  126. mShakeDetected = true;
  127. earthquakeDetected();
  128. } else {
  129. mShakeDetectionIsActive = true;
  130. mShakeDetected = false;
  131. }
  132. }
  133. }.start();
  134. }
  135. }
  136. }
  137. @Override
  138. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  139. }
  140. };
  141. private void earthquakeDetected()
  142. {
  143. Toast.makeText(getApplicationContext(), "Earthquake detected", Toast.LENGTH_SHORT).show();
  144. Log.d(TAG,"!Earthquake detected!");
  145. //Hier wird bei Vibration in DB geschrieben
  146. getDataBaseValuesNoListenerForVibration();
  147. Button buttonReadyToDetect = (Button) findViewById(R.id.buttonReadyToDetect);
  148. buttonReadyToDetect.setEnabled(true);
  149. }
  150. //################################################################## ^^^^ Detection Code ^^^^ ###################################################################################
  151. //##########################################################################################################################################################################
  152. //##########################################################################################################################################################################
  153. //################################################################## vvv Get Location from Device vvv ####################################################################
  154. private void getLocationPermission() {
  155. String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
  156. if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  157. mLocationPermissionsGranted = true;
  158. getDeviceLocation();
  159. } else {
  160. ActivityCompat.requestPermissions(this, permissions, LOCATION_PERMISSION_REQUEST_CODE);
  161. }
  162. }
  163. private void getDeviceLocation(){
  164. mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
  165. try {
  166. if (mLocationPermissionsGranted){
  167. final Task location = mFusedLocationProviderClient.getLastLocation();
  168. location.addOnCompleteListener(new OnCompleteListener() {
  169. @Override
  170. public void onComplete(@NonNull Task task) {
  171. if (task.isSuccessful()){
  172. currentLocation = (Location) task.getResult();
  173. if(useOwnGPS)
  174. {
  175. breitengrad = currentLocation.getLatitude();
  176. laengengrad = currentLocation.getLongitude();
  177. }
  178. currentLocation.setLatitude(breitengrad);
  179. currentLocation.setLongitude(laengengrad);
  180. //setText();
  181. }
  182. else{
  183. Toast.makeText(MainActivity.this, "Current Location unavailable", Toast.LENGTH_SHORT).show();
  184. }
  185. }
  186. });
  187. }
  188. }catch (SecurityException e){
  189. Log.e(TAG,"Device Location not found" + e.getMessage());
  190. }
  191. }
  192. //################################################################## ^^^^ Get Location from Device ^^^^ ##################################################################
  193. //##########################################################################################################################################################################
  194. //##########################################################################################################################################################################
  195. //################################################################## vvv DB Code vvv #####################################################################################
  196. //TODO Aron
  197. //###### Für Start der APP ####
  198. public void initDataBase()
  199. {
  200. getDataBaseValuesNoListener();
  201. }
  202. public void getDataBaseValuesNoListener()
  203. {
  204. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  205. mDatenbank.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
  206. @Override
  207. public void onComplete(@NonNull Task<DataSnapshot> task) {
  208. if (!task.isSuccessful()) {
  209. System.out.println("Datenbank Fehler in getDataBaseValuesNoListener");
  210. }
  211. else {
  212. analyzeForEmptyDB(task.getResult());
  213. }
  214. }
  215. });
  216. }
  217. //Methode bestimmt, ob DB mit leer IDGn Felder überhaupt vorhanden ist
  218. public void analyzeForEmptyDB(DataSnapshot data)
  219. {
  220. String androidid;
  221. String localedatetime;
  222. String breitengrad;
  223. String laengengrad;
  224. String timestamp;
  225. String ampltiude;
  226. String welle;
  227. int emptyIDG =0;
  228. for (int i=1; i<=data.getChildrenCount(); i++)
  229. {
  230. androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  231. localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  232. breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  233. laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  234. timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  235. ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  236. welle = data.child("IDG" + i).child("w_wellengeschwindigkeit").getValue().toString();
  237. if(androidid.isEmpty() && localedatetime.isEmpty() && breitengrad.isEmpty() && laengengrad.isEmpty() && timestamp.isEmpty() && ampltiude.isEmpty() && welle.isEmpty())
  238. {
  239. emptyIDG++;
  240. }else{
  241. emptyIDG--;
  242. }
  243. }
  244. //Wenn DB nicht vorhanden ODER leer ist, erstelle diese DB mit IDG 1-5
  245. if(emptyIDG == data.getChildrenCount())
  246. {
  247. for (int i=1; i<=5; i++)
  248. {
  249. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  250. writeEmptyToSecondDataBase(i);
  251. }
  252. }
  253. }
  254. }
  255. @RequiresApi(api = Build.VERSION_CODES.O)
  256. public void writeEmptyToSecondDataBase(int k)
  257. {
  258. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  259. mDatenbank.child("IDG" + k).child("a_androidid").setValue("");
  260. mDatenbank.child("IDG" + k).child("b_localdatetime").setValue("");
  261. mDatenbank.child("IDG" + k).child("d_breitengrad").setValue("");
  262. mDatenbank.child("IDG" + k).child("e_laengengrad").setValue("");
  263. mDatenbank.child("IDG" + k).child("h_timestamp").setValue("");
  264. mDatenbank.child("IDG" + k).child("i_amplitude").setValue("");
  265. mDatenbank.child("IDG" + k).child("w_wellengeschwindigkeit").setValue("");
  266. }
  267. //###### Bei Vibration der APP ####
  268. public void getDataBaseValuesNoListenerForVibration()
  269. {
  270. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  271. mDatenbank.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
  272. @Override
  273. public void onComplete(@NonNull Task<DataSnapshot> task) {
  274. if (!task.isSuccessful()) {
  275. System.out.println("Datenbank Fehler in getDataBaseValuesNoListener");
  276. }
  277. else {
  278. checkForEmptyIDG(task.getResult());
  279. }
  280. }
  281. });
  282. }
  283. public void checkForEmptyIDG(DataSnapshot data)
  284. {
  285. for (int i=1; i<=data.getChildrenCount(); i++)
  286. {
  287. if(analyzeForEmptyIDG(data,i))
  288. {
  289. writeDeviceToDB(i);
  290. break;
  291. }
  292. }
  293. }
  294. public boolean analyzeForEmptyIDG(DataSnapshot data, int i)
  295. {
  296. String androidid;
  297. String localedatetime;
  298. String breitengrad;
  299. String laengengrad;
  300. String timestamp;
  301. String ampltiude;
  302. String welle;
  303. androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  304. localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  305. breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  306. laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  307. timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  308. ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  309. welle = data.child("IDG" + i).child("w_wellengeschwindigkeit").getValue().toString();
  310. if(androidid.isEmpty() && localedatetime.isEmpty() && breitengrad.isEmpty() && laengengrad.isEmpty() && timestamp.isEmpty() && ampltiude.isEmpty() && welle.isEmpty())
  311. {
  312. return true;
  313. }else
  314. {
  315. return false;
  316. }
  317. }
  318. public void writeDeviceToDB (int k)
  319. {
  320. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  321. mDatenbank.child("IDG" + k).child("a_androidid").setValue(getandroidid());
  322. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  323. mDatenbank.child("IDG" + k).child("b_localdatetime").setValue(LocalDateTime.now(ZoneOffset.UTC).toString());
  324. }
  325. mDatenbank.child("IDG" + k).child("d_breitengrad").setValue(currentLocation.getLatitude());
  326. mDatenbank.child("IDG" + k).child("e_laengengrad").setValue(currentLocation.getLongitude());
  327. mDatenbank.child("IDG" + k).child("h_timestamp").setValue(Calendar.getInstance().getTimeInMillis());
  328. mDatenbank.child("IDG" + k).child("i_amplitude").setValue(1000);
  329. mDatenbank.child("IDG" + k).child("w_wellengeschwindigkeit").setValue(4500);
  330. }
  331. //###### Bei Eintragungen in der DB ####
  332. public void getDataBaseValuesWithListener()
  333. {
  334. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  335. mDatenbank.addValueEventListener(new ValueEventListener() {
  336. @Override
  337. public void onDataChange(@NonNull DataSnapshot snapshot) {
  338. analyzeDBchanges(snapshot);
  339. }
  340. @Override
  341. public void onCancelled(@NonNull DatabaseError error) {
  342. getDataBaseFailure(error);
  343. }
  344. });
  345. }
  346. public void analyzeDBchanges(DataSnapshot data)
  347. {
  348. int takenIDG = 0;
  349. for (int i=1; i<=data.getChildrenCount(); i++)
  350. {
  351. if(analyzeForFullIDG(data,i))
  352. {
  353. takenIDG++;
  354. }
  355. }
  356. if(takenIDG>=1 && takenIDG <=5)
  357. {
  358. //Schreibt bei einer neuen vollen IDG ins Display
  359. for(writtenDevices = writtenDevices; writtenDevices <= takenIDG; writtenDevices++)
  360. {
  361. setDisplayText(data,writtenDevices);
  362. enableDisplayReset = true; //Für DB zurücksetzen
  363. }
  364. }else
  365. {
  366. if(enableDisplayReset)
  367. {
  368. resetDisplayText();
  369. enableDisplayReset = false;
  370. }
  371. }
  372. //TODO @Aron hier Jans Code anschließen
  373. //Bei genau 3 Einträgen:
  374. if(takenIDG >= 3 && allowCalculation)
  375. {
  376. Toast.makeText(MainActivity.this, "3 Geräte regisrtiert - Berechnung möglich!", Toast.LENGTH_SHORT).show();
  377. allowCalculation = false;
  378. Button btnEarthquake = (Button) findViewById(R.id.btnEarthquakeLocation);
  379. btnEarthquake.setEnabled(true);
  380. }
  381. }
  382. public boolean analyzeForFullIDG(DataSnapshot data, int i)
  383. {
  384. String androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  385. String localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  386. String breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  387. String laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  388. String timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  389. String ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  390. String welle = data.child("IDG" + i).child("w_wellengeschwindigkeit").getValue().toString();
  391. if(!androidid.isEmpty() && !localedatetime.isEmpty() && !breitengrad.isEmpty() && !laengengrad.isEmpty() && !timestamp.isEmpty() && !ampltiude.isEmpty() && !welle.isEmpty())
  392. {
  393. return true;
  394. }else
  395. {
  396. return false;
  397. }
  398. }
  399. public void setDisplayText(DataSnapshot data, int i)
  400. {
  401. String androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  402. String localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  403. String breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  404. String laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  405. String timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  406. String ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  407. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  408. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  409. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  410. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  411. switch (i){
  412. case 1:
  413. txtDevice1.setText("Device ID 1: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  414. break;
  415. case 2:
  416. txtDevice2.setText("Device ID 2: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  417. break;
  418. case 3:
  419. txtDevice3.setText("Device ID 3: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  420. break;
  421. case 4:
  422. txtDevice4.setText("Device ID 4: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  423. break;
  424. default:
  425. System.out.println("Default Case trifft zu");
  426. break;
  427. }
  428. }
  429. public void resetDisplayText()
  430. {
  431. Button btnEarthquake = (Button) findViewById(R.id.btnEarthquakeLocation);
  432. btnEarthquake.setEnabled(false);
  433. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  434. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  435. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  436. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  437. txtDevice1.setText("");
  438. txtDevice2.setText("");
  439. txtDevice3.setText("");
  440. txtDevice4.setText("");
  441. }
  442. public String getandroidid ()
  443. {
  444. return Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
  445. }
  446. public void getDataBaseFailure (DatabaseError error)
  447. {
  448. System.out.println("Fehler");
  449. Log.w("Datenbankfehler", error.toException());
  450. }
  451. //################################################################## ^^^^ DB Code ^^^^ ###################################################################################
  452. //##########################################################################################################################################################################
  453. //##########################################################################################################################################################################
  454. //################################################################## vvv Set Text of Device 1/2/3 on MainActivity vvv ####################################################
  455. private void setText(){
  456. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  457. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  458. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  459. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  460. txtDevice1.setText("Device 1"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  461. txtDevice2.setText("Device 2"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  462. txtDevice3.setText("Device 3"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  463. txtDevice4.setText("Device 4"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  464. }
  465. //################################################################## ^^^^ Set Text of Device 1/2/3 on MainActivity ^^^^ ##################################################
  466. //##########################################################################################################################################################################
  467. //##########################################################################################################################################################################
  468. //################################################################## vvv Maps Code vvv ###################################################################################
  469. public boolean isServiceOK(){
  470. Log.d(TAG, "isServicesOK(): checking google services version");
  471. int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
  472. if (available== ConnectionResult.SUCCESS){
  473. Log.d(TAG,"isServicesOK: Google Play Services is working");
  474. return true;
  475. }
  476. else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)){
  477. Log.d(TAG, "isServicesOK(): an error occured but we can fix it");
  478. Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST);
  479. dialog.show();
  480. }
  481. else {
  482. Toast.makeText(this, "You can`t make map request", Toast.LENGTH_SHORT).show();
  483. }
  484. return false;
  485. }
  486. private void init() {
  487. Button btnEarthquake = (Button) findViewById(R.id.btnEarthquakeLocation);
  488. btnEarthquake.setEnabled(false);
  489. btnEarthquake.setOnClickListener(new View.OnClickListener() {
  490. @Override
  491. public void onClick(View v) {
  492. Intent intent = new Intent(MainActivity.this, EarthquakeLocation.class);
  493. startActivity(intent);
  494. }
  495. });
  496. Button btnResetDB = (Button) findViewById(R.id.btnResetDB);
  497. btnResetDB.setOnClickListener(new View.OnClickListener() {
  498. @RequiresApi(api = Build.VERSION_CODES.O)
  499. @Override
  500. public void onClick(View v) {
  501. for (int i=1; i<=5; i++)
  502. {
  503. writeEmptyToSecondDataBase(i);
  504. }
  505. }
  506. });
  507. Button btnReadyToDetect = (Button) findViewById(R.id.buttonReadyToDetect);
  508. btnReadyToDetect.setOnClickListener(new View.OnClickListener() {
  509. @RequiresApi(api = Build.VERSION_CODES.O)
  510. @Override
  511. public void onClick(View v) {
  512. mShakeDetectionIsActive = true;
  513. mShakeDetected = false;
  514. btnReadyToDetect.setEnabled(false);
  515. }
  516. });
  517. }
  518. //################################################################## ^^^^ Maps Code ^^^^ ##################################################################
  519. //###########################################################################################################################################################
  520. }