Skip to content

Commit

Permalink
[android] - mapbox#2666 removed compass sensor data for milestone 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun authored and Leith Bade committed Oct 22, 2015
1 parent 23de24b commit 9eb47c6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,7 @@

import java.lang.ref.WeakReference;

final class CompassView extends ImageView implements SensorEventListener {

private CompassDelegate mCompassDelegate;

// Sensor model
private SensorManager mSensorManager;
private Sensor mSensorAccelerometer;
private Sensor mSensorMagneticField;

// Sensor data
private boolean mSensorValid;
private float[] mValuesAccelerometer = new float[3];
private float[] mValuesMagneticField = new float[3];
private float[] mMatrixR = new float[9];
private float[] mMatrixI = new float[9];
private float[] mMatrixValues = new float[3];

// Location data
private GeomagneticField mGeomagneticField;
private Location mGpsLocation;

// Compass date
private float mCompassBearing;
private boolean mCompassValid;
final class CompassView extends ImageView {

public CompassView(Context context) {
super(context);
Expand All @@ -57,11 +34,6 @@ public CompassView(Context context, AttributeSet attrs, int defStyleAttr) {
}

private void initialize(Context context) {
// Sensor initialisation
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensorAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorMagneticField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

// View configuration
setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.compass));
setContentDescription(getResources().getString(R.string.compassContentDescription));
Expand All @@ -78,67 +50,6 @@ public void setEnabled(boolean enabled) {
setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
}

public boolean isValid() {
return mCompassValid;
}

public float getBearing() {
return mCompassBearing;
}

public void registerListeners(CompassDelegate compassDelegate) {
mSensorManager.registerListener(this, mSensorAccelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mSensorMagneticField, SensorManager.SENSOR_DELAY_UI);
mCompassDelegate = compassDelegate;
}

public void unRegisterListeners() {
mCompassDelegate = null;
mSensorManager.unregisterListener(this, mSensorMagneticField);
mSensorManager.unregisterListener(this, mSensorAccelerometer);
}

@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccelerometer, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagneticField, 0, 3);
break;
}

mSensorValid = SensorManager.getRotationMatrix(mMatrixR, mMatrixI,
mValuesAccelerometer,
mValuesMagneticField);

if (mSensorValid && mCompassDelegate != null) {
SensorManager.getOrientation(mMatrixR, mMatrixValues);
mGpsLocation = mCompassDelegate.getLocation();
if (mGpsLocation != null) {
mGeomagneticField = new GeomagneticField(
(float) mGpsLocation.getLatitude(),
(float) mGpsLocation.getLongitude(),
(float) mGpsLocation.getAltitude(),
System.currentTimeMillis());
mCompassBearing = (float) Math.toDegrees(mMatrixValues[0]) + mGeomagneticField.getDeclination();
mCompassValid = true;
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO: ignore unreliable stuff
}

public interface CompassDelegate {

Location getLocation();

}

public static class CompassClickListener implements View.OnClickListener {

private WeakReference<MapView> mMapView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2979,11 +2979,9 @@ private void toggleGps(boolean enableGps) {
updateLocation(LocationServices.FusedLocationApi.getLastLocation());
mLocationListener = new MyLocationListener();
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationRequest, mLocationListener);
mCompassView.registerListeners(new CompassDelegate());
}
} else {
if (mLocationClient.isConnected()) {
mCompassView.unRegisterListeners();
LocationServices.FusedLocationApi.removeLocationUpdates(mLocationListener);
mLocationListener = null;
mLocationClient.disconnect();
Expand Down Expand Up @@ -3112,13 +3110,6 @@ private void updateCompass() {
}
}

private class CompassDelegate implements CompassView.CompassDelegate {
@Override
public Location getLocation() {
return mGpsLocation;
}
}

//
// Logo
//
Expand Down

0 comments on commit 9eb47c6

Please sign in to comment.