blob: a361846ea416b0ab4786d12dc2956fb69b5b91b0 [file] [log] [blame]
Dianne Hackborn69969e42010-05-04 11:40:40 -07001/*
2 * Copyright (C) 2010 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#ifndef ANDROID_NATIVE_ACTIVITY_H
19#define ANDROID_NATIVE_ACTIVITY_H
20
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <jni.h>
25
Christopher Tate6cce32b2010-07-12 18:21:36 -070026#include <android/asset_manager.h>
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070027#include <android/input.h>
Dianne Hackborn54a181b2010-06-30 18:35:14 -070028#include <android/native_window.h>
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070029
Dianne Hackborn69969e42010-05-04 11:40:40 -070030#ifdef __cplusplus
31extern "C" {
32#endif
33
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070034struct ANativeActivityCallbacks;
Dianne Hackborn69969e42010-05-04 11:40:40 -070035
Dianne Hackborn74323fd2010-05-18 17:56:23 -070036/**
37 * This structure defines the native side of an android.app.NativeActivity.
38 * It is created by the framework, and handed to the application's native
39 * code as it is being launched.
40 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070041typedef struct ANativeActivity {
Dianne Hackborn74323fd2010-05-18 17:56:23 -070042 /**
43 * Pointer to the callback function table of the native application.
44 * You can set the functions here to your own callbacks. The callbacks
45 * pointer itself here should not be changed; it is allocated and managed
46 * for you by the framework.
47 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070048 struct ANativeActivityCallbacks* callbacks;
Dianne Hackborn69969e42010-05-04 11:40:40 -070049
Dianne Hackborn74323fd2010-05-18 17:56:23 -070050 /**
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070051 * The global handle on the process's Java VM.
52 */
53 JavaVM* vm;
54
55 /**
56 * JNI context for the main thread of the app. Note that this field
57 * can ONLY be used from the main thread of the process; that is, the
58 * thread that calls into the ANativeActivityCallbacks.
Dianne Hackborn74323fd2010-05-18 17:56:23 -070059 */
Dianne Hackborn69969e42010-05-04 11:40:40 -070060 JNIEnv* env;
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -070061
Dianne Hackborn74323fd2010-05-18 17:56:23 -070062 /**
63 * The NativeActivity Java class.
64 */
Dianne Hackborn69969e42010-05-04 11:40:40 -070065 jobject clazz;
66
Dianne Hackborn74323fd2010-05-18 17:56:23 -070067 /**
Dianne Hackborn68267412010-07-02 18:52:01 -070068 * Path to this application's internal data directory.
69 */
70 const char* internalDataPath;
71
72 /**
73 * Path to this application's external (removable/mountable) data directory.
74 */
75 const char* externalDataPath;
76
77 /**
78 * The platform's SDK version code.
79 */
80 int32_t sdkVersion;
81
82 /**
Dianne Hackborn74323fd2010-05-18 17:56:23 -070083 * This is the native instance of the application. It is not used by
84 * the framework, but can be set by the application to its own instance
85 * state.
86 */
Dianne Hackborn69969e42010-05-04 11:40:40 -070087 void* instance;
Christopher Tate6cce32b2010-07-12 18:21:36 -070088
89 /**
90 * Pointer to the Asset Manager instance for the application. The application
91 * uses this to access binary assets bundled inside its own .apk file.
92 */
93 AAssetManager* assetManager;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -080094
95 /**
96 * Available starting with Honeycomb: path to the directory containing
97 * the application's OBB files (if any). If the app doesn't have any
98 * OBB files, this directory may not exist.
99 */
100 const char* obbPath;
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700101} ANativeActivity;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700102
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700103/**
104 * These are the callbacks the framework makes into a native application.
105 * All of these callbacks happen on the main thread of the application.
106 * By default, all callbacks are NULL; set to a pointer to your own function
107 * to have it called.
108 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700109typedef struct ANativeActivityCallbacks {
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700110 /**
111 * NativeActivity has started. See Java documentation for Activity.onStart()
112 * for more information.
113 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700114 void (*onStart)(ANativeActivity* activity);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700115
116 /**
117 * NativeActivity has resumed. See Java documentation for Activity.onResume()
118 * for more information.
119 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700120 void (*onResume)(ANativeActivity* activity);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700121
122 /**
123 * Framework is asking NativeActivity to save its current instance state.
124 * See Java documentation for Activity.onSaveInstanceState() for more
125 * information. The returned pointer needs to be created with malloc();
126 * the framework will call free() on it for you. You also must fill in
127 * outSize with the number of bytes in the allocation. Note that the
128 * saved state will be persisted, so it can not contain any active
129 * entities (pointers to memory, file descriptors, etc).
130 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700131 void* (*onSaveInstanceState)(ANativeActivity* activity, size_t* outSize);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700132
133 /**
134 * NativeActivity has paused. See Java documentation for Activity.onPause()
135 * for more information.
136 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700137 void (*onPause)(ANativeActivity* activity);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700138
139 /**
140 * NativeActivity has stopped. See Java documentation for Activity.onStop()
141 * for more information.
142 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700143 void (*onStop)(ANativeActivity* activity);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700144
145 /**
146 * NativeActivity is being destroyed. See Java documentation for Activity.onDestroy()
147 * for more information.
148 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700149 void (*onDestroy)(ANativeActivity* activity);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700150
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700151 /**
152 * Focus has changed in this NativeActivity's window. This is often used,
153 * for example, to pause a game when it loses input focus.
154 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700155 void (*onWindowFocusChanged)(ANativeActivity* activity, int hasFocus);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700156
157 /**
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700158 * The drawing window for this native activity has been created. You
159 * can use the given native window object to start drawing.
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700160 */
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700161 void (*onNativeWindowCreated)(ANativeActivity* activity, ANativeWindow* window);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700162
163 /**
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700164 * The drawing window for this native activity has been resized. You should
165 * retrieve the new size from the window and ensure that your rendering in
166 * it now matches.
167 */
168 void (*onNativeWindowResized)(ANativeActivity* activity, ANativeWindow* window);
169
170 /**
171 * The drawing window for this native activity needs to be redrawn. To avoid
172 * transient artifacts during screen changes (such resizing after rotation),
173 * applications should not return from this function until they have finished
174 * drawing their window in its current state.
175 */
176 void (*onNativeWindowRedrawNeeded)(ANativeActivity* activity, ANativeWindow* window);
177
178 /**
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700179 * The drawing window for this native activity is going to be destroyed.
180 * You MUST ensure that you do not touch the window object after returning
181 * from this function: in the common case of drawing to the window from
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700182 * another thread, that means the implementation of this callback must
183 * properly synchronize with the other thread to stop its drawing before
184 * returning from here.
185 */
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700186 void (*onNativeWindowDestroyed)(ANativeActivity* activity, ANativeWindow* window);
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700187
188 /**
189 * The input queue for this native activity's window has been created.
190 * You can use the given input queue to start retrieving input events.
191 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700192 void (*onInputQueueCreated)(ANativeActivity* activity, AInputQueue* queue);
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700193
194 /**
195 * The input queue for this native activity's window is being destroyed.
196 * You should no longer try to reference this object upon returning from this
197 * function.
198 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700199 void (*onInputQueueDestroyed)(ANativeActivity* activity, AInputQueue* queue);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700200
201 /**
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700202 * The rectangle in the window in which content should be placed has changed.
203 */
204 void (*onContentRectChanged)(ANativeActivity* activity, const ARect* rect);
205
206 /**
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700207 * The current device AConfiguration has changed. The new configuration can
208 * be retrieved from assetManager.
209 */
210 void (*onConfigurationChanged)(ANativeActivity* activity);
211
212 /**
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700213 * The system is running low on memory. Use this callback to release
214 * resources you do not need, to help the system avoid killing more
215 * important processes.
216 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700217 void (*onLowMemory)(ANativeActivity* activity);
218} ANativeActivityCallbacks;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700219
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700220/**
221 * This is the function that must be in the native code to instantiate the
222 * application's native activity. It is called with the activity instance (see
223 * above); if the code is being instantiated from a previously saved instance,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700224 * the savedState will be non-NULL and point to the saved data. You must make
225 * any copy of this data you need -- it will be released after you return from
226 * this function.
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700227 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700228typedef void ANativeActivity_createFunc(ANativeActivity* activity,
Dianne Hackborn69969e42010-05-04 11:40:40 -0700229 void* savedState, size_t savedStateSize);
230
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700231/**
232 * The name of the function that NativeInstance looks for when launching its
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700233 * native code. This is the default function that is used, you can specify
234 * "android.app.func_name" string meta-data in your manifest to use a different
235 * function.
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700236 */
Dianne Hackborn2e9f93e2010-06-28 15:27:30 -0700237extern ANativeActivity_createFunc ANativeActivity_onCreate;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700238
Dianne Hackborndb28a942010-10-21 17:22:30 -0700239/**
240 * Finish the given activity. Its finish() method will be called, causing it
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700241 * to be stopped and destroyed. Note that this method can be called from
242 * *any* thread; it will send a message to the main thread of the process
243 * where the Java finish call will take place.
Dianne Hackborndb28a942010-10-21 17:22:30 -0700244 */
245void ANativeActivity_finish(ANativeActivity* activity);
246
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700247/**
248 * Change the window format of the given activity. Calls getWindow().setFormat()
249 * of the given activity. Note that this method can be called from
250 * *any* thread; it will send a message to the main thread of the process
251 * where the Java finish call will take place.
252 */
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700253void ANativeActivity_setWindowFormat(ANativeActivity* activity, int32_t format);
254
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700255/**
256 * Change the window flags of the given activity. Calls getWindow().setFlags()
257 * of the given activity. Note that this method can be called from
258 * *any* thread; it will send a message to the main thread of the process
259 * where the Java finish call will take place. See window.h for flag constants.
260 */
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700261void ANativeActivity_setWindowFlags(ANativeActivity* activity,
262 uint32_t addFlags, uint32_t removeFlags);
263
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700264/**
265 * Flags for ANativeActivity_showSoftInput; see the Java InputMethodManager
266 * API for documentation.
267 */
268enum {
269 ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = 0x0001,
270 ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = 0x0002,
271};
272
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700273/**
274 * Show the IME while in the given activity. Calls InputMethodManager.showSoftInput()
275 * for the given activity. Note that this method can be called from
276 * *any* thread; it will send a message to the main thread of the process
277 * where the Java finish call will take place.
278 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700279void ANativeActivity_showSoftInput(ANativeActivity* activity, uint32_t flags);
280
281/**
282 * Flags for ANativeActivity_hideSoftInput; see the Java InputMethodManager
283 * API for documentation.
284 */
285enum {
286 ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = 0x0001,
287 ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = 0x0002,
288};
289
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700290/**
291 * Hide the IME while in the given activity. Calls InputMethodManager.hideSoftInput()
292 * for the given activity. Note that this method can be called from
293 * *any* thread; it will send a message to the main thread of the process
294 * where the Java finish call will take place.
295 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700296void ANativeActivity_hideSoftInput(ANativeActivity* activity, uint32_t flags);
297
Dianne Hackborn69969e42010-05-04 11:40:40 -0700298#ifdef __cplusplus
299};
300#endif
301
302#endif // ANDROID_NATIVE_ACTIVITY_H
303