]> www.average.org Git - WhereAmI.git/blob - src/org/average/whereami/LastLocation.java
implement autostart on boot
[WhereAmI.git] / src / org / average / whereami / LastLocation.java
1 package org.average.whereami;
2
3 import org.average.whereami.PersistentStore;
4 import org.average.whereami.SayWhen;
5
6 import java.lang.Math;
7 import java.io.IOException;
8
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;
14
15 import android.content.Context;
16 import android.content.res.Resources;
17 import android.text.format.Time;
18 import android.util.Log;
19
20 public final class LastLocation extends Oracle {
21
22         final String TAG = getClass().getName();
23
24         private Latitude latitude;
25
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;
33
34         public LastLocation(final Resources res, final PersistentStore store) {
35                 super(res, 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);
43
44                 latitude = Latitude.builder(httpTransport, jsonFactory)
45                         .setHttpRequestInitializer(accessProtectedResource)
46                         .setApplicationName("WhereAmI/1.0").build();
47         }
48
49         @Override
50         public final String getResult() {
51                 try {
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;
68                         } else {
69                                 return e.getMessage();
70                         }
71                 } catch (HttpResponseException e) {
72                         Log.e(TAG, "HttpResponseException: " + e);
73                         return e.getMessage();
74                 } catch (IOException e) {
75                         e.printStackTrace();
76                         return e.getMessage();
77                 }
78         }
79
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] +
90                                 " is " + dist);
91                         if ((loc_dists[i] == "0") ||
92                             (dist < Double.parseDouble(loc_dists[i]))) {
93                                 locmsg = loc_names[i];
94                                 break;
95                         }
96                 }
97                 return myname + " " + locmsg + ", " + registered +
98                         " " + sayWhen.say(tsm);
99         }
100
101         private Double toRad(Double deg) {
102                 return deg * 3.14159265 / 180;
103         }
104
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));
113                 return 6371 * c;
114         }
115 }