// --------------------------------------------------------------------------------------------- // Method for checking if the user is connected on the internet. ------------------------------- public static boolean isOnline(Context contextFrom) { ConnectivityManager connectivityManager = (ConnectivityManager) contextFrom.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } else { AppServices.displayToast(contextFrom, "Κανένα διαθέσιμο δίκτυο"); return false; } }
// --------------------------------------------------------------------------------------------- // Method that draws the route between two locations on the map. ------------------------------- private void drawRoute(List<List<HashMap<String, String>>> routes) { ArrayList<LatLng> points; PolylineOptions polylineOptions = null; if (routes == null) { AppServices.displayToast( VetFirstActivity.this, "Προέκυψε κάποιο σφάλμα κατά τον προσδιορισμό της διαδρομής"); } else { for (int i = 0; i < routes.size(); i++) { points = new ArrayList<>(); polylineOptions = new PolylineOptions(); List<HashMap<String, String>> path = routes.get(i); for (int j = 0; j < path.size(); j++) { HashMap<String, String> point = path.get(j); if (j == 0) { distance = point.get("distance"); continue; } else if (j == 1) { duration = point.get("duration"); continue; } double lat = Double.parseDouble(point.get("lat")); double lng = Double.parseDouble(point.get("lng")); LatLng position = new LatLng(lat, lng); points.add(position); } polylineOptions.addAll(points); polylineOptions.width(4); polylineOptions.color(Color.rgb(48, 48, 48)); } Log.d( "VetFirstActivity", "distance (" + distance + ") and duration (" + duration + ") are set"); vetMap.addPolyline(polylineOptions); updateDistanceAndDurationInfo(distance, duration); } }
// --------------------------------------------------------------------------------------------- @Override // Method that handles the different options of the menu. ---------------------------- public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_login) { if (ConnectionManager.isOnline(this)) { Intent loginIntent = new Intent(this, LoginActivity.class); startActivity(loginIntent); } } else if (id == R.id.action_logout) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); SharedPreferences.Editor editor = sharedPreferences.edit(); Integer loggedInUserID = sharedPreferences.getInt("user_id", 0); Integer checkInLocationID = sharedPreferences.getInt("check_in_location_id", 0); if (checkInLocationID != 0) { ConnectionManager.getCheckOutURL(loggedInUserID, checkInLocationID); } editor.clear(); editor.apply(); AppServices.displayToast(this, "Επιτυχής αποσύνδεση!"); } else if (id == R.id.action_google_play_licence) { if (ConnectionManager.isOnline(this)) { Intent gpsLicenceIntent = new Intent(this, GooglePlayLicenceActivity.class); startActivity(gpsLicenceIntent); } } else if (id == R.id.action_app_licence) { Intent appLicenceIntent = new Intent(this, AppLicenceActivity.class); startActivity(appLicenceIntent); } else if (id == R.id.action_privacy_policy) { Intent privacyPolicyIntent = new Intent(this, PrivacyPolicyActivity.class); startActivity(privacyPolicyIntent); } else if (id == R.id.action_about) { Intent aboutIntent = new Intent(this, AboutActivity.class); startActivity(aboutIntent); } return true; }
// --------------------------------------------------------------------------------------------- // Method for finding current location of the user. -------------------------------------------- private boolean specifyCurrentLocation() { vetMap.setMyLocationEnabled(true); Log.d("VetFirstActivity", "defining current location..."); final String locationProvider = LocationManager.NETWORK_PROVIDER; LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { currentLocationLat = location.getLatitude(); currentLocationLng = location.getLongitude(); Log.d( "VetFirstActivity", "onLocationChanged just set current location (" + currentLocationLat + ", " + currentLocationLng + ")"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("VetFirstActivity", "onStatusChanged"); } @Override public void onProviderEnabled(String provider) { LocationManager tempLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location tempMyLocation = tempLocationManager.getLastKnownLocation(locationProvider); currentLocationLat = tempMyLocation.getLatitude(); currentLocationLng = tempMyLocation.getLongitude(); Log.d( "VetFirstActivity", "onProviderEnabled just set current location (" + currentLocationLat + ", " + currentLocationLng + ")"); } @Override public void onProviderDisabled(String provider) { Log.d("VetFirstActivity", "onProviderDisabled"); AppServices.displayToast( VetFirstActivity.this, "Η λειτουργία εντοπισμού της τοποθεσίας σας δεν είναι ενεργοποιημένη"); } }; locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener); if ((currentLocationLat == null) && (currentLocationLng == null)) { Location myLocation = locationManager.getLastKnownLocation(locationProvider); if (myLocation != null) { currentLocationLat = myLocation.getLatitude(); currentLocationLng = myLocation.getLongitude(); Log.d( "VetFirstActivity", "myLocation (" + currentLocationLat + ", " + currentLocationLng + ")"); } } if ((currentLocationLat == null) && (currentLocationLng == null)) { AppServices.displayToast(VetFirstActivity.this, "Αδυναμία προσδιορισμού της τοποθεσίας σας"); return false; } else { currentLocationLatLng = new LatLng(currentLocationLat, currentLocationLng); vetMap.addMarker( new MarkerOptions() .position(currentLocationLatLng) .title("Βρίσκεστε εδώ") .icon(currentLocationIcon)); Log.d("VetFirstActivity", "marker of current location is set on map"); // Stop listening for updates of the current location. locationManager.removeUpdates(locationListener); return true; } }