package com jtaxi services detectors import android content Context im

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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.helper.MapHelper;
import com.jtaxi.services.JsonClientUsage;
import com.jtaxi.services.JsonResponseHandler;
import com.jtaxi.services.http.RequestAction;
import org.json.JSONObject;
/**
* Created by Roman on 3/26/14.
*/
public class EtaDetector {
public static final int LESS_MINUTE = 0;
public static final int MINUTE = 1;
public static RequestAction calculateETA(Context context, Address fromAddress, Address toAddress, Callback callback) {
LatLng from = MapHelper.fromAddress(fromAddress);
LatLng to = MapHelper.fromAddress(toAddress);
return calculateETA(context, from, to, callback);
}
public static RequestAction calculateETA(Context context, Location fromLocation, Location toLocation, Callback callback) {
LatLng from = MapHelper.fromLocation(fromLocation);
LatLng to = MapHelper.fromLocation(toLocation);
return calculateETA(context, from, to, callback);
}
public static RequestAction calculateETA(final Context context, LatLng from, LatLng to, final Callback callback) {
return JsonClientUsage.getHours(context, from, to, new JsonResponseHandler.RequestCallback() {
private static final String MINUTES = "minutes";
@Override
public void onSuccess(JSONObject data) {
Integer minutes = null;
try {
if (data.opt(MINUTES) != null) {
minutes = data.getInt(MINUTES);
}
} catch (Exception e) {
if (ProjectConfig.DEBUG) {
e.printStackTrace();
}
}
if (minutes != null) {
callback.onTimeCalculated(minutes);
} else {
callback.onFailure("Неможливо визначити час прибуття");
}
}
@Override
public void onFailure(String message) {
callback.onFailure(message);
}
});
}
public static interface Callback {
public void onTimeCalculated(int minutes);
public void onFailure(String message);
}
}