public DirectionData calculateDirectionData(final float[] rotationMatrix) {

        //calculate "raw" orientation data
        SensorManager.getOrientation(rotationMatrix, orientation);

        //calculate/estimate current device orientation.
        final DirectionData.DeviceOrientation devOr;
        if (Settings.getDeviceOrientationMode() == DirectionData.DeviceOrientation.AUTO) {
            devOr = getDeviceOrientation(calculateDeviceOrientationFrom(orientation), true);
        } else {
            devOr = getDeviceOrientation(Settings.getDeviceOrientationMode(), false);
        }


        //If necessary, remap coordinates based on device orientation and recalculate direction
        switch (devOr) {
            case UPRIGHT:
                SensorManager.remapCoordinateSystem(rotationMatrix, AXIS_X, AXIS_Z, adjustedRotationMatrix);
                SensorManager.getOrientation(adjustedRotationMatrix, adjustedOrientation);
                break;
            case FLAT:
            case UNKNOWN:
            default:
                //in these case, no recalculation is necessary. direction = raw azimuth
                System.arraycopy(rotationMatrix, 0, adjustedRotationMatrix, 0, rotationMatrix.length);
                System.arraycopy(orientation, 0, adjustedOrientation, 0, orientation.length);
                break;
        }

        //collect data and create event data
        return DirectionData.createFor(adjustedOrientation[0], devOr, orientation);
    }
Helper for calculating orientation values as well as (corrected) direction
out of "raw" orientation vector and rotation matrix from one of the sensors
main/src/main/java/cgeo/geocaching/sensors/DirectionDataCalculator.java