diff options
author | DvTonder <david.vantonder@gmail.com> | 2014-01-25 16:08:03 -0500 |
---|---|---|
committer | DvTonder <david.vantonder@gmail.com> | 2014-01-25 16:08:03 -0500 |
commit | 73f951edf42c588882b356fc44832ebbb3b1f2d5 (patch) | |
tree | 424bf0465c06009dc99ae6aed0f55dc40109e9e4 | |
parent | 76e9afc4f5e047bbb5856184276e38e7cb90e2bb (diff) | |
download | android_packages_apps_LockClock-73f951edf42c588882b356fc44832ebbb3b1f2d5.tar.gz android_packages_apps_LockClock-73f951edf42c588882b356fc44832ebbb3b1f2d5.tar.bz2 android_packages_apps_LockClock-73f951edf42c588882b356fc44832ebbb3b1f2d5.zip |
LockClock: Workaround for OpenWeatherMap temperature craziness
OpenWeatherMap sometimes returns temperatures in Kelvin even if we ask it
for deg C or deg F. Detect this and convert accordingly.
Change-Id: I4041035442bb503c7bd43f761ba3d14feb791c32
-rw-r--r-- | src/com/cyanogenmod/lockclock/weather/OpenWeatherMapProvider.java | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/com/cyanogenmod/lockclock/weather/OpenWeatherMapProvider.java b/src/com/cyanogenmod/lockclock/weather/OpenWeatherMapProvider.java index 808077c..fa70ede 100644 --- a/src/com/cyanogenmod/lockclock/weather/OpenWeatherMapProvider.java +++ b/src/com/cyanogenmod/lockclock/weather/OpenWeatherMapProvider.java @@ -112,7 +112,7 @@ public class OpenWeatherMapProvider implements WeatherProvider { JSONObject conditionData = conditions.getJSONObject("main"); JSONObject windData = conditions.getJSONObject("wind"); ArrayList<DayForecast> forecasts = - parseForecasts(new JSONObject(forecastResponse).getJSONArray("list")); + parseForecasts(new JSONObject(forecastResponse).getJSONArray("list"), metric); int speedUnitResId = metric ? R.string.weather_kph : R.string.weather_mph; if (localizedCityName == null) { localizedCityName = conditions.getString("name"); @@ -122,7 +122,7 @@ public class OpenWeatherMapProvider implements WeatherProvider { /* condition */ weather.getString("main"), /* conditionCode */ mapConditionIconToCode( weather.getString("icon"), weather.getInt("id")), - /* temperature */ (float) conditionData.getDouble("temp"), + /* temperature */ sanitizeTemperature(conditionData.getDouble("temp"), metric), /* tempUnit */ metric ? "C" : "F", /* humidity */ (float) conditionData.getDouble("humidity"), /* wind */ (float) windData.getDouble("speed"), @@ -141,7 +141,7 @@ public class OpenWeatherMapProvider implements WeatherProvider { return null; } - private ArrayList<DayForecast> parseForecasts(JSONArray forecasts) throws JSONException { + private ArrayList<DayForecast> parseForecasts(JSONArray forecasts, boolean metric) throws JSONException { ArrayList<DayForecast> result = new ArrayList<DayForecast>(); int count = forecasts.length(); @@ -153,8 +153,8 @@ public class OpenWeatherMapProvider implements WeatherProvider { JSONObject temperature = forecast.getJSONObject("temp"); JSONObject data = forecast.getJSONArray("weather").getJSONObject(0); DayForecast item = new DayForecast( - /* low */ (float) temperature.getDouble("min"), - /* high */ (float) temperature.getDouble("max"), + /* low */ sanitizeTemperature(temperature.getDouble("min"), metric), + /* high */ sanitizeTemperature(temperature.getDouble("max"), metric), /* condition */ data.getString("main"), /* conditionCode */ mapConditionIconToCode( data.getString("icon"), data.getInt("id"))); @@ -164,6 +164,22 @@ public class OpenWeatherMapProvider implements WeatherProvider { return result; } + // OpenWeatherMap sometimes returns temperatures in Kelvin even if we ask it + // for deg C or deg F. Detect this and convert accordingly. + private static float sanitizeTemperature(double value, boolean metric) { + // threshold chosen to work for both C and F. 170 deg F is hotter + // than the hottest place on earth. + if (value > 170) { + // K -> deg C + value -= 273.15; + if (!metric) { + // deg C -> deg F + value = (value * 1.8) + 32; + } + } + return (float) value; + } + private static final HashMap<String, Integer> ICON_MAPPING = new HashMap<String, Integer>(); static { ICON_MAPPING.put("01d", 32); |