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 String[] loc_names;
25 private String[] loc_lats;
26 private String[] loc_lons;
27 private String[] loc_dists;
28 private String myname;
29 private String registered;
30 private SayWhen sayWhen;
32 public LastLocation(final Resources res, final PersistentStore store) {
34 loc_names = res.getStringArray(R.array.loc_names);
35 loc_lats = res.getStringArray(R.array.loc_lats);
36 loc_lons = res.getStringArray(R.array.loc_lons);
37 loc_dists = res.getStringArray(R.array.loc_dists);
38 myname = res.getString(R.string.myname);
39 registered = res.getString(R.string.registered);
40 sayWhen = new SayWhen(res);
44 public final String getResult() {
45 Latitude latitude = Latitude.builder(httpTransport, jsonFactory)
46 .setHttpRequestInitializer(accessProtectedResource())
47 .setApplicationName("WhereAmI/1.0").build();
49 Log.v(TAG, "entering getResult");
50 Latitude.CurrentLocation.Get request =
51 latitude.currentLocation().get();
52 request.setGranularity("best");
53 Location cloc = request.execute();
54 Double lat = Double.parseDouble(
55 cloc.getLatitude().toString());
56 Double lon = Double.parseDouble(
57 cloc.getLongitude().toString());
58 Long tsm = Long.parseLong(
59 cloc.getTimestampMs().toString());
60 return locationMessage(lat, lon, tsm);
61 } catch (GoogleJsonResponseException e) {
62 Log.e(TAG, "GoogleJsonResponseException: " + e);
63 if (e.getResponse().getStatusCode() == 401) {
64 return authErrorMessage;
66 return e.getMessage();
68 } catch (HttpResponseException e) {
69 Log.e(TAG, "HttpResponseException: " + e);
70 return e.getMessage();
71 } catch (IOException e) {
73 return e.getMessage();
77 private String locationMessage(Double lat, Double lon, Long tsm) {
78 Log.v(TAG, "entering locationMessage(" + lat +
79 "," + lon + "," + tsm + ")");
80 String locmsg = "<UNSET>";
81 String agomsg = "<UNSET>";
82 for (int i = 0; i < loc_names.length; i++) {
83 Double dist = haversine(lat, lon,
84 Double.parseDouble(loc_lats[i]),
85 Double.parseDouble(loc_lons[i]));
86 Log.v(TAG, "Dist from " + loc_names[i] +
88 if ((loc_dists[i] == "0") ||
89 (dist < Double.parseDouble(loc_dists[i]))) {
90 locmsg = loc_names[i];
94 return myname + " " + locmsg + ", " + registered +
95 " " + sayWhen.say(tsm);
98 private Double toRad(Double deg) {
99 return deg * 3.14159265 / 180;
102 private Double haversine(Double lat1, Double lon1,
103 Double lat2, Double lon2) {
104 Double dlat = toRad(lat2 - lat1);
105 Double dlon = toRad(lon2 - lon1);
106 Double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
107 Math.sin(dlon / 2) * Math.sin(dlon / 2) *
108 Math.cos(toRad(lat1)) * Math.cos(toRad(lat2));
109 Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));