AndroidRuntime.cpp revision 163935113919a184122b8b3bd672ef08c8df65dc
1ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala/* //device/libs/android_runtime/AndroidRuntime.cpp
2ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala**
3ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** Copyright 2006, The Android Open Source Project
4ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala**
5ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** Licensed under the Apache License, Version 2.0 (the "License");
6ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** you may not use this file except in compliance with the License.
7ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** You may obtain a copy of the License at
8ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala**
9ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala**     http://www.apache.org/licenses/LICENSE-2.0
10ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala**
11ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** Unless required by applicable law or agreed to in writing, software
12ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** distributed under the License is distributed on an "AS IS" BASIS,
13ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** See the License for the specific language governing permissions and
15ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala** limitations under the License.
16ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala*/
174bb8118816874c696d9f1adab48490df1da365f7Eino-Ville Talvala
18ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#define LOG_TAG "AndroidRuntime"
19ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala//#define LOG_NDEBUG 0
20ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
21a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#include <android_runtime/AndroidRuntime.h>
22a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#include <binder/IBinder.h>
2373bbd1f1c493835f191ea2b0b72439292496b40aEino-Ville Talvala#include <binder/IServiceManager.h>
2473bbd1f1c493835f191ea2b0b72439292496b40aEino-Ville Talvala#include <utils/Log.h>
25ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <utils/misc.h>
26ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <binder/Parcel.h>
271a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian#include <utils/StringArray.h>
28ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <utils/threads.h>
297b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include <cutils/properties.h>
307b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala
317b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include <SkGraphics.h>
327b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include <SkImageDecoder.h>
337b82efe7a376c882f8f938e1c41b8311a8cdda4aEino-Ville Talvala#include <SkImageRef_GlobalPool.h>
34ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
35ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include "jni.h"
36ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include "JNIHelp.h"
37ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include "android_util_Binder.h"
38da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
39d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala#include <stdio.h>
40da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include <signal.h>
41ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <sys/stat.h>
42d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala#include <sys/types.h>
43da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala#include <signal.h>
44d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala#include <dirent.h>
45ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala#include <assert.h>
46ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
47ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
48ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalausing namespace android;
49da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
50ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern void register_BindTest();
51cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala
52ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_Binder(JNIEnv* env);
53ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_Process(JNIEnv* env);
54da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalaextern int register_android_graphics_Bitmap(JNIEnv*);
55ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_BitmapFactory(JNIEnv*);
56ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Camera(JNIEnv* env);
57ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Graphics(JNIEnv* env);
58ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Interpolator(JNIEnv* env);
59ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_LayerRasterizer(JNIEnv*);
60ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_MaskFilter(JNIEnv* env);
61ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Movie(JNIEnv* env);
62da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalaextern int register_android_graphics_NinePatch(JNIEnv*);
63ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_PathEffect(JNIEnv* env);
64ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Region(JNIEnv* env);
65ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Shader(JNIEnv* env);
66ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Typeface(JNIEnv* env);
67ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_YuvImage(JNIEnv* env);
68ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
69d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
70d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
71d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_opengl_jni_GLES10(JNIEnv* env);
72d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
73d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_opengl_jni_GLES11(JNIEnv* env);
74ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
75ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_opengl_jni_GLES20(JNIEnv* env);
7628c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun He
7728c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun Heextern int register_android_hardware_Camera(JNIEnv *env);
7828c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun He
7928c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun Heextern int register_android_hardware_SensorManager(JNIEnv *env);
80ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
81ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_media_AudioRecord(JNIEnv *env);
82ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_media_AudioSystem(JNIEnv *env);
83ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_media_AudioTrack(JNIEnv *env);
84ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_media_JetPlayer(JNIEnv *env);
858aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stozaextern int register_android_media_ToneGenerator(JNIEnv *env);
868aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza
878aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stozaextern int register_android_message_digest_sha1(JNIEnv *env);
888aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stoza
89ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_util_FloatMath(JNIEnv* env);
90ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
918aa0f0619ea867e8fb240cf27913d4f8ae767385Dan Stozanamespace android {
922d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala
932d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala/*
942d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala * JNI-based registration functions.  Note these are properly contained in
952d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala * namespace android.
962d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvala */
972d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_content_AssetManager(JNIEnv* env);
982d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_util_EventLog(JNIEnv* env);
992d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_util_Log(JNIEnv* env);
1002d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_content_StringBlock(JNIEnv* env);
101ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_content_XmlBlock(JNIEnv* env);
1022d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_emoji_EmojiFactory(JNIEnv* env);
1032d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_graphics_Canvas(JNIEnv* env);
1042d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_graphics_ColorFilter(JNIEnv* env);
105a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayextern int register_android_graphics_DrawFilter(JNIEnv* env);
106ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Matrix(JNIEnv* env);
107d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_graphics_Paint(JNIEnv* env);
108ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Path(JNIEnv* env);
109ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_PathMeasure(JNIEnv* env);
110ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Picture(JNIEnv*);
1112d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_graphics_PorterDuff(JNIEnv* env);
1122d077d0acdbb26eeb1357489c999ec875f6ce297Eino-Ville Talvalaextern int register_android_graphics_Rasterizer(JNIEnv* env);
113ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_Xfermode(JNIEnv* env);
114ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_graphics_PixelFormat(JNIEnv* env);
115ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_com_android_internal_graphics_NativeUtils(JNIEnv *env);
116ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_view_Display(JNIEnv* env);
117ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_view_GLES20Canvas(JNIEnv* env);
118ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_view_Surface(JNIEnv* env);
119ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_view_ViewRoot(JNIEnv* env);
120ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_database_CursorWindow(JNIEnv* env);
121ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_database_SQLiteCompiledSql(JNIEnv* env);
122d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_database_SQLiteDatabase(JNIEnv* env);
123ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_database_SQLiteDebug(JNIEnv* env);
124ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_database_SQLiteProgram(JNIEnv* env);
125ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_database_SQLiteQuery(JNIEnv* env);
126ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_database_SQLiteStatement(JNIEnv* env);
127ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkinextern int register_android_debug_JNITest(JNIEnv* env);
128d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_nio_utils(JNIEnv* env);
129ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_pim_EventRecurrence(JNIEnv* env);
130dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkinextern int register_android_text_format_Time(JNIEnv* env);
131dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkinextern int register_android_os_Debug(JNIEnv* env);
132dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkinextern int register_android_os_MessageQueue(JNIEnv* env);
133dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkinextern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
134dca254aef0c09867e50fe6613c6fad405de72842Igor Murashkinextern int register_android_os_Power(JNIEnv *env);
135ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_StatFs(JNIEnv *env);
136ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_SystemProperties(JNIEnv *env);
137d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_os_SystemClock(JNIEnv* env);
138ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_FileObserver(JNIEnv *env);
139ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_FileUtils(JNIEnv *env);
140ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_UEventObserver(JNIEnv* env);
141ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_os_MemoryFile(JNIEnv* env);
142ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_net_LocalSocketImpl(JNIEnv* env);
143ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_net_NetworkUtils(JNIEnv* env);
144ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_net_TrafficStats(JNIEnv* env);
145ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_net_wifi_WifiManager(JNIEnv* env);
146ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_security_Md5MessageDigest(JNIEnv *env);
147ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_text_AndroidCharacter(JNIEnv *env);
14828c9b6f298134624cb52b1af4ed8716dddb983d3Zhijun Heextern int register_android_text_AndroidBidi(JNIEnv *env);
149ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_text_KeyCharacterMap(JNIEnv *env);
150ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_opengl_classes(JNIEnv *env);
151d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_bluetooth_HeadsetBase(JNIEnv* env);
152ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env);
153ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_bluetooth_BluetoothSocket(JNIEnv *env);
154ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_server_BluetoothService(JNIEnv* env);
155ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_server_BluetoothEventLoop(JNIEnv *env);
156ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_server_BluetoothA2dpService(JNIEnv* env);
157ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_server_Watchdog(JNIEnv* env);
158ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
159ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
160da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvalaextern int register_android_backup_BackupDataInput(JNIEnv *env);
161ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_backup_BackupDataOutput(JNIEnv *env);
162ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
163ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
164ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_app_NativeActivity(JNIEnv *env);
165ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalaextern int register_android_view_InputChannel(JNIEnv* env);
166d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_view_InputQueue(JNIEnv* env);
167d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_view_KeyEvent(JNIEnv* env);
168d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_view_MotionEvent(JNIEnv* env);
169d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvalaextern int register_android_content_res_ObbScanner(JNIEnv* env);
170d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala
171ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic AndroidRuntime* gCurRuntime = NULL;
172ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
173cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvalastatic void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
174cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala{
175cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala    if (jniThrowException(env, exc, msg) != 0)
176cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        assert(false);
177cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala}
178ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
179ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala/*
180ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * Code written in the Java Programming Language calls here from main().
181ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala */
182ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz)
183da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala{
184ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    gCurRuntime->onStarted();
185ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
186ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
187ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)
188ddf3c5025e2f6f35a4c188c19f30142c64a092c4Igor Murashkin{
189ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    gCurRuntime->onZygoteInit();
190ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
191da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala
192ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz)
193ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala{
194ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return 1;
195ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
196ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
197ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz)
198ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala{
199ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
200ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
201ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz)
202ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala{
203ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    char* value = getenv("qwerty");
204ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    if (value != NULL && strcmp(value, "true") == 0) {
205d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala        return 1;
206ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    }
207ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
208ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    return 0;
209ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala}
210ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
211d09801b99503b57c35e321ad9afa7e861e012813Eino-Ville Talvala/*
212ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * JNI registration.
213ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala */
214ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatic JNINativeMethod gMethods[] = {
215138851bd3cadfb60238f87567e24808925731837Zhijun He    { "finishInit", "()V",
216ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        (void*) com_android_internal_os_RuntimeInit_finishInit },
217ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    { "zygoteInitNative", "()V",
218ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        (void*) com_android_internal_os_RuntimeInit_zygoteInit },
219138851bd3cadfb60238f87567e24808925731837Zhijun He    { "isComputerOn", "()I",
220138851bd3cadfb60238f87567e24808925731837Zhijun He        (void*) com_android_internal_os_RuntimeInit_isComputerOn },
221138851bd3cadfb60238f87567e24808925731837Zhijun He    { "turnComputerOn", "()V",
222138851bd3cadfb60238f87567e24808925731837Zhijun He        (void*) com_android_internal_os_RuntimeInit_turnComputerOn },
223138851bd3cadfb60238f87567e24808925731837Zhijun He    { "getQwertyKeyboard", "()I",
224ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala        (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard },
225ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala};
226138851bd3cadfb60238f87567e24808925731837Zhijun He
227138851bd3cadfb60238f87567e24808925731837Zhijun Heint register_com_android_internal_os_RuntimeInit(JNIEnv* env)
228138851bd3cadfb60238f87567e24808925731837Zhijun He{
229138851bd3cadfb60238f87567e24808925731837Zhijun He    return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
230138851bd3cadfb60238f87567e24808925731837Zhijun He        gMethods, NELEM(gMethods));
231138851bd3cadfb60238f87567e24808925731837Zhijun He}
232138851bd3cadfb60238f87567e24808925731837Zhijun He
233138851bd3cadfb60238f87567e24808925731837Zhijun He// ----------------------------------------------------------------------
234138851bd3cadfb60238f87567e24808925731837Zhijun He
235ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
236138851bd3cadfb60238f87567e24808925731837Zhijun He
237138851bd3cadfb60238f87567e24808925731837Zhijun He
238ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville TalvalaAndroidRuntime::AndroidRuntime()
239138851bd3cadfb60238f87567e24808925731837Zhijun He{
240138851bd3cadfb60238f87567e24808925731837Zhijun He    SkGraphics::Init();
241138851bd3cadfb60238f87567e24808925731837Zhijun He    // this sets our preference for 16bit images during decode
242138851bd3cadfb60238f87567e24808925731837Zhijun He    // in case the src is opaque and 24bit
243138851bd3cadfb60238f87567e24808925731837Zhijun He    SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
244138851bd3cadfb60238f87567e24808925731837Zhijun He    // This cache is shared between browser native images, and java "purgeable"
245138851bd3cadfb60238f87567e24808925731837Zhijun He    // bitmaps. This globalpool is for images that do not either use the java
246138851bd3cadfb60238f87567e24808925731837Zhijun He    // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
247a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    // java call site.
248138851bd3cadfb60238f87567e24808925731837Zhijun He    SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
249138851bd3cadfb60238f87567e24808925731837Zhijun He    // There is also a global font cache, but its budget is specified in code
250138851bd3cadfb60238f87567e24808925731837Zhijun He    // see SkFontHost_android.cpp
251138851bd3cadfb60238f87567e24808925731837Zhijun He
252138851bd3cadfb60238f87567e24808925731837Zhijun He    // Pre-allocate enough space to hold a fair number of options.
253138851bd3cadfb60238f87567e24808925731837Zhijun He    mOptions.setCapacity(20);
254138851bd3cadfb60238f87567e24808925731837Zhijun He
255138851bd3cadfb60238f87567e24808925731837Zhijun He    assert(gCurRuntime == NULL);        // one per process
2565c679d41714e42f153f6aeb492df54510a195bc1Colin Cross    gCurRuntime = this;
257138851bd3cadfb60238f87567e24808925731837Zhijun He}
258138851bd3cadfb60238f87567e24808925731837Zhijun He
259138851bd3cadfb60238f87567e24808925731837Zhijun HeAndroidRuntime::~AndroidRuntime()
260138851bd3cadfb60238f87567e24808925731837Zhijun He{
261138851bd3cadfb60238f87567e24808925731837Zhijun He    SkGraphics::Term();
262138851bd3cadfb60238f87567e24808925731837Zhijun He}
263138851bd3cadfb60238f87567e24808925731837Zhijun He
264138851bd3cadfb60238f87567e24808925731837Zhijun He/*
265ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * Register native methods using JNI.
266138851bd3cadfb60238f87567e24808925731837Zhijun He */
267138851bd3cadfb60238f87567e24808925731837Zhijun He/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
268ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    const char* className, const JNINativeMethod* gMethods, int numMethods)
269fe580e57081885dd6059e0d407a6cd96a1be7e51Eino-Ville Talvala{
270fe580e57081885dd6059e0d407a6cd96a1be7e51Eino-Ville Talvala    return jniRegisterNativeMethods(env, className, gMethods, numMethods);
271a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray}
272ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
273fe580e57081885dd6059e0d407a6cd96a1be7e51Eino-Ville Talvala/*
274ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala * Call a static Java Programming Language function that takes no arguments and returns void.
275ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala */
276ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvalastatus_t AndroidRuntime::callStatic(const char* className, const char* methodName)
277a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray{
278a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    JNIEnv* env;
279a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jclass clazz;
280a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jmethodID methodId;
281a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
282a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    env = getJNIEnv();
283a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (env == NULL)
284a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return UNKNOWN_ERROR;
285a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
286a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    clazz = findClass(env, className);
287a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (clazz == NULL) {
288a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        LOGE("ERROR: could not find class '%s'\n", className);
289a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return UNKNOWN_ERROR;
290a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
291a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    methodId = env->GetStaticMethodID(clazz, methodName, "()V");
292a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (methodId == NULL) {
293a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        LOGE("ERROR: could not find method %s.%s\n", className, methodName);
294a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return UNKNOWN_ERROR;
295a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
296a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
297a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    env->CallStaticVoidMethod(clazz, methodId);
298a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
299a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    return NO_ERROR;
300a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray}
301a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
302a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Raystatus_t AndroidRuntime::callMain(
303a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    const char* className, int argc, const char* const argv[])
304a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray{
305a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    JNIEnv* env;
306a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jclass clazz;
307a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jmethodID methodId;
308a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
309a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    env = getJNIEnv();
310a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (env == NULL)
311a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return UNKNOWN_ERROR;
312a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
313a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    clazz = findClass(env, className);
314a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (clazz == NULL) {
315a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        LOGE("ERROR: could not find class '%s'\n", className);
316a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return UNKNOWN_ERROR;
317a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
318a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
319a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
320a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (methodId == NULL) {
321a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        LOGE("ERROR: could not find method %s.main(String[])\n", className);
322a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return UNKNOWN_ERROR;
323a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
324a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
325a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    /*
326a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * We want to call main() with a String array with our arguments in it.
327a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * Create an array and populate it.
328a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     */
329a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jclass stringClass;
330a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jobjectArray strArray;
331a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
332a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    stringClass = env->FindClass("java/lang/String");
333a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    strArray = env->NewObjectArray(argc, stringClass, NULL);
334a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
335a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    for (int i = 0; i < argc; i++) {
336a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        jstring argStr = env->NewStringUTF(argv[i]);
3370fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray        env->SetObjectArrayElement(strArray, i, argStr);
338a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
339a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
3400fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    env->CallStaticVoidMethod(clazz, methodId, strArray);
3410fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    return NO_ERROR;
3420fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray}
343a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
344a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray/*
345a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray * Find the named class.
346a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray */
347a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Rayjclass AndroidRuntime::findClass(JNIEnv* env, const char* className)
3480fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray{
3490fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray    char* convName = NULL;
350e5729fac81c8a984e984fefc90afc64135817d4fColin Cross
351a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    if (env->ExceptionCheck()) {
352a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        LOGE("ERROR: exception pending on entry to findClass()\n");
353a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        return NULL;
354a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    }
355a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
356e5729fac81c8a984e984fefc90afc64135817d4fColin Cross    /*
357a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * JNI FindClass uses class names with slashes, but ClassLoader.loadClass
358a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * uses the dotted "binary name" format.  We don't need to convert the
359a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * name with the new approach.
3600fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray     */
3610fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray#if 0
362a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    /* (convName only created if necessary -- use className) */
363a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    for (char* cp = const_cast<char*>(className); *cp != '\0'; cp++) {
364a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        if (*cp == '.') {
365a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            if (convName == NULL) {
366a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray                convName = strdup(className);
367a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray                cp = convName + (cp-className);
368a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray                className = convName;
3690fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray            }
370a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray            *cp = '/';
371a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        }
372e5729fac81c8a984e984fefc90afc64135817d4fColin Cross    }
373a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray#endif
374a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
375a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    /*
376e5729fac81c8a984e984fefc90afc64135817d4fColin Cross     * This is a little awkward because the JNI FindClass call uses the
377a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * class loader associated with the native method we're executing in.
378a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * Because this native method is part of a "boot" class, JNI doesn't
379a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * look for the class in CLASSPATH, which unfortunately is a likely
380e5729fac81c8a984e984fefc90afc64135817d4fColin Cross     * location for it.  (Had we issued the FindClass call before calling
381a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * into the VM -- at which point there isn't a native method frame on
382a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * the stack -- the VM would have checked CLASSPATH.  We have to do
383a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * this because we call into Java Programming Language code and
384a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * bounce back out.)
385a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     *
386a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray     * JNI lacks a "find class in a specific class loader" operation, so we
387ddf3c5025e2f6f35a4c188c19f30142c64a092c4Igor Murashkin     * have to do things the hard way.
3880fa1e760f7093d025c977e5813e8d3a04c863ce3Alex Ray     */
389a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jclass cls = NULL;
390a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    //cls = env->FindClass(className);
391a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
392a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jclass javaLangClassLoader;
393a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jmethodID getSystemClassLoader, loadClass;
394a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jobject systemClassLoader;
395a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    jstring strClassName;
396a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray
397a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    /* find the "system" class loader; none of this is expected to fail */
398a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    javaLangClassLoader = env->FindClass("java/lang/ClassLoader");
399a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    assert(javaLangClassLoader != NULL);
400e5729fac81c8a984e984fefc90afc64135817d4fColin Cross    getSystemClassLoader = env->GetStaticMethodID(javaLangClassLoader,
401a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        "getSystemClassLoader", "()Ljava/lang/ClassLoader;");
402a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    loadClass = env->GetMethodID(javaLangClassLoader,
403e5729fac81c8a984e984fefc90afc64135817d4fColin Cross        "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
404a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    assert(getSystemClassLoader != NULL && loadClass != NULL);
405a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray    systemClassLoader = env->CallStaticObjectMethod(javaLangClassLoader,
406a6b4c40e70eafc5aba16163999de6d3e26667b89Alex Ray        getSystemClassLoader);
407ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala    assert(systemClassLoader != NULL);
408ea0d51b5ed0b474433b02414f9133b835f972569Eino-Ville Talvala
409    /* create an object for the class name string; alloc could fail */
410    strClassName = env->NewStringUTF(className);
411    if (env->ExceptionCheck()) {
412        LOGE("ERROR: unable to convert '%s' to string\n", className);
413        goto bail;
414    }
415    LOGV("system class loader is %p, loading %p (%s)\n",
416        systemClassLoader, strClassName, className);
417
418    /* try to find the named class */
419    cls = (jclass) env->CallObjectMethod(systemClassLoader, loadClass,
420                        strClassName);
421    if (env->ExceptionCheck()) {
422        LOGE("ERROR: unable to load class '%s' from %p\n",
423            className, systemClassLoader);
424        cls = NULL;
425        goto bail;
426    }
427
428bail:
429    free(convName);
430    return cls;
431}
432
433/*
434 * The VM calls this through the "exit" hook.
435 */
436static void runtime_exit(int code)
437{
438    gCurRuntime->onExit(code);
439    exit(code);
440}
441
442/*
443 * The VM calls this through the "vfprintf" hook.
444 *
445 * We ignore "fp" and just write the results to the log file.
446 */
447static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
448{
449    LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
450}
451
452
453/**
454 * Add VM arguments to the to-be-executed VM
455 * Stops at first non '-' argument (also stops at an argument of '--')
456 * Returns the number of args consumed
457 */
458int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
459{
460    int i;
461
462    for (i = 0; i<argc; i++) {
463        if (argv[i][0] != '-') {
464            return i;
465        }
466        if (argv[i][1] == '-' && argv[i][2] == 0) {
467            return i+1;
468        }
469
470        JavaVMOption opt;
471        memset(&opt, 0, sizeof(opt));
472        opt.optionString = (char*)argv[i];
473        mOptions.add(opt);
474    }
475    return i;
476}
477
478static int hasDir(const char* dir)
479{
480    struct stat s;
481    int res = stat(dir, &s);
482    if (res == 0) {
483        return S_ISDIR(s.st_mode);
484    }
485    return 0;
486}
487
488/*
489 * We just want failed write() calls to just return with an error.
490 */
491static void blockSigpipe()
492{
493    sigset_t mask;
494
495    sigemptyset(&mask);
496    sigaddset(&mask, SIGPIPE);
497    if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
498        LOGW("WARNING: SIGPIPE not blocked\n");
499}
500
501/*
502 * Read the persistent locale.
503 */
504static void readLocale(char* language, char* region)
505{
506    char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
507
508    property_get("persist.sys.language", propLang, "");
509    property_get("persist.sys.country", propRegn, "");
510    if (*propLang == 0 && *propRegn == 0) {
511        /* Set to ro properties, default is en_US */
512        property_get("ro.product.locale.language", propLang, "en");
513        property_get("ro.product.locale.region", propRegn, "US");
514    }
515    strncat(language, propLang, 2);
516    strncat(region, propRegn, 2);
517    //LOGD("language=%s region=%s\n", language, region);
518}
519
520/*
521 * Parse a property containing space-separated options that should be
522 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
523 *
524 * This will cut up "extraOptsBuf" as we chop it into individual options.
525 *
526 * Adds the strings, if any, to mOptions.
527 */
528void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
529{
530    JavaVMOption opt;
531    char* start;
532    char* end;
533
534    memset(&opt, 0, sizeof(opt));
535    start = extraOptsBuf;
536    while (*start != '\0') {
537        while (*start == ' ')                   /* skip leading whitespace */
538            start++;
539        if (*start == '\0')                     /* was trailing ws, bail */
540            break;
541
542        end = start+1;
543        while (*end != ' ' && *end != '\0')     /* find end of token */
544            end++;
545        if (*end == ' ')
546            *end++ = '\0';          /* mark end, advance to indicate more */
547
548        opt.optionString = start;
549        mOptions.add(opt);
550        start = end;
551    }
552}
553
554/*
555 * Start the Dalvik Virtual Machine.
556 *
557 * Various arguments, most determined by system properties, are passed in.
558 * The "mOptions" vector is updated.
559 *
560 * Returns 0 on success.
561 */
562int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
563{
564    int result = -1;
565    JavaVMInitArgs initArgs;
566    JavaVMOption opt;
567    char propBuf[PROPERTY_VALUE_MAX];
568    char stackTraceFileBuf[PROPERTY_VALUE_MAX];
569    char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
570    char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
571    char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
572    char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
573    char extraOptsBuf[PROPERTY_VALUE_MAX];
574    char* stackTraceFile = NULL;
575    bool checkJni = false;
576    bool checkDexSum = false;
577    bool logStdio = false;
578    enum {
579      kEMDefault,
580      kEMIntPortable,
581      kEMIntFast,
582#if defined(WITH_JIT)
583      kEMJitCompiler,
584#endif
585    } executionMode = kEMDefault;
586
587
588    property_get("dalvik.vm.checkjni", propBuf, "");
589    if (strcmp(propBuf, "true") == 0) {
590        checkJni = true;
591    } else if (strcmp(propBuf, "false") != 0) {
592        /* property is neither true nor false; fall back on kernel parameter */
593        property_get("ro.kernel.android.checkjni", propBuf, "");
594        if (propBuf[0] == '1') {
595            checkJni = true;
596        }
597    }
598
599    property_get("dalvik.vm.execution-mode", propBuf, "");
600    if (strcmp(propBuf, "int:portable") == 0) {
601        executionMode = kEMIntPortable;
602    } else if (strcmp(propBuf, "int:fast") == 0) {
603        executionMode = kEMIntFast;
604#if defined(WITH_JIT)
605    } else if (strcmp(propBuf, "int:jit") == 0) {
606        executionMode = kEMJitCompiler;
607#endif
608    }
609
610    property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
611
612    property_get("dalvik.vm.check-dex-sum", propBuf, "");
613    if (strcmp(propBuf, "true") == 0) {
614        checkDexSum = true;
615    }
616
617    property_get("log.redirect-stdio", propBuf, "");
618    if (strcmp(propBuf, "true") == 0) {
619        logStdio = true;
620    }
621
622    strcpy(enableAssertBuf, "-ea:");
623    property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
624
625    strcpy(jniOptsBuf, "-Xjniopts:");
626    property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
627
628    /* route exit() to our handler */
629    opt.extraInfo = (void*) runtime_exit;
630    opt.optionString = "exit";
631    mOptions.add(opt);
632
633    /* route fprintf() to our handler */
634    opt.extraInfo = (void*) runtime_vfprintf;
635    opt.optionString = "vfprintf";
636    mOptions.add(opt);
637
638    opt.extraInfo = NULL;
639
640    /* enable verbose; standard options are { jni, gc, class } */
641    //options[curOpt++].optionString = "-verbose:jni";
642    opt.optionString = "-verbose:gc";
643    mOptions.add(opt);
644    //options[curOpt++].optionString = "-verbose:class";
645
646    strcpy(heapsizeOptsBuf, "-Xmx");
647    property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
648    //LOGI("Heap size: %s", heapsizeOptsBuf);
649    opt.optionString = heapsizeOptsBuf;
650    mOptions.add(opt);
651
652    /*
653     * Enable or disable dexopt features, such as bytecode verification and
654     * calculation of register maps for precise GC.
655     */
656    property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
657    if (dexoptFlagsBuf[0] != '\0') {
658        const char* opc;
659        const char* val;
660
661        opc = strstr(dexoptFlagsBuf, "v=");     /* verification */
662        if (opc != NULL) {
663            switch (*(opc+2)) {
664            case 'n':   val = "-Xverify:none";      break;
665            case 'r':   val = "-Xverify:remote";    break;
666            case 'a':   val = "-Xverify:all";       break;
667            default:    val = NULL;                 break;
668            }
669
670            if (val != NULL) {
671                opt.optionString = val;
672                mOptions.add(opt);
673            }
674        }
675
676        opc = strstr(dexoptFlagsBuf, "o=");     /* optimization */
677        if (opc != NULL) {
678            switch (*(opc+2)) {
679            case 'n':   val = "-Xdexopt:none";      break;
680            case 'v':   val = "-Xdexopt:verified";  break;
681            case 'a':   val = "-Xdexopt:all";       break;
682            default:    val = NULL;                 break;
683            }
684
685            if (val != NULL) {
686                opt.optionString = val;
687                mOptions.add(opt);
688            }
689        }
690
691        opc = strstr(dexoptFlagsBuf, "m=y");    /* register map */
692        if (opc != NULL) {
693            opt.optionString = "-Xgenregmap";
694            mOptions.add(opt);
695
696            /* turn on precise GC while we're at it */
697            opt.optionString = "-Xgc:precise";
698            mOptions.add(opt);
699        }
700    }
701
702    /* enable debugging; set suspend=y to pause during VM init */
703#ifdef HAVE_ANDROID_OS
704    /* use android ADB transport */
705    opt.optionString =
706        "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
707#else
708    /* use TCP socket; address=0 means start at port 8000 and probe up */
709    LOGI("Using TCP socket for JDWP\n");
710    opt.optionString =
711        "-agentlib:jdwp=transport=dt_socket,suspend=n,server=y,address=0";
712#endif
713    mOptions.add(opt);
714
715    char enableDPBuf[sizeof("-Xdeadlockpredict:") + PROPERTY_VALUE_MAX];
716    property_get("dalvik.vm.deadlock-predict", propBuf, "");
717    if (strlen(propBuf) > 0) {
718        strcpy(enableDPBuf, "-Xdeadlockpredict:");
719        strcat(enableDPBuf, propBuf);
720        opt.optionString = enableDPBuf;
721        mOptions.add(opt);
722    }
723
724    LOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
725    if (checkJni) {
726        /* extended JNI checking */
727        opt.optionString = "-Xcheck:jni";
728        mOptions.add(opt);
729
730        /* set a cap on JNI global references */
731        opt.optionString = "-Xjnigreflimit:2000";
732        mOptions.add(opt);
733
734        /* with -Xcheck:jni, this provides a JNI function call trace */
735        //opt.optionString = "-verbose:jni";
736        //mOptions.add(opt);
737    }
738
739    char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
740    property_get("dalvik.vm.lockprof.threshold", propBuf, "");
741    if (strlen(propBuf) > 0) {
742      strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
743      strcat(lockProfThresholdBuf, propBuf);
744      opt.optionString = lockProfThresholdBuf;
745      mOptions.add(opt);
746    }
747
748#if defined(WITH_JIT)
749    /* Minimal profile threshold to trigger JIT compilation */
750    char jitThresholdBuf[sizeof("-Xjitthreshold:") + PROPERTY_VALUE_MAX];
751    property_get("dalvik.vm.jit.threshold", propBuf, "");
752    if (strlen(propBuf) > 0) {
753        strcpy(jitThresholdBuf, "-Xjitthreshold:");
754        strcat(jitThresholdBuf, propBuf);
755        opt.optionString = jitThresholdBuf;
756        mOptions.add(opt);
757    }
758
759    /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
760    char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
761    property_get("dalvik.vm.jit.op", propBuf, "");
762    if (strlen(propBuf) > 0) {
763        strcpy(jitOpBuf, "-Xjitop:");
764        strcat(jitOpBuf, propBuf);
765        opt.optionString = jitOpBuf;
766        mOptions.add(opt);
767    }
768
769    /*
770     * Reverse the polarity of dalvik.vm.jit.op and force interpreter-only
771     * for non-selected opcodes.
772     */
773    property_get("dalvik.vm.jit.includeop", propBuf, "");
774    if (strlen(propBuf) > 0) {
775        opt.optionString = "-Xincludeselectedop";
776        mOptions.add(opt);
777    }
778
779    /* Force interpreter-only mode for selected methods */
780    char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
781    property_get("dalvik.vm.jit.method", propBuf, "");
782    if (strlen(propBuf) > 0) {
783        strcpy(jitMethodBuf, "-Xjitmethod:");
784        strcat(jitMethodBuf, propBuf);
785        opt.optionString = jitMethodBuf;
786        mOptions.add(opt);
787    }
788
789    /*
790     * Reverse the polarity of dalvik.vm.jit.method and force interpreter-only
791     * for non-selected methods.
792     */
793    property_get("dalvik.vm.jit.includemethod", propBuf, "");
794    if (strlen(propBuf) > 0) {
795        opt.optionString = "-Xincludeselectedmethod";
796        mOptions.add(opt);
797    }
798
799    /*
800     * Enable profile collection on JIT'ed code.
801     */
802    property_get("dalvik.vm.jit.profile", propBuf, "");
803    if (strlen(propBuf) > 0) {
804        opt.optionString = "-Xjitprofile";
805        mOptions.add(opt);
806    }
807
808    /*
809     * Disable optimizations by setting the corresponding bit to 1.
810     */
811    char jitOptBuf[sizeof("-Xjitdisableopt:") + PROPERTY_VALUE_MAX];
812    property_get("dalvik.vm.jit.disableopt", propBuf, "");
813    if (strlen(propBuf) > 0) {
814        strcpy(jitOptBuf, "-Xjitdisableopt:");
815        strcat(jitOptBuf, propBuf);
816        opt.optionString = jitOptBuf;
817        mOptions.add(opt);
818    }
819#endif
820
821    if (executionMode == kEMIntPortable) {
822        opt.optionString = "-Xint:portable";
823        mOptions.add(opt);
824    } else if (executionMode == kEMIntFast) {
825        opt.optionString = "-Xint:fast";
826        mOptions.add(opt);
827#if defined(WITH_JIT)
828    } else if (executionMode == kEMJitCompiler) {
829        opt.optionString = "-Xint:jit";
830        mOptions.add(opt);
831#endif
832    }
833
834    if (checkDexSum) {
835        /* perform additional DEX checksum tests */
836        opt.optionString = "-Xcheckdexsum";
837        mOptions.add(opt);
838    }
839
840    if (logStdio) {
841        /* convert stdout/stderr to log messages */
842        opt.optionString = "-Xlog-stdio";
843        mOptions.add(opt);
844    }
845
846    if (enableAssertBuf[4] != '\0') {
847        /* accept "all" to mean "all classes and packages" */
848        if (strcmp(enableAssertBuf+4, "all") == 0)
849            enableAssertBuf[3] = '\0';
850        LOGI("Assertions enabled: '%s'\n", enableAssertBuf);
851        opt.optionString = enableAssertBuf;
852        mOptions.add(opt);
853    } else {
854        LOGV("Assertions disabled\n");
855    }
856
857    if (jniOptsBuf[10] != '\0') {
858        LOGI("JNI options: '%s'\n", jniOptsBuf);
859        opt.optionString = jniOptsBuf;
860        mOptions.add(opt);
861    }
862
863    if (stackTraceFileBuf[0] != '\0') {
864        static const char* stfOptName = "-Xstacktracefile:";
865
866        stackTraceFile = (char*) malloc(strlen(stfOptName) +
867            strlen(stackTraceFileBuf) +1);
868        strcpy(stackTraceFile, stfOptName);
869        strcat(stackTraceFile, stackTraceFileBuf);
870        opt.optionString = stackTraceFile;
871        mOptions.add(opt);
872    }
873
874    /* extra options; parse this late so it overrides others */
875    property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
876    parseExtraOpts(extraOptsBuf);
877
878    /* Set the properties for locale */
879    {
880        char langOption[sizeof("-Duser.language=") + 3];
881        char regionOption[sizeof("-Duser.region=") + 3];
882        strcpy(langOption, "-Duser.language=");
883        strcpy(regionOption, "-Duser.region=");
884        readLocale(langOption, regionOption);
885        opt.extraInfo = NULL;
886        opt.optionString = langOption;
887        mOptions.add(opt);
888        opt.optionString = regionOption;
889        mOptions.add(opt);
890    }
891
892    /*
893     * We don't have /tmp on the device, but we often have an SD card.  Apps
894     * shouldn't use this, but some test suites might want to exercise it.
895     */
896    opt.optionString = "-Djava.io.tmpdir=/sdcard";
897    mOptions.add(opt);
898
899    initArgs.version = JNI_VERSION_1_4;
900    initArgs.options = mOptions.editArray();
901    initArgs.nOptions = mOptions.size();
902    initArgs.ignoreUnrecognized = JNI_FALSE;
903
904    /*
905     * Initialize the VM.
906     *
907     * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
908     * If this call succeeds, the VM is ready, and we can start issuing
909     * JNI calls.
910     */
911    if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
912        LOGE("JNI_CreateJavaVM failed\n");
913        goto bail;
914    }
915
916    result = 0;
917
918bail:
919    free(stackTraceFile);
920    return result;
921}
922
923/*
924 * Start the Android runtime.  This involves starting the virtual machine
925 * and calling the "static void main(String[] args)" method in the class
926 * named by "className".
927 */
928void AndroidRuntime::start(const char* className, const bool startSystemServer)
929{
930    LOGD("\n>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<\n");
931
932    char* slashClassName = NULL;
933    char* cp;
934    JNIEnv* env;
935
936    blockSigpipe();
937
938    /*
939     * 'startSystemServer == true' means runtime is obslete and not run from
940     * init.rc anymore, so we print out the boot start event here.
941     */
942    if (startSystemServer) {
943        /* track our progress through the boot sequence */
944        const int LOG_BOOT_PROGRESS_START = 3000;
945        LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
946                       ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
947    }
948
949    const char* rootDir = getenv("ANDROID_ROOT");
950    if (rootDir == NULL) {
951        rootDir = "/system";
952        if (!hasDir("/system")) {
953            LOG_FATAL("No root directory specified, and /android does not exist.");
954            goto bail;
955        }
956        setenv("ANDROID_ROOT", rootDir, 1);
957    }
958
959    //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
960    //LOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
961
962    /* start the virtual machine */
963    if (startVm(&mJavaVM, &env) != 0)
964        goto bail;
965
966    /*
967     * Register android functions.
968     */
969    if (startReg(env) < 0) {
970        LOGE("Unable to register all android natives\n");
971        goto bail;
972    }
973
974    /*
975     * We want to call main() with a String array with arguments in it.
976     * At present we only have one argument, the class name.  Create an
977     * array to hold it.
978     */
979    jclass stringClass;
980    jobjectArray strArray;
981    jstring classNameStr;
982    jstring startSystemServerStr;
983
984    stringClass = env->FindClass("java/lang/String");
985    assert(stringClass != NULL);
986    strArray = env->NewObjectArray(2, stringClass, NULL);
987    assert(strArray != NULL);
988    classNameStr = env->NewStringUTF(className);
989    assert(classNameStr != NULL);
990    env->SetObjectArrayElement(strArray, 0, classNameStr);
991    startSystemServerStr = env->NewStringUTF(startSystemServer ?
992                                                 "true" : "false");
993    env->SetObjectArrayElement(strArray, 1, startSystemServerStr);
994
995    /*
996     * Start VM.  This thread becomes the main thread of the VM, and will
997     * not return until the VM exits.
998     */
999    jclass startClass;
1000    jmethodID startMeth;
1001
1002    slashClassName = strdup(className);
1003    for (cp = slashClassName; *cp != '\0'; cp++)
1004        if (*cp == '.')
1005            *cp = '/';
1006
1007    startClass = env->FindClass(slashClassName);
1008    if (startClass == NULL) {
1009        LOGE("JavaVM unable to locate class '%s'\n", slashClassName);
1010        /* keep going */
1011    } else {
1012        startMeth = env->GetStaticMethodID(startClass, "main",
1013            "([Ljava/lang/String;)V");
1014        if (startMeth == NULL) {
1015            LOGE("JavaVM unable to find main() in '%s'\n", className);
1016            /* keep going */
1017        } else {
1018            env->CallStaticVoidMethod(startClass, startMeth, strArray);
1019
1020#if 0
1021            if (env->ExceptionCheck())
1022                threadExitUncaughtException(env);
1023#endif
1024        }
1025    }
1026
1027    LOGD("Shutting down VM\n");
1028    if (mJavaVM->DetachCurrentThread() != JNI_OK)
1029        LOGW("Warning: unable to detach main thread\n");
1030    if (mJavaVM->DestroyJavaVM() != 0)
1031        LOGW("Warning: VM did not shut down cleanly\n");
1032
1033bail:
1034    free(slashClassName);
1035}
1036
1037void AndroidRuntime::start()
1038{
1039    start("com.android.internal.os.RuntimeInit",
1040        false /* Don't start the system server */);
1041}
1042
1043void AndroidRuntime::onExit(int code)
1044{
1045    LOGI("AndroidRuntime onExit calling exit(%d)", code);
1046    exit(code);
1047}
1048
1049/*
1050 * Get the JNIEnv pointer for this thread.
1051 *
1052 * Returns NULL if the slot wasn't allocated or populated.
1053 */
1054/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
1055{
1056    JNIEnv* env;
1057    JavaVM* vm = AndroidRuntime::getJavaVM();
1058    assert(vm != NULL);
1059
1060    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
1061        return NULL;
1062    return env;
1063}
1064
1065/*
1066 * Makes the current thread visible to the VM.
1067 *
1068 * The JNIEnv pointer returned is only valid for the current thread, and
1069 * thus must be tucked into thread-local storage.
1070 */
1071static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
1072{
1073    JavaVMAttachArgs args;
1074    JavaVM* vm;
1075    jint result;
1076
1077    vm = AndroidRuntime::getJavaVM();
1078    assert(vm != NULL);
1079
1080    args.version = JNI_VERSION_1_4;
1081    args.name = (char*) threadName;
1082    args.group = NULL;
1083
1084    result = vm->AttachCurrentThread(pEnv, (void*) &args);
1085    if (result != JNI_OK)
1086        LOGI("NOTE: attach of thread '%s' failed\n", threadName);
1087
1088    return result;
1089}
1090
1091/*
1092 * Detach the current thread from the set visible to the VM.
1093 */
1094static int javaDetachThread(void)
1095{
1096    JavaVM* vm;
1097    jint result;
1098
1099    vm = AndroidRuntime::getJavaVM();
1100    assert(vm != NULL);
1101
1102    result = vm->DetachCurrentThread();
1103    if (result != JNI_OK)
1104        LOGE("ERROR: thread detach failed\n");
1105    return result;
1106}
1107
1108/*
1109 * When starting a native thread that will be visible from the VM, we
1110 * bounce through this to get the right attach/detach action.
1111 * Note that this function calls free(args)
1112 */
1113/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
1114    void* start = ((void**)args)[0];
1115    void* userData = ((void **)args)[1];
1116    char* name = (char*) ((void **)args)[2];        // we own this storage
1117    free(args);
1118    JNIEnv* env;
1119    int result;
1120
1121    /* hook us into the VM */
1122    if (javaAttachThread(name, &env) != JNI_OK)
1123        return -1;
1124
1125    /* start the thread running */
1126    result = (*(android_thread_func_t)start)(userData);
1127
1128    /* unhook us */
1129    javaDetachThread();
1130    free(name);
1131
1132    return result;
1133}
1134
1135/*
1136 * This is invoked from androidCreateThreadEtc() via the callback
1137 * set with androidSetCreateThreadFunc().
1138 *
1139 * We need to create the new thread in such a way that it gets hooked
1140 * into the VM before it really starts executing.
1141 */
1142/*static*/ int AndroidRuntime::javaCreateThreadEtc(
1143                                android_thread_func_t entryFunction,
1144                                void* userData,
1145                                const char* threadName,
1146                                int32_t threadPriority,
1147                                size_t threadStackSize,
1148                                android_thread_id_t* threadId)
1149{
1150    void** args = (void**) malloc(3 * sizeof(void*));   // javaThreadShell must free
1151    int result;
1152
1153    assert(threadName != NULL);
1154
1155    args[0] = (void*) entryFunction;
1156    args[1] = userData;
1157    args[2] = (void*) strdup(threadName);   // javaThreadShell must free
1158
1159    result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1160        threadName, threadPriority, threadStackSize, threadId);
1161    return result;
1162}
1163
1164/*
1165 * Create a thread that is visible from the VM.
1166 *
1167 * This is called from elsewhere in the library.
1168 */
1169/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
1170    void (*start)(void *), void* arg)
1171{
1172    android_thread_id_t threadId = 0;
1173    javaCreateThreadEtc((android_thread_func_t) start, arg, name,
1174        ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1175    return threadId;
1176}
1177
1178#if 0
1179static void quickTest(void* arg)
1180{
1181    const char* str = (const char*) arg;
1182
1183    printf("In quickTest: %s\n", str);
1184}
1185#endif
1186
1187#ifdef NDEBUG
1188    #define REG_JNI(name)      { name }
1189    struct RegJNIRec {
1190        int (*mProc)(JNIEnv*);
1191    };
1192#else
1193    #define REG_JNI(name)      { name, #name }
1194    struct RegJNIRec {
1195        int (*mProc)(JNIEnv*);
1196        const char* mName;
1197    };
1198#endif
1199
1200typedef void (*RegJAMProc)();
1201
1202static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1203{
1204    for (size_t i = 0; i < count; i++) {
1205        if (array[i].mProc(env) < 0) {
1206#ifndef NDEBUG
1207            LOGD("----------!!! %s failed to load\n", array[i].mName);
1208#endif
1209            return -1;
1210        }
1211    }
1212    return 0;
1213}
1214
1215static void register_jam_procs(const RegJAMProc array[], size_t count)
1216{
1217    for (size_t i = 0; i < count; i++) {
1218        array[i]();
1219    }
1220}
1221
1222static const RegJNIRec gRegJNI[] = {
1223    REG_JNI(register_android_debug_JNITest),
1224    REG_JNI(register_com_android_internal_os_RuntimeInit),
1225    REG_JNI(register_android_os_SystemClock),
1226    REG_JNI(register_android_util_EventLog),
1227    REG_JNI(register_android_util_Log),
1228    REG_JNI(register_android_util_FloatMath),
1229    REG_JNI(register_android_text_format_Time),
1230    REG_JNI(register_android_pim_EventRecurrence),
1231    REG_JNI(register_android_content_AssetManager),
1232    REG_JNI(register_android_content_StringBlock),
1233    REG_JNI(register_android_content_XmlBlock),
1234    REG_JNI(register_android_emoji_EmojiFactory),
1235    REG_JNI(register_android_security_Md5MessageDigest),
1236    REG_JNI(register_android_text_AndroidCharacter),
1237    REG_JNI(register_android_text_AndroidBidi),
1238    REG_JNI(register_android_text_KeyCharacterMap),
1239    REG_JNI(register_android_os_Process),
1240    REG_JNI(register_android_os_Binder),
1241    REG_JNI(register_android_view_Display),
1242    REG_JNI(register_android_nio_utils),
1243    REG_JNI(register_android_graphics_PixelFormat),
1244    REG_JNI(register_android_graphics_Graphics),
1245    REG_JNI(register_android_view_GLES20Canvas),
1246    REG_JNI(register_android_view_Surface),
1247    REG_JNI(register_android_view_ViewRoot),
1248    REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1249    REG_JNI(register_com_google_android_gles_jni_GLImpl),
1250    REG_JNI(register_android_opengl_jni_GLES10),
1251    REG_JNI(register_android_opengl_jni_GLES10Ext),
1252    REG_JNI(register_android_opengl_jni_GLES11),
1253    REG_JNI(register_android_opengl_jni_GLES11Ext),
1254    REG_JNI(register_android_opengl_jni_GLES20),
1255
1256    REG_JNI(register_android_graphics_Bitmap),
1257    REG_JNI(register_android_graphics_BitmapFactory),
1258    REG_JNI(register_android_graphics_Camera),
1259    REG_JNI(register_android_graphics_Canvas),
1260    REG_JNI(register_android_graphics_ColorFilter),
1261    REG_JNI(register_android_graphics_DrawFilter),
1262    REG_JNI(register_android_graphics_Interpolator),
1263    REG_JNI(register_android_graphics_LayerRasterizer),
1264    REG_JNI(register_android_graphics_MaskFilter),
1265    REG_JNI(register_android_graphics_Matrix),
1266    REG_JNI(register_android_graphics_Movie),
1267    REG_JNI(register_android_graphics_NinePatch),
1268    REG_JNI(register_android_graphics_Paint),
1269    REG_JNI(register_android_graphics_Path),
1270    REG_JNI(register_android_graphics_PathMeasure),
1271    REG_JNI(register_android_graphics_PathEffect),
1272    REG_JNI(register_android_graphics_Picture),
1273    REG_JNI(register_android_graphics_PorterDuff),
1274    REG_JNI(register_android_graphics_Rasterizer),
1275    REG_JNI(register_android_graphics_Region),
1276    REG_JNI(register_android_graphics_Shader),
1277    REG_JNI(register_android_graphics_Typeface),
1278    REG_JNI(register_android_graphics_Xfermode),
1279    REG_JNI(register_android_graphics_YuvImage),
1280    REG_JNI(register_com_android_internal_graphics_NativeUtils),
1281
1282    REG_JNI(register_android_database_CursorWindow),
1283    REG_JNI(register_android_database_SQLiteCompiledSql),
1284    REG_JNI(register_android_database_SQLiteDatabase),
1285    REG_JNI(register_android_database_SQLiteDebug),
1286    REG_JNI(register_android_database_SQLiteProgram),
1287    REG_JNI(register_android_database_SQLiteQuery),
1288    REG_JNI(register_android_database_SQLiteStatement),
1289    REG_JNI(register_android_os_Debug),
1290    REG_JNI(register_android_os_FileObserver),
1291    REG_JNI(register_android_os_FileUtils),
1292    REG_JNI(register_android_os_MessageQueue),
1293    REG_JNI(register_android_os_ParcelFileDescriptor),
1294    REG_JNI(register_android_os_Power),
1295    REG_JNI(register_android_os_StatFs),
1296    REG_JNI(register_android_os_SystemProperties),
1297    REG_JNI(register_android_os_UEventObserver),
1298    REG_JNI(register_android_net_LocalSocketImpl),
1299    REG_JNI(register_android_net_NetworkUtils),
1300    REG_JNI(register_android_net_TrafficStats),
1301    REG_JNI(register_android_net_wifi_WifiManager),
1302    REG_JNI(register_android_os_MemoryFile),
1303    REG_JNI(register_com_android_internal_os_ZygoteInit),
1304    REG_JNI(register_android_hardware_Camera),
1305    REG_JNI(register_android_hardware_SensorManager),
1306    REG_JNI(register_android_media_AudioRecord),
1307    REG_JNI(register_android_media_AudioSystem),
1308    REG_JNI(register_android_media_AudioTrack),
1309    REG_JNI(register_android_media_JetPlayer),
1310    REG_JNI(register_android_media_ToneGenerator),
1311
1312    REG_JNI(register_android_opengl_classes),
1313    REG_JNI(register_android_bluetooth_HeadsetBase),
1314    REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
1315    REG_JNI(register_android_bluetooth_BluetoothSocket),
1316    REG_JNI(register_android_server_BluetoothService),
1317    REG_JNI(register_android_server_BluetoothEventLoop),
1318    REG_JNI(register_android_server_BluetoothA2dpService),
1319    REG_JNI(register_android_server_Watchdog),
1320    REG_JNI(register_android_message_digest_sha1),
1321    REG_JNI(register_android_ddm_DdmHandleNativeHeap),
1322    REG_JNI(register_android_backup_BackupDataInput),
1323    REG_JNI(register_android_backup_BackupDataOutput),
1324    REG_JNI(register_android_backup_FileBackupHelperBase),
1325    REG_JNI(register_android_backup_BackupHelperDispatcher),
1326
1327    REG_JNI(register_android_app_NativeActivity),
1328    REG_JNI(register_android_view_InputChannel),
1329    REG_JNI(register_android_view_InputQueue),
1330    REG_JNI(register_android_view_KeyEvent),
1331    REG_JNI(register_android_view_MotionEvent),
1332
1333    REG_JNI(register_android_content_res_ObbScanner),
1334};
1335
1336/*
1337 * Register android native functions with the VM.
1338 */
1339/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1340{
1341    /*
1342     * This hook causes all future threads created in this process to be
1343     * attached to the JavaVM.  (This needs to go away in favor of JNI
1344     * Attach calls.)
1345     */
1346    androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1347
1348    LOGD("--- registering native functions ---\n");
1349
1350    /*
1351     * Every "register" function calls one or more things that return
1352     * a local reference (e.g. FindClass).  Because we haven't really
1353     * started the VM yet, they're all getting stored in the base frame
1354     * and never released.  Use Push/Pop to manage the storage.
1355     */
1356    env->PushLocalFrame(200);
1357
1358    if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1359        env->PopLocalFrame(NULL);
1360        return -1;
1361    }
1362    env->PopLocalFrame(NULL);
1363
1364    //createJavaThread("fubar", quickTest, (void*) "hello");
1365
1366    return 0;
1367}
1368
1369AndroidRuntime* AndroidRuntime::getRuntime()
1370{
1371    return gCurRuntime;
1372}
1373
1374/**
1375 * Used by WithFramework to register native functions.
1376 */
1377extern "C"
1378jint Java_com_android_internal_util_WithFramework_registerNatives(
1379        JNIEnv* env, jclass clazz) {
1380    return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1381}
1382
1383/**
1384 * Used by LoadClass to register native functions.
1385 */
1386extern "C"
1387jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1388    return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1389}
1390
1391}   // namespace android
1392