compute distance (wip)
authorEugene Crosser <Eugene.Crosser@ru.ibm.com>
Thu, 2 Feb 2012 14:02:17 +0000 (18:02 +0400)
committerEugene Crosser <Eugene.Crosser@ru.ibm.com>
Thu, 2 Feb 2012 14:02:17 +0000 (18:02 +0400)
res/values/arrays.xml
src/org/average/whereami/LastLocation.java
src/org/average/whereami/PhoneLog.java
src/org/average/whereami/WhereAmI.java

index 4463e72b948f2a2e6f3f227092e7331b43e8fe28..1dc3f559ebb4166cb4705912667039c2b55276d6 100644 (file)
     <item>1200000</item>
     <item>3600000</item>
   </string-array>
+  <string-array name="loc_names">
+    <item>дома</item>
+    <item>на работе</item>
+    <item>в Москве</item>
+    <item>в отъезде</item>
+  </string-array>
+  <string-array name="loc_lats">
+    <item>55.79214</item>
+    <item>55.65619</item>
+    <item>55.743</item>
+    <item>0</item>
+  </string-array>
+  <string-array name="loc_lons">
+    <item>37.56634</item>
+    <item>37.5261</item>
+    <item>37.614</item>
+    <item>0</item>
+  </string-array>
+  <string-array name="loc_dists">
+    <item>1</item>
+    <item>1</item>
+    <item>25</item>
+    <item>0</item>
+  </string-array>
 </resources>
index 230b8488fcc67b9b172384216c1aa7ec863d9d47..914a615217ddec08903f1f33dbc390bf5d8f8fd4 100644 (file)
@@ -2,7 +2,7 @@ package org.average.whereami;
 
 import org.average.whereami.CredentialStore;
 
-//import java.lang.Long;
+import java.lang.Math;
 
 import com.google.api.client.extensions.android2.AndroidHttp;
 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
@@ -18,6 +18,7 @@ import com.google.api.services.latitude.model.Location;
 
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.content.res.Resources;
 import android.util.Log;
 
 public final class LastLocation extends Oracle {
@@ -26,7 +27,17 @@ public final class LastLocation extends Oracle {
 
        private Latitude latitude;
 
-       public LastLocation(final CredentialStore store) {
+       private String[] loc_names;
+       private String[] loc_lats;
+       private String[] loc_lons;
+       private String[] loc_dists;
+
+       public LastLocation(final Resources res, final CredentialStore store) {
+               loc_names = res.getStringArray(R.array.loc_names);
+               loc_lats = res.getStringArray(R.array.loc_lats);
+               loc_lons = res.getStringArray(R.array.loc_lons);
+               loc_lons = res.getStringArray(R.array.loc_dists);
+
                HttpTransport transport = AndroidHttp.newCompatibleTransport();
                JsonFactory jsonFactory = new JacksonFactory();
                AccessTokenResponse token = store.read();
@@ -85,6 +96,34 @@ public final class LastLocation extends Oracle {
        private String locationMessage(Double lat, Double lon, Long tsm) {
                Log.v(TAG, "entering locationMessage(" + lat +
                                "," + lon + "," + tsm + ")");
-               return "Location: " + lat + "N " + lon + "E at " + tsm/1000;
+               String locmsg = "<UNSET>";
+               for (int i = 0; i < loc_names.length; i++) {
+                       Double dist = haversine(lat, lon,
+                                       Double.parseDouble(loc_lats[i]),
+                                       Double.parseDouble(loc_lons[i]));
+                       Log.v(TAG, "Dist from " + loc_names[i] +
+                               " is " + dist);
+                       //if ((loc_dists[i] == "0") ||
+                       //    (dist < Double.parseDouble(loc_dists[i]))) {
+                       //      locmsg = loc_names[i];
+                       //      break;
+                       //}
+               }
+               return locmsg;
+       }
+
+       private Double toRad(Double deg) {
+               return deg * 3.14159265 / 180;
+       }
+
+       private Double haversine(Double lat1, Double lon1,
+                               Double lat2, Double lon2) {
+               Double dlat = toRad(lat2 - lat1);
+               Double dlon = toRad(lon2 - lon1);
+               Double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
+                       Math.sin(dlon / 2) * Math.sin(dlon / 2) *
+                       Math.cos(toRad(lat1)) * Math.cos(toRad(lat2));
+               Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
+               return 6371 * c;
        }
 }
index 8522e841b05df02c524bf0975c8dcecb16b83969..dbb735357eaff4a7086edb4e51f3236a4dbebc11 100644 (file)
@@ -22,6 +22,7 @@ import com.google.api.services.calendar.model.Events;
 import com.google.common.collect.Lists;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.content.SharedPreferences;
 import android.util.Log;
 
@@ -31,7 +32,7 @@ public final class PhoneLog extends Oracle {
 
        private Calendar calendar;
 
-       public PhoneLog(final CredentialStore store) {
+       public PhoneLog(final Resources res, final CredentialStore store) {
                HttpTransport transport = AndroidHttp.newCompatibleTransport();
                JsonFactory jsonFactory = new JacksonFactory();
                AccessTokenResponse token = store.read();
index e78cb04a9da2319a368161b65b0fa27b3b931dac..959bd0e876f41a48b97fea8c88e6170401c47164 100644 (file)
@@ -213,9 +213,9 @@ public class WhereAmI extends Activity
         tvs = (TextView)findViewById(R.id.timestamp);
        ut = new UpdateTarget[] {
             new UpdateTarget((TextView)findViewById(R.id.location),
-                             new PhoneLog(store)),
+                             new PhoneLog(res, store)),
             new UpdateTarget((TextView)findViewById(R.id.phonecall),
-                             new LastLocation(store))
+                             new LastLocation(res, store))
         };
         Log.w(TAG, "created UI, about to start update task");
        mHandler.post(updateClock);