void
OrientationObserver::Notify(const hal::SensorData& aSensorData)
{
  // Sensor will call us on the main thread.
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSensorData.sensor() == hal::SensorType::SENSOR_ORIENTATION);

  InfallibleTArray<float> values = aSensorData.values();
  // Azimuth (values[0]): the device's horizontal orientation
  // (0 degree is north). It's unused for screen rotation.
  float pitch = values[1];
  float roll = values[2];

  PRUint32 rotation;
  if (roll > 45) {
    rotation = nsIScreen::ROTATION_90_DEG;
  } else if (roll < -45) {
    rotation = nsIScreen::ROTATION_270_DEG;
  } else if (pitch < -45) {
    rotation = nsIScreen::ROTATION_0_DEG;
  } else if (pitch > 45) {
    rotation = nsIScreen::ROTATION_180_DEG;
  } else {
    // Don't rotate if neither pitch nor roll exceeds the 45 degree threshold.
    return;
  }

  nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
  if (!screen) {
    return;
  }

  PRUint32 currRotation;
  if (NS_FAILED(screen->GetRotation(&currRotation)) ||
      rotation == currRotation) {
    return;
  }

  ScreenOrientation orientation;
  if (NS_FAILED(ConvertToDomOrientation(rotation, &orientation))) {
    return;
  }

  if ((mAllowedOrientations & orientation) == eScreenOrientation_None) {
    // The orientation from sensor is not allowed.
    return;
  }

  PRTime now = PR_Now();
  MOZ_ASSERT(now > mLastUpdate);
  if (now - mLastUpdate < sMinUpdateInterval) {
    return;
  }
  mLastUpdate = now;

  if (NS_FAILED(screen->SetRotation(rotation))) {
    // Don't notify dom on rotation failure.
    return;
  }
}
void
OrientationObserver::Notify(const hal::SensorData& aSensorData)
{
  // Sensor will call us on the main thread.
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSensorData.sensor() == hal::SensorType::SENSOR_ACCELERATION);

  nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
  if (!screen) {
    return;
  }

  uint32_t currRotation;
  if(NS_FAILED(screen->GetRotation(&currRotation))) {
    return;
  }

  int rotation = mOrientation->OnSensorChanged(aSensorData, static_cast<int>(currRotation));
  if (rotation < 0 || rotation == currRotation) {
    return;
  }

  ScreenOrientation orientation;
  if (NS_FAILED(ConvertToDomOrientation(rotation, &orientation))) {
    return;
  }

  if ((mAllowedOrientations & orientation) == eScreenOrientation_None) {
    // The orientation from sensor is not allowed.
    return;
  }

  if (NS_FAILED(screen->SetRotation(static_cast<uint32_t>(rotation)))) {
    // Don't notify dom on rotation failure.
    return;
  }
}