I'm a little confused about what you're asking. Do you want to put a device in your pocket and walk around, and make a plot of everywhere you walked? Or do you want to wave the device around in your hand like a 3-d mouse, for painting?
For the latter, see Ali's suggestion above. If you want to do motion tracking, here is what I can tell you:
You can easily use accelerometer (or gravity if available) plus magnetometer combined with SensorManager.getRotationMatrix()
to know which direction the device is facing.
In fact, the ROTATION sensor does all this for you. You just need enough math to convert a quaternion into a transformation matrix; it's pretty easy.
(If you're trying to implement a gyro mouse, you can stop reading here.)
Then you can use the linear acceleration sensor to determine the direction the device is moving. This would be a sort of poor man's inertial navigation system. But that assumes the device is sitting still when you start the app, and that the sensors are really precise enough to do what you want.
The acceleration will give you a vector in device coordinates. Multiply that vector by the matrix you got from the ROTATION sensor or SensorManager.getRotationMatrix() to get an acceleration vector in world coordinates. Then integrate that vector over time to get a velocity vector and then integrate that vector over time to get distance and direction moved since you started the app.
Errors will accumulate rapidly. This algorithm is probably good for a few minutes at best before it starts returning complete nonsense. You could tune the application to detect when the device is no longer moving, and use that opportunity to reset the velocity vector to zero.
Frankly, this is a very very hard problem to solve. There's probably a PhD or at least a couple of papers in it for you if you solve it. Search for the term "indoor navigation" for more references.
But bear in mind that without some sort of location service (network or gps), there's really no way to be sure which direction the device is moving or where it is.
My understanding is that all the existing indoor navigation algorithms depend on getting occasional GPS fixes, or by knowing the layout of the indoor space being navigated and using it for clues, or by knowing the locations of nearby WiFi transmitters and using their signal strengths to generate fixes.
Seriously, unless you're talking very small distances here, GPS is your best bet.