Skip to content

Instantly share code, notes, and snippets.

@mustafasevgi
Created December 12, 2015 18:26
Show Gist options
  • Save mustafasevgi/5c7952abd3dad933d922 to your computer and use it in GitHub Desktop.
Save mustafasevgi/5c7952abd3dad933d922 to your computer and use it in GitHub Desktop.
Calculate orientation with acceloremeter in android
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
/**
* Created by MustafaS on 10.6.2015.
*/
public abstract class QrBaseActivity extends BaseActivity implements SensorEventListener {
public static final int OrientationOptionLandscapeLeft = 0;
public static final int OrientationOptionLandscapeRight = 1;
public static final int OrientationOptionPortrait = 2;
public static final int OrientationOptionUpsideDown = 3;
public static final int OrientationOptionFaceUp = 4;
public static final int OrientationOptionFaceDown = 5;
public static final int OrientationOptionCount = 6;
public static final int DeviceOrientationNone = 0;
public static final int DeviceOrientationLandscapeLeft = 1 << OrientationOptionLandscapeLeft;
public static final int DeviceOrientationLandscapeRight = 1 << OrientationOptionLandscapeRight;
public static final int DeviceOrientationPortrait = 1 << OrientationOptionPortrait;
public static final int DeviceOrientationUpsideDown = 1 << OrientationOptionUpsideDown;
public static final int DeviceOrientationFaceUp = 1 << OrientationOptionFaceUp;
public static final int DeviceOrientationFaceDown = 1 << OrientationOptionFaceDown;
public static final int DeviceOrientationAllLandscape = DeviceOrientationLandscapeLeft | DeviceOrientationLandscapeRight;
public static final int DeviceOrientationAllPortrait = DeviceOrientationPortrait | DeviceOrientationUpsideDown;
public static final int DeviceOrientationAll = DeviceOrientationAllLandscape | DeviceOrientationAllPortrait | DeviceOrientationFaceUp | DeviceOrientationFaceDown;
private final static float[][] orientations = {{9.81f, 0, 0}, // DeviceOrientationLandscapeLeft
{-9.81f, 0, 0}, // DeviceOrientationLandscapeRight
{0, -9.81f, 0}, // DeviceOrientationPortrait
{0, 9.81f, 0}, // DeviceOrientationUpsideDown
{0, 0, -9.81f}, // DeviceOrientationFaceUp
{0, 0, 9.81f}}; // DeviceOrientationFaceDown
int interestedOrientations = DeviceOrientationAllLandscape | DeviceOrientationAllPortrait;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private int NONE;
private int currentOrientation = NONE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
if (DMSApplication.user == null) {
DMSApplication.sharedPrefHelper.clearData();
Utils.logOut(this);
}
}
@Override
public void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
final float x1 = event.values[0];
final float y1 = event.values[1];
final float z1 = event.values[2];
float threshold = 3.5f;
int currentOrientation = NONE;
for (int i = 0; i < 6; i++) {
float x = orientations[i][0];
float y = orientations[i][1];
float z = orientations[i][2];
if ((x1 > (x - threshold)) && (x1 < (x + threshold)) && (y1 > (y - threshold)) && (y1 < (y + threshold)) && (z1 > (z - threshold * 2.0)) && (z1 < (z + threshold * 2.0))) {
currentOrientation = 1 << i;
break;
}
}
if (currentOrientation != this.currentOrientation) {
boolean orientationChanged = false;
switch (currentOrientation) {
case DeviceOrientationLandscapeLeft : {
orientationChanged = (currentOrientation & this.interestedOrientations) == DeviceOrientationLandscapeLeft;
break;
}
case DeviceOrientationLandscapeRight : {
orientationChanged = (currentOrientation & this.interestedOrientations) == DeviceOrientationLandscapeRight;
break;
}
case DeviceOrientationPortrait : {
orientationChanged = (currentOrientation & this.interestedOrientations) == DeviceOrientationPortrait;
break;
}
case DeviceOrientationUpsideDown : {
orientationChanged = (currentOrientation & this.interestedOrientations) == DeviceOrientationUpsideDown;
break;
}
case DeviceOrientationFaceUp : {
orientationChanged = (currentOrientation & this.interestedOrientations) == DeviceOrientationFaceUp;
break;
}
case DeviceOrientationFaceDown : {
orientationChanged = (currentOrientation & this.interestedOrientations) == DeviceOrientationFaceDown;
break;
}
default :
break;
}
if (orientationChanged) {
this.currentOrientation = currentOrientation;
openQr(currentOrientation);
}
}
}
private void openQr(int currentOrientation) {
switch (currentOrientation) {
case DeviceOrientationLandscapeLeft : {
// if (this instanceof LandscapeBaseActivity) {
// finish();
// }
Intent intent = new Intent(this, ShowQrLandscapeActivity.class);
startActivity(intent);
break;
}
case DeviceOrientationLandscapeRight : {
// if (this instanceof LandscapeBaseActivity) {
// finish();
// }
Intent intent = new Intent(this, ShowQrReverseLandscapeActivity.class);
startActivity(intent);
break;
}
case DeviceOrientationPortrait : {
if (this instanceof LandscapeBaseActivity)
finish();
break;
}
case DeviceOrientationUpsideDown : {
if (this instanceof LandscapeBaseActivity)
finish();
break;
}
case DeviceOrientationFaceUp : {
break;
}
case DeviceOrientationFaceDown : {
break;
}
default :
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public abstract Toolbar getToolbar();
@NonNull
@Override
public abstract int getLayoutResource();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment