diff --git a/Sources/app/src/main/java/com/furwaz/roomview/PhotoActivity.java b/Sources/app/src/main/java/com/furwaz/roomview/PhotoActivity.java index 0cd08eebf67624e2bbd53ca8db338cfdefc4f9a3..000daf709a408e1ac15bf25d23f7decafc529d00 100644 --- a/Sources/app/src/main/java/com/furwaz/roomview/PhotoActivity.java +++ b/Sources/app/src/main/java/com/furwaz/roomview/PhotoActivity.java @@ -27,6 +27,7 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; +import android.widget.TextView; import android.widget.Toast; import java.io.File; @@ -54,7 +55,10 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe SensorManager sm = null; Sensor accelerometer = null; + Sensor magnet = null; LevelCanvas levelView = null; + float[] accelValues = new float[]{0f, 0f, 0f}; + float[] magnetValues = new float[]{0f, 0f, 0f}; BuildingInfo building = null; RoomInfo room = null; @@ -143,6 +147,7 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe sm = (SensorManager) getSystemService(SENSOR_SERVICE); if (sm != null) { accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + magnet = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); register(); } } @@ -220,13 +225,21 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe } protected void register() { - if (sm != null && accelerometer != null) - sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME); + if (sm != null) { + if (accelerometer != null) + sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); + if (magnet != null) + sm.registerListener(this, magnet, SensorManager.SENSOR_DELAY_UI); + } } protected void unregister() { - if (sm != null && accelerometer != null) - sm.unregisterListener(this, accelerometer); + if (sm != null) { + if (accelerometer != null) + sm.unregisterListener(this, accelerometer); + if (magnet != null) + sm.unregisterListener(this, magnet); + } } @Override @@ -235,14 +248,29 @@ public class PhotoActivity extends AppCompatActivity implements SensorEventListe @Override public void onSensorChanged(SensorEvent sensorEvent) { if (sensorEvent.sensor == accelerometer) { + accelValues = sensorEvent.values; float x_val = sensorEvent.values[0]; float y_val = sensorEvent.values[1]; float orient = (float) Math.atan2(y_val, x_val); if (levelView != null) levelView.askForDraw(orient); - if (orient < 1) - displayOrientation(Orientation.NORTH); - else displayOrientation(Orientation.EST); + } + if (sensorEvent.sensor == magnet) { + magnetValues = sensorEvent.values; + float res[] = new float[9]; + SensorManager.getRotationMatrix(res, null, accelValues, magnetValues); + float values[] = new float[3]; + SensorManager.getOrientation(res, values); + int angle = ((int)(values[0] * 180 / Math.PI) + 135) % 360; + if (angle < 0) angle = 360 + angle; + + Orientation orient = Orientation.NORTH; + if (angle >= 0 && angle < 90) orient = Orientation.WEST; + if (angle >= 90 && angle < 180) orient = Orientation.NORTH; + if (angle >= 180 && angle < 270) orient = Orientation.EST; + if (angle >= 270 && angle < 360) orient = Orientation.SOUTH; + + displayOrientation(orient); } }