aboutsummaryrefslogtreecommitdiff
path: root/src/org/codefirex/cfxweather/HttpService.java
blob: 3c677dc6b16eb82ca632e926f9e09f9afae43c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.codefirex.cfxweather;

import org.codefirex.utils.WeatherInfo;

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.ResultReceiver;

public class HttpService extends IntentService {
	private static final String TAG = "WeatherService";
	static final String RESULT_TAG = "weather_service_result";
	static final String RESULT_CODE_TAG = "result_code_tag";
	static final int RESULT_CODE = 2001;
	static final int RESULT_SUCCEED = 2002;
	static final int RESULT_FAIL = 2003;

	private YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils
			.getInstance();

	public HttpService() {
		super(TAG);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		String action = intent.getAction();
		if (WeatherService.LOCATION_ACQUIRED_ACTION.equals(action)) {
			Location location = (Location) intent
					.getParcelableExtra(WeatherService.LOCATION_EXTRA);
			if (location != null) {
				int result = RESULT_FAIL;
				ResultReceiver rec = intent.getParcelableExtra(RESULT_TAG);
				String lat = String.valueOf(location.getLatitude());
				String lon = String.valueOf(location.getLongitude());
				try {
					WeatherInfo weatherInfo = yahooWeatherUtils
							.queryYahooWeather(this, lat, lon);
					weatherInfo.setCurrentScale(WeatherPrefs
							.getDegreeType(this));
					WeatherPrefs.setPrefsFromInfo(this, weatherInfo);
					result = RESULT_SUCCEED;
				} catch (Exception e) {
					result = RESULT_FAIL;
				} finally {
					if (rec != null) {
						Bundle b = new Bundle();
						b.putInt(RESULT_CODE_TAG, result);
						rec.send(RESULT_CODE, b);
					}
				}
			}
		}
	}
}