AndroidRuntime.h revision 81ea83d10883886013bc95eac2fe032acf1e7aa9
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    /**
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    /**
49     * Call a static Java function that takes no arguments and returns void.
50     */
51    status_t callStatic(const char* className, const char* methodName);
52
53    /**
54     * Call a class's static main method with the given arguments,
55     */
56    status_t callMain(const char* className, int argc, const char* const argv[]);
57
58    /**
59     * Find a class, with the input either of the form
60     * "package/class" or "package.class".
61     */
62    static jclass findClass(JNIEnv* env, const char* className);
63
64    int addVmArguments(int argc, const char* const argv[]);
65
66    void start(const char *classname, const bool startSystemServer);
67    void start();       // start in android.util.RuntimeInit
68
69    static AndroidRuntime* getRuntime();
70
71    /**
72     * This gets called after the JavaVM has initialized.  Override it
73     * with the system's native entry point.
74     */
75    virtual void onStarted() = 0;
76
77    /**
78     * This gets called after the JavaVM has initialized after a Zygote
79     * fork. Override it to initialize threads, etc. Upon return, the
80     * correct static main will be invoked.
81     */
82    virtual void onZygoteInit() {};
83
84
85    /**
86     * Called when the Java application exits.  The default
87     * implementation calls exit(code).
88     */
89    virtual void onExit(int code);
90
91    /** create a new thread that is visible from Java */
92    static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
93        void* arg);
94
95    /** return a pointer to the VM running in this process */
96    static JavaVM* getJavaVM() { return mJavaVM; }
97
98    /** return a pointer to the JNIEnv pointer for this thread */
99    static JNIEnv* getJNIEnv();
100
101private:
102    static int startReg(JNIEnv* env);
103    int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
104
105    Vector<JavaVMOption> mOptions;
106
107    /* JNI JavaVM pointer */
108    static JavaVM* mJavaVM;
109
110    /*
111     * Thread creation helpers.
112     */
113    static int javaCreateThreadEtc(
114                                android_thread_func_t entryFunction,
115                                void* userData,
116                                const char* threadName,
117                                int32_t threadPriority,
118                                size_t threadStackSize,
119                                android_thread_id_t* threadId);
120    static int javaThreadShell(void* args);
121};
122
123// Returns the Unix file descriptor for a ParcelFileDescriptor object
124extern int getParcelFileDescriptorFD(JNIEnv* env, jobject object);
125
126extern CursorWindow * get_window_from_object(JNIEnv * env, jobject javaWindow);
127
128}
129
130#endif
131