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

Truncate RotationVector in SensorInterpreter to avoid Samsung getRotationMatrixFromVector bug #18

Merged
merged 1 commit into from
Oct 13, 2015
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: 2 additions & 0 deletions motion/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:support-annotations:23.0.0'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override
public void onSensorChanged(SensorEvent event) {
if (mSensorInterpreter == null) return;
final float [] vectors = mSensorInterpreter.interpretSensorEvent(getContext(), event);
final float[] vectors = mSensorInterpreter.interpretSensorEvent(getContext(), event);

// Return if interpretation of data failed
if (vectors == null) return;
Expand Down Expand Up @@ -156,6 +156,7 @@ public void unregisterSensorManager() {
/**
* Unregisters the ParallaxImageView's SensorManager. Should be called in onPause from
* an Activity or Fragment to avoid continuing sensor usage.
*
* @param resetTranslation if the image translation should be reset to the origin
*/
public void unregisterSensorManager(boolean resetTranslation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.view.Surface;
import android.view.WindowManager;

Expand Down Expand Up @@ -51,32 +54,40 @@ class SensorInterpreter {
private float[] mRotationMatrix = new float[16];
private float[] mOrientedRotationMatrix = new float[16];

/**
* Holds a shortened version of the rotation vector for compatibility purposes.
*/
private float[] mTruncatedRotationVector;

/**
* The sensitivity the parallax effect has towards tilting.
*/
private float mTiltSensitivity = 2.0f;

/**
* Converts sensor data to yaw, pitch, and roll.
* Converts sensor data in a {@link SensorEvent} to yaw, pitch, and roll.
*
* @param context the context of the
* @param event the event to interpret
* @param event the event to interpret
* @return an interpreted vector of yaw, pitch, and roll delta values
*/
@SuppressWarnings("SuspiciousNameCombination")
public float[] interpretSensorEvent(Context context, SensorEvent event) {
public float[] interpretSensorEvent(@NonNull Context context, @Nullable SensorEvent event) {
if (event == null) {
return null;
}

// Retrieves the RotationVector from SensorEvent
float[] rotationVector = getRotationVectorFromSensorEvent(event);

// Set target rotation if none has been set
if (!mTargeted) {
setTargetVector(event.values);
setTargetVector(rotationVector);
return null;
}

// Get rotation matrix from event's values
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.getRotationMatrixFromVector(mRotationMatrix, rotationVector);

// Acquire rotation of screen
final int rotation = ((WindowManager) context
Expand Down Expand Up @@ -125,6 +136,31 @@ public float[] interpretSensorEvent(Context context, SensorEvent event) {
return mTiltVector;
}

/**
* Pulls out the rotation vector from a {@link SensorEvent}, with a maximum length
* vector of four elements to avoid potential compatibility issues.
*
* @param event the sensor event
* @return the events rotation vector, potentially truncated
*/
@NonNull
@VisibleForTesting
float[] getRotationVectorFromSensorEvent(@NonNull SensorEvent event) {
if (event.values.length > 4) {
// On some Samsung devices SensorManager.getRotationMatrixFromVector
// appears to throw an exception if rotation vector has length > 4.
// For the purposes of this class the first 4 values of the
// rotation vector are sufficient (see crbug.com/335298 for details).
if (mTruncatedRotationVector == null) {
mTruncatedRotationVector = new float[4];
}
System.arraycopy(event.values, 0, mTruncatedRotationVector, 0, 4);
return mTruncatedRotationVector;
} else {
return event.values;
}
}

/**
* Sets the target direction used for angle deltas to determine tilt.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.nvanbenschoten.motion;

import android.hardware.SensorEvent;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/*
* Copyright 2015 Nathan VanBenschoten
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class SamsungRotationMatrixBugTest {

@Test
public void testRotationVectorForEventWithoutAccuracyValue() throws Exception {
SensorInterpreter sensorInterpreter = new SensorInterpreter();
SensorEvent event = TestUtils.mockSensorEvent(new float[]{0.1f, 0.2f, 0.3f, 0.4f});

float[] rotationVector = sensorInterpreter.getRotationVectorFromSensorEvent(event);
assertNotNull(rotationVector);
assertTrue(rotationVector.length <= 4);
}

@Test
public void testRotationVectorForEventWithAccuracyValue() throws Exception {
SensorInterpreter sensorInterpreter = new SensorInterpreter();
SensorEvent event = TestUtils.mockSensorEvent(new float[]{0.1f, 0.2f, 0.3f, 0.4f, -1});

float[] rotationVector = sensorInterpreter.getRotationVectorFromSensorEvent(event);
assertNotNull(rotationVector);
assertTrue(rotationVector.length <= 4);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

import android.content.Context;
import android.hardware.SensorEvent;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import org.junit.Test;
import org.mockito.Mockito;

import java.lang.reflect.Constructor;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

/*
* Copyright 2014 Nathan VanBenschoten
Expand All @@ -40,8 +34,8 @@ public void testInterpretSensorEventPortraitWithNoOffset() throws Exception {
sensorInterpreter.setTiltSensitivity(2);
sensorInterpreter.setTargetVector(new float[]{0.5f, 0.6f, 0.7f});

Context context = mockRotationContext(Surface.ROTATION_0);
SensorEvent event = mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});
Context context = TestUtils.mockRotationContext(Surface.ROTATION_0);
SensorEvent event = TestUtils.mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});

float[] interpreted = sensorInterpreter.interpretSensorEvent(context, event);

Expand All @@ -54,8 +48,8 @@ public void testInterpretSensorEventLandscapeRightWithNoOffset() throws Exceptio
sensorInterpreter.setTiltSensitivity(2);
sensorInterpreter.setTargetVector(new float[]{0.5f, 0.6f, 0.7f});

Context context = mockRotationContext(Surface.ROTATION_90);
SensorEvent event = mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});
Context context = TestUtils.mockRotationContext(Surface.ROTATION_90);
SensorEvent event = TestUtils.mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});

float[] interpreted = sensorInterpreter.interpretSensorEvent(context, event);

Expand All @@ -68,8 +62,8 @@ public void testInterpretSensorEventLandscapeLeftWithNoOffset() throws Exception
sensorInterpreter.setTiltSensitivity(2);
sensorInterpreter.setTargetVector(new float[]{0.5f, 0.6f, 0.7f});

Context context = mockRotationContext(Surface.ROTATION_270);
SensorEvent event = mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});
Context context = TestUtils.mockRotationContext(Surface.ROTATION_270);
SensorEvent event = TestUtils.mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});

float[] interpreted = sensorInterpreter.interpretSensorEvent(context, event);

Expand All @@ -82,8 +76,8 @@ public void testInterpretSensorEventUpsideDownWithNoOffset() throws Exception {
sensorInterpreter.setTiltSensitivity(2);
sensorInterpreter.setTargetVector(new float[]{0.5f, 0.6f, 0.7f});

Context context = mockRotationContext(Surface.ROTATION_180);
SensorEvent event = mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});
Context context = TestUtils.mockRotationContext(Surface.ROTATION_180);
SensorEvent event = TestUtils.mockSensorEvent(new float[]{0.7f, 0.7f, 0.7f});

float[] interpreted = sensorInterpreter.interpretSensorEvent(context, event);

Expand All @@ -96,8 +90,8 @@ public void testInterpretSensorClampValues() throws Exception {
sensorInterpreter.setTiltSensitivity(1.5f);
sensorInterpreter.setTargetVector(new float[]{0f, 0f, 0f});

Context context = mockRotationContext(Surface.ROTATION_0);
SensorEvent event = mockSensorEvent(new float[]{3.1f, 0f, 3.1f});
Context context = TestUtils.mockRotationContext(Surface.ROTATION_0);
SensorEvent event = TestUtils.mockSensorEvent(new float[]{3.1f, 0f, 3.1f});

float[] interpreted = sensorInterpreter.interpretSensorEvent(context, event);

Expand All @@ -117,26 +111,4 @@ public void testSetNegativeTiltSensitivity() throws Exception {
sensorInterpreter.setTiltSensitivity(-1);
}

private Context mockRotationContext(int rotation) {
Display display = Mockito.mock(Display.class);
when(display.getRotation()).thenReturn(rotation);

WindowManager windowManager = Mockito.mock(WindowManager.class);
when(windowManager.getDefaultDisplay()).thenReturn(display);

Context context = Mockito.mock(Context.class);
when(context.getSystemService(Context.WINDOW_SERVICE)).thenReturn(windowManager);
return context;
}

private SensorEvent mockSensorEvent(float[] values) throws Exception {
Constructor<SensorEvent> c = SensorEvent.class.getDeclaredConstructor(int.class);
c.setAccessible(true);

SensorEvent event = c.newInstance(values.length);
System.arraycopy(values, 0, event.values, 0, values.length);

return event;
}

}
51 changes: 51 additions & 0 deletions motion/src/test/java/com/nvanbenschoten/motion/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.nvanbenschoten.motion;

import android.content.Context;
import android.hardware.SensorEvent;
import android.view.Display;
import android.view.WindowManager;

import org.mockito.Mockito;

import java.lang.reflect.Constructor;

import static org.mockito.Mockito.when;

public class TestUtils {

/**
* Creates a mock {@link Context} with the given rotation.
*
* @param rotation the screens orientation
* @return the mock Context
*/
public static Context mockRotationContext(int rotation) {
Display display = Mockito.mock(Display.class);
when(display.getRotation()).thenReturn(rotation);

WindowManager windowManager = Mockito.mock(WindowManager.class);
when(windowManager.getDefaultDisplay()).thenReturn(display);

Context context = Mockito.mock(Context.class);
when(context.getSystemService(Context.WINDOW_SERVICE)).thenReturn(windowManager);
return context;
}

/**
* Creates a mock {@link SensorEvent} with the provided values.
*
* @param values the values
* @return the mock SensorEvent
* @throws Exception
*/
public static SensorEvent mockSensorEvent(float[] values) throws Exception {
Constructor<SensorEvent> c = SensorEvent.class.getDeclaredConstructor(int.class);
c.setAccessible(true);

SensorEvent event = c.newInstance(values.length);
System.arraycopy(values, 0, event.values, 0, values.length);

return event;
}

}