Example usage for android.hardware SensorManager AXIS_Z

List of usage examples for android.hardware SensorManager AXIS_Z

Introduction

In this page you can find the example usage for android.hardware SensorManager AXIS_Z.

Prototype

int AXIS_Z

To view the source code for android.hardware SensorManager AXIS_Z.

Click Source Link

Document

see #remapCoordinateSystem

Usage

From source file:com.adstrosoftware.notificationcompass.CompassService.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
        if (previousEventTimeStamp == Long.MIN_VALUE
                || (event.timestamp - previousEventTimeStamp) > DELAY_IN_NS) {
            previousEventTimeStamp = event.timestamp;

            float[] orientation = new float[3];
            float[] rotationMatrix = new float[16];
            float[] remappedRotationMatrix = new float[16];

            SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

            if (event.values[0] <= -45) {
                SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        remappedRotationMatrix);
            } else {
                remappedRotationMatrix = rotationMatrix;
            }//from   www  . ja va2s. c om

            SensorManager.getOrientation(remappedRotationMatrix, orientation);

            Notification notification;

            double azimuth = Math.toDegrees(orientation[0]);

            if (BuildConfig.DEBUG) {
                Log.d(TAG, "Azimuth = " + azimuth);
            }

            if (azimuth <= (NORTH + ANGLE) && azimuth >= (NORTH - ANGLE)) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH");
                notification = buildNotification(R.string.north, R.drawable.ic_stat_north, azimuth);
            } else if (azimuth <= (NORTH_EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH_EAST");
                notification = buildNotification(R.string.north_east, R.drawable.ic_stat_north_east, azimuth);
            } else if (azimuth >= (NORTH_WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "NORTH_WEST");
                notification = buildNotification(R.string.north_west, R.drawable.ic_stat_north_west, azimuth);
            } else if (azimuth <= (EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "EAST");
                notification = buildNotification(R.string.east, R.drawable.ic_stat_east, azimuth);
            } else if (azimuth >= (WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "WEST");
                notification = buildNotification(R.string.west, R.drawable.ic_stat_west, azimuth);
            } else if (azimuth <= (SOUTH_EAST + ANGLE) && azimuth > 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH_EAST");
                notification = buildNotification(R.string.south_east, R.drawable.ic_stat_south_east, azimuth);
            } else if (azimuth >= (SOUTH_WEST - ANGLE) && azimuth < 0) {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH_WEST");
                notification = buildNotification(R.string.south_west, R.drawable.ic_stat_south_west, azimuth);
            } else {
                if (BuildConfig.DEBUG)
                    Log.d(TAG, "SOUTH");
                notification = buildNotification(R.string.south, R.drawable.ic_stat_south, azimuth);
            }

            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
}

From source file:com.example.casthelloworld.MainActivity.java

@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return;// w ww  .jav a2 s . c  o m
    }

    SensorManager.getRotationMatrixFromVector(mRotationMatrix, sensorEvent.values);

    SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            mRotationMatrix);

    SensorManager.getOrientation(mRotationMatrix, orientationVals);

    orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

    float Roll = orientationVals[2];

    if (Roll < -85 && Roll > -95 && lastVal != 0.0f) {
        // Log.d("Middle: ", "" + Roll);
        lastVal = 0.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -95 && Roll > -105 && lastVal != -0.2f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -85 && Roll < -75 && lastVal != 0.2f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.2f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -105 && Roll > -115 && lastVal != -0.4f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -75 && Roll < -65 && lastVal != 0.4f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.4f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -115 && Roll > -125 && lastVal != -0.6f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -65 && Roll < -55 && lastVal != 0.6f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.6f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -125 && Roll > -135 && lastVal != -0.8f) {
        // Log.d("Left", "" + Roll);
        lastVal = -0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -55 && Roll < -45 && lastVal != 0.8f) {
        // Log.d("Right", "" + Roll);
        lastVal = 0.8f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll <= -135 && lastVal != -1.0f && lastVal != 1.0f) {
        // Log.d("Left", "" + Roll);
        lastVal = -1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    } else if (Roll >= -45 && lastVal != 1.0f && lastVal != -1.0f) {
        // Log.d("Right", "" + Roll);
        lastVal = 1.0f;
        try {
            directionMessage.put("direction", (double) lastVal);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mGameManagerClient.sendGameMessage(directionMessage);
    }

}

From source file:jp.co.recruit_lifestyle.android.widget.BeerSwipeRefreshLayout.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() != Sensor.TYPE_ROTATION_VECTOR || !mBeerView.isMax()) {
        return;/*from  www  . jav  a 2 s  .c  o  m*/
    }

    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);

    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
            adjustedRotationMatrix);

    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);

    float roll = orientation[2] * -57;

    if (roll < ROLL_LIMIT && roll > -ROLL_LIMIT) {
        mBeerView.drawGlass(-roll);
        mBeerView.drawGlassFroth(-roll, 1);
        mOldRoll = -roll;
    } else {
        mBeerView.drawGlass(mOldRoll);
        mBeerView.drawGlassFroth(mOldRoll, 1);
    }
}

From source file:net.line2soft.preambul.controllers.SlippyMapListener.java

@Override
public void onSensorChanged(SensorEvent event) {
    CompassView cp = (CompassView) activity.findViewById(R.id.compass);
    CompassView cpBig = (CompassView) activity.findViewById(R.id.compassBig);
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        System.arraycopy(event.values, 0, mGravity, 0, 3);
        break;/*from  www  .ja  v  a 2 s  . com*/
    case Sensor.TYPE_MAGNETIC_FIELD:
        System.arraycopy(event.values, 0, mMagnetic, 0, 3);
        break;
    default:
        return;
    }
    if (SensorManager.getRotationMatrix(mRotationM, null, mGravity, mMagnetic)) {
        SensorManager.remapCoordinateSystem(mRotationM, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                mRemapedRotationM);
        SensorManager.getOrientation(mRemapedRotationM, mOrientation);
        int mAzimuth = (int) Math.round((Math.toDegrees(mOrientation[0])) * 2) / 2;
        cp.updateDirection(mAzimuth);
        cpBig.updateDirection(mAzimuth);
    }
}

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.BusStopDetailsFragment.java

/**
 * Update the direction needle so that it is pointing towards the bus stop,
 * based on the device location and the direction it is facing.
 *//*w w  w.  ja  v  a 2s.com*/
private void updateDirectionNeedle() {
    // We need values for location, the accelerometer and magnetometer to
    // continue.
    if (lastLocation == null || accelerometerValues == null || magnetometerValues == null) {
        // Make sure the needle isn't showing.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);

        return;
    }

    // Calculating the rotation matrix may fail, for example, if the device
    // is in freefall. In that case we cannot continue as the values will
    // be unreliable.
    if (!SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerValues, magnetometerValues)) {
        return;
    }

    // The screen rotation was obtained earlier.
    switch (screenRotation) {
    // There's lots of information about this elsewhere, but briefly;
    // The values from the sensors are in the device's coordinate system
    // which may be correct if the device is in its natural orientation,
    // but it needs to be remapped if the device is rotated.
    case Surface.ROTATION_0:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                rotationMatrix);
        break;
    case Surface.ROTATION_90:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X,
                rotationMatrix);
        break;
    case Surface.ROTATION_180:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X,
                SensorManager.AXIS_MINUS_Z, rotationMatrix);
        break;
    case Surface.ROTATION_270:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X,
                rotationMatrix);
        break;
    }

    // Get the X, Y and Z orientations, which are in radians. Covert this
    // in to degrees East of North.
    SensorManager.getOrientation(rotationMatrix, headings);
    double heading = Math.toDegrees(headings[0]);

    // If there's a GeomagneticField value, then adjust the heading to take
    // this in to account.
    if (geoField != null) {
        heading -= geoField.getDeclination();
    }

    // The orientation is in the range of -180 to +180. Convert this in to
    // a range of 0 to 360.
    final float bearingTo = distance[1] < 0 ? distance[1] + 360 : distance[1];

    // This is the heading to the bus stop.
    heading = bearingTo - heading;

    // The above calculation may come out as a negative number again. Put
    // this back in to the range of 0 to 360.
    if (heading < 0) {
        heading += 360;
    }

    // This 'if' statement is required to prevent a crash during device
    // rotation. It ensured that the Fragment is still part of the Activity.
    if (isAdded()) {
        // Get the arrow bitmap from the resources.
        final Bitmap needleIn = BitmapFactory.decodeResource(getResources(), R.drawable.heading_arrow);
        // Get an identity matrix and rotate it by the required amount.
        final Matrix m = new Matrix();
        m.setRotate((float) heading % 360, (float) needleIn.getWidth() / 2, (float) needleIn.getHeight() / 2);
        // Apply the rotation matrix to the Bitmap, to create a new Bitmap.
        final Bitmap needleOut = Bitmap.createBitmap(needleIn, 0, 0, needleIn.getWidth(), needleIn.getHeight(),
                m, true);

        // Recycle the needle read in if it's not the same as the rotated
        // needle.
        if (needleIn != needleOut) {
            needleIn.recycle();
        }

        // This Bitmap needs to be converted to a Drawable type.
        final BitmapDrawable drawable = new BitmapDrawable(getResources(), needleOut);
        // Set the new needle to be on the right hand side of the TextView.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        recycleNeedleBitmapIfNotNull(needleOut);
    } else {
        // If the Fragment is not added to the Activity, then make sure
        // there's no needle.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);
    }
}