package com.example.compasstest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Matrix;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView textview = null;
	TextView zhizhen = null;
	private SensorManager sm = null;
	private Sensor aSensor = null;
	private Sensor mSensor = null;

	float[] accelerometerValues = new float[3];
	float[] magneticFieldValues = new float[3];
	float[] values = new float[3];

	float[] R2 = new float[9];
	Matrix matrix = new Matrix();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		textview = (TextView) findViewById(R.id.weidu);
		zhizhen = (TextView) findViewById(R.id.zhizhen);
		sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
		sm.registerListener(myListener, aSensor,
				SensorManager.SENSOR_DELAY_GAME);
		sm.registerListener(myListener, mSensor,
				SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	// 注意activity暂停的时候释放
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		sm.unregisterListener(myListener);
	}

	final SensorEventListener myListener = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub
			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				accelerometerValues = event.values;
			}
			if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
				magneticFieldValues = event.values;
			}
			// 调用getRotaionMatrix获得变换矩阵R[]
			SensorManager.getRotationMatrix(R2, null, accelerometerValues,
					magneticFieldValues);
			SensorManager.getOrientation(R2, values);
			// 经过SensorManager.getOrientation(R, values);得到的values值为弧度
			// 转换为角度
			values[0] = (float) Math.toDegrees(values[0]);

			textview.setText("x=" + Math.round(values[0]));
			zhizhen.setRotation(-(Math.round(values[0]) / 5 * 5));
		}
	};
}
