Browse Source

fixed synchronization with tracing

Alexander Brakowski 9 years ago
parent
commit
cbcb121484
1 changed files with 29 additions and 21 deletions
  1. 29 21
      src/de/tudarmstadt/informatik/hostage/sync/android/SyncUtils.java

+ 29 - 21
src/de/tudarmstadt/informatik/hostage/sync/android/SyncUtils.java

@@ -5,6 +5,8 @@ import android.accounts.AccountManager;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.location.Address;
+import android.location.Geocoder;
 import android.location.Location;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
@@ -47,6 +49,7 @@ import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
@@ -263,37 +266,42 @@ public class SyncUtils {
 
             JSONObject condition = new JSONObject();
 
-            /**if(fromTime > 0){
-             Calendar calendar = GregorianCalendar.getInstance();
-             calendar.setTimeInMillis(fromTime);
+            if(fromTime > 0){
+                Calendar calendar = GregorianCalendar.getInstance();
+                calendar.setTimeInMillis(fromTime);
 
-             condition.put("date", fromCalendar(calendar));
-             }**/
+                condition.put("date", fromCalendar(calendar));
+            }
 
-            /**
-             TODO(alex): UNCOMMENT THIS AGAIN, WHEN WE ARE SURE ABOUT USING LOCATIONS THIS WAY.
-             =======================================================================
+            String country = null;
 
-             Location location = MyLocationManager.getNewestLocation();
-             if(location != null){
-             JSONArray ll = new JSONArray();
-             ll.put(location.getLongitude());
-             ll.put(location.getLatitude());
+            Location location = MyLocationManager.getNewestLocation();
 
-             condition.put("location", ll);
-             condition.put("distance", 500);
-             } else {
-             // We could not get the gps coordinates, try to retrieve the country code from the SIM card
-             TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
-             String countryCodeValue = tm.getNetworkCountryIso();
+            if(location != null){
+                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
+                List<Address> fromLocation = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
 
-             condition.put("country", countryCodeValue);
-             }*/
+                if(fromLocation.size() > 0){
+                    Address address = fromLocation.get(0);
+                    country = address.getCountryCode();
+                }
+            }
+
+            if(country == null){
+                // We could not get the gps coordinates, try to retrieve the country code from the SIM card
+                TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
+                country = tm.getNetworkCountryIso();
+            }
+
+            if(country == null || country.trim().isEmpty()) country = Locale.getDefault().getCountry();
+
+            condition.put("country", country);
 
             JSONObject req = new JSONObject();
             req.put("condition", condition);
             req.put("info", deviceMap);
 
+
             StringEntity se = new StringEntity(req.toString());
             httppost.addHeader("content-type", "application/json");
             httppost.setEntity(se);