aboutsummaryrefslogtreecommitdiff
path: root/src/org/codefirex/cfxweather/WeatherService.java
blob: 853d7f6fb4bae2db479fa415e439c8313bcf87dc (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package org.codefirex.cfxweather;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Set;

import org.codefirex.cfxweather.ResourceMaps.ResInfo;
import org.codefirex.utils.WeatherAdapter;
import org.codefirex.utils.WeatherInfo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.ResultReceiver;

public class WeatherService extends Service {
	private static final String TAG = "WeatherService";

	static final String LOCATION_ACQUIRED_ACTION = "location_acquired";
	static final String LOCATION_UNAVAILABLE_ACTION = "location_unavailable";
	static final String LOCATION_REFRESHING = "location_refreshing";
	// coordinates sent to HttpService
	static final String LOCATION_EXTRA = "location_extra";

	// handler codes
	static final int INTERVAL_CHANGED = 1001;
	static final int LOCATION_MODE_CHANGED = 1002;
	static final int SCALE_CHANGED = 1003;
	static final int REFRESH_NOW = 1004;
	static final int PAUSE_SERVICE = 1005;
	static final int RESUME_SERVICE = 1006;

	static final String ALARM_TICKED = "cfx_weather_alarm_ticked";

	private LocationManager mLocationManager;
	private IntentFilter mFilter;
	private PendingIntent mAlarmPending;
	private WeatherNotification mNotification;
	private boolean mEnabled = false;
	private int mFailCount = 0;

	private Binder mBinder = new WeatherBinder();

	private static final long MINUTE_MULTIPLE = (1000 * 60);

	private LocationListener mLocationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			sendPosition(location);
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status,
				Bundle extras) {
		}
	};

	class CacheIcon extends AsyncTask<Void, Void, Void> {

		private Context mContext;

		public CacheIcon(Context ctx) {
			mContext = ctx;
		}

		@Override
		protected Void doInBackground(Void... params) {
			Set<Integer> set = ResourceMaps.weather_map.keySet();
			for (Integer i : set) {
				ResInfo info = ResourceMaps.weather_map.get(i);
				String filename = info.iconName + ".png";
				File f = new File(mContext.getCacheDir() + "/" + filename);
				InputStream is;
				try {
					if (!f.exists()) {
						is = mContext.getAssets().open(filename);
						int size = is.available();
						byte[] buffer = new byte[size];
						is.read(buffer);
						is.close();
						FileOutputStream fos = new FileOutputStream(f);
						fos.write(buffer);
						fos.close();
					}
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			return null;
		}

	    @Override
	    protected void onPostExecute(Void voidz) {
			sendAdapterBroadcast(mEnabled ? WeatherAdapter.STATE_ON : WeatherAdapter.STATE_OFF);	    	
	    }
	}

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ALARM_TICKED)) {
				resetLocationListener();
			}
		}
	};

	public class WeatherBinder extends Binder {
		WeatherService getService() {
			return WeatherService.this;
		}
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case INTERVAL_CHANGED:
				resetAlarm();
				resetLocationListener();
				break;
			case LOCATION_MODE_CHANGED:
				resetLocationListener();
				break;
			case SCALE_CHANGED:
				sendAdapterBroadcast(WeatherAdapter.STATE_SCALE);
				break;
			case REFRESH_NOW:
				resetLocationListener();
				break;
			case PAUSE_SERVICE:
				mEnabled = false;
				sendAdapterBroadcast(WeatherAdapter.STATE_OFF);
				removeLocationListener();
				unregisterReceiver(mReceiver);
				cancelAlarm();
				break;
			case RESUME_SERVICE:
				mEnabled = true;
				sendAdapterBroadcast(WeatherAdapter.STATE_ON);
				registerReceiver(mReceiver, mFilter);
				resetAlarm();
				resetLocationListener();
				break;
			}
		}
	};

	private ResultReceiver mHttpReciever = new ResultReceiver(new Handler()) {
		@Override
		protected void onReceiveResult(int resultCode, Bundle resultData) {
			int code = resultData.getInt(HttpService.RESULT_CODE_TAG);
			if (HttpService.RESULT_SUCCEED == code) {
				sendAdapterBroadcast(WeatherAdapter.STATE_UPDATED);
			} else if (HttpService.RESULT_FAIL == code) {
				// we we're safe starting http service but something happened 
				// during the http request. initiate fail alarm
				if (mFailCount == 0) {
					startFailAlarm();
					mFailCount++;
				}
			}
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return Service.START_STICKY;
	}

	@Override
	public void onCreate() {
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		mEnabled = WeatherPrefs.getEnabled(this);
		mNotification = new WeatherNotification(this);

		new CacheIcon(this).execute();

		// create pending intent to fire when alarm is triggered
		mAlarmPending = PendingIntent.getBroadcast(this, 0, new Intent(
				ALARM_TICKED), PendingIntent.FLAG_CANCEL_CURRENT);

		mFilter = new IntentFilter();
		mFilter.addAction(ALARM_TICKED);

		if (mEnabled) {
			registerReceiver(mReceiver, mFilter);
			resetAlarm();
			resetLocationListener();
		}

	}

	private void resetLocationListener() {
		removeLocationListener();
		mLocationManager.requestLocationUpdates(getBestLocationMode(), 0, 0,
				mLocationListener);
	}

	private String getBestLocationMode() {
		String pref = WeatherPrefs.getLocationMode(this);
		if (isPreferedLocationAvailable(pref)) {
			return pref;
		} else {
			return LocationManager.PASSIVE_PROVIDER;
		}
	}

	private boolean isPreferedLocationAvailable(String pref) {
		boolean preferedAvailable = false;
		try {
			preferedAvailable = mLocationManager.isProviderEnabled(pref);
		} catch (Exception e) {
		}
		return preferedAvailable;
	}

	private boolean isSafeToUpdate() {
		return hasNetwork() && !isLocDisabled();
	}

	private boolean hasNetwork() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		return networkInfo != null && networkInfo.isConnected();
	}

	private void removeLocationListener() {
		sendAdapterBroadcast(WeatherAdapter.STATE_REFRESHING);
		mLocationManager.removeUpdates(mLocationListener);
	}

	private void resetAlarm() {
		long interval = Long.valueOf(WeatherPrefs.getInterval(this))
				* MINUTE_MULTIPLE;
		long START_TIME = Calendar.getInstance().getTimeInMillis() + interval;
		AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		alarm.cancel(mAlarmPending);
		alarm.setRepeating(AlarmManager.RTC_WAKEUP, START_TIME, interval,
				mAlarmPending);
	}

	private void startFailAlarm() {
		// we'll do 3 attempts at 30 second intervals if we are failing
		// after 3, try again at next primary interval
		long interval = MINUTE_MULTIPLE / 2;
		long START_TIME = Calendar.getInstance().getTimeInMillis() + interval;
		AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		alarm.cancel(mAlarmPending);
		alarm.setRepeating(AlarmManager.RTC_WAKEUP, START_TIME, interval,
				mAlarmPending);		
	}

	private void cancelAlarm() {
		AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		alarm.cancel(mAlarmPending);
	}

	private boolean isGpsLocationAvailable() {
		boolean hasGps = false;
		try {
			hasGps = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		} catch (Exception e) {
		}
		return hasGps;
	}	

	private boolean isNetworkLocationAvailable() {
		boolean hasNetwork = false;
		try {
			hasNetwork = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		} catch (Exception e) {
		}
		return hasNetwork;
	}

	private boolean isLocDisabled() {
		return !isGpsLocationAvailable()
				&& !isNetworkLocationAvailable();
	}

	// send coordinates to HttpService
	private void sendPosition(Location location) {
		if (!isSafeToUpdate()) {
			if (mFailCount == 0) {
				startFailAlarm();
				mFailCount++;
				return;
			} else if (mFailCount > 3) {
				// just try again at normal intervals
				mFailCount = 0;
				resetAlarm();
				return;
			}
			mFailCount++;
			return;
		}
		if (mFailCount > 0) {
			mFailCount = 0;
			resetAlarm();
		}
		Intent bestPosition = new Intent(this, HttpService.class);
		bestPosition.setAction(LOCATION_ACQUIRED_ACTION);
		bestPosition.putExtra(LOCATION_EXTRA, location);
		bestPosition.putExtra(HttpService.RESULT_TAG, mHttpReciever);
		startService(bestPosition);
		removeLocationListener();
	}

	private void sendAdapterBroadcast(int state) {
		Intent intent = new Intent();
		intent.setAction(WeatherAdapter.WEATHER_ACTION);
		intent.putExtra(WeatherAdapter.WEATHER_SERVICE_STATE, state);
		if (state == WeatherAdapter.STATE_SCALE) {
			intent.putExtra(WeatherAdapter.SCALE_TYPE, WeatherPrefs.getDegreeType(this));
		}
		sendStickyBroadcast(intent);
	}

	void sendMessage(Message m) {
		mHandler.sendMessage(m);
	}

	Message getMessage() {
		return mHandler.obtainMessage();
	}

	@Override
	public void onDestroy() {
		removeLocationListener();
		unregisterReceiver(mReceiver);
		cancelAlarm();
		mNotification.unregister();
	}
}