Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic map FPS adjustment for NavigationMapboxMap #1669

Merged
merged 1 commit into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {
]

version = [
mapboxMapSdk : '7.0.0',
mapboxMapSdk : '7.0.1',
mapboxSdkServices : '4.3.0',
mapboxEvents : '4.2.0',
mapboxNavigator : '3.4.12',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* <p>
* If the camera tracking is dismissed, we notify the presenter to adjust UI accordingly.
*/
public class NavigationOnCameraTrackingChangedListener implements OnCameraTrackingChangedListener {
class NavigationOnCameraTrackingChangedListener implements OnCameraTrackingChangedListener {

private final NavigationPresenter navigationPresenter;
private final BottomSheetBehavior summaryBehavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.location.Location;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.mapbox.api.directions.v5.models.DirectionsRoute;
Expand All @@ -16,6 +17,7 @@
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.OnLocationCameraTransitionListener;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
Expand All @@ -29,6 +31,7 @@
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import timber.log.Timber;

Expand All @@ -46,55 +49,48 @@
*/
public class NavigationCamera implements LifecycleObserver {

private static final int ONE_POINT = 1;

private MapboxMap mapboxMap;
private LocationComponent locationComponent;
private MapboxNavigation navigation;
private RouteInformation currentRouteInformation;
private RouteProgress currentRouteProgress;
private ProgressChangeListener progressChangeListener = new ProgressChangeListener() {
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
currentRouteProgress = routeProgress;
if (isTrackingEnabled()) {
currentRouteInformation = buildRouteInformationFromLocation(location, routeProgress);
adjustCameraFromLocation(currentRouteInformation);
}
}
};

@Retention(RetentionPolicy.SOURCE)
@IntDef( {NAVIGATION_TRACKING_MODE_GPS,
NAVIGATION_TRACKING_MODE_NORTH,
NAVIGATION_TRACKING_MODE_NONE
})
public @interface TrackingMode {
}

/**
* Camera tracks the user location, with bearing provided by the location update.
* <p>
* Equivalent of the {@link CameraMode#TRACKING_GPS}.
*/
public static final int NAVIGATION_TRACKING_MODE_GPS = 0;

/**
* Camera tracks the user location, with bearing always set to north (0).
* <p>
* Equivalent of the {@link CameraMode#TRACKING_GPS_NORTH}.
*/
public static final int NAVIGATION_TRACKING_MODE_NORTH = 1;

/**
* Camera does not tack the user location.
* <p>
* Equivalent of the {@link CameraMode#NONE}.
*/
public static final int NAVIGATION_TRACKING_MODE_NONE = 2;

private static final int ONE_POINT = 1;
private final CopyOnWriteArrayList<OnTrackingModeTransitionListener> onTrackingModeTransitionListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<OnTrackingModeChangedListener> onTrackingModeChangedListeners
= new CopyOnWriteArrayList<>();
private final OnLocationCameraTransitionListener cameraTransitionListener
= new NavigationCameraTransitionListener(this);
private MapboxMap mapboxMap;
private LocationComponent locationComponent;
private MapboxNavigation navigation;
private RouteInformation currentRouteInformation;
private RouteProgress currentRouteProgress;
@TrackingMode
private int trackingCameraMode = NAVIGATION_TRACKING_MODE_GPS;
private ProgressChangeListener progressChangeListener = new ProgressChangeListener() {
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
currentRouteProgress = routeProgress;
if (isTrackingEnabled()) {
currentRouteInformation = buildRouteInformationFromLocation(location, routeProgress);
adjustCameraFromLocation(currentRouteInformation);
}
}
};

/**
* Creates an instance of {@link NavigationCamera}.
Expand Down Expand Up @@ -166,8 +162,7 @@ public void resume(Location location) {
* @param trackingMode the tracking mode
*/
public void updateCameraTrackingMode(@TrackingMode int trackingMode) {
trackingCameraMode = trackingMode;
setCameraMode();
setCameraMode(trackingMode);
}

/**
Expand Down Expand Up @@ -249,6 +244,66 @@ public void addProgressChangeListener(MapboxNavigation navigation) {
navigation.addProgressChangeListener(progressChangeListener);
}

/**
* Adds given tracking mode transition listener for receiving notification of camera
* transition updates.
*
* @param listener to be added
*/
public void addOnTrackingModeTransitionListener(@NonNull OnTrackingModeTransitionListener listener) {
onTrackingModeTransitionListeners.add(listener);
}

/**
* Removes given tracking mode transition listener for receiving notification of camera
* transition updates.
*
* @param listener to be removed
*/
public void removeOnTrackingModeTransitionListener(@NonNull OnTrackingModeTransitionListener listener) {
onTrackingModeTransitionListeners.remove(listener);
}

/**
* Adds given tracking mode changed listener for receiving notification of camera
* mode changes.
*
* @param listener to be added
*/
public void addOnTrackingModeChangedListener(@NonNull OnTrackingModeChangedListener listener) {
onTrackingModeChangedListeners.add(listener);
}

/**
* Removes given tracking mode transition listener for receiving notification of camera
* mode changes.
*
* @param listener to be removed
*/
public void removeOnTrackingModeChangedListener(@NonNull OnTrackingModeChangedListener listener) {
onTrackingModeChangedListeners.remove(listener);
}

void updateTransitionListenersFinished(@CameraMode.Mode int cameraMode) {
Integer trackingCameraMode = findTrackingModeFor(cameraMode);
if (trackingCameraMode == null) {
return;
}
for (OnTrackingModeTransitionListener listener : onTrackingModeTransitionListeners) {
listener.onTransitionFinished(trackingCameraMode);
}
}

void updateTransitionListenersCancelled(@CameraMode.Mode int cameraMode) {
Integer trackingCameraMode = findTrackingModeFor(cameraMode);
if (trackingCameraMode == null) {
return;
}
for (OnTrackingModeTransitionListener listener : onTrackingModeTransitionListeners) {
listener.onTransitionCancelled(trackingCameraMode);
}
}

private void initializeWith(MapboxNavigation navigation) {
navigation.setCameraEngine(new DynamicCamera(mapboxMap));
updateCameraTrackingMode(trackingCameraMode);
Expand Down Expand Up @@ -333,21 +388,48 @@ private LatLngBounds convertRoutePointsToLatLngBounds(List<Point> routePoints) {
.build();
}

private void setCameraMode() {
@CameraMode.Mode int mode;
private void setCameraMode(@TrackingMode int trackingCameraMode) {
@CameraMode.Mode Integer cameraMode = findCameraModeFor(trackingCameraMode);
if (cameraMode != null) {
this.trackingCameraMode = trackingCameraMode;
updateTrackingModeListenersWith(this.trackingCameraMode);
if (cameraMode != locationComponent.getCameraMode()) {
locationComponent.setCameraMode(cameraMode, cameraTransitionListener);
}
} else {
Timber.e("Using unsupported camera tracking mode - %d.", trackingCameraMode);
}
}

@Nullable
private Integer findCameraModeFor(@TrackingMode int trackingCameraMode) {
if (trackingCameraMode == NAVIGATION_TRACKING_MODE_GPS) {
mode = CameraMode.TRACKING_GPS;
return CameraMode.TRACKING_GPS;
} else if (trackingCameraMode == NAVIGATION_TRACKING_MODE_NORTH) {
mode = CameraMode.TRACKING_GPS_NORTH;
return CameraMode.TRACKING_GPS_NORTH;
} else if (trackingCameraMode == NAVIGATION_TRACKING_MODE_NONE) {
mode = CameraMode.NONE;
return CameraMode.NONE;
} else {
mode = CameraMode.NONE;
Timber.e("Using unsupported camera tracking mode - %d.", trackingCameraMode);
return null;
}
}

if (mode != locationComponent.getCameraMode()) {
locationComponent.setCameraMode(mode);
@Nullable
private Integer findTrackingModeFor(@CameraMode.Mode int cameraMode) {
if (cameraMode == CameraMode.TRACKING_GPS) {
return NAVIGATION_TRACKING_MODE_GPS;
} else if (cameraMode == CameraMode.TRACKING_GPS_NORTH) {
return NAVIGATION_TRACKING_MODE_NORTH;
} else if (cameraMode == CameraMode.NONE) {
return NAVIGATION_TRACKING_MODE_NONE;
} else {
return null;
}
}

private void updateTrackingModeListenersWith(@TrackingMode int trackingMode) {
for (OnTrackingModeChangedListener listener : onTrackingModeChangedListeners) {
listener.onTrackingModeChanged(trackingMode);
}
}

Expand Down Expand Up @@ -387,4 +469,12 @@ private long getTiltAnimationDuration(double tilt) {
NAVIGATION_MIN_CAMERA_TILT_ADJUSTMENT_ANIMATION_DURATION,
NAVIGATION_MAX_CAMERA_ADJUSTMENT_ANIMATION_DURATION);
}

@Retention(RetentionPolicy.SOURCE)
@IntDef( {NAVIGATION_TRACKING_MODE_GPS,
NAVIGATION_TRACKING_MODE_NORTH,
NAVIGATION_TRACKING_MODE_NONE
})
public @interface TrackingMode {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mapbox.services.android.navigation.ui.v5.camera;

import com.mapbox.mapboxsdk.location.OnLocationCameraTransitionListener;

class NavigationCameraTransitionListener implements OnLocationCameraTransitionListener {

private final NavigationCamera camera;

NavigationCameraTransitionListener(NavigationCamera camera) {
this.camera = camera;
}

@Override
public void onLocationCameraTransitionFinished(int cameraMode) {
camera.updateTransitionListenersFinished(cameraMode);
}

@Override
public void onLocationCameraTransitionCanceled(int cameraMode) {
camera.updateTransitionListenersCancelled(cameraMode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mapbox.services.android.navigation.ui.v5.camera;

/**
* Use this listener to observe changes in the {@link NavigationCamera}
* tracking mode.
*/
public interface OnTrackingModeChangedListener {

/**
* Invoked when {@link NavigationCamera} tracking mode changes.
*
* @param trackingMode the current tracking mode
*/
void onTrackingModeChanged(@NavigationCamera.TrackingMode int trackingMode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mapbox.services.android.navigation.ui.v5.camera;

/**
* Listener that gets invoked when the navigation camera finishes a transition
* to a new tracking mode.
*/
public interface OnTrackingModeTransitionListener {

/**
* Invoked when the camera has finished a transition to a new tracking mode.
*/
void onTransitionFinished(@NavigationCamera.TrackingMode int trackingMode);

/**
* Invoked when the transition to a new tracking mode has been cancelled.
*/
void onTransitionCancelled(@NavigationCamera.TrackingMode int trackingMode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mapbox.services.android.navigation.ui.v5.map;

import android.location.Location;

import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

class FpsDelegateProgressChangeListener implements ProgressChangeListener {

private final MapFpsDelegate fpsDelegate;

FpsDelegateProgressChangeListener(MapFpsDelegate fpsDelegate) {
this.fpsDelegate = fpsDelegate;
}

@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
fpsDelegate.adjustFpsFor(routeProgress);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mapbox.services.android.navigation.ui.v5.map;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;

class MapBatteryMonitor {

private static final int DEFAULT_BATTERY_LEVEL = -1;

boolean isPluggedIn(Context context) {
Intent batteryStatus = registerBatteryUpdates(context);
if (batteryStatus == null) {
return false;
}

int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, DEFAULT_BATTERY_LEVEL);
boolean pluggedUsb = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean pluggedAc = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
boolean isPlugged = pluggedUsb || pluggedAc;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
isPlugged = isPlugged || chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
return isPlugged;
}

private static Intent registerBatteryUpdates(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
return context.registerReceiver(null, filter);
}
}
Loading