33import android .content .Context ;
44import android .location .Location ;
55import android .support .annotation .Nullable ;
6- import android .util .Log ;
76
87import com .mapzen .android .lost .api .LocationListener ;
98import com .mapzen .android .lost .api .LocationRequest ;
1514/**
1615 * Sample LocationEngine using the Open Source Lost library
1716 */
18- public class LostLocationEngine extends LocationEngine implements
19- LostApiClient .ConnectionCallbacks , LocationListener {
20-
21- private static final String LOG_TAG = LostLocationEngine .class .getSimpleName ();
17+ public class LostLocationEngine extends LocationEngine implements LocationListener {
2218
2319 private static LocationEngine instance ;
2420
@@ -28,9 +24,7 @@ public class LostLocationEngine extends LocationEngine implements
2824 public LostLocationEngine (Context context ) {
2925 super ();
3026 this .context = new WeakReference <>(context );
31- lostApiClient = new LostApiClient .Builder (this .context .get ())
32- .addConnectionCallbacks (this )
33- .build ();
27+ lostApiClient = new LostApiClient .Builder (this .context .get ()).build ();
3428 }
3529
3630 public static synchronized LocationEngine getLocationEngine (Context context ) {
@@ -47,7 +41,12 @@ public static synchronized LocationEngine getLocationEngine(Context context) {
4741 */
4842 @ Override
4943 public void activate () {
50- connect ();
44+ if (!lostApiClient .isConnected ()) {
45+ lostApiClient .connect ();
46+ }
47+ for (LocationEngineListener listener : locationListeners ) {
48+ listener .onConnected ();
49+ }
5150 }
5251
5352 /**
@@ -57,7 +56,7 @@ public void activate() {
5756 */
5857 @ Override
5958 public void deactivate () {
60- if (lostApiClient != null && lostApiClient .isConnected ()) {
59+ if (lostApiClient .isConnected ()) {
6160 lostApiClient .disconnect ();
6261 }
6362 }
@@ -74,25 +73,7 @@ public boolean isConnected() {
7473 }
7574
7675 /**
77- * Invoked when the location provider has connected.
78- */
79- @ Override
80- public void onConnected () {
81- for (LocationEngineListener listener : locationListeners ) {
82- listener .onConnected ();
83- }
84- }
85-
86- /**
87- * Invoked when the location provider connection has been suspended.
88- */
89- @ Override
90- public void onConnectionSuspended () {
91- Log .d (LOG_TAG , "Connection suspended" );
92- }
93-
94- /**
95- * Returns the Last known location if the location provider is connected.
76+ * Returns the Last known location if the location provider is connected and location permissions are granted.
9677 *
9778 * @return the last known location
9879 */
@@ -101,7 +82,7 @@ public void onConnectionSuspended() {
10182 public Location getLastLocation () {
10283 if (lostApiClient .isConnected ()) {
10384 //noinspection MissingPermission
104- return LocationServices .FusedLocationApi .getLastLocation (lostApiClient );
85+ return LocationServices .FusedLocationApi .getLastLocation ();
10586 }
10687 return null ;
10788 }
@@ -123,6 +104,7 @@ public void requestLocationUpdates() {
123104 request .setSmallestDisplacement (smallestDisplacement );
124105 }
125106
107+ // Priority matching is straightforward
126108 if (priority == LocationEnginePriority .NO_POWER ) {
127109 request .setPriority (LocationRequest .PRIORITY_NO_POWER );
128110 } else if (priority == LocationEnginePriority .LOW_POWER ) {
@@ -135,25 +117,25 @@ public void requestLocationUpdates() {
135117
136118 if (lostApiClient .isConnected ()) {
137119 //noinspection MissingPermission
138- LocationServices .FusedLocationApi .requestLocationUpdates (lostApiClient , request , this );
120+ LocationServices .FusedLocationApi .requestLocationUpdates (request , this );
139121 }
140122 }
141123
124+ @ Override
125+ public Type obtainType () {
126+ return Type .LOST ;
127+ }
128+
142129 /**
143130 * Dismiss ongoing location update to the location provider.
144131 */
145132 @ Override
146133 public void removeLocationUpdates () {
147134 if (lostApiClient .isConnected ()) {
148- LocationServices .FusedLocationApi .removeLocationUpdates (lostApiClient , this );
135+ LocationServices .FusedLocationApi .removeLocationUpdates (this );
149136 }
150137 }
151138
152- @ Override
153- public Type obtainType () {
154- return Type .LOST ;
155- }
156-
157139 /**
158140 * Invoked when the Location has changed.
159141 *
@@ -165,14 +147,4 @@ public void onLocationChanged(Location location) {
165147 listener .onLocationChanged (location );
166148 }
167149 }
168-
169- private void connect () {
170- if (lostApiClient != null ) {
171- if (lostApiClient .isConnected ()) {
172- onConnected ();
173- } else {
174- lostApiClient .connect ();
175- }
176- }
177- }
178150}
0 commit comments