/*** Location Service *** *** background service for monitoring location * * Copyright (c) 2014 * Randroid Solutions (http://www.randroidsolutions.com/) * All Rights Reserved. * * The right to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of this software, and/or to permit persons to whom the * software is furnished to do so is NOT PERMITTED without the written consent * of Randroid Solutions [dev@randroidsolutions.com]. * */ package com.randroid.library.gps; import android.app.Service; import android.content.Intent; import android.location.Location; import android.os.Bundle; import android.os.IBinder; import com.randroid.library.gps.GlobalPosition.LocationAlert; import com.randroid.library.gps.objects.GpsAddress; import com.randroid.library.gps.objects.LocationMonitor; public abstract class LocationService extends Service { public static final String ACTION = "com.randroid.gpsapi.LocationService.START"; public static final String EXTRA_ACTION = "action"; public static final String EXTRA_PARCEL = "parcel"; public static final String RESULT_ERROR = "error"; public static final String RESULT_STOPPED = "stopped"; public static final String RESULT_UNAVAILABLE = "unavailable"; private static GpsAddress mNavdress = null; private LocationAlert mAlert = null; private Bundle mParcel = null; private String mAction = null; //define our location monitor interface private final LocationMonitor.LocationResult mUpdateResult = new LocationMonitor.LocationResult() { public void update(final Location location) { if (mAction != null) { returnAction(null); } stopSelf(); } public void failure(final String error) { if (mAction != null) { returnAction(error); } stopSelf(); } public void unavailable() { if (mAction != null) { returnAction(RESULT_UNAVAILABLE); } stopSelf(); }; }; @Override public IBinder onBind(final Intent intent) { return null; } @Override public void onStart(final Intent intent, int startId) { //for compatibility with API levels prior to 5 start(intent); } @Override public int onStartCommand(final Intent intent, int flags, int startId) { return start(intent); } @Override public void onDestroy() { if (mAlert != null) { mAlert.cancelUpdates(); mAlert = null; } super.onDestroy(); } public static GpsAddress getNavdress() { return mNavdress; } public static void setNavdress(final GpsAddress navdress) { mNavdress = navdress; } protected int start(final Intent intent) { if (intent != null) { mParcel = intent.getBundleExtra(EXTRA_PARCEL); mAction = intent.getStringExtra(EXTRA_ACTION); //convert the destination address to coordinates //this is sometimes delayed by the geocoding service try { final Location destination = mNavdress.hasLocation() ? mNavdress.getLocation() : GlobalPosition.getLocation(LocationService.this, mNavdress); if ((destination != null) && ((destination.getLatitude() != 0) && (destination.getLongitude() != 0))) { //create an alert monitor for our destination mAlert = GlobalPosition.newAlert(this, mUpdateResult, destination); } else { stopSelf(); } } catch (final Exception e) { stopSelf(); } return START_STICKY; } return START_NOT_STICKY; } protected void returnAction(final String error) { try { final Intent intent = new Intent(mAction); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(RESULT_ERROR, error); if (mParcel != null) { intent.putExtra(EXTRA_PARCEL, mParcel); } startActivity(intent); } catch (final Exception e) {} } }