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