aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java')
-rw-r--r--src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java88
1 files changed, 54 insertions, 34 deletions
diff --git a/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java b/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java
index 7f12f00..15addf6 100644
--- a/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java
+++ b/src/com/cyanogenmod/lockclock/weather/YahooWeatherProvider.java
@@ -28,6 +28,8 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import java.util.Locale;
@@ -40,10 +42,27 @@ public class YahooWeatherProvider implements WeatherProvider {
private static final String URL_LOCATION =
"http://query.yahooapis.com/v1/public/yql?format=json&q=" +
Uri.encode("select woeid, postal, admin1, admin2, admin3, " +
- "locality1, locality2, country from geo.places where text =");
+ "locality1, locality2, country from geo.places where " +
+ "(placetype = 7 or placetype = 8 or placetype = 9 " +
+ "or placetype = 10 or placetype = 11) and text =");
private static final String[] LOCALITY_NAMES = new String[] {
- "locality2", "locality1", "admin3", "admin2", "admin1"
+ "locality1", "locality2", "admin3", "admin2", "admin1"
+ };
+
+ private static class YahooLocationResult extends LocationResult {
+ private int score;
+ };
+ private static final Comparator<YahooLocationResult> LOCATION_COMPARATOR =
+ new Comparator<YahooLocationResult>() {
+ @Override
+ public int compare(YahooLocationResult lhs, YahooLocationResult rhs) {
+ if (lhs.score == rhs.score) {
+ return 0;
+ }
+ // smaller score at the top
+ return lhs.score < rhs.score ? -1 : 1;
+ }
};
private Context mContext;
@@ -63,19 +82,7 @@ public class YahooWeatherProvider implements WeatherProvider {
return null;
}
- ArrayList<LocationResult> results = new ArrayList<LocationResult>();
- for (int i = 0; i < places.length(); i++) {
- try {
- LocationResult result = parsePlace(places.getJSONObject(i));
- if (result != null) {
- results.add(result);
- }
- } catch (JSONException e) {
- Log.w(TAG, "Found invalid JSON place record, ignoring.", e);
- }
- }
-
- return results;
+ return new ArrayList<LocationResult>(parsePlaces(places));
}
public WeatherInfo getWeatherInfo(String id, String localizedCityName) {
@@ -111,8 +118,8 @@ public class YahooWeatherProvider implements WeatherProvider {
/* high */ (float) forecast.getDouble("high"),
/* tempUnit */ units.getString("temperature"),
/* humidity */ (float) data.getJSONObject("atmosphere").getDouble("humidity"),
- /* wind */ (float) wind.getDouble("speed"),
- /* windDir */ wind.getInt("direction"),
+ /* wind */ (float) wind.optDouble("speed", -1),
+ /* windDir */ wind.optInt("direction", -1),
/* speedUnit */ units.getString("speed"),
System.currentTimeMillis());
@@ -134,30 +141,41 @@ public class YahooWeatherProvider implements WeatherProvider {
return null;
}
+ List<YahooLocationResult> results = parsePlaces(places);
+ Collections.sort(results, LOCATION_COMPARATOR);
+
+ for (YahooLocationResult result : results) {
+ Log.d(TAG, "Looking up weather for " + result.city + " (id=" + result.id
+ + ", score=" + result.score + ")");
+ WeatherInfo info = getWeatherInfo(result.id, result.city);
+ if (info != null) {
+ // cache the result for potential reuse
+ // (the placefinder service API is rate limited)
+ Preferences.setCachedLocationId(mContext, result.id);
+ return info;
+ }
+ }
+
+ return null;
+ }
+
+ private List<YahooLocationResult> parsePlaces(JSONArray places) {
+ ArrayList<YahooLocationResult> results = new ArrayList<YahooLocationResult>();
for (int i = 0; i < places.length(); i++) {
- LocationResult result = null;
try {
- result = parsePlace(places.getJSONObject(i));
+ YahooLocationResult result = parsePlace(places.getJSONObject(i));
+ if (result != null) {
+ results.add(result);
+ }
} catch (JSONException e) {
Log.w(TAG, "Found invalid JSON place record, ignoring.", e);
}
- if (result != null) {
- Log.d(TAG, "Looking up weather for " + result.city + " (" + result.id + ")");
- WeatherInfo info = getWeatherInfo(result.id, result.city);
- if (info != null) {
- // cache the result for potential reuse
- // (the placefinder service API is rate limited)
- Preferences.setCachedLocationId(mContext, result.id);
- return info;
- }
- }
}
-
- return null;
+ return results;
}
- private LocationResult parsePlace(JSONObject place) throws JSONException {
- LocationResult result = new LocationResult();
+ private YahooLocationResult parsePlace(JSONObject place) throws JSONException {
+ YahooLocationResult result = new YahooLocationResult();
JSONObject country = place.getJSONObject("country");
result.id = place.getString("woeid");
@@ -167,9 +185,11 @@ public class YahooWeatherProvider implements WeatherProvider {
result.postal = place.getJSONObject("postal").getString("content");
}
- for (String locName : LOCALITY_NAMES) {
+ for (int i = 0; i < LOCALITY_NAMES.length; i++) {
+ String locName = LOCALITY_NAMES[i];
if (!place.isNull(locName)) {
result.city = place.getJSONObject(locName).getString("content");
+ result.score = i;
break;
}
}