AndroidRuntime.h revision ebed7d6e35f7f960e6e6add2b8ab7c7a31a511c3
1/*
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>
23#include <binder/IBinder.h>
24#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 {
33
34class CursorWindow;
35
36class AndroidRuntime
37{
38public:
39    AndroidRuntime();
40    virtual ~AndroidRuntime();
41
42    enum StartMode {
43        Zygote,
44        SystemServer,
45        Application,
46        Tool,
47    };
48
49    /**
50     * Register a set of methods in the specified class.
51     */
52    static int registerNativeMethods(JNIEnv* env,
53        const char* className, const JNINativeMethod* gMethods, int numMethods);
54
55    /**
56     * Call a class's static main method with the given arguments,
57     */
58    status_t callMain(const char* className, jclass clazz, int argc,
59        const char* const argv[]);
60
61    /**
62     * Find a class, with the input either of the form
63     * "package/class" or "package.class".
64     */
65    static jclass findClass(JNIEnv* env, const char* className);
66
67    int addVmArguments(int argc, const char* const argv[]);
68
69    void start(const char *classname, const char* options);
70
71    static AndroidRuntime* getRuntime();
72
73    /**
74     * This gets called after the VM has been created, but before we
75     * run any code. Override it to make any FindClass calls that need
76     * to use CLASSPATH.
77     */
78    virtual void onVmCreated(JNIEnv* env);
79
80    /**
81     * This gets called after the JavaVM has initialized.  Override it
82     * with the system's native entry point.
83     */
84    virtual void onStarted() = 0;
85
86    /**
87     * This gets called after the JavaVM has initialized after a Zygote
88     * fork. Override it to initialize threads, etc. Upon return, the
89     * correct static main will be invoked.
90     */
91    virtual void onZygoteInit() {};
92
93
94    /**
95     * Called when the Java application exits.  The default
96     * implementation calls exit(code).
97     */
98    virtual void onExit(int code);
99
100    /** create a new thread that is visible from Java */
101    static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
102        void* arg);
103
104    /** return a pointer to the VM running in this process */
105    static JavaVM* getJavaVM() { return mJavaVM; }
106
107    /** return a pointer to the JNIEnv pointer for this thread */
108    static JNIEnv* getJNIEnv();
109
110    /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
111    static char* toSlashClassName(const char* className);
112
113private:
114    static int startReg(JNIEnv* env);
115    void parseExtraOpts(char* extraOptsBuf);
116    int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
117
118    Vector<JavaVMOption> mOptions;
119
120    /* JNI JavaVM pointer */
121    static JavaVM* mJavaVM;
122
123    /*
124     * Thread creation helpers.
125     */
126    static int javaCreateThreadEtc(
127                                android_thread_func_t entryFunction,
128                                void* userData,
129                                const char* threadName,
130                                int32_t threadPriority,
131                                size_t threadStackSize,
132                                android_thread_id_t* threadId);
133    static int javaThreadShell(void* args);
134};
135
136extern CursorWindow * get_window_from_object(JNIEnv * env, jobject javaWindow);
137
138}
139
140#endif
141