package com.example.mathm.geolocalisationindoor;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

/**
 * Class which knows the azimuth of the smartphone(using accelerometer and magnetometer sensors).
 * @author Camille Quenin
 */
public class CompassActivity implements SensorEventListener {

    //private CompassView compassView; //TODO: once it is useless, the compassView must be deleted.
    public SensorManager sensorManager;
    public Sensor accelerometer;
    public Sensor magnetometer;
    private float[] accelerometerReading;
    private float[] magnetometerReading;
    private float azimuth=0; // Angle between the device's current compass direction and magnetic north.

    public float getAzimuth(){
        return azimuth;
    }


    /* Methods of Activity */

    public CompassActivity(CarteActivity activity){
        sensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }


    /* Methods of SensorEventListener */
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
            accelerometerReading = event.values;
        }
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
            magnetometerReading = event.values;
        }
        if (accelerometerReading != null && magnetometerReading != null) {
            float rotationMatrix[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading);
            if (success) {
                float orientation[] = new float[3];
                SensorManager.getOrientation(rotationMatrix, orientation);
                if(Math.abs(orientation[0] - azimuth) < 15)
                azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll
            }
        }
        //compassView.invalidate();
    }
}

@@ -62,152 +62,23 @@
 oid.
-app.Activity;%0Aimport android.content.Context;%0Aimport android.graphics.Canvas;%0Aimport android.graphics.Paint;%0Aimport android.graphics.Paint.Style
+content.Context
 ;%0Aim
@@ -231,60 +231,8 @@
 ger;
-%0Aimport android.os.Bundle;%0Aimport android.view.View;
 %0A%0A/*
@@ -252,12 +252,17 @@
 ich 
-know
+determine
 s th
@@ -288,16 +288,17 @@
 artphone
+ 
 (using a
@@ -434,108 +434,8 @@
  %7B%0A%0A
-    //private CompassView compassView; //TODO: once it is useless, the compassView must be deleted.%0A
     
@@ -790,40 +790,8 @@
  %7D%0A%0A
-%0A    /* Methods of Activity */%0A%0A
     
@@ -1105,50 +1105,8 @@
  %7D%0A%0A
-%0A    /* Methods of SensorEventListener */%0A
     
@@ -2086,44 +2086,8 @@
   %7D%0A
-        //compassView.invalidate();%0A
     

clean CompassActivity