1 package org.average.whereami;
3 import org.average.whereami.PersistentStore;
4 import org.average.whereami.SayWhen;
7 import java.io.IOException;
9 import com.google.api.client.googleapis.json.GoogleJsonResponseException;
10 import com.google.api.client.http.HttpResponseException;
11 import com.google.api.services.latitude.Latitude;
12 import com.google.api.services.latitude.LatitudeRequest;
13 import com.google.api.services.latitude.model.Location;
15 import android.content.Context;
16 import android.content.res.Resources;
17 import android.text.format.Time;
18 import android.util.Log;
20 public final class LastLocation extends Oracle {
22 final String TAG = getClass().getName();
24 private Latitude latitude;
26 private String[] loc_names;
27 private String[] loc_lats;
28 private String[] loc_lons;
29 private String[] loc_dists;
30 private String myname;
31 private String registered;
32 private SayWhen sayWhen;
34 public LastLocation(final Resources res, final PersistentStore store) {
36 loc_names = res.getStringArray(R.array.loc_names);
37 loc_lats = res.getStringArray(R.array.loc_lats);
38 loc_lons = res.getStringArray(R.array.loc_lons);
39 loc_dists = res.getStringArray(R.array.loc_dists);
40 myname = res.getString(R.string.myname);
41 registered = res.getString(R.string.registered);
42 sayWhen = new SayWhen(res);
44 latitude = Latitude.builder(httpTransport, jsonFactory)
45 .setHttpRequestInitializer(accessProtectedResource)
46 .setApplicationName("WhereAmI/1.0").build();
50 public final String getResult() {
52 Log.v(TAG, "entering getResult");
53 Latitude.CurrentLocation.Get request =
54 latitude.currentLocation().get();
55 request.setGranularity("best");
56 Location cloc = request.execute();
57 Double lat = Double.parseDouble(
58 cloc.getLatitude().toString());
59 Double lon = Double.parseDouble(
60 cloc.getLongitude().toString());
61 Long tsm = Long.parseLong(
62 cloc.getTimestampMs().toString());
63 return locationMessage(lat, lon, tsm);
64 } catch (GoogleJsonResponseException e) {
65 Log.e(TAG, "GoogleJsonResponseException: " + e);
66 if (e.getResponse().getStatusCode() == 401) {
67 return authErrorMessage;
69 return e.getMessage();
71 } catch (HttpResponseException e) {
72 Log.e(TAG, "HttpResponseException: " + e);
73 return e.getMessage();
74 } catch (IOException e) {
76 return e.getMessage();
80 private String locationMessage(Double lat, Double lon, Long tsm) {
81 Log.v(TAG, "entering locationMessage(" + lat +
82 "," + lon + "," + tsm + ")");
83 String locmsg = "<UNSET>";
84 String agomsg = "<UNSET>";
85 for (int i = 0; i < loc_names.length; i++) {
86 Double dist = haversine(lat, lon,
87 Double.parseDouble(loc_lats[i]),
88 Double.parseDouble(loc_lons[i]));
89 Log.v(TAG, "Dist from " + loc_names[i] +
91 if ((loc_dists[i] == "0") ||
92 (dist < Double.parseDouble(loc_dists[i]))) {
93 locmsg = loc_names[i];
97 return myname + " " + locmsg + ", " + registered +
98 " " + sayWhen.say(tsm);
101 private Double toRad(Double deg) {
102 return deg * 3.14159265 / 180;
105 private Double haversine(Double lat1, Double lon1,
106 Double lat2, Double lon2) {
107 Double dlat = toRad(lat2 - lat1);
108 Double dlon = toRad(lon2 - lon1);
109 Double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
110 Math.sin(dlon / 2) * Math.sin(dlon / 2) *
111 Math.cos(toRad(lat1)) * Math.cos(toRad(lat2));
112 Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));