Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jun 1, 2017
1 parent b866bf5 commit 3494b21
Show file tree
Hide file tree
Showing 19 changed files with 308 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;
import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -30,6 +31,7 @@ public final class Mapbox {
private Context context;
private String accessToken;
private Boolean connected;
private LocationSource lostLocationSource;

/**
* Get an instance of Mapbox.
Expand All @@ -45,8 +47,8 @@ public final class Mapbox {
public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull String accessToken) {
if (INSTANCE == null) {
Context appContext = context.getApplicationContext();
INSTANCE = new Mapbox(appContext, accessToken);
LocationEngine locationEngine = LocationSource.getLocationEngine(appContext);
INSTANCE = new Mapbox(appContext, accessToken, new LocationSource(appContext));
LocationEngine locationEngine = new LocationSource(appContext);
locationEngine.setPriority(LocationEnginePriority.NO_POWER);
MapboxTelemetry.getInstance().initialize(
appContext, accessToken, BuildConfig.MAPBOX_EVENTS_USER_AGENT, locationEngine);
Expand All @@ -55,9 +57,10 @@ public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull
return INSTANCE;
}

Mapbox(@NonNull Context context, @NonNull String accessToken) {
Mapbox(@NonNull Context context, @NonNull String accessToken, LocationSource locationSource) {
this.context = context;
this.accessToken = accessToken;
this.lostLocationSource = locationSource;
}

/**
Expand Down Expand Up @@ -128,4 +131,8 @@ public static synchronized Boolean isConnected() {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return (activeNetwork != null && activeNetwork.isConnected());
}

public static LocationSource getDefaultLocationSource() {
return INSTANCE.lostLocationSource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEngineListener;
Expand Down Expand Up @@ -40,9 +41,9 @@ public class LocationSource extends LocationEngine implements
private WeakReference<Context> context;
private LostApiClient lostApiClient;

private LocationSource(Context context) {
public LocationSource(Context context) {
super();
this.context = new WeakReference<>(context);
this.context = new WeakReference<>(context.getApplicationContext());
lostApiClient = new LostApiClient.Builder(this.context.get())
.addConnectionCallbacks(this)
.build();
Expand All @@ -68,8 +69,11 @@ public static synchronized LocationEngine getLocationEngine(@NonNull Context con
*/
@Override
public void activate() {
if (lostApiClient != null && !lostApiClient.isConnected()) {
Timber.e("LOCDEBUG %s activate - going to connect %s", hashCode(), !lostApiClient.isConnected());
if (!lostApiClient.isConnected()) {
lostApiClient.connect();
}else{
Timber.e("LOCDEBUG %s activate - NO connect", hashCode());
}
}

Expand All @@ -80,7 +84,8 @@ public void activate() {
*/
@Override
public void deactivate() {
if (lostApiClient != null && lostApiClient.isConnected()) {
Timber.e("LOCDEBUG %s deactivate - going to disconnect %s", hashCode(), lostApiClient.isConnected());
if (lostApiClient.isConnected()) {
lostApiClient.disconnect();
}
}
Expand All @@ -101,6 +106,7 @@ public boolean isConnected() {
*/
@Override
public void onConnected() {
Timber.e("LOCDEBUG %s onConnected", hashCode());
for (LocationEngineListener listener : locationListeners) {
listener.onConnected();
}
Expand All @@ -112,6 +118,7 @@ public void onConnected() {
@Override
public void onConnectionSuspended() {
Timber.d("Connection suspended.");
Timber.e("LOCDEBUG %s ConnectedSuspended", hashCode());
}

/**
Expand All @@ -120,6 +127,7 @@ public void onConnectionSuspended() {
* @return the last known location
*/
@Override
@Nullable
public Location getLastLocation() {
if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context.get())) {
//noinspection MissingPermission
Expand Down Expand Up @@ -153,6 +161,7 @@ public void requestLocationUpdates() {

if (lostApiClient.isConnected() && PermissionsManager.areLocationPermissionsGranted(context.get())) {
//noinspection MissingPermission
Timber.e("LOCDEBUG %s requestLocationUpdates with priority %s", hashCode(), priority);
LocationServices.FusedLocationApi.requestLocationUpdates(lostApiClient, request, this);
}
}
Expand All @@ -163,6 +172,7 @@ public void requestLocationUpdates() {
@Override
public void removeLocationUpdates() {
if (lostApiClient.isConnected()) {
Timber.e("LOCDEBUG %s removeLocationUpdates", hashCode());
LocationServices.FusedLocationApi.removeLocationUpdates(lostApiClient, this);
}
}
Expand All @@ -175,6 +185,7 @@ public void removeLocationUpdates() {
@Override
public void onLocationChanged(Location location) {
for (LocationEngineListener listener : locationListeners) {
Timber.e("LOCDEBUG %s onLocationChanged %s", hashCode(), location);
listener.onLocationChanged(location);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.location.LocationSource;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1860,15 +1860,6 @@ public void setOnMyLocationChangeListener(@Nullable MapboxMap.OnMyLocationChange
trackingSettings.setOnMyLocationChangeListener(listener);
}

/**
* Removes custom location source of the my-location layer and brings back default {@link LocationSource}
* location source.
*/
@UiThread
public void removeLocationSource() {
trackingSettings.removeLocationSource();
}

/**
* Replaces the location source of the my-location layer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
Expand Down Expand Up @@ -47,7 +48,7 @@ public final class TrackingSettings {
}

void initialise(MapboxMapOptions options) {
locationSource = LocationSource.getLocationEngine(myLocationView.getContext());
locationSource = Mapbox.getDefaultLocationSource();
setMyLocationEnabled(options.getLocationEnabled());
}

Expand Down Expand Up @@ -356,17 +357,19 @@ private void setMyLocationEnabled(boolean locationEnabled, boolean isCustomLocat
}

void setLocationSource(LocationEngine locationSource) {
if (this.locationSource != null && this.locationSource.equals(locationSource)) {
// this source is already active
return;
}

this.isCustomLocationSource = locationSource != null;
if (locationSource == null) {
locationSource = Mapbox.getDefaultLocationSource();
}
this.locationSource = locationSource;
this.isCustomLocationSource = true;
myLocationView.setLocationSource(locationSource);
}

void removeLocationSource() {
locationSource = LocationSource.getLocationEngine(myLocationView.getContext());
this.isCustomLocationSource = false;
myLocationView.removeLocationSource();
}

void update() {
if (!myLocationView.isEnabled()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
Expand All @@ -40,6 +41,8 @@

import java.lang.ref.WeakReference;

import timber.log.Timber;

/**
* UI element overlaid on a map to show the user's location.
*/
Expand Down Expand Up @@ -147,6 +150,10 @@ private void init(Context context) {
compassListener = new CompassListener(context);
}

public void init(LocationSource locationSource) {
this.locationSource = locationSource;
}

public final void setForegroundDrawables(Drawable defaultDrawable, Drawable bearingDrawable) {
if (defaultDrawable == null) {
return;
Expand Down Expand Up @@ -430,34 +437,27 @@ private void toggleGps(boolean enableGps, boolean isCustomLocationSource) {
if (enableGps) {
if (locationSource == null) {
if (!isCustomLocationSource) {
locationSource = LocationSource.getLocationEngine(this.getContext());
locationSource = Mapbox.getDefaultLocationSource();
} else {
return;
}
}
// Set an initial location if one available
Location lastLocation = locationSource.getLastLocation();

if (lastLocation != null) {
setLocation(lastLocation);
}

if (userLocationListener == null) {
userLocationListener = new GpsLocationListener(this, locationSource);
}

locationSource.addLocationEngineListener(userLocationListener);
locationSource.activate();

locationSource.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationSource.activate();
} else {
if (locationSource == null) {
return;
}
// Disable location and user dot
location = null;
locationSource.removeLocationUpdates();
locationSource.removeLocationEngineListener(userLocationListener);
locationSource.removeLocationUpdates();
locationSource.deactivate();
}
}
Expand Down Expand Up @@ -571,14 +571,7 @@ public void setLocationSource(LocationEngine locationSource) {
toggleGps(false);
this.locationSource = locationSource;
this.userLocationListener = null;
setEnabled(isEnabled(), true);
}

public void removeLocationSource() {
toggleGps(false);
this.locationSource = LocationSource.getLocationEngine(getContext());
this.userLocationListener = null;
setEnabled(isEnabled(), false);
setEnabled(isEnabled(), locationSource != null);
}

private static class GpsLocationListener implements LocationEngineListener {
Expand All @@ -596,8 +589,6 @@ public void onConnected() {
MyLocationView locationView = userLocationView.get();
if (locationView != null) {
LocationEngine locationEngine = locationSource.get();
Location location = locationEngine.getLastLocation();
locationView.setLocation(location);
locationEngine.requestLocationUpdates();
}
}
Expand Down Expand Up @@ -641,6 +632,9 @@ public void onPause() {
}

public boolean isSensorAvailable() {
if (rotationVectorSensor == null) {
Timber.e("Sensor.TYPE_ROTATION_VECTOR is missing from this device. Unable to use MyBearingTracking.COMPASS.");
}
return rotationVectorSensor != null;
}

Expand Down
Loading

0 comments on commit 3494b21

Please sign in to comment.