Cleaned up code
This commit is contained in:
@@ -26,44 +26,56 @@ import com.skyhookwireless.wps.WPSContinuation;
|
||||
import com.skyhookwireless.wps.WPSReturnCode;
|
||||
|
||||
/**
|
||||
* This service class will be used broadcast the users location either one time, or periodically.
|
||||
* To use as a one shot location service:
|
||||
* <blockquote><pre>PendingIntent pendingIntent = PendingIntent.getService(context, 0, SkyHookService.startService(context), 0);
|
||||
* This service class will be used broadcast the users location either one time,
|
||||
* or periodically. To use as a one shot location service: <blockquote>
|
||||
*
|
||||
* <pre>
|
||||
* PendingIntent pendingIntent = PendingIntent.getService(context, 0, SkyHookService.startService(context), 0);
|
||||
* or
|
||||
* Intent service = new Intent(context, SkyHookService.class);
|
||||
* context.startService(service);<pre></bloackquote>
|
||||
* context.startService(service);
|
||||
*
|
||||
* <pre>
|
||||
* </bloackquote>
|
||||
* To use as a recurring service:
|
||||
* <blockquote>SkyHookService.startService(this, (60000 * Integer.parseInt(ringer.getString(UPDATE_INTVERVAL , "5")))).run();</bloackquote>
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class SkyHookService extends Service implements GeoPointLocationListener, RegistrationCallback{
|
||||
public class SkyHookService extends Service implements GeoPointLocationListener, RegistrationCallback {
|
||||
|
||||
public static final String TAG = "SkyHookService";
|
||||
public static final int REQUEST_CODE = 32741942;
|
||||
|
||||
/**
|
||||
* a convince method for getting an intent to start the service
|
||||
*
|
||||
* @param context
|
||||
* @return a intent that will be used to start the service
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static Intent getStartServiceIntent(final Context context){
|
||||
public static Intent getStartServiceIntent(final Context context) {
|
||||
return new Intent(context, SkyHookService.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* a convince method for stopping the service and removing its que from the alarm manager
|
||||
* a convince method for stopping the service and removing its que from the
|
||||
* alarm manager
|
||||
*
|
||||
* @param context
|
||||
* @return a runnable that will stop the service
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static Runnable stopService(final Context context){
|
||||
return new Runnable(){
|
||||
public static Runnable stopService(final Context context) {
|
||||
return new Runnable() {
|
||||
@Override
|
||||
public void run(){
|
||||
public void run() {
|
||||
context.stopService(new Intent(context, SkyHookService.class));
|
||||
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).cancel(PendingIntent.getService(context, REQUEST_CODE, new Intent(context, SkyHookService.class), 0));
|
||||
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).cancel(PendingIntent.getService(context, REQUEST_CODE, new Intent(context,
|
||||
SkyHookService.class), 0));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private SkyHook mSkyhook;
|
||||
protected long mPeriod = -1;
|
||||
private GeoPoint mLocation;
|
||||
@@ -82,7 +94,7 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
private void braodcastLocation() {
|
||||
if (mLocation != null) {
|
||||
final Intent locationUpdate = new Intent();
|
||||
if(mIntent.getAction() != null)
|
||||
if (mIntent.getAction() != null)
|
||||
locationUpdate.setAction(mIntent.getAction());
|
||||
else
|
||||
locationUpdate.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
|
||||
@@ -93,13 +105,14 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
|
||||
/**
|
||||
* converts skyhook's location object into android's location object
|
||||
*
|
||||
* @return converted location
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public Location convertLocation(){
|
||||
public Location convertLocation() {
|
||||
final Location location = new Location("location");
|
||||
location.setLatitude(mLocation.getLatitudeE6() /1e6);
|
||||
location.setLongitude(mLocation.getLongitudeE6() /1e6);
|
||||
location.setLatitude(mLocation.getLatitudeE6() / 1e6);
|
||||
location.setLongitude(mLocation.getLongitudeE6() / 1e6);
|
||||
location.setAccuracy(mAccuracy);
|
||||
return location;
|
||||
}
|
||||
@@ -111,18 +124,21 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
}
|
||||
|
||||
/*
|
||||
* I believe that this method is no longer needed as we are not supporting pre 2.1
|
||||
* I believe that this method is no longer needed as we are not supporting
|
||||
* pre 2.1
|
||||
*/
|
||||
// /**
|
||||
// * To keep backwards compatibility we override onStart which is the equivalent of onStartCommand in pre android 2.x
|
||||
// * @author ricky barrette
|
||||
// */
|
||||
// @Override
|
||||
// public void onStart(Intent intent, int startId) {
|
||||
// Log.i(SkyHook.TAG, "onStart.Service started with start id of: " + startId);
|
||||
// parseIntent(intent);
|
||||
// this.mSkyhook.getUpdates();
|
||||
// }
|
||||
// /**
|
||||
// * To keep backwards compatibility we override onStart which is the
|
||||
// equivalent of onStartCommand in pre android 2.x
|
||||
// * @author ricky barrette
|
||||
// */
|
||||
// @Override
|
||||
// public void onStart(Intent intent, int startId) {
|
||||
// Log.i(SkyHook.TAG, "onStart.Service started with start id of: " +
|
||||
// startId);
|
||||
// parseIntent(intent);
|
||||
// this.mSkyhook.getUpdates();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public WPSContinuation handleError(final WPSReturnCode arg0) {
|
||||
@@ -138,6 +154,7 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see android.app.Service#onBind(android.content.Intent)
|
||||
* @param arg0
|
||||
* @return
|
||||
@@ -149,35 +166,35 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(){
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
mSkyhook = new SkyHook(this);
|
||||
mSkyhook.setLocationListener(this);
|
||||
|
||||
/*
|
||||
* fail safe
|
||||
* this will stop the service after the maximum running time, if location has not been reported
|
||||
* fail safe this will stop the service after the maximum running time,
|
||||
* if location has not been reported
|
||||
*/
|
||||
new Handler().postDelayed(new Runnable(){
|
||||
new Handler().postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run(){
|
||||
public void run() {
|
||||
stopSelfResult(mStartID);
|
||||
}
|
||||
}, LocationLibraryConstants.MAX_LOCATION_SERVICE_RUN_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* aborts location services
|
||||
* (non-Javadoc)
|
||||
* aborts location services (non-Javadoc)
|
||||
*
|
||||
* @see android.app.Service#onDestroy()
|
||||
* @author Ricky Barrette
|
||||
*/
|
||||
@Override
|
||||
public void onDestroy(){
|
||||
public void onDestroy() {
|
||||
mSkyhook.removeUpdates();
|
||||
braodcastLocation();
|
||||
//ask android to restart service if mPeriod is set
|
||||
if(mPeriod > -1)
|
||||
// ask android to restart service if mPeriod is set
|
||||
if (mPeriod > -1)
|
||||
registerWakeUp();
|
||||
super.onDestroy();
|
||||
}
|
||||
@@ -193,23 +210,24 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
mLocation = point;
|
||||
mAccuracy = accuracy;
|
||||
/*
|
||||
* fail safe
|
||||
* if the accuracy is greater than the minimum required accuracy
|
||||
* then continue
|
||||
* else stop to report location
|
||||
* fail safe if the accuracy is greater than the minimum required
|
||||
* accuracy then continue else stop to report location
|
||||
*/
|
||||
if(accuracy < (mRequiredAccuracy > -1 ? mRequiredAccuracy : LocationLibraryConstants.MINIMUM_REQUIRED_ACCURACY) || LocationLibraryConstants.REPORT_FIRST_LOCATION)
|
||||
if (accuracy < (mRequiredAccuracy > -1 ? mRequiredAccuracy : LocationLibraryConstants.MINIMUM_REQUIRED_ACCURACY)
|
||||
|| LocationLibraryConstants.REPORT_FIRST_LOCATION)
|
||||
this.stopSelf(mStartID);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when startService is called. only used in 2.x android.
|
||||
* This method is called when startService is called. only used in 2.x
|
||||
* android.
|
||||
*
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public int onStartCommand(final Intent intent, final int flags, final int startId) {
|
||||
Log.i(SkyHook.TAG , "onStartCommand.Service started with start id of: " + startId);
|
||||
Log.i(SkyHook.TAG, "onStartCommand.Service started with start id of: " + startId);
|
||||
mStartID = startId;
|
||||
parseIntent(intent);
|
||||
mSkyhook.getUpdates();
|
||||
@@ -221,11 +239,11 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
*
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private void parseIntent(final Intent intent){
|
||||
private void parseIntent(final Intent intent) {
|
||||
|
||||
mIntent = intent;
|
||||
|
||||
if(intent != null){
|
||||
if (intent != null) {
|
||||
if (intent.hasExtra(LocationLibraryConstants.INTENT_EXTRA_PERIOD_BETWEEN_UPDATES))
|
||||
mPeriod = intent.getLongExtra(LocationLibraryConstants.INTENT_EXTRA_PERIOD_BETWEEN_UPDATES, LocationLibraryConstants.FAIL_SAFE_UPDATE_INVERVAL);
|
||||
|
||||
@@ -236,9 +254,10 @@ public class SkyHookService extends Service implements GeoPointLocationListener,
|
||||
|
||||
/**
|
||||
* registers our Receiver the starts the service with the alarm manager
|
||||
*
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private void registerWakeUp(){
|
||||
private void registerWakeUp() {
|
||||
final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + mPeriod, PendingIntent.getService(this, REQUEST_CODE, mIntent, 0));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user