1/* 2 * Copyright 2010, The Android Open Source Project 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "DeviceMotionClientImpl.h" 28 29#include "WebViewCore.h" 30#include <DeviceMotionController.h> 31#include <Frame.h> 32#include <JNIHelp.h> 33 34namespace android { 35 36using JSC::Bindings::getJNIEnv; 37 38enum javaServiceClassMethods { 39 ServiceMethodStart = 0, 40 ServiceMethodStop, 41 ServiceMethodSuspend, 42 ServiceMethodResume, 43 ServiceMethodCount 44}; 45static jmethodID javaServiceClassMethodIDs[ServiceMethodCount]; 46 47DeviceMotionClientImpl::DeviceMotionClientImpl(WebViewCore* webViewCore) 48 : m_webViewCore(webViewCore) 49 , m_javaServiceObject(0) 50{ 51 ASSERT(m_webViewCore); 52} 53 54DeviceMotionClientImpl::~DeviceMotionClientImpl() 55{ 56 releaseJavaInstance(); 57} 58 59jobject DeviceMotionClientImpl::getJavaInstance() 60{ 61 // Lazily get the Java object. We can't do this until the WebViewCore is all 62 // set up. 63 if (m_javaServiceObject) 64 return m_javaServiceObject; 65 66 JNIEnv* env = getJNIEnv(); 67 68 ASSERT(m_webViewCore); 69 jobject object = m_webViewCore->getDeviceMotionService(); 70 if (!object) 71 return 0; 72 73 // Get the Java DeviceMotionService class. 74 jclass javaServiceClass = env->GetObjectClass(object); 75 ASSERT(javaServiceClass); 76 77 // Set up the methods we wish to call on the Java DeviceMotionService 78 // class. 79 javaServiceClassMethodIDs[ServiceMethodStart] = 80 env->GetMethodID(javaServiceClass, "start", "()V"); 81 javaServiceClassMethodIDs[ServiceMethodStop] = 82 env->GetMethodID(javaServiceClass, "stop", "()V"); 83 javaServiceClassMethodIDs[ServiceMethodSuspend] = 84 env->GetMethodID(javaServiceClass, "suspend", "()V"); 85 javaServiceClassMethodIDs[ServiceMethodResume] = 86 env->GetMethodID(javaServiceClass, "resume", "()V"); 87 env->DeleteLocalRef(javaServiceClass); 88 89 m_javaServiceObject = getJNIEnv()->NewGlobalRef(object); 90 getJNIEnv()->DeleteLocalRef(object); 91 92 ASSERT(m_javaServiceObject); 93 return m_javaServiceObject; 94} 95 96void DeviceMotionClientImpl::releaseJavaInstance() 97{ 98 if (m_javaServiceObject) 99 getJNIEnv()->DeleteGlobalRef(m_javaServiceObject); 100} 101 102void DeviceMotionClientImpl::startUpdating() 103{ 104 jobject javaInstance = getJavaInstance(); 105 if (!javaInstance) 106 return; 107 getJNIEnv()->CallVoidMethod(javaInstance, javaServiceClassMethodIDs[ServiceMethodStart]); 108} 109 110void DeviceMotionClientImpl::stopUpdating() 111{ 112 jobject javaInstance = getJavaInstance(); 113 if (!javaInstance) 114 return; 115 getJNIEnv()->CallVoidMethod(javaInstance, javaServiceClassMethodIDs[ServiceMethodStop]); 116} 117 118void DeviceMotionClientImpl::onMotionChange(PassRefPtr<DeviceMotionData> motion) 119{ 120 m_lastMotion = motion; 121 m_controller->didChangeDeviceMotion(m_lastMotion.get()); 122} 123 124void DeviceMotionClientImpl::suspend() 125{ 126 jobject javaInstance = getJavaInstance(); 127 if (!javaInstance) 128 return; 129 getJNIEnv()->CallVoidMethod(javaInstance, javaServiceClassMethodIDs[ServiceMethodSuspend]); 130} 131 132void DeviceMotionClientImpl::resume() 133{ 134 jobject javaInstance = getJavaInstance(); 135 if (!javaInstance) 136 return; 137 getJNIEnv()->CallVoidMethod(javaInstance, javaServiceClassMethodIDs[ServiceMethodResume]); 138} 139 140} // namespace android 141