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