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 "DeviceOrientationClientImpl.h" 28 29 #include "WebViewCore.h" 30 #include <DeviceOrientationController.h> 31 #include <Frame.h> 32 #include <JNIHelp.h> 33 34 namespace android { 35 36 using JSC::Bindings::getJNIEnv; 37 38 enum javaDeviceOrientationServiceClassMethods { 39 DeviceOrientationServiceMethodStart = 0, 40 DeviceOrientationServiceMethodStop, 41 DeviceOrientationServiceMethodSuspend, 42 DeviceOrientationServiceMethodResume, 43 DeviceOrientationServiceMethodCount 44 }; 45 static jmethodID javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodCount]; 46 47 DeviceOrientationClientImpl::DeviceOrientationClientImpl(WebViewCore* webViewCore) 48 : m_webViewCore(webViewCore) 49 , m_javaDeviceOrientationServiceObject(0) 50 { 51 ASSERT(m_webViewCore); 52 } 53 54 DeviceOrientationClientImpl::~DeviceOrientationClientImpl() 55 { 56 releaseJavaInstance(); 57 } 58 59 jobject DeviceOrientationClientImpl::getJavaInstance() 60 { 61 // Lazily get the Java object. We can't do this until the WebViewCore is all 62 // set up. 63 if (m_javaDeviceOrientationServiceObject) 64 return m_javaDeviceOrientationServiceObject; 65 66 JNIEnv* env = getJNIEnv(); 67 68 ASSERT(m_webViewCore); 69 jobject object = m_webViewCore->getDeviceOrientationService(); 70 if (!object) 71 return 0; 72 73 // Get the Java DeviceOrientationService class. 74 jclass javaDeviceOrientationServiceClass = env->GetObjectClass(object); 75 ASSERT(javaDeviceOrientationServiceClass); 76 77 // Set up the methods we wish to call on the Java DeviceOrientationService 78 // class. 79 javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodStart] = 80 env->GetMethodID(javaDeviceOrientationServiceClass, "start", "()V"); 81 javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodStop] = 82 env->GetMethodID(javaDeviceOrientationServiceClass, "stop", "()V"); 83 javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodSuspend] = 84 env->GetMethodID(javaDeviceOrientationServiceClass, "suspend", "()V"); 85 javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodResume] = 86 env->GetMethodID(javaDeviceOrientationServiceClass, "resume", "()V"); 87 env->DeleteLocalRef(javaDeviceOrientationServiceClass); 88 89 m_javaDeviceOrientationServiceObject = getJNIEnv()->NewGlobalRef(object); 90 getJNIEnv()->DeleteLocalRef(object); 91 92 ASSERT(m_javaDeviceOrientationServiceObject); 93 return m_javaDeviceOrientationServiceObject; 94 } 95 96 void DeviceOrientationClientImpl::releaseJavaInstance() 97 { 98 if (m_javaDeviceOrientationServiceObject) 99 getJNIEnv()->DeleteGlobalRef(m_javaDeviceOrientationServiceObject); 100 } 101 102 void DeviceOrientationClientImpl::startUpdating() 103 { 104 jobject javaInstance = getJavaInstance(); 105 if (!javaInstance) 106 return; 107 getJNIEnv()->CallVoidMethod(javaInstance, javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodStart]); 108 } 109 110 void DeviceOrientationClientImpl::stopUpdating() 111 { 112 jobject javaInstance = getJavaInstance(); 113 if (!javaInstance) 114 return; 115 getJNIEnv()->CallVoidMethod(javaInstance, javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodStop]); 116 } 117 118 void DeviceOrientationClientImpl::onOrientationChange(PassRefPtr<DeviceOrientation> orientation) 119 { 120 m_lastOrientation = orientation; 121 m_controller->didChangeDeviceOrientation(m_lastOrientation.get()); 122 } 123 124 void DeviceOrientationClientImpl::suspend() 125 { 126 jobject javaInstance = getJavaInstance(); 127 if (!javaInstance) 128 return; 129 getJNIEnv()->CallVoidMethod(javaInstance, javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodSuspend]); 130 } 131 132 void DeviceOrientationClientImpl::resume() 133 { 134 jobject javaInstance = getJavaInstance(); 135 if (!javaInstance) 136 return; 137 getJNIEnv()->CallVoidMethod(javaInstance, javaDeviceOrientationServiceClassMethodIDs[DeviceOrientationServiceMethodResume]); 138 } 139 140 } // namespace android 141