aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
diff options
context:
space:
mode:
authorDanny Baumann <dannybaumann@web.de>2013-10-09 15:29:06 +0200
committerDanny Baumann <dannybaumann@web.de>2013-10-13 13:37:00 +0200
commitd0ea3088d7b866315e9a54e875de6caae71b48b1 (patch)
treeb78a178e620c8e01555e0e4ca67bd7ba530b90eb /src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
parent1a961f9d5780f1c97270703dad20eace4c475bf6 (diff)
downloadandroid_packages_apps_LockClock-d0ea3088d7b866315e9a54e875de6caae71b48b1.tar.gz
android_packages_apps_LockClock-d0ea3088d7b866315e9a54e875de6caae71b48b1.tar.bz2
android_packages_apps_LockClock-d0ea3088d7b866315e9a54e875de6caae71b48b1.zip
Improve weather retrieval code.
- add weather provider abstraction - centralize yahoo specific bits at one place - switch to parsing JSON instead of XML - switch to using places instead of placefinder, which allows localized city names and custom location selection Change-Id: I5acfa084fda5024bd1af940a866fcdf77ef2895a
Diffstat (limited to 'src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java')
-rwxr-xr-xsrc/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java93
1 files changed, 21 insertions, 72 deletions
diff --git a/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java b/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
index 720d904..2d1e688 100755
--- a/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
+++ b/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
@@ -38,17 +38,12 @@ import com.cyanogenmod.lockclock.ClockWidgetProvider;
import com.cyanogenmod.lockclock.misc.Constants;
import com.cyanogenmod.lockclock.misc.Preferences;
-import java.io.IOException;
import java.util.Date;
-import org.w3c.dom.Document;
-
public class WeatherUpdateService extends Service {
private static final String TAG = "WeatherUpdateService";
private static final boolean D = Constants.DEBUG;
- private static final String URL_YAHOO_API_WEATHER = "http://weather.yahooapis.com/forecastrss?w=%s&u=";
-
public static final String ACTION_FORCE_UPDATE = "com.cyanogenmod.lockclock.action.FORCE_WEATHER_UPDATE";
private WeatherUpdateTask mTask;
@@ -140,27 +135,6 @@ public class WeatherUpdateService extends Service {
mWakeLock.acquire();
}
- private String getWoeidForCustomLocation(String location) {
- // first try with the cached woeid, no need to constantly query constant information
- String woeid = Preferences.getCachedWoeid(mContext);
- if (woeid == null) {
- woeid = YahooPlaceFinder.geoCode(mContext, location);
- }
- if (D) Log.v(TAG, "Yahoo location code for " + location + " is " + woeid);
- return woeid;
- }
-
- private String getWoeidForCurrentLocation(Location location) {
- String woeid = YahooPlaceFinder.reverseGeoCode(mContext,
- location.getLatitude(), location.getLongitude());
- if (woeid == null) {
- // we couldn't fetch up-to-date information, fall back to cache
- woeid = Preferences.getCachedWoeid(mContext);
- }
- if (D) Log.v(TAG, "Yahoo location code for current geolocation " + location + " is " + woeid);
- return woeid;
- }
-
private Location getCurrentLocation() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
@@ -168,63 +142,38 @@ public class WeatherUpdateService extends Service {
return location;
}
- private String getCachedLocation() {
- WeatherInfo weatherInfo = Preferences.getCachedWeatherInfo(mContext);
- String location = (weatherInfo != null) ? weatherInfo.getCity() : null;
- if (D) Log.v(TAG, "Last known, cached location is " + location);
- return location;
- }
-
- private Document getDocument(String woeid) {
- boolean celcius = Preferences.useMetricUnits(mContext);
- String urlWithUnit = URL_YAHOO_API_WEATHER + (celcius ? "c" : "f");
-
- try {
- return new HttpRetriever().getDocumentFromURL(String.format(urlWithUnit, woeid));
- } catch (IOException e) {
- Log.e(TAG, "Couldn't fetch weather data", e);
- }
- return null;
- }
-
@Override
protected WeatherInfo doInBackground(Void... params) {
- String customLocation = null;
- String woeid = null;
+ WeatherProvider provider = new YahooWeatherProvider(mContext);
+ String customLocationId = null, customLocationName = null;
if (Preferences.useCustomWeatherLocation(mContext)) {
- customLocation = Preferences.customWeatherLocation(mContext);
+ customLocationId = Preferences.customWeatherLocationId(mContext);
+ customLocationName = Preferences.customWeatherLocationCity(mContext);
}
- if (customLocation != null) {
- woeid = getWoeidForCustomLocation(customLocation);
- } else {
- Location location = getCurrentLocation();
- if (location != null) {
- woeid = getWoeidForCurrentLocation(location);
- } else {
- // work with cached location from last request for now
- customLocation = getCachedLocation();
- if (customLocation != null) {
- woeid = getWoeidForCustomLocation(customLocation);
- }
- // If lastKnownLocation is not present because none of the apps in the
- // device has requested the current location to the system yet,
- // then try to get the current location use an non-accuracy/network provider.
- WeatherLocationListener.registerIfNeeded(mContext, LocationManager.NETWORK_PROVIDER);
- }
+ if (customLocationId != null) {
+ return provider.getWeatherInfo(customLocationId, customLocationName);
}
- if (woeid == null || isCancelled()) {
- return null;
+ Location location = getCurrentLocation();
+ if (location != null) {
+ WeatherInfo info = provider.getWeatherInfo(location);
+ if (info != null) {
+ return info;
+ }
}
-
- Document doc = getDocument(woeid);
- if (doc == null || isCancelled()) {
- return null;
+ // work with cached location from last request for now
+ WeatherInfo cachedInfo = Preferences.getCachedWeatherInfo(mContext);
+ if (cachedInfo != null) {
+ return provider.getWeatherInfo(cachedInfo.getId(), cachedInfo.getCity());
}
+ // If lastKnownLocation is not present because none of the apps in the
+ // device has requested the current location to the system yet,
+ // then try to get the current location use an non-accuracy/network provider.
+ WeatherLocationListener.registerIfNeeded(mContext, LocationManager.NETWORK_PROVIDER);
- return new WeatherXmlParser(mContext).parseWeatherResponse(doc);
+ return null;
}
@Override