diff options
author | Danny Baumann <dannybaumann@web.de> | 2013-10-20 14:00:21 +0200 |
---|---|---|
committer | Danny Baumann <dannybaumann@web.de> | 2013-10-20 14:16:18 +0200 |
commit | 3ca8b9d11545603af9318fd213720f31748c9d36 (patch) | |
tree | c15a0663d51cde03a9f8912f84061e2ee80a16f2 /src | |
parent | e0c75abcd8f774af1ae6bbe981900798e8011dcf (diff) | |
download | android_packages_apps_LockClock-3ca8b9d11545603af9318fd213720f31748c9d36.tar.gz android_packages_apps_LockClock-3ca8b9d11545603af9318fd213720f31748c9d36.tar.bz2 android_packages_apps_LockClock-3ca8b9d11545603af9318fd213720f31748c9d36.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java | 12 | ||||
-rw-r--r-- | src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java | 20 |
2 files changed, 16 insertions, 16 deletions
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<LocationResult> results) { boolean needCountry = false, needPostal = false; String countryId = results.get(0).countryId; - String postalCity = null, postalCountryId = null; + HashSet<String> postalIds = new HashSet<String>(); 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<LocationResult> results = new ArrayList<LocationResult>(); 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); } |