| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 26 | #include <android/asset_manager.h> |
| Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 27 | #include <android/input.h> |
| Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 28 | #include <android/native_window.h> |
| Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 29 | |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 34 | struct ANativeActivityCallbacks; |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 35 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 36 | /** |
| 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 Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 41 | typedef struct ANativeActivity { |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 42 | /** |
| 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 Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 48 | struct ANativeActivityCallbacks* callbacks; |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 49 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 50 | /** |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 51 | * 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 Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 59 | */ |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 60 | JNIEnv* env; |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 61 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 62 | /** |
| 63 | * The NativeActivity Java class. |
| 64 | */ |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 65 | jobject clazz; |
| 66 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 67 | /** |
| Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 68 | * 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 Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 83 | * 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 Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 87 | void* instance; |
| Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 88 | |
| 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 Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 94 | } ANativeActivity; |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 95 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 96 | /** |
| 97 | * These are the callbacks the framework makes into a native application. |
| 98 | * All of these callbacks happen on the main thread of the application. |
| 99 | * By default, all callbacks are NULL; set to a pointer to your own function |
| 100 | * to have it called. |
| 101 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 102 | typedef struct ANativeActivityCallbacks { |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 103 | /** |
| 104 | * NativeActivity has started. See Java documentation for Activity.onStart() |
| 105 | * for more information. |
| 106 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 107 | void (*onStart)(ANativeActivity* activity); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * NativeActivity has resumed. See Java documentation for Activity.onResume() |
| 111 | * for more information. |
| 112 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 113 | void (*onResume)(ANativeActivity* activity); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Framework is asking NativeActivity to save its current instance state. |
| 117 | * See Java documentation for Activity.onSaveInstanceState() for more |
| 118 | * information. The returned pointer needs to be created with malloc(); |
| 119 | * the framework will call free() on it for you. You also must fill in |
| 120 | * outSize with the number of bytes in the allocation. Note that the |
| 121 | * saved state will be persisted, so it can not contain any active |
| 122 | * entities (pointers to memory, file descriptors, etc). |
| 123 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 124 | void* (*onSaveInstanceState)(ANativeActivity* activity, size_t* outSize); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * NativeActivity has paused. See Java documentation for Activity.onPause() |
| 128 | * for more information. |
| 129 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 130 | void (*onPause)(ANativeActivity* activity); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * NativeActivity has stopped. See Java documentation for Activity.onStop() |
| 134 | * for more information. |
| 135 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 136 | void (*onStop)(ANativeActivity* activity); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * NativeActivity is being destroyed. See Java documentation for Activity.onDestroy() |
| 140 | * for more information. |
| 141 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 142 | void (*onDestroy)(ANativeActivity* activity); |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 143 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Focus has changed in this NativeActivity's window. This is often used, |
| 146 | * for example, to pause a game when it loses input focus. |
| 147 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 148 | void (*onWindowFocusChanged)(ANativeActivity* activity, int hasFocus); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 149 | |
| 150 | /** |
| Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 151 | * The drawing window for this native activity has been created. You |
| 152 | * can use the given native window object to start drawing. |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 153 | */ |
| Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 154 | void (*onNativeWindowCreated)(ANativeActivity* activity, ANativeWindow* window); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 157 | * The drawing window for this native activity has been resized. You should |
| 158 | * retrieve the new size from the window and ensure that your rendering in |
| 159 | * it now matches. |
| 160 | */ |
| 161 | void (*onNativeWindowResized)(ANativeActivity* activity, ANativeWindow* window); |
| 162 | |
| 163 | /** |
| 164 | * The drawing window for this native activity needs to be redrawn. To avoid |
| 165 | * transient artifacts during screen changes (such resizing after rotation), |
| 166 | * applications should not return from this function until they have finished |
| 167 | * drawing their window in its current state. |
| 168 | */ |
| 169 | void (*onNativeWindowRedrawNeeded)(ANativeActivity* activity, ANativeWindow* window); |
| 170 | |
| 171 | /** |
| Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 172 | * The drawing window for this native activity is going to be destroyed. |
| 173 | * You MUST ensure that you do not touch the window object after returning |
| 174 | * from this function: in the common case of drawing to the window from |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 175 | * another thread, that means the implementation of this callback must |
| 176 | * properly synchronize with the other thread to stop its drawing before |
| 177 | * returning from here. |
| 178 | */ |
| Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 179 | void (*onNativeWindowDestroyed)(ANativeActivity* activity, ANativeWindow* window); |
| Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * The input queue for this native activity's window has been created. |
| 183 | * You can use the given input queue to start retrieving input events. |
| 184 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 185 | void (*onInputQueueCreated)(ANativeActivity* activity, AInputQueue* queue); |
| Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * The input queue for this native activity's window is being destroyed. |
| 189 | * You should no longer try to reference this object upon returning from this |
| 190 | * function. |
| 191 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 192 | void (*onInputQueueDestroyed)(ANativeActivity* activity, AInputQueue* queue); |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
| Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 195 | * The rectangle in the window in which content should be placed has changed. |
| 196 | */ |
| 197 | void (*onContentRectChanged)(ANativeActivity* activity, const ARect* rect); |
| 198 | |
| 199 | /** |
| Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 200 | * The current device AConfiguration has changed. The new configuration can |
| 201 | * be retrieved from assetManager. |
| 202 | */ |
| 203 | void (*onConfigurationChanged)(ANativeActivity* activity); |
| 204 | |
| 205 | /** |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 206 | * The system is running low on memory. Use this callback to release |
| 207 | * resources you do not need, to help the system avoid killing more |
| 208 | * important processes. |
| 209 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 210 | void (*onLowMemory)(ANativeActivity* activity); |
| 211 | } ANativeActivityCallbacks; |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 212 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 213 | /** |
| 214 | * This is the function that must be in the native code to instantiate the |
| 215 | * application's native activity. It is called with the activity instance (see |
| 216 | * above); if the code is being instantiated from a previously saved instance, |
| Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 217 | * the savedState will be non-NULL and point to the saved data. You must make |
| 218 | * any copy of this data you need -- it will be released after you return from |
| 219 | * this function. |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 220 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 221 | typedef void ANativeActivity_createFunc(ANativeActivity* activity, |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 222 | void* savedState, size_t savedStateSize); |
| 223 | |
| Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 224 | /** |
| 225 | * The name of the function that NativeInstance looks for when launching its |
| 226 | * native code. |
| 227 | */ |
| Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 228 | extern ANativeActivity_createFunc ANativeActivity_onCreate; |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 229 | |
| Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 230 | void ANativeActivity_setWindowFormat(ANativeActivity* activity, int32_t format); |
| 231 | |
| 232 | void ANativeActivity_setWindowFlags(ANativeActivity* activity, |
| 233 | uint32_t addFlags, uint32_t removeFlags); |
| 234 | |
| Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Flags for ANativeActivity_showSoftInput; see the Java InputMethodManager |
| 237 | * API for documentation. |
| 238 | */ |
| 239 | enum { |
| 240 | ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = 0x0001, |
| 241 | ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = 0x0002, |
| 242 | }; |
| 243 | |
| 244 | void ANativeActivity_showSoftInput(ANativeActivity* activity, uint32_t flags); |
| 245 | |
| 246 | /** |
| 247 | * Flags for ANativeActivity_hideSoftInput; see the Java InputMethodManager |
| 248 | * API for documentation. |
| 249 | */ |
| 250 | enum { |
| 251 | ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = 0x0001, |
| 252 | ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = 0x0002, |
| 253 | }; |
| 254 | |
| 255 | void ANativeActivity_hideSoftInput(ANativeActivity* activity, uint32_t flags); |
| 256 | |
| Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 257 | #ifdef __cplusplus |
| 258 | }; |
| 259 | #endif |
| 260 | |
| 261 | #endif // ANDROID_NATIVE_ACTIVITY_H |
| 262 | |