aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid van Tonder <david.vantonder@gmail.com>2013-10-11 11:26:59 +0200
committerDanny Baumann <dannybaumann@web.de>2013-10-13 13:37:05 +0200
commit25e9f72f805cc04338fe2f5f8cca14c909253283 (patch)
treec22e2c5e549e1b41e67b1a7fab25c0d62888cbb2 /src
parent505d24a32029d4b98b88885ff4a85c12c9be66fd (diff)
downloadandroid_packages_apps_LockClock-25e9f72f805cc04338fe2f5f8cca14c909253283.tar.gz
android_packages_apps_LockClock-25e9f72f805cc04338fe2f5f8cca14c909253283.tar.bz2
android_packages_apps_LockClock-25e9f72f805cc04338fe2f5f8cca14c909253283.zip
Tweak weather provider/location service code
Change-Id: I9334276560be28563530d2d503cdcba6109f099b
Diffstat (limited to 'src')
-rwxr-xr-xsrc/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java52
1 files changed, 39 insertions, 13 deletions
diff --git a/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java b/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
index 2d1e688..badf20b 100755
--- a/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
+++ b/src/com/cyanogenmod/lockclock/weather/WeatherUpdateService.java
@@ -21,6 +21,7 @@ import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
+import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
@@ -32,6 +33,7 @@ import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
+import android.text.TextUtils;
import android.util.Log;
import com.cyanogenmod.lockclock.ClockWidgetProvider;
@@ -48,6 +50,14 @@ public class WeatherUpdateService extends Service {
private WeatherUpdateTask mTask;
+ private static final Criteria sLocationCriteria;
+ static {
+ sLocationCriteria = new Criteria();
+ sLocationCriteria.setPowerRequirement(Criteria.POWER_LOW);
+ sLocationCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
+ sLocationCriteria.setCostAllowed(false);
+ }
+
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (D) Log.v(TAG, "Got intent " + intent);
@@ -125,6 +135,7 @@ public class WeatherUpdateService extends Service {
private Context mContext;
public WeatherUpdateTask() {
+ if (D) Log.d(TAG, "Starting weather update task");
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mContext = WeatherUpdateService.this;
@@ -132,12 +143,13 @@ public class WeatherUpdateService extends Service {
@Override
protected void onPreExecute() {
+ if (D) Log.d(TAG, "ACQUIRING WAKELOCK");
mWakeLock.acquire();
}
private Location getCurrentLocation() {
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
+ LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
+ Location location = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (D) Log.v(TAG, "Current location is " + location);
return location;
}
@@ -169,9 +181,16 @@ public class WeatherUpdateService extends Service {
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);
+ // device has requested the current location to the system yet, then try to
+ // get the current location use the provider that best matches the criteria.
+ if (D) Log.d(TAG, "Getting best location provider");
+ LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
+ String locationProvider = lm.getBestProvider(sLocationCriteria, true);
+ if (TextUtils.isEmpty(locationProvider)) {
+ Log.e(TAG, "No available location providers matching criteria.");
+ } else {
+ WeatherLocationListener.registerIfNeeded(mContext, locationProvider);
+ }
return null;
}
@@ -188,6 +207,7 @@ public class WeatherUpdateService extends Service {
private void finish(WeatherInfo result) {
if (result != null) {
+ if (D) Log.d(TAG, "Weather update received, caching data and updating widget");
long now = System.currentTimeMillis();
Preferences.setCachedWeatherInfo(mContext, now, result);
scheduleUpdate(mContext, Preferences.weatherRefreshIntervalInMs(mContext), false);
@@ -195,14 +215,16 @@ public class WeatherUpdateService extends Service {
Intent updateIntent = new Intent(mContext, ClockWidgetProvider.class);
sendBroadcast(updateIntent);
} else if (isCancelled()) {
- /* cancelled, likely due to lost network - we'll get restarted
- * when network comes back */
+ // cancelled, likely due to lost network - we'll get restarted
+ // when network comes back
} else {
- /* failure, schedule next download in 30 minutes */
+ // failure, schedule next download in 30 minutes
+ if (D) Log.d(TAG, "Weather refresh failed, scheduling update in 30 minutes");
long interval = 30 * 60 * 1000;
scheduleUpdate(mContext, interval, false);
}
+ if (D) Log.d(TAG, "RELEASING WAKELOCK");
mWakeLock.release();
stopSelf();
}
@@ -214,21 +236,24 @@ public class WeatherUpdateService extends Service {
static void registerIfNeeded(Context context, String provider) {
synchronized (WeatherLocationListener.class) {
+ if (D) Log.d(TAG, "Registering location listener");
if (sInstance == null) {
- final Context realContext = context.getApplicationContext();
+ final Context appContext = context.getApplicationContext();
final LocationManager locationManager =
- (LocationManager) realContext.getSystemService(Context.LOCATION_SERVICE);
+ (LocationManager) appContext.getSystemService(Context.LOCATION_SERVICE);
// Check location provider after set sInstance, so, if the provider is not
// supported, we never enter here again.
- sInstance = new WeatherLocationListener(realContext);
+ sInstance = new WeatherLocationListener(appContext);
// Check whether the provider is supported.
// NOTE!!! Actually only WeatherUpdateService class is calling this function
// with the NETWORK_PROVIDER, so setting the instance is safe. We must
// change this if this call receive differents providers
LocationProvider lp = locationManager.getProvider(provider);
if (lp != null) {
- locationManager.requestSingleUpdate(provider, sInstance, realContext.getMainLooper());
+ if (D) Log.d(TAG, "LocationManager - Requesting single update");
+ locationManager.requestSingleUpdate(provider, sInstance,
+ appContext.getMainLooper());
}
}
}
@@ -242,6 +267,7 @@ public class WeatherUpdateService extends Service {
@Override
public void onLocationChanged(Location location) {
// Now, we have a location to use. Schedule a weather update right now.
+ if (D) Log.d(TAG, "The location has changed, schedule an update ");
synchronized (WeatherLocationListener.class) {
WeatherUpdateService.scheduleUpdate(mContext, 0, true);
sInstance = null;
@@ -268,7 +294,7 @@ public class WeatherUpdateService extends Service {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long due = System.currentTimeMillis() + timeFromNow;
- if (D) Log.v(TAG, "Scheduling next update at " + new Date(due));
+ if (D) Log.d(TAG, "Scheduling next update at " + new Date(due));
am.set(AlarmManager.RTC_WAKEUP, due, getUpdateIntent(context, force));
}