1+ package com .mapbox .services .android .location ;
2+
3+ import android .content .Context ;
4+ import android .location .Location ;
5+ import android .support .annotation .Nullable ;
6+ import android .util .Log ;
7+
8+ import com .mapbox .services .android .telemetry .location .LocationEngine ;
9+ import com .mapbox .services .android .telemetry .location .LocationEngineListener ;
10+ import com .mapbox .services .android .telemetry .location .LocationEnginePriority ;
11+ import com .mapzen .android .lost .api .LocationListener ;
12+ import com .mapzen .android .lost .api .LocationRequest ;
13+ import com .mapzen .android .lost .api .LocationServices ;
14+ import com .mapzen .android .lost .api .LostApiClient ;
15+
16+ import java .lang .ref .WeakReference ;
17+
18+ /**
19+ * Sample LocationEngine using the Open Source Lost library
20+ *
21+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
22+ */
23+ @ Deprecated
24+ public class LostLocationEngine extends LocationEngine implements
25+ LostApiClient .ConnectionCallbacks , LocationListener {
26+
27+ private static final String LOG_TAG = LostLocationEngine .class .getSimpleName ();
28+
29+ private static LocationEngine instance ;
30+
31+ private WeakReference <Context > context ;
32+ private LostApiClient lostApiClient ;
33+
34+ @ Deprecated
35+ public LostLocationEngine (Context context ) {
36+ super ();
37+ this .context = new WeakReference <>(context );
38+ lostApiClient = new LostApiClient .Builder (this .context .get ())
39+ .addConnectionCallbacks (this )
40+ .build ();
41+ }
42+
43+ @ Deprecated
44+ public static synchronized LocationEngine getLocationEngine (Context context ) {
45+ if (instance == null ) {
46+ instance = new LostLocationEngine (context .getApplicationContext ());
47+ }
48+
49+ return instance ;
50+ }
51+
52+ /**
53+ * Activate the location engine which will connect whichever location provider you are using. You'll need to call
54+ * this before requesting user location updates using {@link LocationEngine#requestLocationUpdates()}.
55+ *
56+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
57+ */
58+ @ Deprecated
59+ @ Override
60+ public void activate () {
61+ connect ();
62+ }
63+
64+ /**
65+ * Disconnect the location engine which is useful when you no longer need location updates or requesting the users
66+ * {@link LocationEngine#getLastLocation()}. Before deactivating, you'll need to stop request user location updates
67+ * using {@link LocationEngine#removeLocationUpdates()}.
68+ *
69+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
70+ */
71+ @ Deprecated
72+ @ Override
73+ public void deactivate () {
74+ if (lostApiClient != null && lostApiClient .isConnected ()) {
75+ lostApiClient .disconnect ();
76+ }
77+ }
78+
79+ /**
80+ * Check if your location provider has been activated/connected. This is mainly used internally but is also useful in
81+ * the rare case when you'd like to know if your location engine is connected or not.
82+ *
83+ * @return boolean true if the location engine has been activated/connected, else false.
84+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
85+ */
86+ @ Deprecated
87+ @ Override
88+ public boolean isConnected () {
89+ return lostApiClient .isConnected ();
90+ }
91+
92+ /**
93+ * Invoked when the location provider has connected.
94+ *
95+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
96+ */
97+ @ Deprecated
98+ @ Override
99+ public void onConnected () {
100+ for (LocationEngineListener listener : locationListeners ) {
101+ listener .onConnected ();
102+ }
103+ }
104+
105+ /**
106+ * Invoked when the location provider connection has been suspended.
107+ *
108+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
109+ */
110+ @ Deprecated
111+ @ Override
112+ public void onConnectionSuspended () {
113+ Log .d (LOG_TAG , "Connection suspended" );
114+ }
115+
116+ /**
117+ * Returns the Last known location if the location provider is connected.
118+ *
119+ * @return the last known location
120+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
121+ */
122+ @ Deprecated
123+ @ Override
124+ @ Nullable
125+ public Location getLastLocation () {
126+ if (lostApiClient .isConnected ()) {
127+ //noinspection MissingPermission
128+ return LocationServices .FusedLocationApi .getLastLocation (lostApiClient );
129+ }
130+ return null ;
131+ }
132+
133+ /**
134+ * Request location updates to the location provider.
135+ *
136+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
137+ */
138+ @ Deprecated
139+ @ Override
140+ public void requestLocationUpdates () {
141+ LocationRequest request = LocationRequest .create ();
142+
143+ if (interval != null ) {
144+ request .setInterval (interval );
145+ }
146+ if (fastestInterval != null ) {
147+ request .setFastestInterval (fastestInterval );
148+ }
149+ if (smallestDisplacement != null ) {
150+ request .setSmallestDisplacement (smallestDisplacement );
151+ }
152+
153+ if (priority == LocationEnginePriority .NO_POWER ) {
154+ request .setPriority (LocationRequest .PRIORITY_NO_POWER );
155+ } else if (priority == LocationEnginePriority .LOW_POWER ) {
156+ request .setPriority (LocationRequest .PRIORITY_LOW_POWER );
157+ } else if (priority == LocationEnginePriority .BALANCED_POWER_ACCURACY ) {
158+ request .setPriority (LocationRequest .PRIORITY_BALANCED_POWER_ACCURACY );
159+ } else if (priority == LocationEnginePriority .HIGH_ACCURACY ) {
160+ request .setPriority (LocationRequest .PRIORITY_HIGH_ACCURACY );
161+ }
162+
163+ if (lostApiClient .isConnected ()) {
164+ //noinspection MissingPermission
165+ LocationServices .FusedLocationApi .requestLocationUpdates (lostApiClient , request , this );
166+ }
167+ }
168+
169+ /**
170+ * Dismiss ongoing location update to the location provider.
171+ *
172+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
173+ */
174+ @ Deprecated
175+ @ Override
176+ public void removeLocationUpdates () {
177+ if (lostApiClient .isConnected ()) {
178+ LocationServices .FusedLocationApi .removeLocationUpdates (lostApiClient , this );
179+ }
180+ }
181+
182+ @ Deprecated
183+ @ Override
184+ public Type obtainType () {
185+ return Type .LOST ;
186+ }
187+
188+ /**
189+ * Invoked when the Location has changed.
190+ *
191+ * @param location the new location
192+ * @deprecated Use {@link com.mapbox.services.android.telemetry.location.LostLocationEngine} instead
193+ */
194+ @ Deprecated
195+ @ Override
196+ public void onLocationChanged (Location location ) {
197+ for (LocationEngineListener listener : locationListeners ) {
198+ listener .onLocationChanged (location );
199+ }
200+ }
201+
202+ private void connect () {
203+ if (lostApiClient != null ) {
204+ if (lostApiClient .isConnected ()) {
205+ onConnected ();
206+ } else {
207+ lostApiClient .connect ();
208+ }
209+ }
210+ }
211+ }
0 commit comments