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

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