package com.houseofmoran.sensorplay;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.TriggerEventListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private SensorManager mSensorManager;
    private Sensor mMagneticSensor;
    private final MagneticSensorListener mListener = new MagneticSensorListener();

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

        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mMagneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mListener, mMagneticSensor, 1000 * 1000);
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(mListener);
    }

    private class MagneticSensorListener implements SensorEventListener {
        @Override
        public void onSensorChanged(SensorEvent event) {
            Log.i(this.getClass().getName(), Arrays.toString(event.values));
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    }
}

@@ -188,54 +188,8 @@
 er;%0A
-import android.hardware.TriggerEventListener;%0A
 impo
@@ -311,16 +311,59 @@
 rrays;%0A%0A
+import static android.R.attr.orientation;%0A%0A
 public c
@@ -503,33 +503,61 @@
 ate 
-final MagneticSensorListe
+Sensor mAccelSensor;%0A    private final BearingDetermi
 ner 
@@ -572,35 +572,30 @@
 r = new 
-MagneticSensorListe
+BearingDetermi
 ner();%0A%0A
@@ -920,16 +920,99 @@
 FIELD);%0A
+        mAccelSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);%0A
     %7D%0A%0A 
@@ -1063,24 +1063,69 @@
 .onResume();
+%0A%0A        int samplingPeriodUs = 1000 * 1000;
 %0A        mSe
@@ -1181,27 +1181,116 @@
 Sensor, 
-1000 * 1000
+samplingPeriodUs);%0A        mSensorManager.registerListener(mListener, mAccelSensor, samplingPeriodUs
 );%0A    %7D
@@ -1398,16 +1398,101 @@
 Listener
+, mMagneticSensor);%0A        mSensorManager.unregisterListener(mListener, mAccelSensor
 );%0A    %7D
@@ -1515,27 +1515,22 @@
 ass 
-MagneticSensorListe
+BearingDetermi
 ner 
@@ -1574,150 +1574,1353 @@
     
-@Override%0A        public void onSensorChanged(SensorEvent event) %7B%0A            Log.i(this.getClass().getName(), Arrays.toString(event.values))
+private float%5B%5D mLastAccel = null;%0A        private float%5B%5D mLastMagnetic = null;%0A%0A        @Override%0A        public void onSensorChanged(SensorEvent event) %7B%0A            if (event.sensor == mAccelSensor) %7B%0A                mLastAccel = copyArray(event.values);%0A            %7D%0A            else if (event.sensor == mMagneticSensor) %7B%0A                mLastMagnetic = copyArray(event.values);%0A            %7D%0A%0A            if (mLastAccel != null && mLastMagnetic != null) %7B%0A                float azimuth = toAzimuth(mLastAccel, mLastMagnetic);%0A                Log.i(this.getClass().getName(), String.format(%22azimuth: %25f%22, azimuth));%0A            %7D%0A        %7D%0A%0A        private float toAzimuth(float%5B%5D accel, float%5B%5D magnetic) %7B%0A            float%5B%5D rotationMatrix = new float%5B9%5D;%0A            float%5B%5D orientation = new float%5B3%5D;%0A            SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnetic);%0A            SensorManager.getOrientation(rotationMatrix, orientation);%0A            float azimuthInRadians = orientation%5B0%5D;%0A            float azimuthInDegrees = (float)(Math.toDegrees(azimuthInRadians)+360)%25360;%0A            return azimuthInDegrees;%0A        %7D%0A%0A        private float%5B%5D copyArray(float%5B%5D values) %7B%0A            float%5B%5D copied = new float%5B3%5D;%0A            System.arraycopy(values, 0, copied, 0, values.length);%0A            return copied
 ;%0A  
@@ -3015,16 +3015,37 @@
 racy) %7B%0A
+            // ignore
 %0A       

convert to azimuth