blob: 78bef91e8595bcc1b01cf75e02601a3c9ce82145 [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>
23#include <utils/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
102 Vector<JavaVMOption> mOptions;
103
104 /* JNI JavaVM pointer */
105 static JavaVM* mJavaVM;
106
107 /*
108 * Thread creation helpers.
109 */
110 static int javaCreateThreadEtc(
111 android_thread_func_t entryFunction,
112 void* userData,
113 const char* threadName,
114 int32_t threadPriority,
115 size_t threadStackSize,
116 android_thread_id_t* threadId);
117 static int javaThreadShell(void* args);
118};
119
120// Returns the Unix file descriptor for a ParcelFileDescriptor object
121extern int getParcelFileDescriptorFD(JNIEnv* env, jobject object);
122
123}
124
125#endif