1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Android code.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2010
19 * the Initial Developer. All Rights Reserved.
22 * Vladimir Vukicevic <vladimir@pobox.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 package org
.mozilla
.gecko
;
42 import java
.util
.zip
.*;
47 import android
.text
.*;
48 import android
.view
.*;
49 import android
.view
.inputmethod
.*;
50 import android
.content
.*;
51 import android
.graphics
.*;
52 import android
.widget
.*;
53 import android
.hardware
.*;
54 import android
.location
.*;
56 import android
.util
.*;
57 import android
.content
.DialogInterface
;
58 import android
.content
.pm
.PackageManager
;
59 import android
.content
.pm
.ResolveInfo
;
65 sGeckoRunning
= false;
68 // static members only
69 private GeckoAppShell() { }
71 static boolean sGeckoRunning
;
73 static private boolean gRestartScheduled
= false;
75 static protected Timer mSoftKBTimer
;
77 /* The Android-side API: API methods that Android calls */
79 // Initialization methods
80 public static native void nativeInit();
81 public static native void nativeRun(String args
);
84 public static native void setInitialSize(int width
, int height
);
85 public static native void setSurfaceView(GeckoSurfaceView sv
);
86 public static native void putenv(String map
);
87 public static native void onResume();
90 public static void loadGeckoLibs() {
91 // The package data lib directory isn't placed in ld.so's
92 // search path, so we have to manually load libraries that
93 // libxul will depend on. Not ideal.
96 System
.loadLibrary("mozalloc");
99 System
.loadLibrary("nspr4");
100 System
.loadLibrary("plc4");
101 System
.loadLibrary("plds4");
104 System
.loadLibrary("mozsqlite3");
107 System
.loadLibrary("nssutil3");
108 System
.loadLibrary("nss3");
109 System
.loadLibrary("ssl3");
110 System
.loadLibrary("smime3");
113 System
.loadLibrary("mozjs");
116 System
.loadLibrary("xul");
118 // xpcom glue -- needed to load binary components
119 System
.loadLibrary("xpcom");
121 // Root certs. someday we may teach security/manager/ssl/src/nsNSSComponent.cpp to find ckbi itself
122 System
.loadLibrary("nssckbi");
123 System
.loadLibrary("freebl3");
124 System
.loadLibrary("softokn3");
127 public static void runGecko(String apkPath
, String args
, String url
) {
128 // run gecko -- it will spawn its own thread
129 GeckoAppShell
.nativeInit();
131 // Tell Gecko where the target surface view is for rendering
132 GeckoAppShell
.setSurfaceView(GeckoApp
.surfaceView
);
134 sGeckoRunning
= true;
136 // First argument is the .apk path
137 String combinedArgs
= apkPath
;
139 combinedArgs
+= " " + args
;
141 combinedArgs
+= " " + url
;
143 GeckoAppShell
.nativeRun(combinedArgs
);
146 private static GeckoEvent mLastDrawEvent
;
148 public static void sendEventToGecko(GeckoEvent e
) {
150 notifyGeckoOfEvent(e
);
153 // Tell the Gecko event loop that an event is available.
154 public static native void notifyGeckoOfEvent(GeckoEvent event
);
157 * The Gecko-side API: API methods that Gecko calls
159 public static void scheduleRedraw() {
161 scheduleRedraw(0, -1, -1, -1, -1);
164 public static void scheduleRedraw(int nativeWindow
, int x
, int y
, int w
, int h
) {
168 e
= new GeckoEvent(GeckoEvent
.DRAW
, null);
170 e
= new GeckoEvent(GeckoEvent
.DRAW
, new Rect(x
, y
, w
, h
));
173 e
.mNativeWindow
= nativeWindow
;
178 public static void showIME(int state
) {
179 GeckoApp
.surfaceView
.mIMEState
= state
;
181 if (mSoftKBTimer
== null) {
182 mSoftKBTimer
= new Timer();
183 mSoftKBTimer
.schedule(new TimerTask() {
185 InputMethodManager imm
= (InputMethodManager
)
186 GeckoApp
.surfaceView
.getContext().getSystemService(Context
.INPUT_METHOD_SERVICE
);
188 if (GeckoApp
.surfaceView
.mIMEState
!= 0)
189 imm
.showSoftInput(GeckoApp
.surfaceView
, 0);
191 imm
.hideSoftInputFromWindow(GeckoApp
.surfaceView
.getWindowToken(), 0);
199 public static void enableAccelerometer(boolean enable
) {
200 SensorManager sm
= (SensorManager
)
201 GeckoApp
.surfaceView
.getContext().getSystemService(Context
.SENSOR_SERVICE
);
204 Sensor accelSensor
= sm
.getDefaultSensor(Sensor
.TYPE_ACCELEROMETER
);
205 if (accelSensor
== null)
208 sm
.registerListener(GeckoApp
.surfaceView
, accelSensor
, SensorManager
.SENSOR_DELAY_GAME
);
210 sm
.unregisterListener(GeckoApp
.surfaceView
);
214 public static void enableLocation(boolean enable
) {
215 LocationManager lm
= (LocationManager
)
216 GeckoApp
.surfaceView
.getContext().getSystemService(Context
.LOCATION_SERVICE
);
219 Criteria crit
= new Criteria();
220 crit
.setAccuracy(Criteria
.ACCURACY_FINE
);
221 String provider
= lm
.getBestProvider(crit
, true);
222 if (provider
== null)
225 sendEventToGecko(new GeckoEvent(lm
.getLastKnownLocation(provider
)));
226 lm
.requestLocationUpdates(provider
, 100, (float).5, GeckoApp
.surfaceView
, Looper
.getMainLooper());
228 lm
.removeUpdates(GeckoApp
.surfaceView
);
232 public static void moveTaskToBack() {
233 GeckoApp
.mAppContext
.moveTaskToBack(true);
236 public static void returnIMEQueryResult(String result
, int selectionStart
, int selectionEnd
) {
237 GeckoApp
.surfaceView
.inputConnection
.mSelectionStart
= selectionStart
;
238 GeckoApp
.surfaceView
.inputConnection
.mSelectionEnd
= selectionEnd
;
240 GeckoApp
.surfaceView
.inputConnection
.mQueryResult
.put(result
);
241 } catch (InterruptedException e
) {
245 static void onXreExit() {
246 sGeckoRunning
= false;
247 Log
.i("GeckoAppJava", "XRE exited");
248 if (gRestartScheduled
) {
249 GeckoApp
.mAppContext
.doRestart();
251 Log
.i("GeckoAppJava", "we're done, good bye");
256 static void scheduleRestart() {
257 Log
.i("GeckoAppJava", "scheduling restart");
258 gRestartScheduled
= true;
261 static String
[] getHandlersForMimeType(String aMimeType
) {
263 GeckoApp
.surfaceView
.getContext().getPackageManager();
264 Intent intent
= new Intent();
265 intent
.setType(aMimeType
);
266 List
<ResolveInfo
> list
= pm
.queryIntentActivities(intent
, 0);
268 String
[] ret
= new String
[list
.size() * numAttr
];
269 for (int i
= 0; i
< list
.size(); i
++) {
270 ret
[i
* numAttr
] = list
.get(i
).loadLabel(pm
).toString();
271 if (list
.get(i
).isDefault
)
272 ret
[i
* numAttr
+ 1] = "default";
274 ret
[i
* numAttr
+ 1] = "";
279 static String
getMimeTypeFromExtension(String aFileExt
) {
280 return android
.webkit
.MimeTypeMap
.getSingleton().getMimeTypeFromExtension(aFileExt
);
283 static boolean openUriExternal(String aUriSpec
, String aMimeType
) {
284 // XXX: It's not clear if we should set the action to view or leave it open
285 Intent intent
= new Intent(Intent
.ACTION_VIEW
);
286 intent
.setDataAndType(android
.net
.Uri
.parse(aUriSpec
), aMimeType
);
287 intent
.setFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
289 GeckoApp
.surfaceView
.getContext().startActivity(intent
);
291 } catch(ActivityNotFoundException e
) {