From 3ca8b9d11545603af9318fd213720f31748c9d36 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Sun, 20 Oct 2013 14:00:21 +0200 Subject: Fix location query under some circumstances. - Yahoo returns a single JSON object instead of an array in case there's only one match; adjust for that. - A postal code is not guaranteed to be returned. - Fix determination whether postal code needs to be shown: It needs to be shown when there's any duplicate name/country pair, not only if there's a duplicate to the first entry. Change-Id: I132de203cca72ec9e78ded53837540bbd0044788 --- .../preference/CustomLocationPreference.java | 12 +++++------- .../lockclock/weather/YahooWeatherProvider.java | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java b/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java index 33454b4..e9fe73d 100644 --- a/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java +++ b/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java @@ -36,6 +36,7 @@ import com.cyanogenmod.lockclock.misc.Preferences; import com.cyanogenmod.lockclock.weather.WeatherProvider.LocationResult; import com.cyanogenmod.lockclock.weather.YahooWeatherProvider; +import java.util.HashSet; import java.util.List; public class CustomLocationPreference extends EditTextPreference { @@ -153,20 +154,17 @@ public class CustomLocationPreference extends EditTextPreference { private CharSequence[] buildItemList(List results) { boolean needCountry = false, needPostal = false; String countryId = results.get(0).countryId; - String postalCity = null, postalCountryId = null; + HashSet postalIds = new HashSet(); for (LocationResult result : results) { if (!TextUtils.equals(result.countryId, countryId)) { needCountry = true; } - if (TextUtils.equals(result.city, postalCity) - && TextUtils.equals(result.countryId, postalCountryId)) { + String postalId = result.countryId + "##" + result.city; + if (postalIds.contains(postalId)) { needPostal = true; } - if (postalCity == null) { - postalCity = result.city; - postalCountryId = result.countryId; - } + postalIds.add(postalId); if (needPostal && needCountry) { break; } diff --git a/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java b/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java index 2a07e5b..7f12f00 100644 --- a/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java +++ b/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java @@ -66,7 +66,7 @@ public class YahooWeatherProvider implements WeatherProvider { ArrayList results = new ArrayList(); for (int i = 0; i < places.length(); i++) { try { - LocationResult result = parsePlace(places.getJSONObject(i), true); + LocationResult result = parsePlace(places.getJSONObject(i)); if (result != null) { results.add(result); } @@ -137,7 +137,7 @@ public class YahooWeatherProvider implements WeatherProvider { for (int i = 0; i < places.length(); i++) { LocationResult result = null; try { - result = parsePlace(places.getJSONObject(i), false); + result = parsePlace(places.getJSONObject(i)); } catch (JSONException e) { Log.w(TAG, "Found invalid JSON place record, ignoring.", e); } @@ -156,8 +156,7 @@ public class YahooWeatherProvider implements WeatherProvider { return null; } - private LocationResult parsePlace(JSONObject place, boolean requireFullDataSet) - throws JSONException { + private LocationResult parsePlace(JSONObject place) throws JSONException { LocationResult result = new LocationResult(); JSONObject country = place.getJSONObject("country"); @@ -175,10 +174,7 @@ public class YahooWeatherProvider implements WeatherProvider { } } - if (result.id == null || result.city == null) { - return null; - } - if (requireFullDataSet && (result.countryId == null || result.postal == null)) { + if (result.id == null || result.city == null || result.countryId == null) { return null; } @@ -194,7 +190,13 @@ public class YahooWeatherProvider implements WeatherProvider { try { JSONObject rootObject = new JSONObject(response); JSONObject results = rootObject.getJSONObject("query").getJSONObject("results"); - return results.getJSONArray("place"); + JSONArray places = results.optJSONArray("place"); + if (places == null) { + // Yahoo returns an object instead of an array when there's only one result + places = new JSONArray(); + places.put(results.getJSONObject("place")); + } + return places; } catch (JSONException e) { Log.w(TAG, "Received malformed places data", e); } -- cgit v1.2.3