linux-man_lostleaf/LostLeaf/Sensors.pde
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

Context context;
SensorManager manager;
Sensor sensor;
AccelerometerListener listener;

void startSensors() {
  context = getActivity();
  manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
  sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  listener = new AccelerometerListener();
  manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
}

class AccelerometerListener implements SensorEventListener {
  public void onSensorChanged(SensorEvent event) {
    if(tiltX) tilt.x = int(-event.values[0] / 3 * maxTilt / (maxTilt / 5)) * (maxTilt / 5);
    if(tiltY) tilt.y = int((event.values[1] - 5) / 3 * maxTilt / (maxTilt / 5)) * (maxTilt / 5);
    tilt.limit(maxTilt);
    if(tilt.mag() < maxTilt / 5) tilt.set(0, 0);
  }
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }
}

public void onResume() {
  super.onResume();
  if (manager != null) {
    manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
  }
}

public void onPause() {
  super.onPause();
  if (manager != null) {
    manager.unregisterListener(listener);
  }
}
