A simple Android Studio project to demonstrate how to convert the motion sensors values from the device relative coordinate system to the earth absolute coordinate system.
Note that this project can only run on a device with gyroscope
Feel free to suggest edits or a better approach.
You can read the full explanation from the Medium article