blob: b02a057efab102c18f03bce0fdf39a304349f9ed [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18
19#ifndef _RUNTIME_ANDROID_RUNTIME_H
20#define _RUNTIME_ANDROID_RUNTIME_H
21
22#include <utils/Errors.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IBinder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/String8.h>
25#include <utils/String16.h>
26#include <utils/Vector.h>
27#include <utils/threads.h>
28#include <pthread.h>
29#include <nativehelper/jni.h>
30
31
32namespace android {
Mike Lockwood81ea83d2010-06-30 17:49:41 -040033
34class CursorWindow;
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036class AndroidRuntime
37{
38public:
39 AndroidRuntime();
40 virtual ~AndroidRuntime();
41
42 /**
43 * Register a set of methods in the specified class.
44 */
45 static int registerNativeMethods(JNIEnv* env,
46 const char* className, const JNINativeMethod* gMethods, int numMethods);
47
48 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * Call a class's static main method with the given arguments,
50 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -070051 status_t callMain(const char* className, jclass clazz, int argc,
52 const char* const argv[]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 /**
Elliott Hughesa3804cf2011-04-11 16:50:19 -070055 * Find a class, with the input either of the form
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * "package/class" or "package.class".
57 */
58 static jclass findClass(JNIEnv* env, const char* className);
59
60 int addVmArguments(int argc, const char* const argv[]);
61
62 void start(const char *classname, const bool startSystemServer);
63 void start(); // start in android.util.RuntimeInit
64
65 static AndroidRuntime* getRuntime();
Elliott Hughesa3804cf2011-04-11 16:50:19 -070066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 /**
Elliott Hughesd195e5a2011-04-13 15:39:37 -070068 * This gets called after the VM has been created, but before we
69 * run any code. Override it to make any FindClass calls that need
70 * to use CLASSPATH.
71 */
72 virtual void onVmCreated(JNIEnv* env);
73
74 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * This gets called after the JavaVM has initialized. Override it
76 * with the system's native entry point.
77 */
78 virtual void onStarted() = 0;
79
80 /**
81 * This gets called after the JavaVM has initialized after a Zygote
82 * fork. Override it to initialize threads, etc. Upon return, the
83 * correct static main will be invoked.
84 */
85 virtual void onZygoteInit() {};
86
87
88 /**
89 * Called when the Java application exits. The default
90 * implementation calls exit(code).
91 */
92 virtual void onExit(int code);
93
94 /** create a new thread that is visible from Java */
Mike Lockwoodf602d362010-06-20 14:28:16 -070095 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 void* arg);
97
98 /** return a pointer to the VM running in this process */
99 static JavaVM* getJavaVM() { return mJavaVM; }
100
101 /** return a pointer to the JNIEnv pointer for this thread */
102 static JNIEnv* getJNIEnv();
103
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700104 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
105 static char* toSlashClassName(const char* className);
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107private:
108 static int startReg(JNIEnv* env);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700109 void parseExtraOpts(char* extraOptsBuf);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700110 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
112 Vector<JavaVMOption> mOptions;
113
114 /* JNI JavaVM pointer */
115 static JavaVM* mJavaVM;
116
117 /*
118 * Thread creation helpers.
119 */
120 static int javaCreateThreadEtc(
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700121 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 void* userData,
123 const char* threadName,
124 int32_t threadPriority,
125 size_t threadStackSize,
126 android_thread_id_t* threadId);
127 static int javaThreadShell(void* args);
128};
129
Mike Lockwood81ea83d2010-06-30 17:49:41 -0400130extern CursorWindow * get_window_from_object(JNIEnv * env, jobject javaWindow);
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132}
133
134#endif