diff options
author | Danny Baumann <dannybaumann@web.de> | 2013-10-22 16:28:42 +0200 |
---|---|---|
committer | Danny Baumann <dannybaumann@web.de> | 2013-10-23 08:42:02 +0200 |
commit | 17496f4eb6afa0b5492cd88580854320bf87d548 (patch) | |
tree | 00ef1cdd213f199abaa264c576f2d3117c66f33b /src/com/cyanogenmod | |
parent | 3ca8b9d11545603af9318fd213720f31748c9d36 (diff) | |
download | android_packages_apps_LockClock-17496f4eb6afa0b5492cd88580854320bf87d548.tar.gz android_packages_apps_LockClock-17496f4eb6afa0b5492cd88580854320bf87d548.tar.bz2 android_packages_apps_LockClock-17496f4eb6afa0b5492cd88580854320bf87d548.zip |
Tweak weather and location fetching code.
- Make sure to always use the smallest locality for weather lookup by
location, but prefer locality1 (town) over locality2 (neighborhood) as
that is what most users will expect to see.
- Show progress dialog during location fetching
- Ignore non-locality places
- Make wind data optional. Yahoo seemingly doesn't provide wind data for
some places, and we aren't really using this data anyway.
Change-Id: Id841525b13b3d3e274ee9b8a82278495f9d349d3
Diffstat (limited to 'src/com/cyanogenmod')
3 files changed, 60 insertions, 42 deletions
diff --git a/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java b/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java index e9fe73d..d88809a 100644 --- a/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java +++ b/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java @@ -26,7 +26,6 @@ import android.os.Bundle; import android.preference.EditTextPreference; import android.text.TextUtils; import android.util.AttributeSet; -import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; @@ -40,8 +39,6 @@ import java.util.HashSet; import java.util.List; public class CustomLocationPreference extends EditTextPreference { - private static final String TAG = "CustomLocationPreference"; - public CustomLocationPreference(Context context) { super(context); } @@ -110,6 +107,7 @@ public class CustomLocationPreference extends EditTextPreference { cancel(true); } }); + mProgressDialog.show(); } @Override diff --git a/src/com/cyanogenmod/lockclock/weather/WeatherInfo.java b/src/com/cyanogenmod/lockclock/weather/WeatherInfo.java index 0d7a089..a857734 100755 --- a/src/com/cyanogenmod/lockclock/weather/WeatherInfo.java +++ b/src/com/cyanogenmod/lockclock/weather/WeatherInfo.java @@ -130,17 +130,17 @@ public class WeatherInfo { } public String getFormattedWindSpeed() { + if (wind < 0) { + return mContext.getString(R.string.unknown); + } return getFormattedValue(wind, speedUnit); } public String getWindDirection() { int resId; - if (windDirection < 0) { - return ""; - } - - if (windDirection < 23) resId = R.string.weather_N; + if (windDirection < 0) resId = R.string.unknown; + else if (windDirection < 23) resId = R.string.weather_N; else if (windDirection < 68) resId = R.string.weather_NE; else if (windDirection < 113) resId = R.string.weather_E; else if (windDirection < 158) resId = R.string.weather_SE; 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; } } |