Calendar API WIP
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
1 package org.average.whereami;
2
3 import org.average.whereami.CredentialStore;
4
5 import java.lang.Math;
6 import java.io.IOException;
7
8 import com.google.api.client.googleapis.json.GoogleJsonResponseException;
9 import com.google.api.client.http.HttpResponseException;
10 import com.google.api.services.latitude.Latitude;
11 import com.google.api.services.latitude.LatitudeRequest;
12 import com.google.api.services.latitude.model.Location;
13
14 import android.content.Context;
15 import android.content.res.Resources;
16 import android.text.format.Time;
17 import android.util.Log;
18
19 public final class LastLocation extends Oracle {
20
21         final String TAG = getClass().getName();
22
23         private Latitude latitude;
24
25         private String[] loc_names;
26         private String[] loc_lats;
27         private String[] loc_lons;
28         private String[] loc_dists;
29         private String myname;
30         private String timestamp;
31         private String longago;
32         private String lessthanhour;
33         private String onehour;
34         private String hours2to4;
35         private String hours5up;
36
37         public LastLocation(final Resources res, final CredentialStore store) {
38                 super(res, store);
39                 loc_names = res.getStringArray(R.array.loc_names);
40                 loc_lats = res.getStringArray(R.array.loc_lats);
41                 loc_lons = res.getStringArray(R.array.loc_lons);
42                 loc_dists = res.getStringArray(R.array.loc_dists);
43                 myname = res.getString(R.string.myname);
44                 timestamp = res.getString(R.string.timestamp);
45                 longago = res.getString(R.string.longago);
46                 lessthanhour = res.getString(R.string.lessthanhour);
47                 onehour = res.getString(R.string.onehour);
48                 hours2to4 = res.getString(R.string.hours2to4);
49                 hours5up = res.getString(R.string.hours5up);
50
51                 latitude = Latitude.builder(httpTransport, jsonFactory)
52                         .setHttpRequestInitializer(accessProtectedResource)
53                         .setApplicationName("WhereAmI/1.0").build();
54         }
55
56         @Override
57         public final String getResult() {
58                 try {
59                         Log.v(TAG, "entering getResult");
60                         Latitude.CurrentLocation.Get request =
61                                 latitude.currentLocation().get();
62                         request.setGranularity("best");
63                         Location cloc = request.execute();
64                         Double lat = Double.parseDouble(
65                                         cloc.getLatitude().toString());
66                         Double lon = Double.parseDouble(
67                                         cloc.getLongitude().toString());
68                         Long tsm = Long.parseLong(
69                                         cloc.getTimestampMs().toString());
70                         return locationMessage(lat, lon, tsm);
71                 } catch (GoogleJsonResponseException e) {
72                         Log.v(TAG, "GoogleJsonResponseException: " + e);
73                         if (e.getResponse().getStatusCode() == 401) {
74                                 return authErrorMessage;
75                         } else {
76                                 return e.getMessage();
77                         }
78                 } catch (HttpResponseException e) {
79                         Log.v(TAG, "HttpResponseException: " + e);
80                         return e.getMessage();
81                 } catch (IOException e) {
82                         e.printStackTrace();
83                         return e.getMessage();
84                 }
85         }
86
87         private String locationMessage(Double lat, Double lon, Long tsm) {
88                 Log.v(TAG, "entering locationMessage(" + lat +
89                                 "," + lon + "," + tsm + ")");
90                 String locmsg = "<UNSET>";
91                 String agomsg = "<UNSET>";
92                 for (int i = 0; i < loc_names.length; i++) {
93                         Double dist = haversine(lat, lon,
94                                         Double.parseDouble(loc_lats[i]),
95                                         Double.parseDouble(loc_lons[i]));
96                         Log.v(TAG, "Dist from " + loc_names[i] +
97                                 " is " + dist);
98                         if ((loc_dists[i] == "0") ||
99                             (dist < Double.parseDouble(loc_dists[i]))) {
100                                 locmsg = loc_names[i];
101                                 break;
102                         }
103                 }
104                 Long ago = (System.currentTimeMillis() - tsm) / 3600000;
105                 if (ago < 1) {
106                         agomsg = lessthanhour;
107                 } else if (ago < 2) {
108                         agomsg = onehour;
109                 } else if (ago < 5) {
110                         agomsg = "" + ago + " " + hours2to4;
111                 } else if (ago < 24) {
112                         agomsg = "" + ago + " " + hours5up;
113                 } else {
114                         agomsg = longago;
115                 }
116                 return myname + " " + locmsg + ", " + timestamp +
117                         " " + agomsg;
118         }
119
120         private Double toRad(Double deg) {
121                 return deg * 3.14159265 / 180;
122         }
123
124         private Double haversine(Double lat1, Double lon1,
125                                 Double lat2, Double lon2) {
126                 Double dlat = toRad(lat2 - lat1);
127                 Double dlon = toRad(lon2 - lon1);
128                 Double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
129                         Math.sin(dlon / 2) * Math.sin(dlon / 2) *
130                         Math.cos(toRad(lat1)) * Math.cos(toRad(lat2));
131                 Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
132                 return 6371 * c;
133         }
134 }