Fixed problems resulting from improper merge in upstream

This is the revision in which the work that was undone when the Paul branch was merged is restored. Do not worry about the enormous swathes of code Git says have been changed: for some reason it can't detect the fact that most lines have remained unchained with merely a few insertions here and there.

The code is tested and runs correctly on a Samsung tablet running Android 4.0.4 (including motion sensor) as well as on an emulator running Android 4.2