Example usage for android.hardware Sensor TYPE_ACCELEROMETER

List of usage examples for android.hardware Sensor TYPE_ACCELEROMETER

Introduction

In this page you can find the example usage for android.hardware Sensor TYPE_ACCELEROMETER.

Prototype

int TYPE_ACCELEROMETER

To view the source code for android.hardware Sensor TYPE_ACCELEROMETER.

Click Source Link

Document

A constant describing an accelerometer sensor type.

Usage

From source file:org.godotengine.godot.Godot.java

private void initializeGodot() {

    if (expansion_pack_path != null) {

        String[] new_cmdline;
        int cll = 0;
        if (command_line != null) {
            new_cmdline = new String[command_line.length + 2];
            cll = command_line.length;//from  w  ww. j  a v a  2 s .  co  m
            for (int i = 0; i < command_line.length; i++) {
                new_cmdline[i] = command_line[i];
            }
        } else {
            new_cmdline = new String[2];
        }

        new_cmdline[cll] = "--main-pack";
        new_cmdline[cll + 1] = expansion_pack_path;
        command_line = new_cmdline;
    }

    io = new GodotIO(this);
    io.unique_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
    GodotLib.io = io;
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
    mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
    mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME);
    mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
    mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME);

    GodotLib.initialize(this, getAssets(), use_apk_expansion);

    result_callback = null;

    mPaymentsManager = PaymentsManager.createManager(this).initService();

    godot_initialized = true;
}

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

/**
 * {@inheritDoc}//  w w  w  .  jav a 2  s.  co m
 */
@Override
public void onSensorChanged(final SensorEvent event) {
    final float[] values = event.values;

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        // If there's no accelerometer values yet, create the values
        // array.
        if (accelerometerValues == null) {
            accelerometerValues = new float[3];
            accelerometerValues[0] = values[0];
            accelerometerValues[1] = values[1];
            accelerometerValues[2] = values[2];
        } else {
            smoothValues(accelerometerValues, values);
        }

        break;
    case Sensor.TYPE_MAGNETIC_FIELD:
        // If there's no magnetometer values yet, create the values
        // array.
        if (magnetometerValues == null) {
            magnetometerValues = new float[3];
            magnetometerValues[0] = values[0];
            magnetometerValues[1] = values[1];
            magnetometerValues[2] = values[2];
        } else {
            smoothValues(magnetometerValues, values);
        }

        break;
    default:
        return;
    }

    // Make sure the needle is pointing the correct direction.
    updateDirectionNeedle();
}

From source file:com.thalmic.android.sample.helloworld.HelloWorldActivity.java

public final void onSensorChanged(SensorEvent event) {
    // get reading from the sensor
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        System.arraycopy(event.values, 0, accVector, 0, 3);
        break;/*from   ww w .j  av  a2 s  .c  om*/
    case Sensor.TYPE_MAGNETIC_FIELD:
        System.arraycopy(event.values, 0, magVector, 0, 3);
        break;
    default:
        return;
    }
    rVector[0] = event.values[0];
    rVector[1] = event.values[1];
    rVector[2] = event.values[2];
    calculateAngles(result, rVector, accVector, magVector);
    result[0] = Math.round(filterYaw.lowPass(result[0]));
    deltaOculus = ((Math.abs(oldResult[0] - result[0]) > 1) || (Math.abs(oldResult[1] - result[1]) > 1)
            || (Math.abs(oldResult[2] - result[2]) > 1));
    if (deltaOculus) {
        TextView textView = (TextView) findViewById(R.id.accData);
        textView.setText("Euler Angles are \nyaw: " + result[0] + " \n" + "roll: " + result[1] + " \n"
                + "pitch: " + result[2] + " \n");
        oldResult[0] = result[0];
        oldResult[1] = result[1];
        oldResult[2] = result[2];
        if (headTrackingMode) {

            if (client != null) {
                try {

                    //client.send("servo01", "moveTo",(roll+90.0));
                    client.send("oculus", "computeAnglesAndroid", result[0], result[1], result[2]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.mummyding.app.leisure.ui.MainActivity.java

@Override
public void onSensorChanged(SensorEvent event) {

    if (Settings.isShakeMode == false) {
        return;//from   ww w  . j  a v  a 2s .  c  o  m
    }

    float value[] = event.values;
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        if ((Math.abs(value[0]) + Math.abs(value[1]) + Math.abs(value[2])) > CONSTANT.shakeValue) {
            isShake = true;
            onBackPressed();
        }
    }

}

From source file:com.google.android.apps.santatracker.games.gumball.TiltGameFragment.java

@Override
public void onResume() {
    super.onResume();

    // Resume the game play if the game was not paused
    if (!wasPaused) {
        mRotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
        SensorManager sensorManager = (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (sensor != null) {
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
        }/*from  ww  w .  ja va  2 s.c  om*/
        mCountDownTimer = new GameCountdown(mFramesPerSecond, mTimeLeftInMillis);
        mCountDownTimer.start();
        mGameView.setGameCountDown(mCountDownTimer);
    }

    // Start the game loop if it is not initialised yet
    if (mGameThread == null) {
        mGameThread = new Runnable() {
            public void run() {
                synchronized (mWorld) {
                    if (!wasPaused) {
                        if (mCurrentLevelNum == 0) {
                            mCurrentLevelNum++;
                            loadLevel(mCurrentLevelNum);
                        }
                        mWorld.update();
                        mGameView.invalidate();
                    }
                }
                getActivity().getWindow().getDecorView().postDelayed(mGameThread, 10);
            }
        };
    }
    getActivity().getWindow().getDecorView().postDelayed(mGameThread, 1000);

    loadBackgroundMusic();
    updateSignInButtonVisibility();
}

From source file:org.deviceconnect.android.deviceplugin.host.HostDeviceService.java

/**
 * ????????./*from   w ww  .j av  a  2 s  .  co  m*/
 * 
 * @param sensorEvent .
 */
public void onSensorChanged(final SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        mAccellX = sensorEvent.values[0];
        mAccellY = sensorEvent.values[1];
        mAccellZ = sensorEvent.values[2];

        Bundle orientation = new Bundle();
        Bundle a1 = new Bundle();
        a1.putDouble(DeviceOrientationProfile.PARAM_X, 0.0);
        a1.putDouble(DeviceOrientationProfile.PARAM_Y, 0.0);
        a1.putDouble(DeviceOrientationProfile.PARAM_Z, 0.0);
        Bundle a2 = new Bundle();
        a2.putDouble(DeviceOrientationProfile.PARAM_X, mAccellX);
        a2.putDouble(DeviceOrientationProfile.PARAM_Y, mAccellY);
        a2.putDouble(DeviceOrientationProfile.PARAM_Z, mAccellZ);
        Bundle r = new Bundle();
        r.putDouble(DeviceOrientationProfile.PARAM_ALPHA, mGyroX);
        r.putDouble(DeviceOrientationProfile.PARAM_BETA, mGyroY);
        r.putDouble(DeviceOrientationProfile.PARAM_GAMMA, mGyroZ);
        orientation.putBundle(DeviceOrientationProfile.PARAM_ACCELERATION, a1);
        orientation.putBundle(DeviceOrientationProfile.PARAM_ACCELERATION_INCLUDING_GRAVITY, a2);
        orientation.putBundle(DeviceOrientationProfile.PARAM_ROTATION_RATE, r);
        orientation.putLong(DeviceOrientationProfile.PARAM_INTERVAL, 0);
        DeviceOrientationProfile.setInterval(orientation, INTERVAL_TIME);

        List<Event> events = EventManager.INSTANCE.getEventList(mDeviceId,
                DeviceOrientationProfile.PROFILE_NAME, null,
                DeviceOrientationProfile.ATTRIBUTE_ON_DEVICE_ORIENTATION);

        for (int i = 0; i < events.size(); i++) {
            Event event = events.get(i);
            Intent intent = EventManager.createEventMessage(event);

            intent.putExtra(DeviceOrientationProfile.PARAM_ORIENTATION, orientation);
            getContext().sendBroadcast(intent);
        }

    } else if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        mGyroX = sensorEvent.values[0];
        mGyroY = sensorEvent.values[1];
        mGyroZ = sensorEvent.values[2];
    }
}

From source file:ca.nehil.rter.streamingapp2.StreamingActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Orientation listenever implementation
    myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
        @Override/*from  w  ww.j a va 2s. c o m*/
        public void onOrientationChanged(int orientation) {
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            if (rotation == Surface.ROTATION_270) {
                flipVideo = true;
            } else {
                flipVideo = false;
            }
        }
    };
    myOrientationEventListener.enable();

    // stopService(new Intent(StreamingActivity.this,
    // BackgroundService.class));

    Log.e(TAG, "onCreate");

    AndroidId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
    frameInfo = new FrameInfo();
    // openGL overlay
    overlay = new OverlayController(this);
    // orientation
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMag = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

    //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.activity_streaming);

    // Find the total number of cameras available
    numberOfCameras = Camera.getNumberOfCameras();

    cookies = getSharedPreferences("RterUserCreds", MODE_PRIVATE);
    prefEditor = cookies.edit();
    setUsername = cookies.getString("Username", "not-set");
    setRterCredentials = cookies.getString("RterCreds", "not-set");
    if (setRterCredentials.equalsIgnoreCase("not-set") || setRterCredentials == null) {
        Log.e("PREFS", "Login Not successful, please restart");
    }
    Log.d("PREFS", "Prefs ==> rter_Creds:" + setRterCredentials);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the location provider -> use
    // default

    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.e(TAG, "GPS not available");
    }
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, true);
    Log.d(TAG, "Requesting location");
    locationManager.requestLocationUpdates(provider, 0, 1, this);
    // register the overlay control for location updates as well, so we get
    // the geomagnetic field
    locationManager.requestLocationUpdates(provider, 0, 1000, overlay);
    if (provider != null) {
        Location location = locationManager.getLastKnownLocation(provider);
        // Initialize the location fields
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected. and location " + location);
            onLocationChanged(location);
        } else {
            Toast toast = Toast.makeText(this, "Location not available", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
            lati = (float) (45.505958f);
            longi = (float) (-73.576254f);
            Log.d(TAG, "Location not available");
        }
    }

    // power manager
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, CLASS_LABEL);
    mWakeLock.acquire();

    // test, set desired orienation to north
    overlay.letFreeRoam(false);
    overlay.setDesiredOrientation(0.0f);
    // CharSequence text = "Tap to start..";
    // int duration = Toast.LENGTH_SHORT;
    //
    // Toast toast = Toast.makeText(this, text, duration);
    // toast.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
    // toast.show();

}

From source file:com.waz.zclient.pages.main.drawing.DrawingFragment.java

@Override
public void onStart() {
    super.onStart();
    colorLayout.setOnWidthChangedListener(this);
    shakeEventListener = new ShakeEventListener();
    shakeEventListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
        @Override// w  w w .j  a v a2 s . c om
        public void onShake() {
            if (includeBackgroundImage) {
                drawingCanvasView.removeBackgroundBitmap();
                includeBackgroundImage = false;
            } else {
                drawingCanvasView.drawBackgroundBitmap();
                includeBackgroundImage = true;
            }
        }
    });
    sensorManager.registerListener(shakeEventListener,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    getControllerFactory().getGlobalLayoutController().addKeyboardVisibilityObserver(this);
    getControllerFactory().getAccentColorController().addAccentColorObserver(this);
    getControllerFactory().getRequestPermissionsController().addObserver(this);
}

From source file:io.github.msc42.masterthemaze.GameActivity.java

@Override
protected void onResume() {
    super.onResume();

    if (mGameThread != null) {
        mGameThread.restart();/*w  w w.  ja  va2  s .co m*/
    }

    if (mMotion) {
        if (mSpeed > 0) {
            mAddCurrentDirectionToQueueThread = new AddCurrentDirectionToQueueThread(mMotionQueue,
                    mCurrentMoveDirection, mSpeed);
            mAddCurrentDirectionToQueueThread.start();
        }

        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mAccelerometerEventListener = new AccelerometerEventListener(mRotation, mSensitivity,
                mCurrentMoveDirection, mMotionQueue);

        sensorManager.registerListener(mAccelerometerEventListener, accelerometer,
                SensorManager.SENSOR_DELAY_GAME, mSensorHandler);
    }

    if (mMotion) {
        mMotionDescriptionTextView.setText(R.string.connecting);
    } else {
        mTouchDescriptionTextView.setText(R.string.connecting);
        mUpButton.setVisibility(View.INVISIBLE);
        mRightButton.setVisibility(View.INVISIBLE);
        mDownButton.setVisibility(View.INVISIBLE);
        mLeftButton.setVisibility(View.INVISIBLE);
    }
}

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void unregisterListeners() {
    ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(qiblaManager);

    ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(qiblaManager);
    SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.unregisterListener(qiblaManager, gsensor);
    mSensorManager.unregisterListener(qiblaManager, msensor);
    cancelSchedule();//from  w  w  w  .  j  a v  a  2s . c o  m

}