Skip to content

Commit

Permalink
Merge pull request #59 from martinlaizg/fix-compass-tour
Browse files Browse the repository at this point in the history
Fix compass tour
  • Loading branch information
martinlaizg authored Jul 18, 2019
2 parents a1e7c2a + 6ea7e2d commit dc68c29
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Retrofit getRetrofitInstance() {
.method(originalRequest.method(), originalRequest.body()).build();

Response response = chain.proceed(newRequest);
if(response.code() == 401) { // Code 401 (Unathorized)
if(response.code() == 401) { // Code 401 Unauthorized
try {
userRepo.reLogin();
} catch(APIException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ private void login(Login login) {
// Hide the keyboard
InputMethodManager editTextInput = (InputMethodManager) requireActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
editTextInput.hideSoftInputFromWindow(
Objects.requireNonNull(requireActivity().getCurrentFocus()).getWindowToken(), 0);

View currentFocus = requireActivity().getCurrentFocus();
if(currentFocus != null) {
editTextInput.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,82 @@ public class PlayCompassFragment
implements SensorEventListener {

private static final String TAG = PlayCompassFragment.class.getSimpleName();
private final float[] mLastAccelerometer = new float[3];
private final float[] mLastMagnetometer = new float[3];
private final float[] mR = new float[9];
private static final float MIN_ROTATION = 8f;

private final float[] accData = new float[3];
private final float[] magnetData = new float[3];
private final float[] rotationMatrix = new float[9];
private final float[] mOrientation = new float[3];

@BindView(R.id.navigation_image)
ImageView navigation_image;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;
private float mCurrentDegree = 0f;

private SensorManager sensorManager;
private Sensor accelSensor;
private Sensor magnetSensor;
// Degree from north to destination 0 -> 360
private float bearing;
private float imgRotation;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_play_compass, container, false);
ButterKnife.bind(this, view);
mSensorManager = (SensorManager) requireActivity().getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager = (SensorManager) requireActivity().getSystemService(SENSOR_SERVICE);
accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
return view;
}

@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
} else if(event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if(mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
float azimuthInRadians = mOrientation[0];
float azimuthInDegress = (float) (Math.toDegrees(azimuthInRadians) + 360) % 360;
RotateAnimation ra = new RotateAnimation(mCurrentDegree, -azimuthInDegress,
if(usrLocation != null && placeLocation != null) {

// Copy the sensor data
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, accData, 0, event.values.length);
} else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, magnetData, 0, event.values.length);
}

// Calculate the rotation matrix
SensorManager.getRotationMatrix(rotationMatrix, null, accData, magnetData);
// Calculate the orientation
SensorManager.getOrientation(rotationMatrix, mOrientation);

// Get user orientation
float userOrientation = (float) Math.toDegrees(mOrientation[0]);
if(userOrientation < 0) userOrientation = 360 + userOrientation;

// Get orientation
float direction = bearing - userOrientation;
if(direction < 0) direction = 360 + direction;

Log.i(TAG, " direction " + direction + " \timgRot " + imgRotation);

if(Math.abs(imgRotation - direction) < 180) {
// Smooth the movement
float alpha = 0.95f;
direction = imgRotation * alpha + direction * (1 - alpha);
}

RotateAnimation ra = new RotateAnimation(imgRotation, direction,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(250);
ra.setFillAfter(true);
ra.setDuration(100);
// set the animation after the end of the reservation status
ra.setFillAfter(false);
// Start the animation
navigation_image.startAnimation(ra);
mCurrentDegree = -azimuthInDegress;
imgRotation = direction;
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

// Do nothing
}

@Override
Expand All @@ -90,25 +114,26 @@ protected String TAG() {
@Override
public void onResume() {
super.onResume();
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, accelSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, magnetSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onPause() {
super.onPause();
mSensorManager.unregisterListener(this, mAccelerometer);
mSensorManager.unregisterListener(this, mMagnetometer);
sensorManager.unregisterListener(this, accelSensor);
sensorManager.unregisterListener(this, magnetSensor);
}

@Override
void updateView() {
if(usrLocation != null && placeLocation != null) {
// Rotate image
float bearing = usrLocation.bearingTo(placeLocation);
Log.d(TAG, "updateView: rotate image=" + bearing);
navigation_image.setRotation(bearing);
bearing = usrLocation.bearingTo(placeLocation);
if(bearing < 0) bearing = 360 + bearing;
// declination = new GeomagneticField((float) usrLocation.getLatitude(),
// (float) usrLocation.getLongitude(),
// (float) usrLocation.getAltitude(),
// System.currentTimeMillis()).getDeclination();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ public void onLocationChanged(@NonNull Location location) {
updateView();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG(), "onStatusChanged: ");
}

@Override
public void onProviderEnabled(String provider) {
Log.i(TAG(), "onProviderEnabled: ");
}

@Override
public void onProviderDisabled(String provider) {
Log.i(TAG(), "onProviderDisabled: ");
}

private void completePlace() {
// hide question dialog if exist and is showing
if(questionDialog != null && questionDialog.isShowing()) questionDialog.dismiss();
Expand Down Expand Up @@ -215,7 +230,6 @@ PackageManager.PERMISSION_GRANTED && requireActivity()
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOC_TIME_REQ,
LOC_DIST_REQ, this);
}

}

private void setPlace(Place nextPlace) {
Expand All @@ -233,21 +247,6 @@ private void setPlace(Place nextPlace) {
updateView();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG(), "onStatusChanged: ");
}

@Override
public void onProviderEnabled(String provider) {
Log.i(TAG(), "onProviderEnabled: ");
}

@Override
public void onProviderDisabled(String provider) {
Log.i(TAG(), "onProviderDisabled: ");
}

private void createDialog(Place place) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext());
View dialogView = getLayoutInflater()
Expand Down

0 comments on commit dc68c29

Please sign in to comment.