package com.jtaxi.services.detectors; import android.content.Context; import android.location.Address; import android.location.Location; import com.google.android.gms.maps.model.LatLng; import com.jtaxi.ProjectConfig; import com.jtaxi.services.JsonClientUsage; import com.jtaxi.services.JsonResponseHandler; import com.jtaxi.services.http.RequestAction; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; /** * Created by Roman on 3/16/14. */ public class NearbyCarsDetector { private static final int MAX_CARS = 8; public static RequestAction getNearbyCars(Context context, LatLng latLng, final Callback callback) { double latitude = latLng.latitude; double longitude = latLng.longitude; return getNearbyCars(context, latitude, longitude, callback); } public static RequestAction getNearbyCars(Context context, Location location, final Callback callback) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); return getNearbyCars(context, latitude, longitude, callback); } public static RequestAction getNearbyCars(Context context, Address address, final Callback callback) { double latitude = address.getLatitude(); double longitude = address.getLongitude(); return getNearbyCars(context, latitude, longitude, callback); } public static RequestAction getNearbyCars(Context context, double latitude, double longitude, final Callback callback) { return JsonClientUsage.getNearbyCars(context, latitude, longitude, new JsonResponseHandler.RequestCallback() { @Override public void onSuccess(JSONObject data) { callback.onNotFound(); } @Override public void onSuccess(JSONArray data) { ArrayList nearbyCars = new ArrayList(MAX_CARS); for (int carIndex = 0; carIndex < data.length() && carIndex < MAX_CARS; carIndex++) { try { JSONObject car = data.getJSONObject(carIndex); nearbyCars.add(car); } catch (Exception e) { if (ProjectConfig.DEBUG) { e.printStackTrace(); } } } if (nearbyCars.isEmpty()) { callback.onNotFound(); } else { callback.onFound(nearbyCars); } } @Override public void onFailure(String message) { callback.onFailure(message); } }); } public static interface Callback { public void onFound(ArrayList nearbyCars); public void onNotFound(); public void onFailure(String message); } }