Android Open Source - maze-android Sensor Listener






From Project

Back to project page maze-android.

License

The source code is released under:

GNU General Public License

If you think the Android project maze-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  Maze-Android//from  w  ww  .j av  a 2 s.  co  m
 *  Copyright 2009, 2010, 2011, 2012 Marco Mandrioli
 *
 *  This file is part of Maze-Android.
 *
 *  Maze-Android is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Maze-Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Maze-Android.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package it.zavo.maze.android;

import it.zavo.maze.physics.Physics;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

/**
 * Receives notifications from the SensorManager when sensor values have
 * changed.
 * 
 * @author Marco Mandrioli
 */
public class SensorListener implements SensorEventListener {
  private float[] orientation = new float[3];
  private float[] gravity = new float[3];
  private float[] geomag = new float[3];

  private float[] rmat = new float[16];

  public SensorListener() {
  }

  /** Called when the accuracy of a sensor has changed. */
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }

  /** Called when sensor values have changed. */
  @Override
  public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
      gravity = event.values.clone();
      break;
    case Sensor.TYPE_MAGNETIC_FIELD:
      geomag = event.values.clone();
      break;
    }

    // if gravity and geomag have values then find rotation matrix
    if (gravity != null && geomag != null)
      // checks that the rotation matrix is found
      if (SensorManager.getRotationMatrix(rmat, null, gravity, geomag))
        orientation = SensorManager.getOrientation(rmat, orientation);

    // update data in Physics library
    Physics.updateOrientation(orientation);
  }
}




Java Source Code List

it.zavo.maze.MazeActivity.java
it.zavo.maze.android.SensorListener.java
it.zavo.maze.graphics.Graphics.java
it.zavo.maze.graphics.TextureManager.java
it.zavo.maze.graphics.Texture.java
it.zavo.maze.graphics.shape.Circle.java
it.zavo.maze.graphics.shape.Rectangle.java
it.zavo.maze.maze.Ball.java
it.zavo.maze.maze.Hole.java
it.zavo.maze.maze.Maze.java
it.zavo.maze.maze.Wall.java
it.zavo.maze.physics.Physics.java
it.zavo.maze.util.Status.java
it.zavo.maze.util.Tex.java
it.zavo.maze.util.Xml.java