2010-06-04 00:56:36 +04:00
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2012-05-21 15:12:37 +04:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
2010-06-04 00:56:36 +04:00
2012-02-04 06:35:58 +04:00
# include "mozilla/Util.h"
# include "mozilla/layers/CompositorChild.h"
# include "mozilla/layers/CompositorParent.h"
2010-06-04 00:56:36 +04:00
# include <android/log.h>
2011-06-16 13:03:00 +04:00
# include <dlfcn.h>
2010-06-04 00:56:36 +04:00
2011-11-02 19:35:11 +04:00
# include "mozilla/Hal.h"
2010-08-17 12:07:45 +04:00
# include "nsXULAppAPI.h"
2010-06-04 00:56:36 +04:00
# include <prthread.h>
2010-06-30 06:22:08 +04:00
# include "nsXPCOMStrings.h"
2010-06-04 00:56:36 +04:00
# include "AndroidBridge.h"
2013-02-10 03:50:28 +04:00
# include "AndroidJNIWrapper.h"
2010-06-15 06:17:37 +04:00
# include "nsAppShell.h"
2010-09-30 19:37:36 +04:00
# include "nsOSHelperAppService.h"
2011-04-12 00:41:03 +04:00
# include "nsWindow.h"
2011-06-07 11:38:00 +04:00
# include "mozilla/Preferences.h"
2011-08-04 11:32:57 +04:00
# include "nsThreadUtils.h"
2012-01-30 00:39:30 +04:00
# include "nsIThreadManager.h"
2013-03-01 12:38:47 +04:00
# include "mozilla/dom/mobilemessage/PSms.h"
2012-02-04 10:48:26 +04:00
# include "gfxImageSurface.h"
# include "gfxContext.h"
2013-07-24 17:18:05 +04:00
# include "gfxUtils.h"
2012-02-04 10:48:26 +04:00
# include "nsPresContext.h"
# include "nsIDocShell.h"
# include "nsPIDOMWindow.h"
2012-03-20 17:09:45 +04:00
# include "mozilla/dom/ScreenOrientation.h"
2012-06-02 07:50:57 +04:00
# include "nsIDOMWindowUtils.h"
# include "nsIDOMClientRect.h"
2012-07-20 23:20:51 +04:00
# include "StrongPointer.h"
2012-11-07 01:32:07 +04:00
# include "mozilla/ClearOnShutdown.h"
2013-04-26 21:26:46 +04:00
# include "nsPrintfCString.h"
2010-06-04 00:56:36 +04:00
2011-03-01 07:25:11 +03:00
# ifdef DEBUG
# define ALOG_BRIDGE(args...) ALOG(args)
# else
2013-01-25 23:00:00 +04:00
# define ALOG_BRIDGE(args...) ((void)0)
2011-03-01 07:25:11 +03:00
# endif
2010-06-04 00:56:36 +04:00
using namespace mozilla ;
2013-07-19 06:24:15 +04:00
NS_IMPL_ISUPPORTS0 ( nsFilePickerCallback )
2012-04-05 09:50:06 +04:00
2013-08-12 18:58:36 +04:00
StaticRefPtr < AndroidBridge > AndroidBridge : : sBridge ;
2012-08-22 19:56:38 +04:00
static unsigned sJavaEnvThreadIndex = 0 ;
2012-07-17 18:25:48 +04:00
static void JavaThreadDetachFunc ( void * arg ) ;
2010-06-04 00:56:36 +04:00
2012-07-20 23:20:51 +04:00
// This is a dummy class that can be used in the template for android::sp
class AndroidRefable {
void incStrong ( void * thing ) { }
void decStrong ( void * thing ) { }
} ;
// This isn't in AndroidBridge.h because including StrongPointer.h there is gross
2012-07-30 18:20:58 +04:00
static android : : sp < AndroidRefable > ( * android_SurfaceTexture_getNativeWindow ) ( JNIEnv * env , jobject surfaceTexture ) = nullptr ;
2012-07-20 23:20:51 +04:00
2012-08-01 01:34:06 +04:00
void
2010-06-04 00:56:36 +04:00
AndroidBridge : : ConstructBridge ( JNIEnv * jEnv ,
jclass jGeckoAppShellClass )
{
/* NSS hack -- bionic doesn't handle recursive unloads correctly,
* because library finalizer functions are called with the dynamic
* linker lock still held . This results in a deadlock when trying
* to call dlclose ( ) while we ' re already inside dlclose ( ) .
* Conveniently , NSS has an env var that can prevent it from unloading .
*/
2012-07-02 23:32:34 +04:00
putenv ( " NSS_DISABLE_UNLOAD=1 " ) ;
2010-06-04 00:56:36 +04:00
2012-07-17 18:25:48 +04:00
PR_NewThreadPrivateIndex ( & sJavaEnvThreadIndex , JavaThreadDetachFunc ) ;
2012-08-01 01:34:06 +04:00
AndroidBridge * bridge = new AndroidBridge ( ) ;
if ( ! bridge - > Init ( jEnv , jGeckoAppShellClass ) ) {
delete bridge ;
2010-06-04 00:56:36 +04:00
}
2012-08-01 01:34:06 +04:00
sBridge = bridge ;
2010-06-04 00:56:36 +04:00
}
2011-09-29 10:19:26 +04:00
bool
2010-06-04 00:56:36 +04:00
AndroidBridge : : Init ( JNIEnv * jEnv ,
jclass jGeckoAppShellClass )
{
2011-12-06 10:56:04 +04:00
ALOG_BRIDGE ( " AndroidBridge::Init " ) ;
2010-06-04 00:56:36 +04:00
jEnv - > GetJavaVM ( & mJavaVM ) ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( jEnv ) ;
2012-04-28 00:04:47 +04:00
2012-07-30 18:20:58 +04:00
mJNIEnv = nullptr ;
mThread = nullptr ;
2013-02-28 22:28:24 +04:00
mGLControllerObj = nullptr ;
2011-09-21 20:46:00 +04:00
mOpenedGraphicsLibraries = false ;
2011-06-16 13:03:00 +04:00
mHasNativeBitmapAccess = false ;
2011-09-21 20:46:00 +04:00
mHasNativeWindowAccess = false ;
2012-04-28 00:04:47 +04:00
mHasNativeWindowFallback = false ;
2010-06-04 00:56:36 +04:00
mGeckoAppShellClass = ( jclass ) jEnv - > NewGlobalRef ( jGeckoAppShellClass ) ;
2013-05-29 08:52:40 +04:00
# ifdef MOZ_WEBSMS_BACKEND
2013-01-04 10:26:27 +04:00
jclass jAndroidSmsMessageClass = jEnv - > FindClass ( " android/telephony/SmsMessage " ) ;
mAndroidSmsMessageClass = ( jclass ) jEnv - > NewGlobalRef ( jAndroidSmsMessageClass ) ;
2013-05-29 08:52:40 +04:00
jCalculateLength = ( jmethodID ) jEnv - > GetStaticMethodID ( jAndroidSmsMessageClass , " calculateLength " , " (Ljava/lang/CharSequence;Z)[I " ) ;
# endif
2013-01-04 10:26:27 +04:00
2013-03-20 00:54:49 +04:00
jNotifyIME = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " notifyIME " , " (I)V " ) ;
2013-03-20 00:54:48 +04:00
jNotifyIMEContext = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " notifyIMEContext " , " (ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V " ) ;
2010-08-04 23:47:26 +04:00
jNotifyIMEChange = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " notifyIMEChange " , " (Ljava/lang/String;III)V " ) ;
2013-03-11 02:43:30 +04:00
jAcknowledgeEvent = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " acknowledgeEvent " , " ()V " ) ;
2011-03-02 20:23:02 +03:00
2010-06-05 01:14:43 +04:00
jEnableLocation = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " enableLocation " , " (Z)V " ) ;
2012-04-02 22:52:41 +04:00
jEnableLocationHighAccuracy = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " enableLocationHighAccuracy " , " (Z)V " ) ;
2012-03-14 03:57:51 +04:00
jEnableSensor = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " enableSensor " , " (I)V " ) ;
jDisableSensor = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " disableSensor " , " (I)V " ) ;
2010-06-04 00:56:36 +04:00
jScheduleRestart = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " scheduleRestart " , " ()V " ) ;
jNotifyXreExit = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " onXreExit " , " ()V " ) ;
2010-09-30 19:37:36 +04:00
jGetHandlersForMimeType = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getHandlersForMimeType " , " (Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String; " ) ;
2010-11-24 02:40:46 +03:00
jGetHandlersForURL = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getHandlersForURL " , " (Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String; " ) ;
2010-10-02 05:04:23 +04:00
jOpenUriExternal = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " openUriExternal " , " (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z " ) ;
2010-11-05 20:43:12 +03:00
jGetMimeTypeFromExtensions = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getMimeTypeFromExtensions " , " (Ljava/lang/String;)Ljava/lang/String; " ) ;
2011-03-08 08:45:43 +03:00
jGetExtensionFromMimeType = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getExtensionFromMimeType " , " (Ljava/lang/String;)Ljava/lang/String; " ) ;
2010-06-16 21:20:54 +04:00
jMoveTaskToBack = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " moveTaskToBack " , " ()V " ) ;
2010-06-15 06:17:37 +04:00
jShowAlertNotification = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " showAlertNotification " , " (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V " ) ;
2012-03-18 20:15:56 +04:00
jShowFilePickerForExtensions = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " showFilePickerForExtensions " , " (Ljava/lang/String;)Ljava/lang/String; " ) ;
jShowFilePickerForMimeType = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " showFilePickerForMimeType " , " (Ljava/lang/String;)Ljava/lang/String; " ) ;
2012-04-05 09:50:06 +04:00
jShowFilePickerAsync = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " showFilePickerAsync " , " (Ljava/lang/String;J)V " ) ;
2012-10-05 23:35:26 +04:00
jUnlockProfile = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " unlockProfile " , " ()Z " ) ;
jKillAnyZombies = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " killAnyZombies " , " ()V " ) ;
2010-10-02 01:21:21 +04:00
jAlertsProgressListener_OnProgress = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " alertsProgressListener_OnProgress " , " (Ljava/lang/String;JJLjava/lang/String;)V " ) ;
2013-03-18 17:24:53 +04:00
jCloseNotification = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " closeNotification " , " (Ljava/lang/String;)V " ) ;
2010-10-07 21:28:27 +04:00
jGetDpi = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getDpi " , " ()I " ) ;
2013-07-04 17:53:25 +04:00
jGetScreenDepth = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getScreenDepth " , " ()I " ) ;
2010-11-09 05:11:13 +03:00
jSetFullScreen = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " setFullScreen " , " (Z)V " ) ;
2010-11-10 17:40:00 +03:00
jShowInputMethodPicker = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " showInputMethodPicker " , " ()V " ) ;
2012-04-07 11:09:26 +04:00
jNotifyDefaultPrevented = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " notifyDefaultPrevented " , " (Z)V " ) ;
2010-11-12 23:02:14 +03:00
jHideProgressDialog = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " hideProgressDialog " , " ()V " ) ;
2010-11-22 19:38:02 +03:00
jPerformHapticFeedback = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " performHapticFeedback " , " (Z)V " ) ;
2011-09-30 11:00:48 +04:00
jVibrate1 = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " vibrate " , " (J)V " ) ;
jVibrateA = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " vibrate " , " ([JI)V " ) ;
jCancelVibrate = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " cancelVibrate " , " ()V " ) ;
2010-12-22 01:34:29 +03:00
jSetKeepScreenOn = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " setKeepScreenOn " , " (Z)V " ) ;
2011-01-20 14:06:13 +03:00
jIsNetworkLinkUp = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " isNetworkLinkUp " , " ()Z " ) ;
jIsNetworkLinkKnown = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " isNetworkLinkKnown " , " ()Z " ) ;
2011-02-27 18:50:56 +03:00
jSetSelectedLocale = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " setSelectedLocale " , " (Ljava/lang/String;)V " ) ;
2011-04-08 21:52:13 +04:00
jScanMedia = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " scanMedia " , " (Ljava/lang/String;Ljava/lang/String;)V " ) ;
2011-03-30 22:04:41 +04:00
jGetSystemColors = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getSystemColors " , " ()[I " ) ;
2011-06-14 01:02:13 +04:00
jGetIconForExtension = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getIconForExtension " , " (Ljava/lang/String;I)[B " ) ;
2011-08-09 06:37:57 +04:00
jCreateShortcut = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " createShortcut " , " (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V " ) ;
2011-07-27 05:14:52 +04:00
jGetShowPasswordSetting = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getShowPasswordSetting " , " ()Z " ) ;
2011-09-27 04:25:41 +04:00
jInitCamera = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " initCamera " , " (Ljava/lang/String;III)[I " ) ;
jCloseCamera = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " closeCamera " , " ()V " ) ;
2011-12-15 01:53:38 +04:00
jIsTablet = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " isTablet " , " ()Z " ) ;
2011-11-02 19:35:11 +04:00
jEnableBatteryNotifications = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " enableBatteryNotifications " , " ()V " ) ;
jDisableBatteryNotifications = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " disableBatteryNotifications " , " ()V " ) ;
2011-11-09 12:56:37 +04:00
jGetCurrentBatteryInformation = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getCurrentBatteryInformation " , " ()[D " ) ;
2010-06-26 04:52:37 +04:00
2011-10-24 21:05:18 +04:00
jHandleGeckoMessage = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " handleGeckoMessage " , " (Ljava/lang/String;)Ljava/lang/String; " ) ;
2011-10-25 19:39:32 +04:00
jCheckUriVisited = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " checkUriVisited " , " (Ljava/lang/String;)V " ) ;
jMarkUriVisited = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " markUriVisited " , " (Ljava/lang/String;)V " ) ;
2013-02-18 21:27:30 +04:00
jSetUriTitle = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " setUriTitle " , " (Ljava/lang/String;Ljava/lang/String;)V " ) ;
2010-06-26 04:52:37 +04:00
2012-11-07 01:32:07 +04:00
jSendMessage = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " sendMessage " , " (Ljava/lang/String;Ljava/lang/String;I)V " ) ;
jGetMessage = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getMessage " , " (II)V " ) ;
jDeleteMessage = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " deleteMessage " , " (II)V " ) ;
jCreateMessageList = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " createMessageList " , " (JJ[Ljava/lang/String;IIZI)V " ) ;
jGetNextMessageinList = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getNextMessageInList " , " (II)V " ) ;
2012-01-17 22:43:08 +04:00
jClearMessageList = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " clearMessageList " , " (I)V " ) ;
2011-11-25 13:48:51 +04:00
2012-01-16 17:44:07 +04:00
jGetCurrentNetworkInformation = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getCurrentNetworkInformation " , " ()[D " ) ;
2012-01-16 18:01:07 +04:00
jEnableNetworkNotifications = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " enableNetworkNotifications " , " ()V " ) ;
jDisableNetworkNotifications = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " disableNetworkNotifications " , " ()V " ) ;
2012-01-16 17:44:07 +04:00
2012-03-20 17:09:45 +04:00
jGetScreenOrientation = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getScreenOrientation " , " ()S " ) ;
jEnableScreenOrientationNotifications = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " enableScreenOrientationNotifications " , " ()V " ) ;
jDisableScreenOrientationNotifications = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " disableScreenOrientationNotifications " , " ()V " ) ;
2012-03-30 10:31:12 +04:00
jLockScreenOrientation = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " lockScreenOrientation " , " (I)V " ) ;
jUnlockScreenOrientation = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " unlockScreenOrientation " , " ()V " ) ;
2012-03-20 17:09:45 +04:00
2013-04-23 21:10:29 +04:00
jGeckoJavaSamplerClass = ( jclass ) jEnv - > NewGlobalRef ( jEnv - > FindClass ( " org/mozilla/gecko/GeckoJavaSampler " ) ) ;
jStart = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " start " , " (II)V " ) ;
jStop = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " stop " , " ()V " ) ;
jPause = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " pause " , " ()V " ) ;
jUnpause = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " unpause " , " ()V " ) ;
jGetThreadName = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " getThreadName " , " (I)Ljava/lang/String; " ) ;
jGetFrameName = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " getFrameName " , " (III)Ljava/lang/String; " ) ;
jGetSampleTime = jEnv - > GetStaticMethodID ( jGeckoJavaSamplerClass , " getSampleTime " , " (II)D " ) ;
2012-12-15 19:43:52 +04:00
jThumbnailHelperClass = ( jclass ) jEnv - > NewGlobalRef ( jEnv - > FindClass ( " org/mozilla/gecko/ThumbnailHelper " ) ) ;
2013-01-23 21:18:05 +04:00
jNotifyThumbnail = jEnv - > GetStaticMethodID ( jThumbnailHelperClass , " notifyThumbnail " , " (Ljava/nio/ByteBuffer;IZ)V " ) ;
2012-09-20 01:15:55 +04:00
2012-03-08 04:24:38 +04:00
jStringClass = ( jclass ) jEnv - > NewGlobalRef ( jEnv - > FindClass ( " java/lang/String " ) ) ;
2012-04-28 00:04:47 +04:00
jSurfaceClass = ( jclass ) jEnv - > NewGlobalRef ( jEnv - > FindClass ( " android/view/Surface " ) ) ;
2012-07-20 23:20:51 +04:00
if ( ! GetStaticIntField ( " android/os/Build$VERSION " , " SDK_INT " , & mAPIVersion , jEnv ) )
2012-04-28 00:04:47 +04:00
ALOG_BRIDGE ( " Failed to find API version " ) ;
2013-02-26 18:28:57 +04:00
if ( mAPIVersion < = 8 /* Froyo */ ) {
2012-04-28 00:04:47 +04:00
jSurfacePointerField = jEnv - > GetFieldID ( jSurfaceClass , " mSurface " , " I " ) ;
2013-02-26 18:28:57 +04:00
} else {
2012-04-28 00:04:47 +04:00
jSurfacePointerField = jEnv - > GetFieldID ( jSurfaceClass , " mNativeSurface " , " I " ) ;
2013-02-26 18:28:57 +04:00
// Apparently mNativeSurface doesn't exist in Key Lime Pie, so just clear the
// exception if we have one and move on.
if ( jEnv - > ExceptionCheck ( ) ) {
jEnv - > ExceptionClear ( ) ;
}
}
2012-06-07 06:39:01 +04:00
jNotifyWakeLockChanged = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " notifyWakeLockChanged " , " (Ljava/lang/String;Ljava/lang/String;)V " ) ;
2012-07-05 18:12:10 +04:00
jGetGfxInfoData = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getGfxInfoData " , " ()Ljava/lang/String; " ) ;
2012-11-13 23:25:18 +04:00
jGetProxyForURI = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getProxyForURI " , " (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String; " ) ;
2012-07-20 23:20:51 +04:00
jRegisterSurfaceTextureFrameListener = jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " registerSurfaceTextureFrameListener " , " (Ljava/lang/Object;I)V " ) ;
jUnregisterSurfaceTextureFrameListener = jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " unregisterSurfaceTextureFrameListener " , " (Ljava/lang/Object;)V " ) ;
2012-07-05 18:12:10 +04:00
2012-06-05 09:14:12 +04:00
jPumpMessageLoop = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " pumpMessageLoop " , " ()Z " ) ;
2012-05-03 18:53:31 +04:00
2013-08-13 19:49:21 +04:00
jAddPluginView = jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " addPluginView " , " (Landroid/view/View;FFFFZ)V " ) ;
2012-05-30 20:10:49 +04:00
jRemovePluginView = jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " removePluginView " , " (Landroid/view/View;Z)V " ) ;
2012-04-05 19:28:50 +04:00
jLayerView = ( jclass ) jEnv - > NewGlobalRef ( jEnv - > FindClass ( " org/mozilla/gecko/gfx/LayerView " ) ) ;
2012-01-26 23:23:13 +04:00
2013-02-28 22:28:24 +04:00
jclass glControllerClass = jEnv - > FindClass ( " org/mozilla/gecko/gfx/GLController " ) ;
jProvideEGLSurfaceMethod = jEnv - > GetMethodID ( glControllerClass , " provideEGLSurface " ,
" ()Ljavax/microedition/khronos/egl/EGLSurface; " ) ;
2013-04-26 21:26:46 +04:00
jclass nativePanZoomControllerClass = jEnv - > FindClass ( " org/mozilla/gecko/gfx/NativePanZoomController " ) ;
jRequestContentRepaint = jEnv - > GetMethodID ( nativePanZoomControllerClass , " requestContentRepaint " , " (FFFFF)V " ) ;
2013-04-26 21:26:46 +04:00
jPostDelayedCallback = jEnv - > GetMethodID ( nativePanZoomControllerClass , " postDelayedCallback " , " (J)V " ) ;
2013-04-26 21:26:46 +04:00
2013-02-28 22:28:24 +04:00
jclass eglClass = jEnv - > FindClass ( " com/google/android/gles_jni/EGLSurfaceImpl " ) ;
if ( eglClass ) {
jEGLSurfacePointerField = jEnv - > GetFieldID ( eglClass , " mEGLSurface " , " I " ) ;
} else {
jEGLSurfacePointerField = 0 ;
}
2012-03-08 19:22:42 +04:00
2013-02-10 01:38:08 +04:00
jGetContext = ( jmethodID ) jEnv - > GetStaticMethodID ( jGeckoAppShellClass , " getContext " , " ()Landroid/content/Context; " ) ;
2013-06-08 00:39:37 +04:00
jClipboardClass = ( jclass ) jEnv - > NewGlobalRef ( jEnv - > FindClass ( " org/mozilla/gecko/util/Clipboard " ) ) ;
jClipboardGetText = ( jmethodID ) jEnv - > GetStaticMethodID ( jClipboardClass , " getText " , " ()Ljava/lang/String; " ) ;
jClipboardSetText = ( jmethodID ) jEnv - > GetStaticMethodID ( jClipboardClass , " setText " , " (Ljava/lang/CharSequence;)V " ) ;
2010-06-04 00:56:36 +04:00
InitAndroidJavaWrappers ( jEnv ) ;
// jEnv should NOT be cached here by anything -- the jEnv here
// is not valid for the real gecko main thread, which is set
// at SetMainThread time.
2011-10-17 18:59:28 +04:00
return true ;
2010-06-04 00:56:36 +04:00
}
2011-09-29 10:19:26 +04:00
bool
2010-06-04 00:56:36 +04:00
AndroidBridge : : SetMainThread ( void * thr )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::SetMainThread " ) ;
2010-06-04 00:56:36 +04:00
if ( thr ) {
mThread = thr ;
2012-01-30 00:39:30 +04:00
mJavaVM - > GetEnv ( ( void * * ) & mJNIEnv , JNI_VERSION_1_2 ) ;
return ( bool ) mJNIEnv ;
2010-06-04 00:56:36 +04:00
}
2012-07-30 18:20:58 +04:00
mJNIEnv = nullptr ;
mThread = nullptr ;
2011-10-17 18:59:28 +04:00
return true ;
2010-06-04 00:56:36 +04:00
}
void
2013-03-20 00:54:49 +04:00
AndroidBridge : : NotifyIME ( int aType )
2010-06-04 00:56:36 +04:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::NotifyIME " ) ;
2012-01-30 00:39:30 +04:00
2012-05-04 19:08:45 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
2012-01-30 00:39:30 +04:00
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-07-02 23:32:34 +04:00
env - > CallStaticVoidMethod ( sBridge - > mGeckoAppShellClass ,
2013-03-20 00:54:49 +04:00
sBridge - > jNotifyIME , aType ) ;
2010-08-04 23:47:26 +04:00
}
2012-08-22 19:56:38 +04:00
jstring NewJavaString ( AutoLocalJNIFrame * frame , const PRUnichar * string , uint32_t len ) {
2012-09-05 08:23:02 +04:00
jstring ret = frame - > GetEnv ( ) - > NewString ( string , len ) ;
if ( frame - > CheckForException ( ) )
return NULL ;
return ret ;
}
2012-12-14 02:03:44 +04:00
jstring NewJavaString ( AutoLocalJNIFrame * frame , const nsAString & string ) {
return NewJavaString ( frame , string . BeginReading ( ) , string . Length ( ) ) ;
}
jstring NewJavaString ( AutoLocalJNIFrame * frame , const char * string ) {
return NewJavaString ( frame , NS_ConvertUTF8toUTF16 ( string ) ) ;
}
jstring NewJavaString ( AutoLocalJNIFrame * frame , const nsACString & string ) {
return NewJavaString ( frame , NS_ConvertUTF8toUTF16 ( string ) ) ;
2012-07-02 23:12:50 +04:00
}
2010-11-23 09:48:25 +03:00
void
2013-03-20 00:54:48 +04:00
AndroidBridge : : NotifyIMEContext ( int aState , const nsAString & aTypeHint ,
2012-08-27 06:16:22 +04:00
const nsAString & aModeHint , const nsAString & aActionHint )
2010-11-23 09:48:25 +03:00
{
2013-03-20 00:54:48 +04:00
ALOG_BRIDGE ( " AndroidBridge::NotifyIMEContext " ) ;
2010-11-23 09:48:25 +03:00
if ( ! sBridge )
return ;
2012-05-04 19:08:45 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
2012-01-30 00:39:30 +04:00
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2010-11-23 09:48:25 +03:00
2013-03-20 00:54:48 +04:00
jvalue args [ 4 ] ;
2010-11-23 09:48:25 +03:00
args [ 0 ] . i = aState ;
2012-12-14 02:03:44 +04:00
args [ 1 ] . l = NewJavaString ( & jniFrame , aTypeHint ) ;
args [ 2 ] . l = NewJavaString ( & jniFrame , aModeHint ) ;
args [ 3 ] . l = NewJavaString ( & jniFrame , aActionHint ) ;
2011-06-07 11:38:00 +04:00
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethodA ( sBridge - > mGeckoAppShellClass ,
2013-03-20 00:54:48 +04:00
sBridge - > jNotifyIMEContext , args ) ;
2010-11-23 09:48:25 +03:00
}
2010-08-04 23:47:26 +04:00
void
2012-08-22 19:56:38 +04:00
AndroidBridge : : NotifyIMEChange ( const PRUnichar * aText , uint32_t aTextLen ,
2010-08-04 23:47:26 +04:00
int aStart , int aEnd , int aNewEnd )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::NotifyIMEChange " ) ;
2012-05-04 19:08:47 +04:00
if ( ! sBridge )
2010-08-05 22:11:32 +04:00
return ;
2012-05-04 19:08:45 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
2012-01-30 00:39:30 +04:00
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-01-30 00:39:30 +04:00
2010-08-04 23:47:26 +04:00
jvalue args [ 4 ] ;
2012-07-02 23:12:50 +04:00
args [ 0 ] . l = NewJavaString ( & jniFrame , aText , aTextLen ) ;
2010-08-04 23:47:26 +04:00
args [ 1 ] . i = aStart ;
args [ 2 ] . i = aEnd ;
args [ 3 ] . i = aNewEnd ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethodA ( sBridge - > mGeckoAppShellClass ,
2012-07-02 23:32:34 +04:00
sBridge - > jNotifyIMEChange , args ) ;
2010-06-04 00:56:36 +04:00
}
2011-03-02 20:23:02 +03:00
void
2013-03-11 02:43:30 +04:00
AndroidBridge : : AcknowledgeEvent ( )
2011-03-02 20:23:02 +03:00
{
2013-03-11 02:43:30 +04:00
ALOG_BRIDGE ( " AndroidBridge::AcknowledgeEvent " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2013-03-11 02:43:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jAcknowledgeEvent ) ;
2011-03-02 20:23:02 +03:00
}
2010-06-05 01:14:43 +04:00
void
AndroidBridge : : EnableLocation ( bool aEnable )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::EnableLocation " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-03-14 03:57:51 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jEnableLocation , aEnable ) ;
2010-06-05 01:14:43 +04:00
}
2012-03-21 21:52:35 +04:00
void
AndroidBridge : : EnableLocationHighAccuracy ( bool aEnable )
{
ALOG_BRIDGE ( " AndroidBridge::EnableLocationHighAccuracy " ) ;
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-03-21 21:52:35 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jEnableLocationHighAccuracy , aEnable ) ;
}
2012-02-05 23:51:05 +04:00
void
2012-05-04 19:08:47 +04:00
AndroidBridge : : EnableSensor ( int aSensorType )
{
2012-02-05 23:51:05 +04:00
ALOG_BRIDGE ( " AndroidBridge::EnableSensor " ) ;
2012-03-14 03:57:51 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-03-14 03:57:51 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jEnableSensor , aSensorType ) ;
2012-02-05 23:51:05 +04:00
}
void
2012-05-04 19:08:47 +04:00
AndroidBridge : : DisableSensor ( int aSensorType )
{
2012-02-05 23:51:05 +04:00
ALOG_BRIDGE ( " AndroidBridge::DisableSensor " ) ;
2012-03-14 03:57:51 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-03-14 03:57:51 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jDisableSensor , aSensorType ) ;
2012-02-05 23:51:05 +04:00
}
2010-06-04 00:56:36 +04:00
void
AndroidBridge : : ScheduleRestart ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " scheduling reboot " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jScheduleRestart ) ;
2010-06-04 00:56:36 +04:00
}
void
AndroidBridge : : NotifyXreExit ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " xre exiting " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jNotifyXreExit ) ;
2010-06-04 00:56:36 +04:00
}
2012-07-02 23:32:34 +04:00
static void
2010-09-30 19:37:36 +04:00
getHandlersFromStringArray ( JNIEnv * aJNIEnv , jobjectArray jArr , jsize aLen ,
nsIMutableArray * aHandlersArray ,
nsIHandlerApp * * aDefaultApp ,
const nsAString & aAction = EmptyString ( ) ,
const nsACString & aMimeType = EmptyCString ( ) )
{
nsString empty = EmptyString ( ) ;
for ( jsize i = 0 ; i < aLen ; i + = 4 ) {
2012-07-02 23:32:34 +04:00
nsJNIString name (
2012-04-17 02:31:05 +04:00
static_cast < jstring > ( aJNIEnv - > GetObjectArrayElement ( jArr , i ) ) , aJNIEnv ) ;
2010-09-30 19:37:36 +04:00
nsJNIString isDefault (
2012-04-17 02:31:05 +04:00
static_cast < jstring > ( aJNIEnv - > GetObjectArrayElement ( jArr , i + 1 ) ) , aJNIEnv ) ;
2012-07-02 23:32:34 +04:00
nsJNIString packageName (
2012-04-17 02:31:05 +04:00
static_cast < jstring > ( aJNIEnv - > GetObjectArrayElement ( jArr , i + 2 ) ) , aJNIEnv ) ;
2012-07-02 23:32:34 +04:00
nsJNIString className (
2012-04-17 02:31:05 +04:00
static_cast < jstring > ( aJNIEnv - > GetObjectArrayElement ( jArr , i + 3 ) ) , aJNIEnv ) ;
2010-09-30 19:37:36 +04:00
nsIHandlerApp * app = nsOSHelperAppService : :
CreateAndroidHandlerApp ( name , className , packageName ,
className , aMimeType , aAction ) ;
2012-07-02 23:32:34 +04:00
2011-10-17 18:59:28 +04:00
aHandlersArray - > AppendElement ( app , false ) ;
2010-09-30 19:37:36 +04:00
if ( aDefaultApp & & isDefault . Length ( ) > 0 )
* aDefaultApp = app ;
}
}
2011-09-29 10:19:26 +04:00
bool
2010-09-30 19:37:36 +04:00
AndroidBridge : : GetHandlersForMimeType ( const char * aMimeType ,
nsIMutableArray * aHandlersArray ,
nsIHandlerApp * * aDefaultApp ,
const nsAString & aAction )
2010-06-14 23:04:16 +04:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::GetHandlersForMimeType " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrMimeType = NewJavaString ( & jniFrame , aMimeType ) ;
2011-05-24 19:58:59 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrAction = NewJavaString ( & jniFrame , aAction ) ;
2010-09-30 19:37:36 +04:00
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass ,
2012-07-02 23:32:34 +04:00
jGetHandlersForMimeType ,
jstrMimeType , jstrAction ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return false ;
2010-06-14 23:04:16 +04:00
jobjectArray arr = static_cast < jobjectArray > ( obj ) ;
2010-06-16 03:39:43 +04:00
if ( ! arr )
2011-10-17 18:59:28 +04:00
return false ;
2010-06-30 06:22:08 +04:00
2012-01-30 00:39:30 +04:00
jsize len = env - > GetArrayLength ( arr ) ;
2010-06-30 06:22:08 +04:00
2010-09-30 19:37:36 +04:00
if ( ! aHandlersArray )
2010-06-30 06:22:08 +04:00
return len > 0 ;
2012-07-02 23:32:34 +04:00
getHandlersFromStringArray ( env , arr , len , aHandlersArray ,
2010-09-30 19:37:36 +04:00
aDefaultApp , aAction ,
nsDependentCString ( aMimeType ) ) ;
2011-10-17 18:59:28 +04:00
return true ;
2010-06-30 06:22:08 +04:00
}
2011-09-29 10:19:26 +04:00
bool
2010-11-24 02:40:46 +03:00
AndroidBridge : : GetHandlersForURL ( const char * aURL ,
2011-09-30 11:00:48 +04:00
nsIMutableArray * aHandlersArray ,
nsIHandlerApp * * aDefaultApp ,
const nsAString & aAction )
2010-06-30 06:22:08 +04:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::GetHandlersForURL " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrScheme = NewJavaString ( & jniFrame , aURL ) ;
jstring jstrAction = NewJavaString ( & jniFrame , aAction ) ;
2010-09-30 19:37:36 +04:00
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass ,
2012-07-02 23:32:34 +04:00
jGetHandlersForURL ,
jstrScheme , jstrAction ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return false ;
2010-06-30 06:22:08 +04:00
jobjectArray arr = static_cast < jobjectArray > ( obj ) ;
if ( ! arr )
2011-10-17 18:59:28 +04:00
return false ;
2010-06-30 06:22:08 +04:00
2012-01-30 00:39:30 +04:00
jsize len = env - > GetArrayLength ( arr ) ;
2010-06-30 06:22:08 +04:00
2010-09-30 19:37:36 +04:00
if ( ! aHandlersArray )
2010-06-30 06:22:08 +04:00
return len > 0 ;
2012-07-02 23:32:34 +04:00
getHandlersFromStringArray ( env , arr , len , aHandlersArray ,
2010-09-30 19:37:36 +04:00
aDefaultApp , aAction ) ;
2011-10-17 18:59:28 +04:00
return true ;
2010-06-14 23:04:16 +04:00
}
2011-09-29 10:19:26 +04:00
bool
2010-06-30 06:22:08 +04:00
AndroidBridge : : OpenUriExternal ( const nsACString & aUriSpec , const nsACString & aMimeType ,
2010-09-30 19:37:36 +04:00
const nsAString & aPackageName , const nsAString & aClassName ,
2010-10-02 05:04:23 +04:00
const nsAString & aAction , const nsAString & aTitle )
2010-06-14 23:04:16 +04:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::OpenUriExternal " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2010-06-30 06:22:08 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrUri = NewJavaString ( & jniFrame , aUriSpec ) ;
jstring jstrType = NewJavaString ( & jniFrame , aMimeType ) ;
2011-05-24 19:58:59 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrPackage = NewJavaString ( & jniFrame , aPackageName ) ;
jstring jstrClass = NewJavaString ( & jniFrame , aClassName ) ;
jstring jstrAction = NewJavaString ( & jniFrame , aAction ) ;
jstring jstrTitle = NewJavaString ( & jniFrame , aTitle ) ;
2010-09-30 19:37:36 +04:00
2012-05-04 19:08:47 +04:00
bool ret = env - > CallStaticBooleanMethod ( mGeckoAppShellClass ,
2010-06-14 23:04:16 +04:00
jOpenUriExternal ,
2012-07-02 23:32:34 +04:00
jstrUri , jstrType , jstrPackage ,
2010-10-02 05:04:23 +04:00
jstrClass , jstrAction , jstrTitle ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return false ;
return ret ;
2010-06-14 23:04:16 +04:00
}
void
2011-03-01 07:25:11 +03:00
AndroidBridge : : GetMimeTypeFromExtensions ( const nsACString & aFileExt , nsCString & aMimeType )
{
ALOG_BRIDGE ( " AndroidBridge::GetMimeTypeFromExtensions " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrExt = NewJavaString ( & jniFrame , aFileExt ) ;
2011-03-08 08:45:43 +03:00
jstring jstrType = static_cast < jstring > (
2012-01-30 00:39:30 +04:00
env - > CallStaticObjectMethod ( mGeckoAppShellClass ,
2012-07-02 23:32:34 +04:00
jGetMimeTypeFromExtensions ,
jstrExt ) ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2012-04-17 02:31:05 +04:00
nsJNIString jniStr ( jstrType , env ) ;
2012-12-14 02:03:44 +04:00
CopyUTF16toUTF8 ( jniStr . get ( ) , aMimeType ) ;
2010-06-14 23:04:16 +04:00
}
2011-03-08 08:45:43 +03:00
void
2011-09-15 11:40:17 +04:00
AndroidBridge : : GetExtensionFromMimeType ( const nsACString & aMimeType , nsACString & aFileExt )
2011-03-08 08:45:43 +03:00
{
ALOG_BRIDGE ( " AndroidBridge::GetExtensionFromMimeType " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrType = NewJavaString ( & jniFrame , aMimeType ) ;
2011-03-08 08:45:43 +03:00
jstring jstrExt = static_cast < jstring > (
2012-01-30 00:39:30 +04:00
env - > CallStaticObjectMethod ( mGeckoAppShellClass ,
2012-07-02 23:32:34 +04:00
jGetExtensionFromMimeType ,
jstrType ) ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2012-04-17 02:31:05 +04:00
nsJNIString jniStr ( jstrExt , env ) ;
2012-12-14 02:03:44 +04:00
CopyUTF16toUTF8 ( jniStr . get ( ) , aFileExt ) ;
2011-03-08 08:45:43 +03:00
}
2010-06-16 21:20:54 +04:00
void
AndroidBridge : : MoveTaskToBack ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::MoveTaskToBack " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jMoveTaskToBack ) ;
2010-06-16 21:20:54 +04:00
}
2010-08-27 03:43:23 +04:00
bool
AndroidBridge : : GetClipboardText ( nsAString & aText )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::GetClipboardText " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
jstring jstrType = static_cast < jstring > (
2013-06-08 00:39:37 +04:00
env - > CallStaticObjectMethod ( jClipboardClass ,
jClipboardGetText ) ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) | | ! jstrType )
2011-10-17 18:59:28 +04:00
return false ;
2012-05-04 19:08:47 +04:00
2012-04-17 02:31:05 +04:00
nsJNIString jniStr ( jstrType , env ) ;
2010-08-27 03:43:23 +04:00
aText . Assign ( jniStr ) ;
2011-10-17 18:59:28 +04:00
return true ;
2010-08-27 03:43:23 +04:00
}
void
AndroidBridge : : SetClipboardText ( const nsAString & aText )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::SetClipboardText " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstr = NewJavaString ( & jniFrame , aText ) ;
2013-06-08 00:39:37 +04:00
env - > CallStaticVoidMethod ( jClipboardClass , jClipboardSetText , jstr ) ;
2010-08-27 03:43:23 +04:00
}
bool
AndroidBridge : : ClipboardHasText ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::ClipboardHasText " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
jstring jstrType = static_cast < jstring > (
2013-06-08 00:39:37 +04:00
env - > CallStaticObjectMethod ( jClipboardClass ,
jClipboardGetText ) ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) | | ! jstrType )
2011-10-17 18:59:28 +04:00
return false ;
2012-05-04 19:08:47 +04:00
2011-10-17 18:59:28 +04:00
return true ;
2010-08-27 03:43:23 +04:00
}
void
AndroidBridge : : EmptyClipboard ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::EmptyClipboard " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2013-06-08 00:39:37 +04:00
env - > CallStaticVoidMethod ( jClipboardClass , jClipboardSetText , nullptr ) ;
2010-08-27 03:43:23 +04:00
}
2010-06-15 06:17:37 +04:00
void
AndroidBridge : : ShowAlertNotification ( const nsAString & aImageUrl ,
const nsAString & aAlertTitle ,
const nsAString & aAlertText ,
const nsAString & aAlertCookie ,
nsIObserver * aAlertListener ,
const nsAString & aAlertName )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " ShowAlertNotification " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2010-06-15 06:17:37 +04:00
2013-08-02 22:04:49 +04:00
if ( nsAppShell : : gAppShell & & aAlertListener ) {
// This will remove any observers already registered for this id
2010-06-15 06:17:37 +04:00
nsAppShell : : gAppShell - > AddObserver ( aAlertName , aAlertListener ) ;
2013-08-02 22:04:49 +04:00
}
2010-06-15 06:17:37 +04:00
jvalue args [ 5 ] ;
2012-12-14 02:03:44 +04:00
args [ 0 ] . l = NewJavaString ( & jniFrame , aImageUrl ) ;
args [ 1 ] . l = NewJavaString ( & jniFrame , aAlertTitle ) ;
args [ 2 ] . l = NewJavaString ( & jniFrame , aAlertText ) ;
args [ 3 ] . l = NewJavaString ( & jniFrame , aAlertCookie ) ;
args [ 4 ] . l = NewJavaString ( & jniFrame , aAlertName ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethodA ( mGeckoAppShellClass , jShowAlertNotification , args ) ;
2010-06-15 06:17:37 +04:00
}
2010-10-02 01:21:21 +04:00
void
AndroidBridge : : AlertsProgressListener_OnProgress ( const nsAString & aAlertName ,
2012-08-22 19:56:38 +04:00
int64_t aProgress ,
int64_t aProgressMax ,
2010-10-02 01:21:21 +04:00
const nsAString & aAlertText )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AlertsProgressListener_OnProgress " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2010-10-02 01:21:21 +04:00
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2010-10-02 01:21:21 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrName = NewJavaString ( & jniFrame , aAlertName ) ;
jstring jstrText = NewJavaString ( & jniFrame , aAlertText ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jAlertsProgressListener_OnProgress ,
2012-07-02 23:32:34 +04:00
jstrName , aProgress , aProgressMax , jstrText ) ;
2010-10-02 01:21:21 +04:00
}
2010-10-13 06:17:12 +04:00
void
2013-03-18 17:24:53 +04:00
AndroidBridge : : CloseNotification ( const nsAString & aAlertName )
2010-10-13 06:17:12 +04:00
{
2013-03-18 17:24:53 +04:00
ALOG_BRIDGE ( " CloseNotification " ) ;
2010-10-13 06:17:12 +04:00
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-01-30 00:39:30 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrName = NewJavaString ( & jniFrame , aAlertName ) ;
2013-03-18 17:24:53 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jCloseNotification , jstrName ) ;
2010-10-13 06:17:12 +04:00
}
2010-10-07 21:28:27 +04:00
int
AndroidBridge : : GetDPI ( )
{
2012-09-28 23:05:50 +04:00
static int sDPI = 0 ;
if ( sDPI )
return sDPI ;
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::GetDPI " ) ;
2012-09-28 23:05:50 +04:00
const int DEFAULT_DPI = 160 ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2012-05-04 19:08:47 +04:00
return DEFAULT_DPI ;
AutoLocalJNIFrame jniFrame ( env ) ;
2012-01-30 00:39:30 +04:00
2012-09-28 23:05:50 +04:00
sDPI = ( int ) env - > CallStaticIntMethod ( mGeckoAppShellClass , jGetDpi ) ;
if ( jniFrame . CheckForException ( ) ) {
sDPI = 0 ;
2012-05-04 19:08:47 +04:00
return DEFAULT_DPI ;
2012-09-28 23:05:50 +04:00
}
2012-05-04 19:08:47 +04:00
2012-09-28 23:05:50 +04:00
return sDPI ;
2010-10-07 21:28:27 +04:00
}
2013-07-04 17:53:25 +04:00
int
AndroidBridge : : GetScreenDepth ( )
{
static int sDepth = 0 ;
if ( sDepth )
return sDepth ;
ALOG_BRIDGE ( " AndroidBridge::GetScreenDepth " ) ;
const int DEFAULT_DEPTH = 16 ;
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return DEFAULT_DEPTH ;
AutoLocalJNIFrame jniFrame ( env ) ;
sDepth = ( int ) env - > CallStaticIntMethod ( mGeckoAppShellClass , jGetScreenDepth ) ;
if ( jniFrame . CheckForException ( ) ) {
sDepth = 0 ;
return DEFAULT_DEPTH ;
}
return sDepth ;
}
2010-09-30 19:37:36 +04:00
void
2012-03-18 20:15:56 +04:00
AndroidBridge : : ShowFilePickerForExtensions ( nsAString & aFilePath , const nsAString & aExtensions )
2012-03-17 01:44:00 +04:00
{
2012-03-18 20:15:56 +04:00
ALOG_BRIDGE ( " AndroidBridge::ShowFilePickerForExtensions " ) ;
2012-03-17 01:44:00 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrFilers = NewJavaString ( & jniFrame , aExtensions ) ;
2012-05-04 19:08:47 +04:00
jstring jstr = static_cast < jstring > ( env - > CallStaticObjectMethod (
2012-07-02 23:32:34 +04:00
mGeckoAppShellClass ,
jShowFilePickerForExtensions , jstrFilers ) ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2012-04-17 02:31:05 +04:00
aFilePath . Assign ( nsJNIString ( jstr , env ) ) ;
2012-03-18 20:15:56 +04:00
}
void
AndroidBridge : : ShowFilePickerForMimeType ( nsAString & aFilePath , const nsAString & aMimeType )
{
ALOG_BRIDGE ( " AndroidBridge::ShowFilePickerForMimeType " ) ;
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrFilers = NewJavaString ( & jniFrame , aMimeType ) ;
2012-05-04 19:08:47 +04:00
jstring jstr = static_cast < jstring > ( env - > CallStaticObjectMethod (
mGeckoAppShellClass ,
jShowFilePickerForMimeType , jstrFilers ) ) ;
if ( jniFrame . CheckForException ( ) )
return ;
2012-04-17 02:31:05 +04:00
aFilePath . Assign ( nsJNIString ( jstr , env ) ) ;
2010-09-30 19:37:36 +04:00
}
2010-11-09 05:11:13 +03:00
void
2012-04-05 09:50:06 +04:00
AndroidBridge : : ShowFilePickerAsync ( const nsAString & aMimeType , nsFilePickerCallback * callback )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jMimeType = NewJavaString ( & jniFrame , aMimeType ) ;
2012-04-05 09:50:06 +04:00
callback - > AddRef ( ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jShowFilePickerAsync , jMimeType , ( jlong ) callback ) ;
}
void
2011-09-29 10:19:26 +04:00
AndroidBridge : : SetFullScreen ( bool aFullScreen )
2010-11-09 05:11:13 +03:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::SetFullScreen " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jSetFullScreen , aFullScreen ) ;
2010-11-09 05:11:13 +03:00
}
2010-11-12 23:02:14 +03:00
void
AndroidBridge : : HideProgressDialogOnce ( )
{
static bool once = false ;
2012-05-04 19:08:47 +04:00
if ( once )
return ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jHideProgressDialog ) ;
if ( jniFrame . CheckForException ( ) )
return ;
once = true ;
2010-11-12 23:02:14 +03:00
}
2010-11-22 19:38:02 +03:00
void
2011-09-29 10:19:26 +04:00
AndroidBridge : : PerformHapticFeedback ( bool aIsLongPress )
2010-11-22 19:38:02 +03:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::PerformHapticFeedback " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass ,
2012-07-02 23:32:34 +04:00
jPerformHapticFeedback , aIsLongPress ) ;
2010-11-22 19:38:02 +03:00
}
2011-09-30 11:00:48 +04:00
void
2012-08-22 19:56:38 +04:00
AndroidBridge : : Vibrate ( const nsTArray < uint32_t > & aPattern )
2011-09-30 11:00:48 +04:00
{
2012-05-04 19:08:47 +04:00
ALOG_BRIDGE ( " AndroidBridge::Vibrate " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-04-28 20:55:59 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-08-22 19:56:38 +04:00
uint32_t len = aPattern . Length ( ) ;
2011-09-30 11:00:48 +04:00
if ( ! len ) {
ALOG_BRIDGE ( " invalid 0-length array " ) ;
return ;
}
// It's clear if this worth special-casing, but it creates less
// java junk, so dodges the GC.
if ( len = = 1 ) {
jlong d = aPattern [ 0 ] ;
if ( d < 0 ) {
ALOG_BRIDGE ( " invalid vibration duration < 0 " ) ;
return ;
}
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jVibrate1 , d ) ;
2012-05-04 19:08:45 +04:00
return ;
2011-09-30 11:00:48 +04:00
}
// First element of the array vibrate() expects is how long to wait
// *before* vibrating. For us, this is always 0.
2012-01-30 00:39:30 +04:00
jlongArray array = env - > NewLongArray ( len + 1 ) ;
2011-09-30 11:00:48 +04:00
if ( ! array ) {
ALOG_BRIDGE ( " failed to allocate array " ) ;
return ;
}
2012-07-30 18:20:58 +04:00
jlong * elts = env - > GetLongArrayElements ( array , nullptr ) ;
2011-09-30 11:00:48 +04:00
elts [ 0 ] = 0 ;
2012-08-22 19:56:38 +04:00
for ( uint32_t i = 0 ; i < aPattern . Length ( ) ; + + i ) {
2011-09-30 11:00:48 +04:00
jlong d = aPattern [ i ] ;
if ( d < 0 ) {
ALOG_BRIDGE ( " invalid vibration duration < 0 " ) ;
2012-01-30 00:39:30 +04:00
env - > ReleaseLongArrayElements ( array , elts , JNI_ABORT ) ;
2011-09-30 11:00:48 +04:00
return ;
}
elts [ i + 1 ] = d ;
}
2012-01-30 00:39:30 +04:00
env - > ReleaseLongArrayElements ( array , elts , 0 ) ;
2011-09-30 11:00:48 +04:00
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jVibrateA ,
2012-07-02 23:32:34 +04:00
array , - 1 /*don't repeat*/ ) ;
2011-09-30 11:00:48 +04:00
// GC owns |array| now?
}
void
AndroidBridge : : CancelVibrate ( )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jCancelVibrate ) ;
2011-09-30 11:00:48 +04:00
}
2011-01-20 14:06:13 +03:00
bool
AndroidBridge : : IsNetworkLinkUp ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::IsNetworkLinkUp " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
bool ret = ! ! env - > CallStaticBooleanMethod ( mGeckoAppShellClass , jIsNetworkLinkUp ) ;
if ( jniFrame . CheckForException ( ) )
return false ;
return ret ;
2011-01-20 14:06:13 +03:00
}
bool
AndroidBridge : : IsNetworkLinkKnown ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::IsNetworkLinkKnown " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
bool ret = ! ! env - > CallStaticBooleanMethod ( mGeckoAppShellClass , jIsNetworkLinkKnown ) ;
if ( jniFrame . CheckForException ( ) )
return false ;
return ret ;
2011-01-20 14:06:13 +03:00
}
2011-02-27 18:50:56 +03:00
void
2011-03-17 02:17:59 +03:00
AndroidBridge : : SetSelectedLocale ( const nsAString & aLocale )
2011-03-01 07:25:11 +03:00
{
ALOG_BRIDGE ( " AndroidBridge::SetSelectedLocale " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jLocale = NewJavaString ( & jniFrame , aLocale ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jSetSelectedLocale , jLocale ) ;
2011-02-27 18:50:56 +03:00
}
2011-03-30 22:04:41 +04:00
void
AndroidBridge : : GetSystemColors ( AndroidSystemColors * aColors )
{
ALOG_BRIDGE ( " AndroidBridge::GetSystemColors " ) ;
2012-07-30 18:20:58 +04:00
NS_ASSERTION ( aColors ! = nullptr , " AndroidBridge::GetSystemColors: aColors is null! " ) ;
2011-03-30 22:04:41 +04:00
if ( ! aColors )
return ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2011-03-30 22:04:41 +04:00
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetSystemColors ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2011-03-30 22:04:41 +04:00
jintArray arr = static_cast < jintArray > ( obj ) ;
if ( ! arr )
return ;
2012-08-22 19:56:38 +04:00
uint32_t len = static_cast < uint32_t > ( env - > GetArrayLength ( arr ) ) ;
2012-01-30 00:39:30 +04:00
jint * elements = env - > GetIntArrayElements ( arr , 0 ) ;
2011-03-30 22:04:41 +04:00
2012-08-22 19:56:38 +04:00
uint32_t colorsCount = sizeof ( AndroidSystemColors ) / sizeof ( nscolor ) ;
2011-03-30 22:04:41 +04:00
if ( len < colorsCount )
colorsCount = len ;
// Convert Android colors to nscolor by switching R and B in the ARGB 32 bit value
nscolor * colors = ( nscolor * ) aColors ;
2012-08-22 19:56:38 +04:00
for ( uint32_t i = 0 ; i < colorsCount ; i + + ) {
uint32_t androidColor = static_cast < uint32_t > ( elements [ i ] ) ;
uint8_t r = ( androidColor & 0x00ff0000 ) > > 16 ;
uint8_t b = ( androidColor & 0x000000ff ) ;
2012-07-12 20:52:52 +04:00
colors [ i ] = ( androidColor & 0xff00ff00 ) | ( b < < 16 ) | r ;
2011-03-30 22:04:41 +04:00
}
2012-01-30 00:39:30 +04:00
env - > ReleaseIntArrayElements ( arr , elements , 0 ) ;
2011-03-30 22:04:41 +04:00
}
2011-06-14 01:02:13 +04:00
void
2012-08-22 19:56:38 +04:00
AndroidBridge : : GetIconForExtension ( const nsACString & aFileExt , uint32_t aIconSize , uint8_t * const aBuf )
2011-06-14 01:02:13 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::GetIconForExtension " ) ;
2012-07-30 18:20:58 +04:00
NS_ASSERTION ( aBuf ! = nullptr , " AndroidBridge::GetIconForExtension: aBuf is null! " ) ;
2011-06-14 01:02:13 +04:00
if ( ! aBuf )
return ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2011-06-14 01:02:13 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrFileExt = NewJavaString ( & jniFrame , aFileExt ) ;
2012-07-02 23:32:34 +04:00
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetIconForExtension , jstrFileExt , aIconSize ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2011-06-14 01:02:13 +04:00
jbyteArray arr = static_cast < jbyteArray > ( obj ) ;
2012-07-30 18:20:58 +04:00
NS_ASSERTION ( arr ! = nullptr , " AndroidBridge::GetIconForExtension: Returned pixels array is null! " ) ;
2011-06-14 01:02:13 +04:00
if ( ! arr )
return ;
2012-08-22 19:56:38 +04:00
uint32_t len = static_cast < uint32_t > ( env - > GetArrayLength ( arr ) ) ;
2012-01-30 00:39:30 +04:00
jbyte * elements = env - > GetByteArrayElements ( arr , 0 ) ;
2011-06-14 01:02:13 +04:00
2012-08-22 19:56:38 +04:00
uint32_t bufSize = aIconSize * aIconSize * 4 ;
2011-06-14 01:02:13 +04:00
NS_ASSERTION ( len = = bufSize , " AndroidBridge::GetIconForExtension: Pixels array is incomplete! " ) ;
if ( len = = bufSize )
memcpy ( aBuf , elements , bufSize ) ;
2012-01-30 00:39:30 +04:00
env - > ReleaseByteArrayElements ( arr , elements , 0 ) ;
2011-06-14 01:02:13 +04:00
}
2011-07-27 05:14:52 +04:00
bool
AndroidBridge : : GetShowPasswordSetting ( )
{
ALOG_BRIDGE ( " AndroidBridge::GetShowPasswordSetting " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
bool ret = env - > CallStaticBooleanMethod ( mGeckoAppShellClass , jGetShowPasswordSetting ) ;
if ( jniFrame . CheckForException ( ) )
return false ;
return ret ;
2011-07-27 05:14:52 +04:00
}
2010-06-04 00:56:36 +04:00
void
2012-07-13 18:19:46 +04:00
AndroidBridge : : SetLayerClient ( JNIEnv * env , jobject jobj )
2012-02-17 18:16:14 +04:00
{
2012-07-13 18:19:46 +04:00
// if resetting is true, that means Android destroyed our GeckoApp activity
// and we had to recreate it, but all the Gecko-side things were not destroyed.
// We therefore need to link up the new java objects to Gecko, and that's what
// we do here.
bool resetting = ( mLayerClient ! = NULL ) ;
if ( resetting ) {
// clear out the old layer client
env - > DeleteGlobalRef ( mLayerClient - > wrappedObject ( ) ) ;
delete mLayerClient ;
mLayerClient = NULL ;
}
2012-02-22 23:45:44 +04:00
AndroidGeckoLayerClient * client = new AndroidGeckoLayerClient ( ) ;
2012-07-13 18:19:46 +04:00
client - > Init ( env - > NewGlobalRef ( jobj ) ) ;
2012-02-17 18:16:14 +04:00
mLayerClient = client ;
2012-07-13 18:19:46 +04:00
if ( resetting ) {
2013-02-05 00:13:17 +04:00
// since we are re-linking the new java objects to Gecko, we need to get
// the viewport from the compositor (since the Java copy was thrown away)
// and we do that by setting the first-paint flag.
nsWindow : : ForceIsFirstPaint ( ) ;
2012-07-13 18:19:46 +04:00
}
2010-06-04 00:56:36 +04:00
}
2010-11-10 17:40:00 +03:00
void
AndroidBridge : : ShowInputMethodPicker ( )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::ShowInputMethodPicker " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jShowInputMethodPicker ) ;
2010-11-10 17:40:00 +03:00
}
2012-02-07 01:53:09 +04:00
void
2013-02-28 22:28:23 +04:00
AndroidBridge : : RegisterCompositor ( JNIEnv * env )
2012-01-26 23:23:13 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::RegisterCompositor " ) ;
2013-02-28 22:28:24 +04:00
if ( mGLControllerObj ) {
// we already have this set up, no need to do it again
return ;
}
if ( ! env ) {
2012-07-14 01:34:58 +04:00
env = GetJNIForThread ( ) ; // called on the compositor thread
2013-02-28 22:28:24 +04:00
}
if ( ! env ) {
2012-02-07 01:53:09 +04:00
return ;
2013-02-28 22:28:24 +04:00
}
2012-01-26 23:23:13 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-02-02 11:54:01 +04:00
2012-04-05 19:28:50 +04:00
jmethodID registerCompositor = env - > GetStaticMethodID ( jLayerView , " registerCxxCompositor " , " ()Lorg/mozilla/gecko/gfx/GLController; " ) ;
2012-01-26 23:23:13 +04:00
2012-07-13 05:35:19 +04:00
jobject glController = env - > CallStaticObjectMethod ( jLayerView , registerCompositor ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2012-02-02 01:18:35 +04:00
2013-02-28 22:28:24 +04:00
mGLControllerObj = env - > NewGlobalRef ( glController ) ;
2012-02-07 01:53:09 +04:00
}
EGLSurface
2013-02-28 22:28:23 +04:00
AndroidBridge : : ProvideEGLSurface ( )
2012-02-07 01:53:09 +04:00
{
2013-02-28 22:28:24 +04:00
if ( ! jEGLSurfacePointerField ) {
return NULL ;
}
MOZ_ASSERT ( mGLControllerObj , " AndroidBridge::ProvideEGLSurface called with a null GL controller ref " ) ;
JNIEnv * env = GetJNIForThread ( ) ; // called on the compositor thread
AutoLocalJNIFrame jniFrame ( env ) ;
jobject eglSurface = env - > CallObjectMethod ( mGLControllerObj , jProvideEGLSurfaceMethod ) ;
if ( jniFrame . CheckForException ( ) | | ! eglSurface )
return NULL ;
return reinterpret_cast < EGLSurface > ( env - > GetIntField ( eglSurface , jEGLSurfacePointerField ) ) ;
2012-01-26 23:23:13 +04:00
}
2011-02-28 19:32:05 +03:00
bool
2012-08-22 19:56:38 +04:00
AndroidBridge : : GetStaticIntField ( const char * className , const char * fieldName , int32_t * aInt , JNIEnv * env /* = nullptr */ )
2011-02-28 19:32:05 +03:00
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::GetStaticIntField %s " , fieldName ) ;
2012-04-28 00:04:47 +04:00
if ( ! env ) {
env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
}
2012-01-30 00:39:30 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-01-30 00:39:30 +04:00
jclass cls = env - > FindClass ( className ) ;
2011-02-28 19:32:05 +03:00
if ( ! cls )
return false ;
2012-01-30 00:39:30 +04:00
jfieldID field = env - > GetStaticFieldID ( cls , fieldName , " I " ) ;
2011-02-28 19:32:05 +03:00
if ( ! field )
return false ;
2012-08-22 19:56:38 +04:00
* aInt = static_cast < int32_t > ( env - > GetStaticIntField ( cls , field ) ) ;
2011-02-28 19:32:05 +03:00
return true ;
}
2010-09-11 17:20:20 +04:00
bool
2012-07-30 18:20:58 +04:00
AndroidBridge : : GetStaticStringField ( const char * className , const char * fieldName , nsAString & result , JNIEnv * env /* = nullptr */ )
2010-09-11 17:20:20 +04:00
{
2012-05-04 19:08:47 +04:00
ALOG_BRIDGE ( " AndroidBridge::GetStaticStringField %s " , fieldName ) ;
2011-03-01 07:25:11 +03:00
2012-04-28 00:04:47 +04:00
if ( ! env ) {
env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
}
2012-01-30 00:39:30 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-01-30 00:39:30 +04:00
jclass cls = env - > FindClass ( className ) ;
2010-09-11 17:20:20 +04:00
if ( ! cls )
return false ;
2012-01-30 00:39:30 +04:00
jfieldID field = env - > GetStaticFieldID ( cls , fieldName , " Ljava/lang/String; " ) ;
2010-09-11 17:20:20 +04:00
if ( ! field )
return false ;
2012-01-30 00:39:30 +04:00
jstring jstr = ( jstring ) env - > GetStaticObjectField ( cls , field ) ;
2010-09-11 17:20:20 +04:00
if ( ! jstr )
return false ;
2012-04-17 02:31:05 +04:00
result . Assign ( nsJNIString ( jstr , env ) ) ;
2010-09-11 17:20:20 +04:00
return true ;
}
2010-12-22 01:34:29 +03:00
void
AndroidBridge : : SetKeepScreenOn ( bool on )
{
2011-03-01 07:25:11 +03:00
ALOG_BRIDGE ( " AndroidBridge::SetKeepScreenOn " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( sBridge - > mGeckoAppShellClass ,
sBridge - > jSetKeepScreenOn , on ) ;
2010-12-22 01:34:29 +03:00
}
2010-06-04 00:56:36 +04:00
// Available for places elsewhere in the code to link to.
2011-09-29 10:19:26 +04:00
bool
2010-06-04 00:56:36 +04:00
mozilla_AndroidBridge_SetMainThread ( void * thr )
{
return AndroidBridge : : Bridge ( ) - > SetMainThread ( thr ) ;
}
2010-10-15 22:16:45 +04:00
jclass GetGeckoAppShellClass ( )
{
return mozilla : : AndroidBridge : : GetGeckoAppShellClass ( ) ;
}
2011-04-08 21:52:13 +04:00
void
AndroidBridge : : ScanMedia ( const nsAString & aFile , const nsACString & aMimeType )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrFile = NewJavaString ( & jniFrame , aFile ) ;
2011-04-08 21:52:13 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrMimeTypes = NewJavaString ( & jniFrame , aMimeType ) ;
2011-04-08 21:52:13 +04:00
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jScanMedia , jstrFile , jstrMimeTypes ) ;
2011-04-08 21:52:13 +04:00
}
2011-08-09 06:37:57 +04:00
void
AndroidBridge : : CreateShortcut ( const nsAString & aTitle , const nsAString & aURI , const nsAString & aIconData , const nsAString & aIntent )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrTitle = NewJavaString ( & jniFrame , aTitle ) ;
jstring jstrURI = NewJavaString ( & jniFrame , aURI ) ;
jstring jstrIconData = NewJavaString ( & jniFrame , aIconData ) ;
jstring jstrIntent = NewJavaString ( & jniFrame , aIntent ) ;
2012-07-02 23:32:34 +04:00
2012-01-30 00:39:30 +04:00
if ( ! jstrURI | | ! jstrTitle | | ! jstrIconData )
return ;
2012-07-02 23:32:34 +04:00
2012-05-04 19:08:45 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jCreateShortcut , jstrTitle , jstrURI , jstrIconData , jstrIntent ) ;
2011-08-09 06:37:57 +04:00
}
2012-04-28 00:04:47 +04:00
void *
AndroidBridge : : GetNativeSurface ( JNIEnv * env , jobject surface ) {
2013-02-26 18:28:57 +04:00
if ( ! env | | ! mHasNativeWindowFallback | | ! jSurfacePointerField )
2012-07-30 18:20:58 +04:00
return nullptr ;
2012-04-28 00:04:47 +04:00
return ( void * ) env - > GetIntField ( surface , jSurfacePointerField ) ;
}
2011-09-21 20:46:00 +04:00
void
AndroidBridge : : OpenGraphicsLibraries ( )
2011-06-16 13:03:00 +04:00
{
2011-09-21 20:46:00 +04:00
if ( ! mOpenedGraphicsLibraries ) {
2011-06-16 13:03:00 +04:00
// Try to dlopen libjnigraphics.so for direct bitmap access on
// Android 2.2+ (API level 8)
2011-09-21 20:46:00 +04:00
mOpenedGraphicsLibraries = true ;
2011-09-28 22:43:02 +04:00
mHasNativeWindowAccess = false ;
2012-04-28 00:04:47 +04:00
mHasNativeWindowFallback = false ;
2011-09-28 22:43:02 +04:00
mHasNativeBitmapAccess = false ;
2011-06-16 13:03:00 +04:00
2012-01-13 10:23:02 +04:00
void * handle = dlopen ( " libjnigraphics.so " , RTLD_LAZY | RTLD_LOCAL ) ;
2011-09-21 20:46:00 +04:00
if ( handle ) {
AndroidBitmap_getInfo = ( int ( * ) ( JNIEnv * , jobject , void * ) ) dlsym ( handle , " AndroidBitmap_getInfo " ) ;
AndroidBitmap_lockPixels = ( int ( * ) ( JNIEnv * , jobject , void * * ) ) dlsym ( handle , " AndroidBitmap_lockPixels " ) ;
AndroidBitmap_unlockPixels = ( int ( * ) ( JNIEnv * , jobject ) ) dlsym ( handle , " AndroidBitmap_unlockPixels " ) ;
2011-06-16 13:03:00 +04:00
2011-09-28 22:43:02 +04:00
mHasNativeBitmapAccess = AndroidBitmap_getInfo & & AndroidBitmap_lockPixels & & AndroidBitmap_unlockPixels ;
ALOG_BRIDGE ( " Successfully opened libjnigraphics.so, have native bitmap access? %d " , mHasNativeBitmapAccess ) ;
2011-09-21 20:46:00 +04:00
}
2011-06-16 13:03:00 +04:00
2011-09-21 20:46:00 +04:00
// Try to dlopen libandroid.so for and native window access on
// Android 2.3+ (API level 9)
2012-01-13 10:23:02 +04:00
handle = dlopen ( " libandroid.so " , RTLD_LAZY | RTLD_LOCAL ) ;
2011-09-21 20:46:00 +04:00
if ( handle ) {
ANativeWindow_fromSurface = ( void * ( * ) ( JNIEnv * , jobject ) ) dlsym ( handle , " ANativeWindow_fromSurface " ) ;
ANativeWindow_release = ( void ( * ) ( void * ) ) dlsym ( handle , " ANativeWindow_release " ) ;
ANativeWindow_setBuffersGeometry = ( int ( * ) ( void * , int , int , int ) ) dlsym ( handle , " ANativeWindow_setBuffersGeometry " ) ;
ANativeWindow_lock = ( int ( * ) ( void * , void * , void * ) ) dlsym ( handle , " ANativeWindow_lock " ) ;
ANativeWindow_unlockAndPost = ( int ( * ) ( void * ) ) dlsym ( handle , " ANativeWindow_unlockAndPost " ) ;
2011-09-21 20:46:00 +04:00
2012-07-20 23:20:51 +04:00
// This is only available in Honeycomb and ICS. It was removed in Jelly Bean
ANativeWindow_fromSurfaceTexture = ( void * ( * ) ( JNIEnv * , jobject ) ) dlsym ( handle , " ANativeWindow_fromSurfaceTexture " ) ;
2011-09-28 22:43:02 +04:00
mHasNativeWindowAccess = ANativeWindow_fromSurface & & ANativeWindow_release & & ANativeWindow_lock & & ANativeWindow_unlockAndPost ;
ALOG_BRIDGE ( " Successfully opened libandroid.so, have native window access? %d " , mHasNativeWindowAccess ) ;
}
2012-04-28 00:04:47 +04:00
2012-07-20 23:20:51 +04:00
// We need one symbol from here on Jelly Bean
handle = dlopen ( " libandroid_runtime.so " , RTLD_LAZY | RTLD_LOCAL ) ;
if ( handle ) {
android_SurfaceTexture_getNativeWindow = ( android : : sp < AndroidRefable > ( * ) ( JNIEnv * , jobject ) ) dlsym ( handle , " _ZN7android38android_SurfaceTexture_getNativeWindowEP7_JNIEnvP8_jobject " ) ;
}
2012-04-28 00:04:47 +04:00
if ( mHasNativeWindowAccess )
return ;
// Look up Surface functions, used for native window (surface) fallback
handle = dlopen ( " libsurfaceflinger_client.so " , RTLD_LAZY ) ;
if ( handle ) {
Surface_lock = ( int ( * ) ( void * , void * , void * , bool ) ) dlsym ( handle , " _ZN7android7Surface4lockEPNS0_11SurfaceInfoEPNS_6RegionEb " ) ;
Surface_unlockAndPost = ( int ( * ) ( void * ) ) dlsym ( handle , " _ZN7android7Surface13unlockAndPostEv " ) ;
handle = dlopen ( " libui.so " , RTLD_LAZY ) ;
if ( handle ) {
Region_constructor = ( void ( * ) ( void * ) ) dlsym ( handle , " _ZN7android6RegionC1Ev " ) ;
Region_set = ( void ( * ) ( void * , void * ) ) dlsym ( handle , " _ZN7android6Region3setERKNS_4RectE " ) ;
mHasNativeWindowFallback = Surface_lock & & Surface_unlockAndPost & & Region_constructor & & Region_set ;
}
}
2011-09-24 19:51:46 +04:00
}
2011-09-21 20:46:00 +04:00
}
2011-08-04 11:32:57 +04:00
namespace mozilla {
class TracerRunnable : public nsRunnable {
public :
TracerRunnable ( ) {
mTracerLock = new Mutex ( " TracerRunnable " ) ;
mTracerCondVar = new CondVar ( * mTracerLock , " TracerRunnable " ) ;
mMainThread = do_GetMainThread ( ) ;
2012-07-02 23:32:34 +04:00
2011-08-04 11:32:57 +04:00
}
~ TracerRunnable ( ) {
delete mTracerCondVar ;
delete mTracerLock ;
2012-07-30 18:20:58 +04:00
mTracerLock = nullptr ;
mTracerCondVar = nullptr ;
2011-08-04 11:32:57 +04:00
}
virtual nsresult Run ( ) {
MutexAutoLock lock ( * mTracerLock ) ;
if ( ! AndroidBridge : : Bridge ( ) )
return NS_OK ;
2012-07-02 23:32:34 +04:00
2012-04-04 13:15:10 +04:00
mHasRun = true ;
2011-08-04 11:32:57 +04:00
mTracerCondVar - > Notify ( ) ;
return NS_OK ;
}
2012-07-02 23:32:34 +04:00
2011-08-04 11:32:57 +04:00
bool Fire ( ) {
if ( ! mTracerLock | | ! mTracerCondVar )
return false ;
MutexAutoLock lock ( * mTracerLock ) ;
2012-04-04 13:15:10 +04:00
mHasRun = false ;
2011-08-04 11:32:57 +04:00
mMainThread - > Dispatch ( this , NS_DISPATCH_NORMAL ) ;
while ( ! mHasRun )
mTracerCondVar - > Wait ( ) ;
return true ;
}
void Signal ( ) {
MutexAutoLock lock ( * mTracerLock ) ;
2012-04-04 13:15:10 +04:00
mHasRun = true ;
2011-08-04 11:32:57 +04:00
mTracerCondVar - > Notify ( ) ;
}
private :
Mutex * mTracerLock ;
CondVar * mTracerCondVar ;
2012-04-04 13:15:10 +04:00
bool mHasRun ;
2011-08-04 11:32:57 +04:00
nsCOMPtr < nsIThread > mMainThread ;
} ;
2013-08-12 18:58:36 +04:00
StaticRefPtr < TracerRunnable > sTracerRunnable ;
2011-08-04 11:32:57 +04:00
bool InitWidgetTracing ( ) {
if ( ! sTracerRunnable )
sTracerRunnable = new TracerRunnable ( ) ;
return true ;
}
void CleanUpWidgetTracing ( ) {
2012-07-30 18:20:58 +04:00
sTracerRunnable = nullptr ;
2011-08-04 11:32:57 +04:00
}
bool FireAndWaitForTracerEvent ( ) {
if ( sTracerRunnable )
return sTracerRunnable - > Fire ( ) ;
return false ;
}
2012-07-02 23:32:34 +04:00
void SignalTracerThread ( )
{
if ( sTracerRunnable )
return sTracerRunnable - > Signal ( ) ;
}
2011-08-04 11:32:57 +04:00
}
2011-09-21 20:46:00 +04:00
bool
AndroidBridge : : HasNativeBitmapAccess ( )
{
OpenGraphicsLibraries ( ) ;
2011-06-16 13:03:00 +04:00
return mHasNativeBitmapAccess ;
}
bool
AndroidBridge : : ValidateBitmap ( jobject bitmap , int width , int height )
{
// This structure is defined in Android API level 8's <android/bitmap.h>
// Because we can't depend on this, we get the function pointers via dlsym
// and define this struct ourselves.
struct BitmapInfo {
uint32_t width ;
uint32_t height ;
uint32_t stride ;
uint32_t format ;
uint32_t flags ;
} ;
int err ;
struct BitmapInfo info = { 0 , } ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
if ( ( err = AndroidBitmap_getInfo ( env , bitmap , & info ) ) ! = 0 ) {
2011-06-16 13:03:00 +04:00
ALOG_BRIDGE ( " AndroidBitmap_getInfo failed! (error %d) " , err ) ;
return false ;
}
2012-07-12 20:52:52 +04:00
if ( ( int ) info . width ! = width | | ( int ) info . height ! = height )
2011-06-16 13:03:00 +04:00
return false ;
return true ;
}
2011-09-27 04:25:41 +04:00
bool
2012-08-22 19:56:38 +04:00
AndroidBridge : : InitCamera ( const nsCString & contentType , uint32_t camera , uint32_t * width , uint32_t * height , uint32_t * fps )
2011-09-27 04:25:41 +04:00
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2011-09-27 04:25:41 +04:00
2012-12-14 02:03:44 +04:00
jstring jstrContentType = NewJavaString ( & jniFrame , contentType ) ;
2012-05-04 19:08:47 +04:00
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass , jInitCamera , jstrContentType , camera , * width , * height ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return false ;
2011-09-27 04:25:41 +04:00
jintArray arr = static_cast < jintArray > ( obj ) ;
if ( ! arr )
return false ;
2012-01-30 00:39:30 +04:00
jint * elements = env - > GetIntArrayElements ( arr , 0 ) ;
2011-09-27 04:25:41 +04:00
* width = elements [ 1 ] ;
* height = elements [ 2 ] ;
* fps = elements [ 3 ] ;
bool res = elements [ 0 ] = = 1 ;
2012-01-30 00:39:30 +04:00
env - > ReleaseIntArrayElements ( arr , elements , 0 ) ;
2011-09-27 04:25:41 +04:00
return res ;
}
void
2012-05-04 19:08:47 +04:00
AndroidBridge : : CloseCamera ( )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jCloseCamera ) ;
2011-09-27 04:25:41 +04:00
}
2011-11-02 19:35:11 +04:00
void
AndroidBridge : : EnableBatteryNotifications ( )
{
ALOG_BRIDGE ( " AndroidBridge::EnableBatteryObserver " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jEnableBatteryNotifications ) ;
2011-11-02 19:35:11 +04:00
}
void
AndroidBridge : : DisableBatteryNotifications ( )
{
ALOG_BRIDGE ( " AndroidBridge::DisableBatteryNotifications " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jDisableBatteryNotifications ) ;
2011-11-02 19:35:11 +04:00
}
void
AndroidBridge : : GetCurrentBatteryInformation ( hal : : BatteryInformation * aBatteryInfo )
{
ALOG_BRIDGE ( " AndroidBridge::GetCurrentBatteryInformation " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2011-11-02 19:35:11 +04:00
// To prevent calling too many methods through JNI, the Java method returns
2011-11-09 12:56:37 +04:00
// an array of double even if we actually want a double and a boolean.
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetCurrentBatteryInformation ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2011-11-09 12:56:37 +04:00
jdoubleArray arr = static_cast < jdoubleArray > ( obj ) ;
2012-01-30 00:39:30 +04:00
if ( ! arr | | env - > GetArrayLength ( arr ) ! = 3 ) {
2011-11-02 19:35:11 +04:00
return ;
}
2012-01-30 00:39:30 +04:00
jdouble * info = env - > GetDoubleArrayElements ( arr , 0 ) ;
2011-11-02 19:35:11 +04:00
aBatteryInfo - > level ( ) = info [ 0 ] ;
aBatteryInfo - > charging ( ) = info [ 1 ] = = 1.0f ;
2011-11-09 18:13:37 +04:00
aBatteryInfo - > remainingTime ( ) = info [ 2 ] ;
2011-11-02 19:35:11 +04:00
2012-01-30 00:39:30 +04:00
env - > ReleaseDoubleArrayElements ( arr , info , 0 ) ;
2011-11-02 19:35:11 +04:00
}
2011-11-15 07:12:14 +04:00
void
AndroidBridge : : HandleGeckoMessage ( const nsAString & aMessage , nsAString & aRet )
{
ALOG_BRIDGE ( " %s " , __PRETTY_FUNCTION__ ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2011-11-15 07:12:14 +04:00
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jMessage = NewJavaString ( & jniFrame , aMessage ) ;
2011-11-15 07:12:14 +04:00
jstring returnMessage = static_cast < jstring > ( env - > CallStaticObjectMethod ( mGeckoAppShellClass , jHandleGeckoMessage , jMessage ) ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2011-11-15 07:12:14 +04:00
2012-04-17 01:25:04 +04:00
nsJNIString jniStr ( returnMessage , env ) ;
2011-11-15 07:12:14 +04:00
aRet . Assign ( jniStr ) ;
ALOG_BRIDGE ( " leaving %s " , __PRETTY_FUNCTION__ ) ;
}
void
AndroidBridge : : CheckURIVisited ( const nsAString & aURI )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
2012-07-02 21:54:10 +04:00
AutoLocalJNIFrame jniFrame ( env , 1 ) ;
2012-12-14 02:03:44 +04:00
jstring jstrURI = NewJavaString ( & jniFrame , aURI ) ;
2012-07-02 21:54:10 +04:00
// If creating the string fails, just bail
2012-07-02 23:12:50 +04:00
if ( jstrURI )
2012-07-02 21:54:10 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jCheckUriVisited , jstrURI ) ;
2011-11-15 07:12:14 +04:00
}
void
AndroidBridge : : MarkURIVisited ( const nsAString & aURI )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrURI = NewJavaString ( & jniFrame , aURI ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jMarkUriVisited , jstrURI ) ;
2011-11-02 19:35:11 +04:00
}
2013-02-18 21:27:30 +04:00
void
AndroidBridge : : SetURITitle ( const nsAString & aURI , const nsAString & aTitle )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
jstring jstrURI = NewJavaString ( & jniFrame , aURI ) ;
jstring jstrTitle = NewJavaString ( & jniFrame , aTitle ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jSetUriTitle , jstrURI , jstrTitle ) ;
}
2013-01-04 10:26:27 +04:00
nsresult
AndroidBridge : : GetSegmentInfoForText ( const nsAString & aText ,
2013-03-01 12:38:47 +04:00
dom : : mobilemessage : : SmsSegmentInfoData * aData )
2011-11-25 13:48:51 +04:00
{
2013-05-29 08:52:40 +04:00
# ifndef MOZ_WEBSMS_BACKEND
return NS_ERROR_FAILURE ;
# else
2013-01-04 10:26:27 +04:00
ALOG_BRIDGE ( " AndroidBridge::GetSegmentInfoForText " ) ;
aData - > segments ( ) = 0 ;
aData - > charsPerSegment ( ) = 0 ;
aData - > charsAvailableInLastSegment ( ) = 0 ;
2011-11-25 13:48:51 +04:00
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2013-01-04 10:26:27 +04:00
return NS_ERROR_FAILURE ;
2012-01-30 00:39:30 +04:00
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jText = NewJavaString ( & jniFrame , aText ) ;
2013-01-04 10:26:27 +04:00
jobject obj = env - > CallStaticObjectMethod ( mAndroidSmsMessageClass ,
jCalculateLength , jText , JNI_FALSE ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
2013-01-04 10:26:27 +04:00
return NS_ERROR_FAILURE ;
2012-05-04 19:08:47 +04:00
2013-01-04 10:26:27 +04:00
jintArray arr = static_cast < jintArray > ( obj ) ;
if ( ! arr | | env - > GetArrayLength ( arr ) ! = 4 )
return NS_ERROR_FAILURE ;
jint * info = env - > GetIntArrayElements ( arr , JNI_FALSE ) ;
aData - > segments ( ) = info [ 0 ] ; // msgCount
aData - > charsPerSegment ( ) = info [ 2 ] ; // codeUnitsRemaining
// segmentChars = (codeUnitCount + codeUnitsRemaining) / msgCount
aData - > charsAvailableInLastSegment ( ) = ( info [ 1 ] + info [ 2 ] ) / info [ 0 ] ;
env - > ReleaseIntArrayElements ( arr , info , JNI_ABORT ) ;
return NS_OK ;
2013-05-29 08:52:40 +04:00
# endif
2011-11-25 13:48:51 +04:00
}
2011-11-21 03:00:46 +04:00
void
2013-03-09 11:22:02 +04:00
AndroidBridge : : SendMessage ( const nsAString & aNumber ,
const nsAString & aMessage ,
nsIMobileMessageCallback * aRequest )
2011-11-21 03:00:46 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::SendMessage " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2013-01-25 23:00:00 +04:00
uint32_t requestId ;
if ( ! QueueSmsRequest ( aRequest , & requestId ) )
return ;
2012-11-07 01:32:07 +04:00
2012-01-30 00:39:30 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jNumber = NewJavaString ( & jniFrame , aNumber ) ;
jstring jMessage = NewJavaString ( & jniFrame , aMessage ) ;
2011-11-21 03:00:46 +04:00
2012-11-07 01:32:07 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jSendMessage , jNumber , jMessage , requestId ) ;
2011-11-21 03:00:46 +04:00
}
2011-12-23 02:08:58 +04:00
void
2013-03-09 11:22:02 +04:00
AndroidBridge : : GetMessage ( int32_t aMessageId , nsIMobileMessageCallback * aRequest )
2011-12-23 02:08:58 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::GetMessage " ) ;
2012-04-28 20:55:59 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
2012-01-30 00:39:30 +04:00
if ( ! env )
return ;
2013-01-25 23:00:00 +04:00
uint32_t requestId ;
if ( ! QueueSmsRequest ( aRequest , & requestId ) )
return ;
2012-11-07 01:32:07 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-11-07 01:32:07 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jGetMessage , aMessageId , requestId ) ;
2011-12-23 02:08:58 +04:00
}
2011-12-23 02:15:28 +04:00
void
2013-03-09 11:22:02 +04:00
AndroidBridge : : DeleteMessage ( int32_t aMessageId , nsIMobileMessageCallback * aRequest )
2011-12-23 02:15:28 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::DeleteMessage " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2013-01-25 23:00:00 +04:00
uint32_t requestId ;
if ( ! QueueSmsRequest ( aRequest , & requestId ) )
return ;
2012-11-07 01:32:07 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-11-07 01:32:07 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jDeleteMessage , aMessageId , requestId ) ;
2011-12-23 02:15:28 +04:00
}
2011-12-20 12:07:25 +04:00
void
2013-03-01 12:38:47 +04:00
AndroidBridge : : CreateMessageList ( const dom : : mobilemessage : : SmsFilterData & aFilter , bool aReverse ,
2013-03-09 11:22:02 +04:00
nsIMobileMessageCallback * aRequest )
2011-12-20 12:07:25 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::CreateMessageList " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2013-01-25 23:00:00 +04:00
uint32_t requestId ;
if ( ! QueueSmsRequest ( aRequest , & requestId ) )
return ;
2012-11-07 01:32:07 +04:00
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2011-12-20 12:07:25 +04:00
jobjectArray numbers =
2012-05-04 19:08:45 +04:00
( jobjectArray ) env - > NewObjectArray ( aFilter . numbers ( ) . Length ( ) ,
2012-07-02 23:32:34 +04:00
jStringClass ,
2012-12-14 02:03:44 +04:00
NewJavaString ( & jniFrame , EmptyString ( ) ) ) ;
2011-12-20 12:07:25 +04:00
2012-08-22 19:56:38 +04:00
for ( uint32_t i = 0 ; i < aFilter . numbers ( ) . Length ( ) ; + + i ) {
2012-05-04 19:08:45 +04:00
env - > SetObjectArrayElement ( numbers , i ,
2012-12-14 02:03:44 +04:00
NewJavaString ( & jniFrame , aFilter . numbers ( ) [ i ] ) ) ;
2011-12-20 12:07:25 +04:00
}
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jCreateMessageList ,
2012-07-02 23:32:34 +04:00
aFilter . startDate ( ) , aFilter . endDate ( ) ,
numbers , aFilter . numbers ( ) . Length ( ) ,
2012-11-07 01:32:07 +04:00
aFilter . delivery ( ) , aReverse , requestId ) ;
2011-12-20 12:07:25 +04:00
}
2011-12-23 02:16:59 +04:00
void
2013-03-09 11:22:02 +04:00
AndroidBridge : : GetNextMessageInList ( int32_t aListId , nsIMobileMessageCallback * aRequest )
2011-12-23 02:16:59 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::GetNextMessageInList " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2013-01-25 23:00:00 +04:00
uint32_t requestId ;
if ( ! QueueSmsRequest ( aRequest , & requestId ) )
return ;
2012-11-07 01:32:07 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-11-07 01:32:07 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jGetNextMessageinList , aListId , requestId ) ;
2011-12-23 02:16:59 +04:00
}
2012-01-17 22:43:08 +04:00
void
2012-08-22 19:56:38 +04:00
AndroidBridge : : ClearMessageList ( int32_t aListId )
2012-01-17 22:43:08 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::ClearMessageList " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jClearMessageList , aListId ) ;
2012-01-17 22:43:08 +04:00
}
2013-01-25 23:00:00 +04:00
bool
2013-03-09 11:22:02 +04:00
AndroidBridge : : QueueSmsRequest ( nsIMobileMessageCallback * aRequest , uint32_t * aRequestIdOut )
2012-11-07 01:32:07 +04:00
{
2013-01-25 23:00:00 +04:00
MOZ_ASSERT ( NS_IsMainThread ( ) , " Wrong thread! " ) ;
MOZ_ASSERT ( aRequest & & aRequestIdOut ) ;
2012-11-08 20:37:13 +04:00
2013-04-08 18:29:34 +04:00
const uint32_t length = mSmsRequests . Length ( ) ;
2013-01-25 23:00:00 +04:00
for ( uint32_t i = 0 ; i < length ; i + + ) {
2013-04-08 18:29:34 +04:00
if ( ! ( mSmsRequests ) [ i ] ) {
( mSmsRequests ) [ i ] = aRequest ;
2013-01-25 23:00:00 +04:00
* aRequestIdOut = i ;
return true ;
2012-11-07 01:32:07 +04:00
}
}
2013-04-08 18:29:34 +04:00
mSmsRequests . AppendElement ( aRequest ) ;
2012-11-07 01:32:07 +04:00
2013-01-25 23:00:00 +04:00
// After AppendElement(), previous `length` points to the new tail element.
* aRequestIdOut = length ;
return true ;
2012-11-07 01:32:07 +04:00
}
2013-03-09 11:22:02 +04:00
already_AddRefed < nsIMobileMessageCallback >
2013-01-25 23:00:00 +04:00
AndroidBridge : : DequeueSmsRequest ( uint32_t aRequestId )
2012-11-07 01:32:07 +04:00
{
2013-01-25 23:00:00 +04:00
MOZ_ASSERT ( NS_IsMainThread ( ) , " Wrong thread! " ) ;
2013-04-08 18:29:34 +04:00
MOZ_ASSERT ( aRequestId < mSmsRequests . Length ( ) ) ;
if ( aRequestId > = mSmsRequests . Length ( ) ) {
2012-11-07 01:32:07 +04:00
return nullptr ;
}
2013-04-08 18:29:34 +04:00
return mSmsRequests [ aRequestId ] . forget ( ) ;
2012-11-07 01:32:07 +04:00
}
2012-01-16 17:44:07 +04:00
void
AndroidBridge : : GetCurrentNetworkInformation ( hal : : NetworkInformation * aNetworkInfo )
{
ALOG_BRIDGE ( " AndroidBridge::GetCurrentNetworkInformation " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-07-02 23:32:34 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-01-16 17:44:07 +04:00
// To prevent calling too many methods through JNI, the Java method returns
2013-07-11 19:39:36 +04:00
// an array of double even if we actually want a double, two booleans, and an integer.
2012-01-30 00:39:30 +04:00
jobject obj = env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetCurrentNetworkInformation ) ;
2012-05-04 19:08:47 +04:00
if ( jniFrame . CheckForException ( ) )
return ;
2012-01-16 17:44:07 +04:00
jdoubleArray arr = static_cast < jdoubleArray > ( obj ) ;
2013-07-11 19:39:36 +04:00
if ( ! arr | | env - > GetArrayLength ( arr ) ! = 4 ) {
2012-01-16 17:44:07 +04:00
return ;
}
2012-01-30 00:39:30 +04:00
jdouble * info = env - > GetDoubleArrayElements ( arr , 0 ) ;
2012-01-16 17:44:07 +04:00
aNetworkInfo - > bandwidth ( ) = info [ 0 ] ;
aNetworkInfo - > canBeMetered ( ) = info [ 1 ] = = 1.0f ;
2013-07-11 19:39:36 +04:00
aNetworkInfo - > isWifi ( ) = info [ 2 ] = = 1.0f ;
aNetworkInfo - > dhcpGateway ( ) = info [ 3 ] ;
2012-01-16 17:44:07 +04:00
2012-01-30 00:39:30 +04:00
env - > ReleaseDoubleArrayElements ( arr , info , 0 ) ;
2012-01-16 17:44:07 +04:00
}
2012-01-16 18:01:07 +04:00
void
AndroidBridge : : EnableNetworkNotifications ( )
{
ALOG_BRIDGE ( " AndroidBridge::EnableNetworkNotifications " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jEnableNetworkNotifications ) ;
2012-01-16 18:01:07 +04:00
}
void
AndroidBridge : : DisableNetworkNotifications ( )
{
ALOG_BRIDGE ( " AndroidBridge::DisableNetworkNotifications " ) ;
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-01-30 00:39:30 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jDisableNetworkNotifications ) ;
2012-01-16 18:01:07 +04:00
}
2011-06-16 13:03:00 +04:00
void *
AndroidBridge : : LockBitmap ( jobject bitmap )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2012-07-30 18:20:58 +04:00
return nullptr ;
2012-01-30 00:39:30 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
int err ;
void * buf ;
2012-01-30 00:39:30 +04:00
if ( ( err = AndroidBitmap_lockPixels ( env , bitmap , & buf ) ) ! = 0 ) {
2011-06-16 13:03:00 +04:00
ALOG_BRIDGE ( " AndroidBitmap_lockPixels failed! (error %d) " , err ) ;
2012-07-30 18:20:58 +04:00
buf = nullptr ;
2011-06-16 13:03:00 +04:00
}
return buf ;
}
void
AndroidBridge : : UnlockBitmap ( jobject bitmap )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
int err ;
2012-01-30 00:39:30 +04:00
if ( ( err = AndroidBitmap_unlockPixels ( env , bitmap ) ) ! = 0 )
2011-06-16 13:03:00 +04:00
ALOG_BRIDGE ( " AndroidBitmap_unlockPixels failed! (error %d) " , err ) ;
}
2011-09-21 20:46:00 +04:00
bool
AndroidBridge : : HasNativeWindowAccess ( )
{
OpenGraphicsLibraries ( ) ;
2012-04-28 00:04:47 +04:00
// We have a fallback hack in place, so return true if that will work as well
return mHasNativeWindowAccess | | mHasNativeWindowFallback ;
2011-09-21 20:46:00 +04:00
}
void *
2012-04-28 00:04:47 +04:00
AndroidBridge : : AcquireNativeWindow ( JNIEnv * aEnv , jobject aSurface )
2011-09-21 20:46:00 +04:00
{
2012-04-28 00:04:47 +04:00
OpenGraphicsLibraries ( ) ;
2011-09-21 20:46:00 +04:00
2012-04-28 00:04:47 +04:00
if ( mHasNativeWindowAccess )
return ANativeWindow_fromSurface ( aEnv , aSurface ) ;
2012-07-20 23:20:51 +04:00
if ( mHasNativeWindowFallback )
2012-04-28 00:04:47 +04:00
return GetNativeSurface ( aEnv , aSurface ) ;
2012-07-20 23:20:51 +04:00
2012-07-30 18:20:58 +04:00
return nullptr ;
2011-09-21 20:46:00 +04:00
}
void
AndroidBridge : : ReleaseNativeWindow ( void * window )
{
if ( ! window )
return ;
2012-04-28 00:04:47 +04:00
if ( mHasNativeWindowAccess )
ANativeWindow_release ( window ) ;
// XXX: we don't ref the pointer we get from the fallback (GetNativeSurface), so we
// have nothing to do here. We should probably ref it.
2011-09-21 20:46:00 +04:00
}
2012-07-20 23:20:51 +04:00
void *
AndroidBridge : : AcquireNativeWindowFromSurfaceTexture ( JNIEnv * aEnv , jobject aSurfaceTexture )
{
OpenGraphicsLibraries ( ) ;
if ( mHasNativeWindowAccess & & ANativeWindow_fromSurfaceTexture )
return ANativeWindow_fromSurfaceTexture ( aEnv , aSurfaceTexture ) ;
if ( mHasNativeWindowAccess & & android_SurfaceTexture_getNativeWindow ) {
android : : sp < AndroidRefable > window = android_SurfaceTexture_getNativeWindow ( aEnv , aSurfaceTexture ) ;
return window . get ( ) ;
}
2012-07-30 18:20:58 +04:00
return nullptr ;
2012-07-20 23:20:51 +04:00
}
void
AndroidBridge : : ReleaseNativeWindowForSurfaceTexture ( void * window )
{
if ( ! window )
return ;
// FIXME: we don't ref the pointer we get, so nothing to do currently. We should ref it.
}
2011-09-21 20:46:00 +04:00
bool
AndroidBridge : : LockWindow ( void * window , unsigned char * * bits , int * width , int * height , int * format , int * stride )
{
/* Copied from native_window.h in Android NDK (platform-9) */
typedef struct ANativeWindow_Buffer {
// The number of pixels that are show horizontally.
int32_t width ;
// The number of pixels that are shown vertically.
int32_t height ;
// The number of *pixels* that a line in the buffer takes in
// memory. This may be >= width.
int32_t stride ;
// The format of the buffer. One of WINDOW_FORMAT_*
int32_t format ;
// The actual bits.
void * bits ;
// Do not touch.
uint32_t reserved [ 6 ] ;
} ANativeWindow_Buffer ;
2012-04-28 00:04:47 +04:00
// Very similar to the above, but the 'usage' field is included. We use this
// in the fallback case when NDK support is not available
2012-07-12 20:52:52 +04:00
struct SurfaceInfo {
2012-04-28 00:04:47 +04:00
uint32_t w ;
uint32_t h ;
uint32_t s ;
uint32_t usage ;
uint32_t format ;
unsigned char * bits ;
uint32_t reserved [ 2 ] ;
} ;
2011-09-21 20:46:00 +04:00
2012-04-28 00:04:47 +04:00
int err ;
2011-09-21 20:46:00 +04:00
* bits = NULL ;
* width = * height = * format = 0 ;
2012-07-02 23:32:34 +04:00
2012-04-28 00:04:47 +04:00
if ( mHasNativeWindowAccess ) {
ANativeWindow_Buffer buffer ;
2011-09-21 20:46:00 +04:00
2012-04-28 00:04:47 +04:00
if ( ( err = ANativeWindow_lock ( window , ( void * ) & buffer , NULL ) ) ! = 0 ) {
ALOG_BRIDGE ( " ANativeWindow_lock failed! (error %d) " , err ) ;
return false ;
}
* bits = ( unsigned char * ) buffer . bits ;
* width = buffer . width ;
* height = buffer . height ;
* format = buffer . format ;
* stride = buffer . stride ;
} else if ( mHasNativeWindowFallback ) {
SurfaceInfo info ;
if ( ( err = Surface_lock ( window , & info , NULL , true ) ) ! = 0 ) {
ALOG_BRIDGE ( " Surface_lock failed! (error %d) " , err ) ;
return false ;
}
* bits = info . bits ;
* width = info . w ;
* height = info . h ;
* format = info . format ;
* stride = info . s ;
} else return false ;
2011-09-21 20:46:00 +04:00
return true ;
}
2013-02-10 01:38:08 +04:00
jobject
2013-05-28 20:49:04 +04:00
AndroidBridge : : GetContext ( ) {
2013-02-10 01:38:08 +04:00
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return 0 ;
jobject context = env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetContext ) ;
2013-05-28 20:49:04 +04:00
if ( env - > ExceptionCheck ( ) ) {
env - > ExceptionDescribe ( ) ;
env - > ExceptionClear ( ) ;
2013-02-10 01:38:08 +04:00
return 0 ;
}
2013-05-28 20:49:04 +04:00
return context ;
}
jobject
AndroidBridge : : GetGlobalContextRef ( ) {
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return 0 ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
jobject context = GetContext ( ) ;
2013-02-10 01:38:08 +04:00
jobject globalRef = env - > NewGlobalRef ( context ) ;
MOZ_ASSERT ( globalRef ) ;
return globalRef ;
}
2011-09-21 20:46:00 +04:00
bool
AndroidBridge : : UnlockWindow ( void * window )
{
int err ;
2012-04-28 00:04:47 +04:00
if ( ! HasNativeWindowAccess ( ) )
return false ;
if ( mHasNativeWindowAccess & & ( err = ANativeWindow_unlockAndPost ( window ) ) ! = 0 ) {
2011-09-21 20:46:00 +04:00
ALOG_BRIDGE ( " ANativeWindow_unlockAndPost failed! (error %d) " , err ) ;
return false ;
2012-04-28 00:04:47 +04:00
} else if ( mHasNativeWindowFallback & & ( err = Surface_unlockAndPost ( window ) ) ! = 0 ) {
ALOG_BRIDGE ( " Surface_unlockAndPost failed! (error %d) " , err ) ;
return false ;
2011-09-21 20:46:00 +04:00
}
return true ;
}
2011-10-11 04:40:17 +04:00
2011-12-15 01:53:38 +04:00
bool
AndroidBridge : : IsTablet ( )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
bool ret = env - > CallStaticBooleanMethod ( mGeckoAppShellClass , jIsTablet ) ;
if ( jniFrame . CheckForException ( ) )
return false ;
return ret ;
2011-12-15 01:53:38 +04:00
}
2012-03-12 19:50:21 +04:00
void
2013-06-22 01:03:56 +04:00
AndroidBridge : : SetFirstPaintViewport ( const LayerIntPoint & aOffset , const CSSToLayerScale & aZoom , const CSSRect & aCssPageRect )
2012-03-12 19:50:21 +04:00
{
AndroidGeckoLayerClient * client = mLayerClient ;
if ( ! client )
return ;
2013-06-11 17:46:51 +04:00
client - > SetFirstPaintViewport ( aOffset , aZoom , aCssPageRect ) ;
2012-03-12 19:50:21 +04:00
}
void
2013-06-03 17:52:44 +04:00
AndroidBridge : : SetPageRect ( const CSSRect & aCssPageRect )
2012-03-12 19:50:21 +04:00
{
AndroidGeckoLayerClient * client = mLayerClient ;
if ( ! client )
return ;
2012-06-13 21:49:40 +04:00
client - > SetPageRect ( aCssPageRect ) ;
2012-03-12 19:50:21 +04:00
}
2012-02-04 06:35:58 +04:00
void
2013-06-22 01:03:56 +04:00
AndroidBridge : : SyncViewportInfo ( const LayerIntRect & aDisplayPort , const CSSToLayerScale & aDisplayResolution ,
bool aLayersUpdated , ScreenPoint & aScrollOffset , CSSToScreenScale & aScale ,
2013-07-22 12:50:13 +04:00
LayerMargin & aFixedLayerMargins , ScreenPoint & aOffset )
2012-02-04 06:35:58 +04:00
{
2012-03-14 08:15:11 +04:00
AndroidGeckoLayerClient * client = mLayerClient ;
if ( ! client )
return ;
2013-03-07 14:17:33 +04:00
client - > SyncViewportInfo ( aDisplayPort , aDisplayResolution , aLayersUpdated ,
2013-06-22 01:03:56 +04:00
aScrollOffset , aScale , aFixedLayerMargins ,
2013-05-01 22:12:08 +04:00
aOffset ) ;
2012-02-04 06:35:58 +04:00
}
2013-06-15 00:11:29 +04:00
void AndroidBridge : : SyncFrameMetrics ( const ScreenPoint & aScrollOffset , float aZoom , const CSSRect & aCssPageRect ,
2013-06-22 01:03:56 +04:00
bool aLayersUpdated , const CSSRect & aDisplayPort , const CSSToLayerScale & aDisplayResolution ,
2013-07-22 12:50:13 +04:00
bool aIsFirstPaint , LayerMargin & aFixedLayerMargins , ScreenPoint & aOffset )
2013-04-26 21:26:39 +04:00
{
AndroidGeckoLayerClient * client = mLayerClient ;
if ( ! client )
return ;
2013-05-01 22:12:08 +04:00
client - > SyncFrameMetrics ( aScrollOffset , aZoom , aCssPageRect ,
2013-04-26 21:26:39 +04:00
aLayersUpdated , aDisplayPort , aDisplayResolution ,
2013-05-01 22:12:08 +04:00
aIsFirstPaint , aFixedLayerMargins , aOffset ) ;
2013-04-26 21:26:39 +04:00
}
2012-07-14 01:34:58 +04:00
AndroidBridge : : AndroidBridge ( )
2013-04-26 21:24:28 +04:00
: mLayerClient ( NULL ) ,
mNativePanZoomController ( NULL )
2012-07-14 01:34:58 +04:00
{
}
AndroidBridge : : ~ AndroidBridge ( )
{
}
2011-10-11 04:40:17 +04:00
/* Implementation file */
NS_IMPL_ISUPPORTS1 ( nsAndroidBridge , nsIAndroidBridge )
nsAndroidBridge : : nsAndroidBridge ( )
{
}
nsAndroidBridge : : ~ nsAndroidBridge ( )
{
}
/* void handleGeckoEvent (in AString message); */
2012-07-07 00:14:07 +04:00
NS_IMETHODIMP nsAndroidBridge : : HandleGeckoMessage ( const nsAString & message , nsAString & aRet )
2011-10-11 04:40:17 +04:00
{
2011-10-24 21:05:18 +04:00
AndroidBridge : : Bridge ( ) - > HandleGeckoMessage ( message , aRet ) ;
2011-10-11 04:40:17 +04:00
return NS_OK ;
}
2011-11-18 07:20:29 +04:00
2013-05-30 17:55:08 +04:00
/* nsIAndroidDisplayport getDisplayPort(in boolean aPageSizeUpdate, in boolean isBrowserContentDisplayed, in int32_t tabId, in nsIAndroidViewport metrics); */
NS_IMETHODIMP nsAndroidBridge : : GetDisplayPort ( bool aPageSizeUpdate , bool aIsBrowserContentDisplayed , int32_t tabId , nsIAndroidViewport * metrics , nsIAndroidDisplayport * * displayPort )
{
AndroidBridge : : Bridge ( ) - > GetDisplayPort ( aPageSizeUpdate , aIsBrowserContentDisplayed , tabId , metrics , displayPort ) ;
return NS_OK ;
}
2013-05-30 17:55:23 +04:00
/* void displayedDocumentChanged(); */
NS_IMETHODIMP nsAndroidBridge : : ContentDocumentChanged ( )
{
AndroidBridge : : Bridge ( ) - > ContentDocumentChanged ( ) ;
return NS_OK ;
}
/* boolean isContentDocumentDisplayed(); */
NS_IMETHODIMP nsAndroidBridge : : IsContentDocumentDisplayed ( bool * aRet )
{
* aRet = AndroidBridge : : Bridge ( ) - > IsContentDocumentDisplayed ( ) ;
return NS_OK ;
}
2012-01-25 04:31:33 +04:00
void
2012-05-04 19:08:47 +04:00
AndroidBridge : : NotifyDefaultPrevented ( bool aDefaultPrevented )
{
2012-01-30 00:39:30 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-04-07 11:09:26 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jNotifyDefaultPrevented , ( jboolean ) aDefaultPrevented ) ;
2012-01-25 04:31:33 +04:00
}
2012-01-30 00:39:30 +04:00
// DO NOT USE THIS unless you need to access JNI from
// non-main threads. This is probably not what you want.
// Questions, ask blassey or dougt.
static void
JavaThreadDetachFunc ( void * arg )
{
JNIEnv * env = ( JNIEnv * ) arg ;
JavaVM * vm = NULL ;
env - > GetJavaVM ( & vm ) ;
vm - > DetachCurrentThread ( ) ;
}
extern " C " {
__attribute__ ( ( visibility ( " default " ) ) )
JNIEnv * GetJNIForThread ( )
{
JNIEnv * jEnv = NULL ;
JavaVM * jVm = mozilla : : AndroidBridge : : GetVM ( ) ;
if ( ! jVm ) {
__android_log_print ( ANDROID_LOG_INFO , " GetJNIForThread " , " Returned a null VM " ) ;
return NULL ;
}
2012-07-17 18:25:48 +04:00
jEnv = static_cast < JNIEnv * > ( PR_GetThreadPrivate ( sJavaEnvThreadIndex ) ) ;
if ( jEnv )
return jEnv ;
2012-01-30 00:39:30 +04:00
int status = jVm - > GetEnv ( ( void * * ) & jEnv , JNI_VERSION_1_2 ) ;
2012-07-17 18:25:48 +04:00
if ( status ) {
2012-01-30 00:39:30 +04:00
status = jVm - > AttachCurrentThread ( & jEnv , NULL ) ;
2012-07-17 18:25:48 +04:00
if ( status ) {
2012-01-30 00:39:30 +04:00
__android_log_print ( ANDROID_LOG_INFO , " GetJNIForThread " , " Could not attach " ) ;
return NULL ;
}
2012-07-17 18:25:48 +04:00
2012-01-30 00:39:30 +04:00
PR_SetThreadPrivate ( sJavaEnvThreadIndex , jEnv ) ;
}
if ( ! jEnv ) {
__android_log_print ( ANDROID_LOG_INFO , " GetJNIForThread " , " returning NULL " ) ;
}
return jEnv ;
}
}
2012-01-31 18:40:58 +04:00
2012-05-09 01:36:07 +04:00
uint32_t
AndroidBridge : : GetScreenOrientation ( )
2012-03-20 17:09:45 +04:00
{
ALOG_BRIDGE ( " AndroidBridge::GetScreenOrientation " ) ;
2012-05-04 19:08:47 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2012-05-09 01:36:07 +04:00
return dom : : eScreenOrientation_None ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
jshort orientation = env - > CallStaticShortMethod ( mGeckoAppShellClass , jGetScreenOrientation ) ;
if ( jniFrame . CheckForException ( ) )
2012-05-09 01:36:07 +04:00
return dom : : eScreenOrientation_None ;
2012-05-04 19:08:47 +04:00
2012-05-09 01:36:07 +04:00
return static_cast < dom : : ScreenOrientation > ( orientation ) ;
2012-03-20 17:09:45 +04:00
}
void
AndroidBridge : : EnableScreenOrientationNotifications ( )
{
ALOG_BRIDGE ( " AndroidBridge::EnableScreenOrientationNotifications " ) ;
2012-05-04 19:08:47 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jEnableScreenOrientationNotifications ) ;
2012-03-20 17:09:45 +04:00
}
void
AndroidBridge : : DisableScreenOrientationNotifications ( )
{
ALOG_BRIDGE ( " AndroidBridge::DisableScreenOrientationNotifications " ) ;
2012-05-04 19:08:47 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jDisableScreenOrientationNotifications ) ;
2012-03-20 17:09:45 +04:00
}
2012-03-30 10:31:12 +04:00
void
2012-05-09 01:36:07 +04:00
AndroidBridge : : LockScreenOrientation ( uint32_t aOrientation )
2012-03-30 10:31:12 +04:00
{
2012-05-04 19:08:45 +04:00
ALOG_BRIDGE ( " AndroidBridge::LockScreenOrientation " ) ;
2012-05-04 19:08:47 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-05-09 01:36:07 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jLockScreenOrientation , aOrientation ) ;
2012-03-30 10:31:12 +04:00
}
void
AndroidBridge : : UnlockScreenOrientation ( )
{
2012-05-04 19:08:45 +04:00
ALOG_BRIDGE ( " AndroidBridge::UnlockScreenOrientation " ) ;
2012-05-04 19:08:47 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jUnlockScreenOrientation ) ;
2012-03-30 10:31:12 +04:00
}
2012-02-04 10:48:26 +04:00
2012-06-05 09:14:12 +04:00
bool
2012-05-03 18:53:31 +04:00
AndroidBridge : : PumpMessageLoop ( )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2012-06-05 09:14:12 +04:00
return false ;
2012-05-03 18:53:31 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-05-03 18:53:31 +04:00
if ( ( void * ) pthread_self ( ) ! = mThread )
2012-06-05 09:14:12 +04:00
return false ;
2012-05-03 18:53:31 +04:00
2012-06-05 09:14:12 +04:00
return env - > CallStaticBooleanMethod ( mGeckoAppShellClass , jPumpMessageLoop ) ;
2012-05-03 18:53:31 +04:00
}
2012-06-07 06:39:01 +04:00
void
AndroidBridge : : NotifyWakeLockChanged ( const nsAString & topic , const nsAString & state )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jstrTopic = NewJavaString ( & jniFrame , topic ) ;
jstring jstrState = NewJavaString ( & jniFrame , state ) ;
2012-06-07 06:39:01 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jNotifyWakeLockChanged , jstrTopic , jstrState ) ;
}
2012-07-20 23:20:51 +04:00
void
AndroidBridge : : ScheduleComposite ( )
{
nsWindow : : ScheduleComposite ( ) ;
}
void
AndroidBridge : : RegisterSurfaceTextureFrameListener ( jobject surfaceTexture , int id )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jRegisterSurfaceTextureFrameListener , surfaceTexture , id ) ;
}
void
AndroidBridge : : UnregisterSurfaceTextureFrameListener ( jobject surfaceTexture )
{
2013-04-19 03:35:41 +04:00
// This function is called on a worker thread when the Flash plugin is unloaded.
JNIEnv * env = GetJNIForThread ( ) ;
2012-07-20 23:20:51 +04:00
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jUnregisterSurfaceTextureFrameListener , surfaceTexture ) ;
}
2012-07-05 18:12:10 +04:00
void
AndroidBridge : : GetGfxInfoData ( nsACString & aRet )
{
ALOG_BRIDGE ( " AndroidBridge::GetGfxInfoData " ) ;
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
jstring jstrRet = static_cast < jstring >
( env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetGfxInfoData ) ) ;
if ( jniFrame . CheckForException ( ) )
return ;
nsJNIString jniStr ( jstrRet , env ) ;
CopyUTF16toUTF8 ( jniStr , aRet ) ;
}
2012-11-13 23:25:18 +04:00
nsresult
AndroidBridge : : GetProxyForURI ( const nsACString & aSpec ,
const nsACString & aScheme ,
const nsACString & aHost ,
const int32_t aPort ,
nsACString & aResult )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return NS_ERROR_FAILURE ;
AutoLocalJNIFrame jniFrame ( env ) ;
2012-12-14 02:03:44 +04:00
jstring jSpec = NewJavaString ( & jniFrame , aSpec ) ;
jstring jScheme = NewJavaString ( & jniFrame , aScheme ) ;
jstring jHost = NewJavaString ( & jniFrame , aHost ) ;
2012-11-13 23:25:18 +04:00
jstring jstrRet = static_cast < jstring >
( env - > CallStaticObjectMethod ( mGeckoAppShellClass , jGetProxyForURI , jSpec , jScheme , jHost , aPort ) ) ;
if ( jniFrame . CheckForException ( ) )
return NS_ERROR_FAILURE ;
nsJNIString jniStr ( jstrRet , env ) ;
CopyUTF16toUTF8 ( jniStr , aResult ) ;
return NS_OK ;
}
2012-02-17 22:52:26 +04:00
/* attribute nsIAndroidBrowserApp browserApp; */
NS_IMETHODIMP nsAndroidBridge : : GetBrowserApp ( nsIAndroidBrowserApp * * aBrowserApp )
{
if ( nsAppShell : : gAppShell )
nsAppShell : : gAppShell - > GetBrowserApp ( aBrowserApp ) ;
return NS_OK ;
}
2012-05-04 19:08:47 +04:00
2012-02-17 22:52:26 +04:00
NS_IMETHODIMP nsAndroidBridge : : SetBrowserApp ( nsIAndroidBrowserApp * aBrowserApp )
{
if ( nsAppShell : : gAppShell )
nsAppShell : : gAppShell - > SetBrowserApp ( aBrowserApp ) ;
return NS_OK ;
}
2012-03-27 09:31:57 +04:00
void
2013-08-13 19:49:21 +04:00
AndroidBridge : : AddPluginView ( jobject view , const LayoutDeviceRect & rect , bool isFullScreen ) {
2012-05-04 19:08:45 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
2012-04-28 00:04:47 +04:00
if ( ! env )
return ;
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env ) ;
2012-04-28 00:04:47 +04:00
2013-08-13 19:49:21 +04:00
nsWindow * win = nsWindow : : TopWindow ( ) ;
if ( ! win )
return ;
CSSRect cssRect = rect / CSSToLayoutDeviceScale ( win - > GetDefaultScale ( ) ) ;
2012-04-28 00:04:47 +04:00
env - > CallStaticVoidMethod ( sBridge - > mGeckoAppShellClass ,
sBridge - > jAddPluginView , view ,
2013-08-13 19:49:21 +04:00
cssRect . x , cssRect . y , cssRect . width , cssRect . height ,
2012-06-07 06:39:36 +04:00
isFullScreen ) ;
2012-04-28 00:04:47 +04:00
}
void
2012-05-30 20:10:49 +04:00
AndroidBridge : : RemovePluginView ( jobject view , bool isFullScreen )
2012-05-04 19:08:47 +04:00
{
2012-05-04 19:08:45 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
2012-03-27 09:31:57 +04:00
2012-05-04 19:08:47 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-05-30 20:10:49 +04:00
env - > CallStaticVoidMethod ( mGeckoAppShellClass , jRemovePluginView , view , isFullScreen ) ;
2012-03-27 09:31:57 +04:00
}
2012-02-17 22:52:26 +04:00
extern " C "
__attribute__ ( ( visibility ( " default " ) ) )
jobject JNICALL
2012-05-04 19:08:47 +04:00
Java_org_mozilla_gecko_GeckoAppShell_allocateDirectBuffer ( JNIEnv * env , jclass , jlong size ) ;
2012-02-17 22:52:26 +04:00
2013-04-23 21:10:29 +04:00
void
AndroidBridge : : StartJavaProfiling ( int aInterval , int aSamples )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
env - > CallStaticVoidMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jStart ,
aInterval , aSamples ) ;
}
void
AndroidBridge : : StopJavaProfiling ( )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
env - > CallStaticVoidMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jStop ) ;
}
void
AndroidBridge : : PauseJavaProfiling ( )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
env - > CallStaticVoidMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jPause ) ;
}
void
AndroidBridge : : UnpauseJavaProfiling ( )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env ) ;
env - > CallStaticVoidMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jUnpause ) ;
}
bool
AndroidBridge : : GetThreadNameJavaProfiling ( uint32_t aThreadId , nsCString & aResult )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return false ;
AutoLocalJNIFrame jniFrame ( env ) ;
jstring jstrThreadName = static_cast < jstring > (
env - > CallStaticObjectMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jGetThreadName ,
aThreadId ) ) ;
if ( ! jstrThreadName )
return false ;
nsJNIString jniStr ( jstrThreadName , env ) ;
CopyUTF16toUTF8 ( jniStr . get ( ) , aResult ) ;
return true ;
}
bool
AndroidBridge : : GetFrameNameJavaProfiling ( uint32_t aThreadId , uint32_t aSampleId ,
uint32_t aFrameId , nsCString & aResult )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return false ;
AutoLocalJNIFrame jniFrame ( env ) ;
jstring jstrSampleName = static_cast < jstring > (
env - > CallStaticObjectMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jGetFrameName ,
aThreadId , aSampleId , aFrameId ) ) ;
if ( ! jstrSampleName )
return false ;
nsJNIString jniStr ( jstrSampleName , env ) ;
CopyUTF16toUTF8 ( jniStr . get ( ) , aResult ) ;
return true ;
}
double
AndroidBridge : : GetSampleTimeJavaProfiling ( uint32_t aThreadId , uint32_t aSampleId )
{
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env )
return 0 ;
AutoLocalJNIFrame jniFrame ( env ) ;
jdouble jSampleTime =
env - > CallStaticDoubleMethod ( AndroidBridge : : Bridge ( ) - > jGeckoJavaSamplerClass ,
AndroidBridge : : Bridge ( ) - > jGetSampleTime ,
aThreadId , aSampleId ) ;
return jSampleTime ;
}
2012-02-17 22:52:26 +04:00
2013-01-23 21:18:05 +04:00
void
AndroidBridge : : SendThumbnail ( jobject buffer , int32_t tabId , bool success ) {
// Regardless of whether we successfully captured a thumbnail, we need to
// send a response to process the remaining entries in the queue. If we
// don't get an env here, we'll stall the thumbnail loop, but there isn't
// much we can do about it.
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallStaticVoidMethod ( AndroidBridge : : Bridge ( ) - > jThumbnailHelperClass ,
AndroidBridge : : Bridge ( ) - > jNotifyThumbnail ,
buffer , tabId , success ) ;
}
2012-12-15 19:43:52 +04:00
nsresult AndroidBridge : : CaptureThumbnail ( nsIDOMWindow * window , int32_t bufW , int32_t bufH , int32_t tabId , jobject buffer )
2012-02-04 10:48:26 +04:00
{
2012-05-04 03:29:59 +04:00
nsresult rv ;
2012-06-14 20:08:51 +04:00
float scale = 1.0 ;
2012-07-02 23:32:34 +04:00
2012-06-14 20:08:51 +04:00
if ( ! buffer )
2013-01-23 21:18:05 +04:00
return NS_ERROR_FAILURE ;
2012-05-04 03:29:59 +04:00
// take a screenshot, as wide as possible, proportional to the destination size
2012-12-15 19:43:52 +04:00
nsCOMPtr < nsIDOMWindowUtils > utils = do_GetInterface ( window ) ;
if ( ! utils )
return NS_ERROR_FAILURE ;
nsCOMPtr < nsIDOMClientRect > rect ;
rv = utils - > GetRootBounds ( getter_AddRefs ( rect ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! rect )
return NS_ERROR_FAILURE ;
2012-06-02 07:50:57 +04:00
2012-12-15 19:43:52 +04:00
float left , top , width , height ;
rect - > GetLeft ( & left ) ;
rect - > GetTop ( & top ) ;
rect - > GetWidth ( & width ) ;
rect - > GetHeight ( & height ) ;
if ( width = = 0 | | height = = 0 )
return NS_ERROR_FAILURE ;
int32_t srcX = left ;
int32_t srcY = top ;
int32_t srcW ;
int32_t srcH ;
float aspectRatio = ( ( float ) bufW ) / bufH ;
if ( width / aspectRatio < height ) {
srcW = width ;
srcH = width / aspectRatio ;
} else {
srcW = height * aspectRatio ;
srcH = height ;
2012-05-04 03:29:59 +04:00
}
2012-05-04 19:08:47 +04:00
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
2013-01-23 21:18:05 +04:00
return NS_ERROR_FAILURE ;
2012-05-04 19:08:47 +04:00
2013-01-23 21:18:05 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
2012-05-04 19:08:47 +04:00
2012-02-04 10:48:26 +04:00
nsCOMPtr < nsPIDOMWindow > win = do_QueryInterface ( window ) ;
if ( ! win )
return NS_ERROR_FAILURE ;
nsRefPtr < nsPresContext > presContext ;
nsIDocShell * docshell = win - > GetDocShell ( ) ;
if ( docshell ) {
docshell - > GetPresContext ( getter_AddRefs ( presContext ) ) ;
}
if ( ! presContext )
return NS_ERROR_FAILURE ;
nscolor bgColor = NS_RGB ( 255 , 255 , 255 ) ;
2012-10-27 05:02:57 +04:00
nsCOMPtr < nsIPresShell > presShell = presContext - > PresShell ( ) ;
2012-08-22 19:56:38 +04:00
uint32_t renderDocFlags = ( nsIPresShell : : RENDER_IGNORE_VIEWPORT_SCROLLING |
2012-02-04 10:48:26 +04:00
nsIPresShell : : RENDER_DOCUMENT_RELATIVE ) ;
2012-03-15 08:43:53 +04:00
nsRect r ( nsPresContext : : CSSPixelsToAppUnits ( srcX / scale ) ,
nsPresContext : : CSSPixelsToAppUnits ( srcY / scale ) ,
nsPresContext : : CSSPixelsToAppUnits ( srcW / scale ) ,
nsPresContext : : CSSPixelsToAppUnits ( srcH / scale ) ) ;
2012-02-04 10:48:26 +04:00
2013-07-04 17:53:25 +04:00
bool is24bit = ( GetScreenDepth ( ) = = 24 ) ;
uint32_t stride = bufW * ( is24bit ? 4 : 2 ) ;
2012-02-17 22:52:26 +04:00
2012-05-04 19:08:47 +04:00
void * data = env - > GetDirectBufferAddress ( buffer ) ;
2012-09-25 23:46:17 +04:00
if ( ! data )
return NS_ERROR_FAILURE ;
2013-07-04 17:53:25 +04:00
nsRefPtr < gfxImageSurface > surf =
new gfxImageSurface ( static_cast < unsigned char * > ( data ) , nsIntSize ( bufW , bufH ) , stride ,
is24bit ? gfxASurface : : ImageFormatRGB24 :
gfxASurface : : ImageFormatRGB16_565 ) ;
2012-08-14 04:01:31 +04:00
if ( surf - > CairoStatus ( ) ! = 0 ) {
ALOG_BRIDGE ( " Error creating gfxImageSurface " ) ;
return NS_ERROR_FAILURE ;
}
2012-02-04 10:48:26 +04:00
nsRefPtr < gfxContext > context = new gfxContext ( surf ) ;
2012-12-15 19:43:52 +04:00
gfxPoint pt ( 0 , 0 ) ;
2012-06-14 20:08:51 +04:00
context - > Translate ( pt ) ;
2012-12-15 19:43:52 +04:00
context - > Scale ( scale * bufW / srcW , scale * bufH / srcH ) ;
2012-05-04 03:29:59 +04:00
rv = presShell - > RenderDocument ( r , renderDocFlags , bgColor , context ) ;
2013-07-24 17:18:05 +04:00
if ( is24bit ) {
gfxUtils : : ConvertBGRAtoRGBA ( surf ) ;
}
2012-02-04 10:48:26 +04:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
2012-04-24 23:13:36 +04:00
2013-05-30 17:55:08 +04:00
void
2012-09-13 01:47:08 +04:00
AndroidBridge : : GetDisplayPort ( bool aPageSizeUpdate , bool aIsBrowserContentDisplayed , int32_t tabId , nsIAndroidViewport * metrics , nsIAndroidDisplayport * * displayPort )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env | | ! mLayerClient )
2013-05-30 17:55:08 +04:00
return ;
2012-09-13 01:47:08 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
mLayerClient - > GetDisplayPort ( & jniFrame , aPageSizeUpdate , aIsBrowserContentDisplayed , tabId , metrics , displayPort ) ;
}
2013-05-30 17:55:23 +04:00
void
AndroidBridge : : ContentDocumentChanged ( )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env | | ! mLayerClient )
return ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
mLayerClient - > ContentDocumentChanged ( & jniFrame ) ;
}
bool
AndroidBridge : : IsContentDocumentDisplayed ( )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env | | ! mLayerClient )
return false ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
return mLayerClient - > IsContentDocumentDisplayed ( & jniFrame ) ;
}
2012-10-04 22:45:16 +04:00
bool
2013-06-10 17:05:42 +04:00
AndroidBridge : : ProgressiveUpdateCallback ( bool aHasPendingNewThebesContent , const LayerRect & aDisplayPort , float aDisplayResolution , bool aDrawingCritical , gfx : : Rect & aViewport , float & aScaleX , float & aScaleY )
2012-10-04 22:45:16 +04:00
{
2012-10-05 18:41:03 +04:00
AndroidGeckoLayerClient * client = mLayerClient ;
if ( ! client )
2012-10-04 22:45:16 +04:00
return false ;
2012-11-22 02:34:19 +04:00
return client - > ProgressiveUpdateCallback ( aHasPendingNewThebesContent , aDisplayPort , aDisplayResolution , aDrawingCritical , aViewport , aScaleX , aScaleY ) ;
2012-10-04 22:45:16 +04:00
}
2012-10-05 23:35:26 +04:00
void
AndroidBridge : : KillAnyZombies ( )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return ;
env - > CallStaticVoidMethod ( mGeckoAppShellClass , AndroidBridge : : Bridge ( ) - > jKillAnyZombies ) ;
}
bool
AndroidBridge : : UnlockProfile ( )
{
JNIEnv * env = GetJNIEnv ( ) ;
if ( ! env )
return false ;
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
bool ret = env - > CallStaticBooleanMethod ( mGeckoAppShellClass , jUnlockProfile ) ;
if ( jniFrame . CheckForException ( ) )
return false ;
return ret ;
}
2013-04-26 21:24:28 +04:00
jobject
AndroidBridge : : SetNativePanZoomController ( jobject obj )
{
jobject old = mNativePanZoomController ;
mNativePanZoomController = obj ;
return old ;
}
void
AndroidBridge : : RequestContentRepaint ( const mozilla : : layers : : FrameMetrics & aFrameMetrics )
{
2013-04-26 21:26:46 +04:00
ALOG_BRIDGE ( " AndroidBridge::RequestContentRepaint " ) ;
JNIEnv * env = GetJNIForThread ( ) ; // called on the compositor thread
if ( ! env | | ! mNativePanZoomController ) {
return ;
}
2013-06-26 17:54:14 +04:00
CSSToScreenScale resolution = aFrameMetrics . CalculateResolution ( ) ;
2013-06-15 00:11:44 +04:00
ScreenRect dp = ( aFrameMetrics . mDisplayPort + aFrameMetrics . mScrollOffset ) * resolution ;
2013-04-26 21:26:46 +04:00
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallVoidMethod ( mNativePanZoomController , jRequestContentRepaint ,
2013-06-15 00:11:44 +04:00
dp . x , dp . y , dp . width , dp . height , resolution . scale ) ;
2013-04-26 21:24:28 +04:00
}
void
2013-06-10 17:05:44 +04:00
AndroidBridge : : HandleDoubleTap ( const CSSIntPoint & aPoint )
2013-04-26 21:24:28 +04:00
{
2013-04-26 21:26:46 +04:00
nsCString data = nsPrintfCString ( " { \" x \" : %d, \" y \" : %d } " , aPoint . x , aPoint . y ) ;
nsAppShell : : gAppShell - > PostEvent ( AndroidGeckoEvent : : MakeBroadcastEvent (
NS_LITERAL_CSTRING ( " Gesture:DoubleTap " ) , data ) ) ;
2013-04-26 21:24:28 +04:00
}
void
2013-06-10 17:05:44 +04:00
AndroidBridge : : HandleSingleTap ( const CSSIntPoint & aPoint )
2013-04-26 21:24:28 +04:00
{
2013-04-26 21:26:46 +04:00
nsCString data = nsPrintfCString ( " { \" x \" : %d, \" y \" : %d } " , aPoint . x , aPoint . y ) ;
nsAppShell : : gAppShell - > PostEvent ( AndroidGeckoEvent : : MakeBroadcastEvent (
NS_LITERAL_CSTRING ( " Gesture:SingleTap " ) , data ) ) ;
2013-04-26 21:24:28 +04:00
}
void
2013-06-10 17:05:44 +04:00
AndroidBridge : : HandleLongTap ( const CSSIntPoint & aPoint )
2013-04-26 21:24:28 +04:00
{
2013-04-26 21:26:46 +04:00
nsCString data = nsPrintfCString ( " { \" x \" : %d, \" y \" : %d } " , aPoint . x , aPoint . y ) ;
nsAppShell : : gAppShell - > PostEvent ( AndroidGeckoEvent : : MakeBroadcastEvent (
NS_LITERAL_CSTRING ( " Gesture:LongPress " ) , data ) ) ;
2013-04-26 21:24:28 +04:00
}
void
2013-07-30 22:03:42 +04:00
AndroidBridge : : SendAsyncScrollDOMEvent ( mozilla : : layers : : FrameMetrics : : ViewID aScrollId ,
const CSSRect & aContentRect ,
const CSSSize & aScrollableSize )
2013-04-26 21:24:28 +04:00
{
// FIXME implement this
}
void
2013-04-26 21:26:46 +04:00
AndroidBridge : : PostDelayedTask ( Task * aTask , int aDelayMs )
2013-04-26 21:24:28 +04:00
{
2013-04-26 21:26:46 +04:00
JNIEnv * env = GetJNIForThread ( ) ;
if ( ! env | | ! mNativePanZoomController ) {
return ;
}
// add the new task into the mDelayedTaskQueue, sorted with
// the earliest task first in the queue
DelayedTask * newTask = new DelayedTask ( aTask , aDelayMs ) ;
uint32_t i = 0 ;
while ( i < mDelayedTaskQueue . Length ( ) ) {
if ( newTask - > IsEarlierThan ( mDelayedTaskQueue [ i ] ) ) {
mDelayedTaskQueue . InsertElementAt ( i , newTask ) ;
break ;
}
i + + ;
}
if ( i = = mDelayedTaskQueue . Length ( ) ) {
// this new task will run after all the existing tasks in the queue
mDelayedTaskQueue . AppendElement ( newTask ) ;
}
if ( i = = 0 ) {
// if we're inserting it at the head of the queue, notify Java because
// we need to get a callback at an earlier time than the last scheduled
// callback
AutoLocalJNIFrame jniFrame ( env , 0 ) ;
env - > CallVoidMethod ( mNativePanZoomController , jPostDelayedCallback , ( int64_t ) aDelayMs ) ;
}
}
int64_t
AndroidBridge : : RunDelayedTasks ( )
{
while ( mDelayedTaskQueue . Length ( ) > 0 ) {
DelayedTask * nextTask = mDelayedTaskQueue [ 0 ] ;
int64_t timeLeft = nextTask - > MillisecondsToRunTime ( ) ;
if ( timeLeft > 0 ) {
// this task (and therefore all remaining tasks)
// have not yet reached their runtime. return the
// time left until we should be called again
return timeLeft ;
}
// we have a delayed task to run. extract it from
// the wrapper and free the wrapper
mDelayedTaskQueue . RemoveElementAt ( 0 ) ;
Task * task = nextTask - > GetTask ( ) ;
delete nextTask ;
task - > Run ( ) ;
}
return - 1 ;
2013-04-26 21:24:28 +04:00
}