1/*
2 * Copyright (C) 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#include "config.h"
20#include "DeviceMotionClientBlackBerry.h"
21
22#include "DeviceMotionController.h"
23#include "DeviceMotionData.h"
24#include "WebPage_p.h"
25#include <BlackBerryPlatformDeviceMotionTracker.h>
26
27using namespace WebCore;
28
29DeviceMotionClientBlackBerry::DeviceMotionClientBlackBerry(BlackBerry::WebKit::WebPagePrivate* webPagePrivate)
30    : m_webPagePrivate(webPagePrivate)
31    , m_tracker(0)
32    , m_controller(0)
33    , m_lastEventTime(0)
34{
35}
36
37DeviceMotionClientBlackBerry::~DeviceMotionClientBlackBerry()
38{
39    if (m_tracker)
40        m_tracker->destroy();
41}
42
43void DeviceMotionClientBlackBerry::setController(DeviceMotionController* controller)
44{
45    m_controller = controller;
46}
47
48void DeviceMotionClientBlackBerry::deviceMotionControllerDestroyed()
49{
50    delete this;
51}
52
53void DeviceMotionClientBlackBerry::startUpdating()
54{
55    if (m_tracker)
56        m_tracker->resume();
57    else
58        m_tracker = BlackBerry::Platform::DeviceMotionTracker::create(this);
59}
60
61void DeviceMotionClientBlackBerry::stopUpdating()
62{
63    if (m_tracker)
64        m_tracker->suspend();
65}
66
67DeviceMotionData* DeviceMotionClientBlackBerry::lastMotion() const
68{
69    return m_currentMotion.get();
70}
71
72void DeviceMotionClientBlackBerry::onMotion(const BlackBerry::Platform::DeviceMotionEvent* event)
73{
74    // All boolean parameters in the following create() calls indicate that if the following parameter is validly provided.
75
76    RefPtr<DeviceMotionData::Acceleration> acceleration = DeviceMotionData::Acceleration::create(
77        true, event->acceleration.x, true, event->acceleration.y, true, event->acceleration.z);
78
79    RefPtr<DeviceMotionData::Acceleration> accelerationIncludingGravity = DeviceMotionData::Acceleration::create(
80        true, event->accelerationIncludingGravity.x, true, event->accelerationIncludingGravity.y, true, event->accelerationIncludingGravity.z);
81
82    RefPtr<DeviceMotionData::RotationRate> rotationRate = DeviceMotionData::RotationRate::create(
83        true, event->rotationRate.alpha, true, event->rotationRate.beta, true, event->rotationRate.gamma);
84
85    double now = WTF::currentTimeMS();
86    m_currentMotion = DeviceMotionData::create(acceleration, accelerationIncludingGravity, rotationRate, m_lastEventTime, m_lastEventTime - now);
87    m_lastEventTime = now;
88
89    if (!m_controller)
90        return;
91
92    m_controller->didChangeDeviceMotion(lastMotion());
93}
94