1559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block/*
2559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * Copyright 2010, The Android Open Source Project
3559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *
4559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * Redistribution and use in source and binary forms, with or without
5559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * modification, are permitted provided that the following conditions
6559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * are met:
7559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *  * Redistributions of source code must retain the above copyright
8559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *    notice, this list of conditions and the following disclaimer.
9559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *  * Redistributions in binary form must reproduce the above copyright
10559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *    notice, this list of conditions and the following disclaimer in the
11559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *    documentation and/or other materials provided with the distribution.
12559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block *
13559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block */
25559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
26559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block#include "config.h"
27ce9059cc142923289338127a469177d2eb57579fSteve Block#include "DeviceMotionAndOrientationManager.h"
28559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
29d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block#include "DeviceMotionClientImpl.h"
30a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block#include "DeviceOrientationClientImpl.h"
31a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block#include "DeviceOrientationController.h"
32559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block#include "WebViewCore.h"
33a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block#include "Frame.h"
34a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block#include "Page.h"
35a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block
36559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block#include <DeviceOrientationClientMock.h>
37559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block#include <JNIHelp.h>
38559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
39559136b02c843ca1b495c43291f2e38b991a7e5eSteve Blockusing namespace WebCore;
40559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
41559136b02c843ca1b495c43291f2e38b991a7e5eSteve Blocknamespace android {
42559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
43ce9059cc142923289338127a469177d2eb57579fSteve BlockDeviceMotionAndOrientationManager::DeviceMotionAndOrientationManager(WebViewCore* webViewCore)
44559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block    : m_useMock(false)
45a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block    , m_webViewCore(webViewCore)
46559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
47559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
48559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
49f6e5bbe8804f63dfb72c5dce1e225a13b85177a7Steve Blockvoid DeviceMotionAndOrientationManager::setUseMock()
50559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
51559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block    m_useMock = true;
52559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
53559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
54ce9059cc142923289338127a469177d2eb57579fSteve Blockvoid DeviceMotionAndOrientationManager::setMockMotion(PassRefPtr<DeviceMotionData> motion)
5543cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block{
56fcb462de639304deaa2da761823fbfeef88b36d4Steve Block    // TODO: There is not yet a DeviceMotion mock.
5743cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block}
5843cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block
59ce9059cc142923289338127a469177d2eb57579fSteve Blockvoid DeviceMotionAndOrientationManager::onMotionChange(PassRefPtr<DeviceMotionData> motion)
6043cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block{
6143cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    ASSERT(!m_useMock);
62d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    static_cast<DeviceMotionClientImpl*>(m_motionClient.get())->onMotionChange(motion);
6343cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block}
6443cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block
65ce9059cc142923289338127a469177d2eb57579fSteve Blockvoid DeviceMotionAndOrientationManager::setMockOrientation(PassRefPtr<DeviceOrientation> orientation)
66559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
6743cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    if (m_useMock)
6843cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block        static_cast<DeviceOrientationClientMock*>(orientationClient())->setOrientation(orientation);
6943cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block}
70559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
71ce9059cc142923289338127a469177d2eb57579fSteve Blockvoid DeviceMotionAndOrientationManager::onOrientationChange(PassRefPtr<DeviceOrientation> orientation)
72a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block{
73a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block    ASSERT(!m_useMock);
7443cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    static_cast<DeviceOrientationClientImpl*>(m_orientationClient.get())->onOrientationChange(orientation);
7543cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block}
7643cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block
77ce9059cc142923289338127a469177d2eb57579fSteve Blockvoid DeviceMotionAndOrientationManager::maybeSuspendClients()
7843cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block{
7943cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    if (!m_useMock) {
8043cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block        if (m_motionClient)
81d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block            static_cast<DeviceMotionClientImpl*>(m_motionClient.get())->suspend();
8243cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block        if (m_orientationClient)
8343cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block            static_cast<DeviceOrientationClientImpl*>(m_orientationClient.get())->suspend();
8443cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    }
85a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block}
86a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block
87ce9059cc142923289338127a469177d2eb57579fSteve Blockvoid DeviceMotionAndOrientationManager::maybeResumeClients()
88a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block{
8943cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    if (!m_useMock) {
9043cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block        if (m_motionClient)
91d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block            static_cast<DeviceMotionClientImpl*>(m_motionClient.get())->resume();
9243cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block        if (m_orientationClient)
9343cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block            static_cast<DeviceOrientationClientImpl*>(m_orientationClient.get())->resume();
9443cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    }
95a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block}
96a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block
97ce9059cc142923289338127a469177d2eb57579fSteve BlockDeviceMotionClient* DeviceMotionAndOrientationManager::motionClient()
98a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block{
99d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    // TODO: There is not yet a DeviceMotion mock.
10043cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    if (!m_motionClient)
101d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block        m_motionClient.set(m_useMock ? 0
102d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block                : static_cast<DeviceMotionClient*>(new DeviceMotionClientImpl(m_webViewCore)));
10343cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    ASSERT(m_motionClient);
10443cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    return m_motionClient.get();
105a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block}
106a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block
107ce9059cc142923289338127a469177d2eb57579fSteve BlockDeviceOrientationClient* DeviceMotionAndOrientationManager::orientationClient()
108559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
10943cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    if (!m_orientationClient)
11043cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block        m_orientationClient.set(m_useMock ? new DeviceOrientationClientMock
111a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block                : static_cast<DeviceOrientationClient*>(new DeviceOrientationClientImpl(m_webViewCore)));
11243cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    ASSERT(m_orientationClient);
11343cbb6a517664df15ba6cfa5ed9c612ffe92f646Steve Block    return m_orientationClient.get();
114559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
115559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
116ce9059cc142923289338127a469177d2eb57579fSteve Block// JNI for android.webkit.DeviceMotionAndOrientationManager
117ce9059cc142923289338127a469177d2eb57579fSteve Blockstatic const char* javaDeviceMotionAndOrientationManagerClass = "android/webkit/DeviceMotionAndOrientationManager";
118559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
119559136b02c843ca1b495c43291f2e38b991a7e5eSteve Blockstatic WebViewCore* getWebViewCore(JNIEnv* env, jobject webViewCoreObject)
120559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
121559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block    jclass webViewCoreClass = env->FindClass("android/webkit/WebViewCore");
122559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block    jfieldID nativeClassField = env->GetFieldID(webViewCoreClass, "mNativeClass", "I");
123bf3f67c4fcfd68df256e5c001c67969997a63e28Steve Block    env->DeleteLocalRef(webViewCoreClass);
124559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block    return reinterpret_cast<WebViewCore*>(env->GetIntField(webViewCoreObject, nativeClassField));
125559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
126559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
127f6e5bbe8804f63dfb72c5dce1e225a13b85177a7Steve Blockstatic void setUseMock(JNIEnv* env, jobject, jobject webViewCoreObject)
128559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
129f6e5bbe8804f63dfb72c5dce1e225a13b85177a7Steve Block    getWebViewCore(env, webViewCoreObject)->deviceMotionAndOrientationManager()->setUseMock();
130559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
131559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
132d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Blockstatic void onMotionChange(JNIEnv* env, jobject, jobject webViewCoreObject, bool canProvideX, double x, bool canProvideY, double y, bool canProvideZ, double z, double interval)
133d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block{
134d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    // We only provide accelerationIncludingGravity.
135d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    RefPtr<DeviceMotionData::Acceleration> accelerationIncludingGravity = DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, canProvideZ, z);
136d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    bool canProvideInterval = canProvideX || canProvideY || canProvideZ;
137d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    RefPtr<DeviceMotionData> motion = DeviceMotionData::create(0, accelerationIncludingGravity.release(), 0, canProvideInterval, interval);
138ce9059cc142923289338127a469177d2eb57579fSteve Block    getWebViewCore(env, webViewCoreObject)->deviceMotionAndOrientationManager()->onMotionChange(motion.release());
139d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block}
140d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block
141559136b02c843ca1b495c43291f2e38b991a7e5eSteve Blockstatic void setMockOrientation(JNIEnv* env, jobject, jobject webViewCoreObject, bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
142559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
143559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block    RefPtr<DeviceOrientation> orientation = DeviceOrientation::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
144ce9059cc142923289338127a469177d2eb57579fSteve Block    getWebViewCore(env, webViewCoreObject)->deviceMotionAndOrientationManager()->setMockOrientation(orientation.release());
145559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
146559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
147a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Blockstatic void onOrientationChange(JNIEnv* env, jobject, jobject webViewCoreObject, bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
148a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block{
149a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block    RefPtr<DeviceOrientation> orientation = DeviceOrientation::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
150ce9059cc142923289338127a469177d2eb57579fSteve Block    getWebViewCore(env, webViewCoreObject)->deviceMotionAndOrientationManager()->onOrientationChange(orientation.release());
151a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block}
152a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block
153ce9059cc142923289338127a469177d2eb57579fSteve Blockstatic JNINativeMethod gDeviceMotionAndOrientationManagerMethods[] = {
154f6e5bbe8804f63dfb72c5dce1e225a13b85177a7Steve Block    { "nativeSetUseMock", "(Landroid/webkit/WebViewCore;)V", (void*) setUseMock },
155d9b2f2e1e22ec3dfc73795d9e5476c7ab7ddb43dSteve Block    { "nativeOnMotionChange", "(Landroid/webkit/WebViewCore;ZDZDZDD)V", (void*) onMotionChange },
156a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block    { "nativeSetMockOrientation", "(Landroid/webkit/WebViewCore;ZDZDZD)V", (void*) setMockOrientation },
157a49e216a2b9e2243bdeb847a5ebb66a0a291b902Steve Block    { "nativeOnOrientationChange", "(Landroid/webkit/WebViewCore;ZDZDZD)V", (void*) onOrientationChange }
158559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block};
159559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
160b92850e0391d674b9c4826b66105aaa3dd3eef96Steve Blockint registerDeviceMotionAndOrientationManager(JNIEnv* env)
161559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block{
162bf3f67c4fcfd68df256e5c001c67969997a63e28Steve Block#ifndef NDEBUG
163bf3f67c4fcfd68df256e5c001c67969997a63e28Steve Block    jclass deviceMotionAndOrientationManager = env->FindClass(javaDeviceMotionAndOrientationManagerClass);
1646dd76b804786ec760bb04b137a6bf017064226dcSteve Block    ALOG_ASSERT(deviceMotionAndOrientationManager, "Unable to find class");
165bf3f67c4fcfd68df256e5c001c67969997a63e28Steve Block    env->DeleteLocalRef(deviceMotionAndOrientationManager);
166bf3f67c4fcfd68df256e5c001c67969997a63e28Steve Block#endif
167bf3f67c4fcfd68df256e5c001c67969997a63e28Steve Block
168ce9059cc142923289338127a469177d2eb57579fSteve Block    return jniRegisterNativeMethods(env, javaDeviceMotionAndOrientationManagerClass, gDeviceMotionAndOrientationManagerMethods, NELEM(gDeviceMotionAndOrientationManagerMethods));
169559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block}
170559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block
171559136b02c843ca1b495c43291f2e38b991a7e5eSteve Block} // namespace android
172