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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. int emptyIDG =0;
  227. for (int i=1; i<=data.getChildrenCount(); i++)
  228. {
  229. androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  230. localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  231. breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  232. laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  233. timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  234. ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  235. if(androidid.isEmpty() && localedatetime.isEmpty() && breitengrad.isEmpty() && laengengrad.isEmpty() && timestamp.isEmpty() && ampltiude.isEmpty())
  236. {
  237. emptyIDG++;
  238. }else{
  239. emptyIDG--;
  240. }
  241. }
  242. //Wenn DB nicht vorhanden ODER leer ist, erstelle diese DB mit IDG 1-5
  243. if(emptyIDG == data.getChildrenCount())
  244. {
  245. for (int i=1; i<=5; i++)
  246. {
  247. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  248. writeEmptyToSecondDataBase(i);
  249. }
  250. }
  251. }
  252. }
  253. @RequiresApi(api = Build.VERSION_CODES.O)
  254. public void writeEmptyToSecondDataBase(int k)
  255. {
  256. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  257. mDatenbank.child("IDG" + k).child("a_androidid").setValue("");
  258. mDatenbank.child("IDG" + k).child("b_localdatetime").setValue("");
  259. mDatenbank.child("IDG" + k).child("d_breitengrad").setValue("");
  260. mDatenbank.child("IDG" + k).child("e_laengengrad").setValue("");
  261. mDatenbank.child("IDG" + k).child("h_timestamp").setValue("");
  262. mDatenbank.child("IDG" + k).child("i_amplitude").setValue("");
  263. }
  264. //###### Bei Vibration der APP ####
  265. public void getDataBaseValuesNoListenerForVibration()
  266. {
  267. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  268. mDatenbank.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
  269. @Override
  270. public void onComplete(@NonNull Task<DataSnapshot> task) {
  271. if (!task.isSuccessful()) {
  272. System.out.println("Datenbank Fehler in getDataBaseValuesNoListener");
  273. }
  274. else {
  275. checkForEmptyIDG(task.getResult());
  276. }
  277. }
  278. });
  279. }
  280. public void checkForEmptyIDG(DataSnapshot data)
  281. {
  282. for (int i=1; i<=data.getChildrenCount(); i++)
  283. {
  284. if(analyzeForEmptyIDG(data,i))
  285. {
  286. writeDeviceToDB(i);
  287. break;
  288. }
  289. }
  290. }
  291. public boolean analyzeForEmptyIDG(DataSnapshot data, int i)
  292. {
  293. String androidid;
  294. String localedatetime;
  295. String breitengrad;
  296. String laengengrad;
  297. String timestamp;
  298. String ampltiude;
  299. androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  300. localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  301. breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  302. laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  303. timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  304. ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  305. if(androidid.isEmpty() && localedatetime.isEmpty() && breitengrad.isEmpty() && laengengrad.isEmpty() && timestamp.isEmpty() && ampltiude.isEmpty())
  306. {
  307. return true;
  308. }else
  309. {
  310. return false;
  311. }
  312. }
  313. public void writeDeviceToDB (int k)
  314. {
  315. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  316. mDatenbank.child("IDG" + k).child("a_androidid").setValue(getandroidid());
  317. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  318. mDatenbank.child("IDG" + k).child("b_localdatetime").setValue(LocalDateTime.now(ZoneOffset.UTC).toString());
  319. }
  320. mDatenbank.child("IDG" + k).child("d_breitengrad").setValue(currentLocation.getLatitude());
  321. mDatenbank.child("IDG" + k).child("e_laengengrad").setValue(currentLocation.getLongitude());
  322. mDatenbank.child("IDG" + k).child("h_timestamp").setValue(Calendar.getInstance().getTimeInMillis());
  323. mDatenbank.child("IDG" + k).child("i_amplitude").setValue(1000);
  324. }
  325. //###### Bei Eintragungen in der DB ####
  326. public void getDataBaseValuesWithListener()
  327. {
  328. mDatenbank = FirebaseDatabase.getInstance().getReference().child("overviewdetection");
  329. mDatenbank.addValueEventListener(new ValueEventListener() {
  330. @Override
  331. public void onDataChange(@NonNull DataSnapshot snapshot) {
  332. analyzeDBchanges(snapshot);
  333. }
  334. @Override
  335. public void onCancelled(@NonNull DatabaseError error) {
  336. getDataBaseFailure(error);
  337. }
  338. });
  339. }
  340. public void analyzeDBchanges(DataSnapshot data)
  341. {
  342. int takenIDG = 0;
  343. for (int i=1; i<=data.getChildrenCount(); i++)
  344. {
  345. if(analyzeForFullIDG(data,i))
  346. {
  347. takenIDG++;
  348. }
  349. }
  350. if(takenIDG>=1 && takenIDG <=5)
  351. {
  352. //Schreibt bei einer neuen vollen IDG ins Display
  353. for(writtenDevices = writtenDevices; writtenDevices <= takenIDG; writtenDevices++)
  354. {
  355. setDisplayText(data,writtenDevices);
  356. enableDisplayReset = true; //Für DB zurücksetzen
  357. }
  358. }else
  359. {
  360. if(enableDisplayReset)
  361. {
  362. resetDisplayText();
  363. enableDisplayReset = false;
  364. }
  365. }
  366. //TODO @Aron hier Jans Code anschließen
  367. //Bei genau 3 Einträgen:
  368. if(takenIDG >= 3 && allowCalculation)
  369. {
  370. Toast.makeText(MainActivity.this, "Es wurden 3 Geräteeinträge registriert!", Toast.LENGTH_SHORT).show();
  371. allowCalculation = false;
  372. System.out.println("Gebe an Berechnung raus");
  373. //An Jans Code anknüpfen
  374. }
  375. }
  376. public boolean analyzeForFullIDG(DataSnapshot data, int i)
  377. {
  378. String androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  379. String localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  380. String breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  381. String laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  382. String timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  383. String ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  384. if(!androidid.isEmpty() && !localedatetime.isEmpty() && !breitengrad.isEmpty() && !laengengrad.isEmpty() && !timestamp.isEmpty() && !ampltiude.isEmpty())
  385. {
  386. return true;
  387. }else
  388. {
  389. return false;
  390. }
  391. }
  392. public void setDisplayText(DataSnapshot data, int i)
  393. {
  394. String androidid = data.child("IDG" + i).child("a_androidid").getValue().toString();
  395. String localedatetime = data.child("IDG" + i).child("b_localdatetime").getValue().toString();
  396. String breitengrad = data.child("IDG" + i).child("d_breitengrad").getValue().toString();
  397. String laengengrad = data.child("IDG" + i).child("e_laengengrad").getValue().toString();
  398. String timestamp = data.child("IDG" + i).child("h_timestamp").getValue().toString();
  399. String ampltiude = data.child("IDG" + i).child("i_amplitude").getValue().toString();
  400. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  401. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  402. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  403. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  404. switch (i){
  405. case 1:
  406. txtDevice1.setText("Device ID 1: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  407. break;
  408. case 2:
  409. txtDevice2.setText("Device ID 2: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  410. break;
  411. case 3:
  412. txtDevice3.setText("Device ID 3: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  413. break;
  414. case 4:
  415. txtDevice4.setText("Device ID 4: "+ androidid + "\n" + "Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad +"\n" + "Time Stamp: "+ timestamp + "\n" + "LocalDateTime: " + localedatetime +"\n"+ "Amplitude: "+ ampltiude+"\n");
  416. break;
  417. default:
  418. System.out.println("Default Case trifft zu");
  419. break;
  420. }
  421. }
  422. public void resetDisplayText()
  423. {
  424. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  425. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  426. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  427. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  428. txtDevice1.setText("");
  429. txtDevice2.setText("");
  430. txtDevice3.setText("");
  431. txtDevice4.setText("");
  432. }
  433. public String getandroidid ()
  434. {
  435. return Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
  436. }
  437. public void getDataBaseFailure (DatabaseError error)
  438. {
  439. System.out.println("Fehler");
  440. Log.w("Datenbankfehler", error.toException());
  441. }
  442. //################################################################## ^^^^ DB Code ^^^^ ###################################################################################
  443. //##########################################################################################################################################################################
  444. //##########################################################################################################################################################################
  445. //################################################################## vvv Set Text of Device 1/2/3 on MainActivity vvv ####################################################
  446. private void setText(){
  447. TextView txtDevice1 = (TextView) findViewById(R.id.txtdevice1);
  448. TextView txtDevice2 = (TextView) findViewById(R.id.txtdevice2);
  449. TextView txtDevice3 = (TextView) findViewById(R.id.txtdevice3);
  450. TextView txtDevice4 = (TextView) findViewById(R.id.txtdevice4);
  451. txtDevice1.setText("Device 1"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  452. txtDevice2.setText("Device 2"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  453. txtDevice3.setText("Device 3"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  454. txtDevice4.setText("Device 4"+"\n"+"Time Stamp: "+ Calendar.getInstance().getTimeInMillis()+"\n"+"Latitude: "+breitengrad+"\n"+ "Longitude: "+ laengengrad+"\n");
  455. }
  456. //################################################################## ^^^^ Set Text of Device 1/2/3 on MainActivity ^^^^ ##################################################
  457. //##########################################################################################################################################################################
  458. //##########################################################################################################################################################################
  459. //################################################################## vvv Maps Code vvv ###################################################################################
  460. public boolean isServiceOK(){
  461. Log.d(TAG, "isServicesOK(): checking google services version");
  462. int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
  463. if (available== ConnectionResult.SUCCESS){
  464. Log.d(TAG,"isServicesOK: Google Play Services is working");
  465. return true;
  466. }
  467. else if (GoogleApiAvailability.getInstance().isUserResolvableError(available)){
  468. Log.d(TAG, "isServicesOK(): an error occured but we can fix it");
  469. Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST);
  470. dialog.show();
  471. }
  472. else {
  473. Toast.makeText(this, "You can`t make map request", Toast.LENGTH_SHORT).show();
  474. }
  475. return false;
  476. }
  477. private void init() {
  478. Button btnEarthquake = (Button) findViewById(R.id.btnEarthquakeLocation);
  479. btnEarthquake.setOnClickListener(new View.OnClickListener() {
  480. @Override
  481. public void onClick(View v) {
  482. Intent intent = new Intent(MainActivity.this, EarthquakeLocation.class);
  483. startActivity(intent);
  484. }
  485. });
  486. Button btnResetDB = (Button) findViewById(R.id.btnResetDB);
  487. btnResetDB.setOnClickListener(new View.OnClickListener() {
  488. @RequiresApi(api = Build.VERSION_CODES.O)
  489. @Override
  490. public void onClick(View v) {
  491. for (int i=1; i<=5; i++)
  492. {
  493. writeEmptyToSecondDataBase(i);
  494. }
  495. }
  496. });
  497. Button btnReadyToDetect = (Button) findViewById(R.id.buttonReadyToDetect);
  498. btnReadyToDetect.setOnClickListener(new View.OnClickListener() {
  499. @RequiresApi(api = Build.VERSION_CODES.O)
  500. @Override
  501. public void onClick(View v) {
  502. mShakeDetectionIsActive = true;
  503. mShakeDetected = false;
  504. btnReadyToDetect.setEnabled(false);
  505. }
  506. });
  507. }
  508. //################################################################## ^^^^ Maps Code ^^^^ ##################################################################
  509. //###########################################################################################################################################################
  510. }