Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void onMapReady(final MapboxMap mapboxMap) {
LocationEngine locationEngine = new LostLocationEngine(this);
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationLayerPlugin.setLocationLayerMode(RenderMode.COMPASS);
locationLayerPlugin.setRenderMode(RenderMode.COMPASS);
locationLayerPlugin.addCompassListener(new CompassListener() {
@Override
public void onCompassChanged(float userHeading) {
Expand Down Expand Up @@ -95,7 +95,7 @@ public void onLowMemory() {
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
locationLayerPlugin.removeCompassListener(null);
//locationLayerPlugin.removeCompassListener();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onMapReady(MapboxMap mapboxMap) {
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
locationPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationPlugin.setLocationLayerMode(RenderMode.COMPASS);
locationPlugin.setRenderMode(RenderMode.COMPASS);
}

@OnClick(R.id.fabStyles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public void onMapReady(MapboxMap mapboxMap) {
locationEngine.addLocationEngineListener(this);
locationEngine.activate();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine);
locationLayerPlugin.setOnLocationClickListener(this);
locationLayerPlugin.setLocationLayerEnabled(true);
locationLayerPlugin.addOnLocationClickListener(this);
getLifecycle().addObserver(locationLayerPlugin);
}

Expand Down Expand Up @@ -213,11 +212,11 @@ private void showModeListDialog() {
String selectedMode = modes.get(position);
locationModeBtn.setText(selectedMode);
if (selectedMode.contentEquals("Normal")) {
locationLayerPlugin.setLocationLayerMode(RenderMode.NORMAL);
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
} else if (selectedMode.contentEquals("Compass")) {
locationLayerPlugin.setLocationLayerMode(RenderMode.COMPASS);
locationLayerPlugin.setRenderMode(RenderMode.COMPASS);
} else if (selectedMode.contentEquals("GPS")) {
locationLayerPlugin.setLocationLayerMode(RenderMode.GPS);
locationLayerPlugin.setRenderMode(RenderMode.GPS);
}
listPopup.dismiss();
});
Expand All @@ -240,15 +239,15 @@ private void showTrackingListDialog() {
String selectedTrackingType = trackingTypes.get(position);
locationTrackingBtn.setText(selectedTrackingType);
if (selectedTrackingType.contentEquals("None")) {
locationLayerPlugin.setLocationLayerTracking(CameraMode.NONE);
locationLayerPlugin.setCameraMode(CameraMode.NONE);
} else if (selectedTrackingType.contentEquals("Tracking")) {
locationLayerPlugin.setLocationLayerTracking(CameraMode.TRACKING);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
} else if (selectedTrackingType.contentEquals("Tracking Compass")) {
locationLayerPlugin.setLocationLayerTracking(CameraMode.TRACKING_COMPASS);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING_COMPASS);
} else if (selectedTrackingType.contentEquals("Tracking GPS")) {
locationLayerPlugin.setLocationLayerTracking(CameraMode.TRACKING_GPS);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING_GPS);
} else if (selectedTrackingType.contentEquals("Tracking GPS North")) {
locationLayerPlugin.setLocationLayerTracking(CameraMode.TRACKING_GPS_NORTH);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING_GPS_NORTH);
}
listPopup.dismiss();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onMapReady(MapboxMap mapboxMap) {
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, null);
locationLayerPlugin.setLocationLayerMode(RenderMode.NORMAL);
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
getLifecycle().addObserver(locationLayerPlugin);
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_location_layer_mode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:gravity="center"
android:text="@string/button_location_mode_none"
android:text="@string/button_location_mode_normal"
android:textColor="@android:color/white"/>

<TextView
Expand All @@ -71,7 +71,7 @@
android:layout_height="wrap_content"
android:layout_weight="1.15"
android:gravity="center"
android:text="@string/button_location_mode_tracking"
android:text="@string/button_location_mode_none"
android:textColor="@android:color/white"/>

</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

<!-- Buttons -->
<string name="button_location_mode_none">None</string>
<string name="button_location_mode_normal">Normal</string>
<string name="button_location_mode_tracking">Tracking</string>
<string name="button_location_mode_compass">Compass</string>
<string name="button_location_mode_navigation">Nav</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class CompassManager implements SensorEventListener {

private final WindowManager windowManager;
private final SensorManager sensorManager;
private CompassListener internalCompassListener;
private List<CompassListener> compassListeners;
private final List<CompassListener> compassListeners = new ArrayList<>();

// Not all devices have a compassSensor
@Nullable
private Sensor compassSensor;

private int lastAccuracy;
private float lastHeading;

// CompassManager data
private long compassUpdateNextTimestamp;
Expand All @@ -49,10 +49,7 @@ class CompassManager implements SensorEventListener {
* Construct a new instance of the this class. A internal compass listeners needed to separate it
* from the cleared list of public listeners.
*/
CompassManager(@NonNull Context context, @NonNull CompassListener internalCompassListener) {
this.internalCompassListener = internalCompassListener;
compassListeners = new ArrayList<>();

CompassManager(@NonNull Context context) {
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

Expand All @@ -65,28 +62,30 @@ class CompassManager implements SensorEventListener {
}

void addCompassListener(@NonNull CompassListener compassListener) {
if (compassListeners.isEmpty()) {
onStart();
}
compassListeners.add(compassListener);
}

void removeCompassListener(@Nullable CompassListener compassListener) {
if (compassListener == null) {
compassListeners.clear();
return;
}
void removeCompassListener(@NonNull CompassListener compassListener) {
compassListeners.remove(compassListener);
}

List<CompassListener> getCompassListeners() {
return compassListeners;
if (compassListeners.isEmpty()) {
onStop();
}
}

void onStart() {
// Does nothing if the sensors already registered.
sensorManager.registerListener(this, compassSensor, SENSOR_DELAY_MICROS);
if (isSensorAvailable()) {
// Does nothing if the sensors already registered.
sensorManager.registerListener(this, compassSensor, SENSOR_DELAY_MICROS);
}
}

void onStop() {
sensorManager.unregisterListener(this, compassSensor);
if (isSensorAvailable()) {
sensorManager.unregisterListener(this, compassSensor);
}
}

boolean isSensorAvailable() {
Expand All @@ -95,9 +94,6 @@ boolean isSensorAvailable() {

@Override
public void onSensorChanged(SensorEvent event) {
if (internalCompassListener == null) {
return;
}
// check when the last time the compass was updated, return if too soon.
long currentTime = SystemClock.elapsedRealtime();
if (currentTime < compassUpdateNextTimestamp) {
Expand All @@ -113,17 +109,13 @@ public void onSensorChanged(SensorEvent event) {
// Update the compassUpdateNextTimestamp
compassUpdateNextTimestamp = currentTime + LocationLayerConstants.COMPASS_UPDATE_RATE_MS;
} else if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
internalCompassListener.onCompassChanged((event.values[0] + 360) % 360);
for (CompassListener compassListener : compassListeners) {
compassListener.onCompassChanged((event.values[0] + 360) % 360);
}
notifyCompassChangeListeners((event.values[0] + 360) % 360);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (lastAccuracy != accuracy) {
internalCompassListener.onCompassAccuracyChange(accuracy);
for (CompassListener compassListener : compassListeners) {
compassListener.onCompassAccuracyChange(accuracy);
}
Expand Down Expand Up @@ -170,9 +162,21 @@ private void updateOrientation(float[] rotationVector) {
SensorManager.getOrientation(adjustedRotationMatrix, orientation);

// The x-axis is all we care about here.
internalCompassListener.onCompassChanged((float) Math.toDegrees(orientation[0]));
notifyCompassChangeListeners((float) Math.toDegrees(orientation[0]));
}

private void notifyCompassChangeListeners(float heading) {
for (CompassListener compassListener : compassListeners) {
compassListener.onCompassChanged((float) Math.toDegrees(orientation[0]));
compassListener.onCompassChanged(heading);
}
lastHeading = heading;
}

int getLastAccuracy() {
return lastAccuracy;
}

float getLastHeading() {
return lastHeading;
}
}
Loading