Cleaned Up code related to broadcasting and receving locations updates

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-05-16 10:41:53 -04:00
parent ff27eb8ae3
commit fa9985aed5
7 changed files with 31 additions and 70 deletions

Binary file not shown.

View File

@@ -13,13 +13,14 @@ import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.location.Location; import android.location.Location;
import android.location.LocationManager;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import com.TwentyCodes.android.debug.LocationLibraryConstants; import com.TwentyCodes.android.debug.LocationLibraryConstants;
import com.TwentyCodes.android.location.GeoPointLocationListener; import com.TwentyCodes.android.location.GeoPointLocationListener;
import com.TwentyCodes.android.location.LocationReceiver; import com.TwentyCodes.android.location.BaseLocationReceiver;
import com.google.android.maps.GeoPoint; import com.google.android.maps.GeoPoint;
import com.skyhookwireless.wps.RegistrationCallback; import com.skyhookwireless.wps.RegistrationCallback;
import com.skyhookwireless.wps.WPSContinuation; import com.skyhookwireless.wps.WPSContinuation;
@@ -49,8 +50,8 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
public static final String INTENT_EXTRA_REQUIRED_ACCURACY = "required_accuracy"; public static final String INTENT_EXTRA_REQUIRED_ACCURACY = "required_accuracy";
/** /**
* Used to tell the service the update action to broadcast. If this is not supplied, {@link LocationReceiver.INTENT_EXTRA_ACTION_UPDATE } will be used. * Used to tell the service the update action to broadcast. If this is not supplied, {@link BaseLocationReceiver.INTENT_EXTRA_ACTION_UPDATE } will be used.
* @see LocationReceiver.INTENT_EXTRA_ACTION_UPDATE * @see BaseLocationReceiver.INTENT_EXTRA_ACTION_UPDATE
*/ */
public static final String INTENT_EXTRA_ACTION_UPDATE = "action_update"; public static final String INTENT_EXTRA_ACTION_UPDATE = "action_update";
@@ -75,8 +76,8 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
if(mIntent.getAction() != null) if(mIntent.getAction() != null)
locationUpdate.setAction(mIntent.getAction()); locationUpdate.setAction(mIntent.getAction());
else else
locationUpdate.setAction(LocationReceiver.INTENT_EXTRA_ACTION_UPDATE); locationUpdate.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
locationUpdate.putExtra(LocationReceiver.INTENT_EXTRA_LOCATION_PARCEL, convertLocation()); locationUpdate.putExtra(LocationManager.KEY_LOCATION_CHANGED, convertLocation());
sendBroadcast(locationUpdate); sendBroadcast(locationUpdate);
} }
} }

View File

@@ -1,5 +1,5 @@
/** /**
* Debug.java * LocationLibraryConstants.java
* @date Mar 1, 2011 * @date Mar 1, 2011
* @author ricky barrette * @author ricky barrette
* @author Twenty Codes, LLC * @author Twenty Codes, LLC
@@ -7,6 +7,7 @@
package com.TwentyCodes.android.debug; package com.TwentyCodes.android.debug;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.location.LocationManager;
/** /**
* This class will be used to set the Location Library Constants * This class will be used to set the Location Library Constants
@@ -14,6 +15,12 @@ import android.hardware.SensorManager;
*/ */
public final class LocationLibraryConstants { public final class LocationLibraryConstants {
static{
SUPPORTS_FROYO = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO;
SUPPORTS_GINGERBREAD = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD;
}
/** /**
* Sets the default SkyHook Registration Behavior used by SkyHookRegistration.getUserName() * Sets the default SkyHook Registration Behavior used by SkyHookRegistration.getUserName()
* @author ricky barrette * @author ricky barrette
@@ -48,9 +55,7 @@ public final class LocationLibraryConstants {
public static final boolean SUPPORTS_GINGERBREAD; public static final boolean SUPPORTS_GINGERBREAD;
static{ public static final String INTENT_ACTION_UPDATE = "TwentyCodes.intent.action.LocationUpdate";
SUPPORTS_FROYO = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO;
SUPPORTS_GINGERBREAD = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD; public static final String INTENT_EXTRA_LOCATION_CHANGED = LocationManager.KEY_LOCATION_CHANGED;
}
} }

View File

@@ -1,8 +1,7 @@
/** /**
* PassiveLocationChangedReceiver.java
* @date May 15, 2012
* @author ricky barrette
* @author Twenty Codes, LLC * @author Twenty Codes, LLC
* @author ricky barrette
* @date Oct 18, 2010
*/ */
package com.TwentyCodes.android.location; package com.TwentyCodes.android.location;
@@ -13,11 +12,12 @@ import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
/** /**
* this abstract class will be used as a for classes wishing to be a receiver of location updates from the location services
* @author ricky barrette * @author ricky barrette
*/ */
public abstract class PassiveLocationChangedReceiver extends BroadcastReceiver { public abstract class BaseLocationReceiver extends BroadcastReceiver {
protected Context mContext; public Context mContext;
/** /**
* (non-Javadoc) * (non-Javadoc)

View File

@@ -1,46 +0,0 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Oct 18, 2010
*/
package com.TwentyCodes.android.location;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
/**
* this abstract class will be used as a for classes wishing to be a receiver of location updates from the location services
* @author ricky barrette
*/
public abstract class LocationReceiver extends BroadcastReceiver {
public static final String INTENT_EXTRA_ACTION_UPDATE = "TwentyCodes.intent.action.LocationUpdate";
public static final String INTENT_EXTRA_LOCATION_PARCEL = "location_parcel";
public Context mContext;
/**
* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
* @param contextonBind
* @param intent
* @author ricky barrette
*/
@Override
public void onReceive(final Context context, final Intent intent) {
mContext = context;
if(intent.getParcelableExtra(INTENT_EXTRA_LOCATION_PARCEL) != null){
Location location = intent.getParcelableExtra(INTENT_EXTRA_LOCATION_PARCEL);
onLocationUpdate(location);
}
}
/**
* called when a location update is received
* @param parcelableExtra
* @author ricky barrette
*/
public abstract void onLocationUpdate(Location location);
}

View File

@@ -14,6 +14,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
@@ -48,8 +49,8 @@ public class LocationService extends Service implements LocationListener {
public static final String INTENT_EXTRA_REQUIRED_ACCURACY = "required_accuracy"; public static final String INTENT_EXTRA_REQUIRED_ACCURACY = "required_accuracy";
/** /**
* Used to tell the service the update action to broadcast. If this is not supplied, {@link LocationReceiver.INTENT_EXTRA_ACTION_UPDATE } will be used. * Used to tell the service the update action to broadcast. If this is not supplied, {@link BaseLocationReceiver.INTENT_EXTRA_ACTION_UPDATE } will be used.
* @see LocationReceiver.INTENT_EXTRA_ACTION_UPDATE * @see BaseLocationReceiver.INTENT_EXTRA_ACTION_UPDATE
*/ */
public static final String INTENT_EXTRA_ACTION_UPDATE = "action_update"; public static final String INTENT_EXTRA_ACTION_UPDATE = "action_update";
@@ -95,8 +96,8 @@ public class LocationService extends Service implements LocationListener {
if(mIntent.getAction() != null) if(mIntent.getAction() != null)
locationUpdate.setAction(mIntent.getAction()); locationUpdate.setAction(mIntent.getAction());
else else
locationUpdate.setAction(LocationReceiver.INTENT_EXTRA_ACTION_UPDATE); locationUpdate.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
locationUpdate.putExtra(LocationReceiver.INTENT_EXTRA_LOCATION_PARCEL, mLocation); locationUpdate.putExtra(LocationManager.KEY_LOCATION_CHANGED, mLocation);
sendBroadcast(locationUpdate); sendBroadcast(locationUpdate);
stopSelf(mStartId); stopSelf(mStartId);
} }

View File

@@ -22,13 +22,13 @@ public class PassiveLocationListener {
/** /**
* A convenience method for requesting passive location updates * A convenience method for requesting passive location updates
* @param context * @param context
* @param receiverIntent
* @author ricky barrette * @author ricky barrette
*/ */
public static final void requestPassiveLocationUpdates(final Context context){ public static final void requestPassiveLocationUpdates(final Context context, final Intent receiverIntent){
if (LocationLibraryConstants.SUPPORTS_FROYO) { if (LocationLibraryConstants.SUPPORTS_FROYO) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
final Intent passiveIntent = new Intent(context, PassiveLocationChangedReceiver.class); final PendingIntent locationListenerPassivePendingIntent = PendingIntent.getBroadcast(context, 0, receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent locationListenerPassivePendingIntent = PendingIntent.getBroadcast(context, 0, passiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListenerPassivePendingIntent); locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListenerPassivePendingIntent);
} }
} }