22
33import android .content .Context ;
44import android .location .Location ;
5- import android .util . Log ;
5+ import android .support . annotation . Nullable ;
66
77import com .mapbox .services .android .telemetry .location .LocationEngine ;
88import com .mapbox .services .android .telemetry .location .LocationEngineListener ;
99import com .mapbox .services .android .telemetry .location .LocationEnginePriority ;
10- import com .mapbox .services .android .telemetry .permissions .PermissionsManager ;
1110import com .mapzen .android .lost .api .LocationListener ;
1211import com .mapzen .android .lost .api .LocationRequest ;
1312import com .mapzen .android .lost .api .LocationServices ;
1817/**
1918 * Sample LocationEngine using the Open Source Lost library
2019 */
21- public class LostLocationEngine extends LocationEngine implements
22- LostApiClient .ConnectionCallbacks , LocationListener {
23-
24- private static final String LOG_TAG = LostLocationEngine .class .getSimpleName ();
20+ public class LostLocationEngine extends LocationEngine implements LocationListener {
2521
2622 private static LocationEngine instance ;
2723
@@ -31,9 +27,7 @@ public class LostLocationEngine extends LocationEngine implements
3127 public LostLocationEngine (Context context ) {
3228 super ();
3329 this .context = new WeakReference <>(context );
34- lostApiClient = new LostApiClient .Builder (this .context .get ())
35- .addConnectionCallbacks (this )
36- .build ();
30+ lostApiClient = new LostApiClient .Builder (this .context .get ()).build ();
3731 }
3832
3933 public static synchronized LocationEngine getLocationEngine (Context context ) {
@@ -44,50 +38,63 @@ public static synchronized LocationEngine getLocationEngine(Context context) {
4438 return instance ;
4539 }
4640
41+ /**
42+ * Activate the location engine which will connect whichever location provider you are using. You'll need to call
43+ * this before requesting user location updates using {@link LocationEngine#requestLocationUpdates()}.
44+ */
4745 @ Override
4846 public void activate () {
49- if (lostApiClient != null && !lostApiClient .isConnected ()) {
47+ if (!lostApiClient .isConnected ()) {
5048 lostApiClient .connect ();
5149 }
50+ for (LocationEngineListener listener : locationListeners ) {
51+ listener .onConnected ();
52+ }
5253 }
5354
55+ /**
56+ * Disconnect the location engine which is useful when you no longer need location updates or requesting the users
57+ * {@link LocationEngine#getLastLocation()}. Before deactivating, you'll need to stop request user location updates
58+ * using {@link LocationEngine#removeLocationUpdates()}.
59+ */
5460 @ Override
5561 public void deactivate () {
56- if (lostApiClient != null && lostApiClient .isConnected ()) {
62+ if (lostApiClient .isConnected ()) {
5763 lostApiClient .disconnect ();
5864 }
5965 }
6066
67+ /**
68+ * Check if your location provider has been activated/connected. This is mainly used internally but is also useful in
69+ * the rare case when you'd like to know if your location engine is connected or not.
70+ *
71+ * @return boolean true if the location engine has been activated/connected, else false.
72+ */
6173 @ Override
6274 public boolean isConnected () {
6375 return lostApiClient .isConnected ();
6476 }
6577
78+ /**
79+ * Returns the Last known location if the location provider is connected and location permissions are granted.
80+ *
81+ * @return the last known location
82+ */
6683 @ Override
67- public void onConnected () {
68- for (LocationEngineListener listener : locationListeners ) {
69- listener .onConnected ();
70- }
71- }
72-
73- @ Override
74- public void onConnectionSuspended () {
75- Log .d (LOG_TAG , "Connection suspended." );
76- }
77-
78- @ Override
84+ @ Nullable
7985 public Location getLastLocation () {
80- if (lostApiClient .isConnected () && PermissionsManager . areLocationPermissionsGranted ( context . get ()) ) {
86+ if (lostApiClient .isConnected ()) {
8187 //noinspection MissingPermission
82- return LocationServices .FusedLocationApi .getLastLocation (lostApiClient );
88+ return LocationServices .FusedLocationApi .getLastLocation ();
8389 }
84-
8590 return null ;
8691 }
8792
93+ /**
94+ * Request location updates to the location provider.
95+ */
8896 @ Override
8997 public void requestLocationUpdates () {
90- // Common params
9198 LocationRequest request = LocationRequest .create ();
9299
93100 if (interval != null ) {
@@ -111,23 +118,32 @@ public void requestLocationUpdates() {
111118 request .setPriority (LocationRequest .PRIORITY_HIGH_ACCURACY );
112119 }
113120
114- if (lostApiClient .isConnected () && PermissionsManager . areLocationPermissionsGranted ( context . get ()) ) {
121+ if (lostApiClient .isConnected ()) {
115122 //noinspection MissingPermission
116- LocationServices .FusedLocationApi .requestLocationUpdates (lostApiClient , request , this );
123+ LocationServices .FusedLocationApi .requestLocationUpdates (request , this );
117124 }
118125 }
119126
127+ /**
128+ * Dismiss ongoing location update to the location provider.
129+ */
120130 @ Override
121131 public void removeLocationUpdates () {
122132 if (lostApiClient .isConnected ()) {
123- LocationServices .FusedLocationApi .removeLocationUpdates (lostApiClient , this );
133+ LocationServices .FusedLocationApi .removeLocationUpdates (this );
124134 }
125135 }
126136
137+ /**
138+ * Invoked when the Location has changed.
139+ *
140+ * @param location the new location
141+ */
127142 @ Override
128143 public void onLocationChanged (Location location ) {
129144 for (LocationEngineListener listener : locationListeners ) {
130145 listener .onLocationChanged (location );
131146 }
132147 }
133148}
149+
0 commit comments