native_activity.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * @addtogroup NativeActivity Native Activity
  18. * @{
  19. */
  20. /**
  21. * @file native_activity.h
  22. */
  23. #ifndef ANDROID_NATIVE_ACTIVITY_H
  24. #define ANDROID_NATIVE_ACTIVITY_H
  25. #include <stdint.h>
  26. #include <sys/types.h>
  27. #include <jni.h>
  28. #include <android/asset_manager.h>
  29. #include <android/input.h>
  30. #include <android/native_window.h>
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * {@link ANativeActivityCallbacks}
  36. */
  37. struct ANativeActivityCallbacks;
  38. /**
  39. * This structure defines the native side of an android.app.NativeActivity.
  40. * It is created by the framework, and handed to the application's native
  41. * code as it is being launched.
  42. */
  43. typedef struct ANativeActivity {
  44. /**
  45. * Pointer to the callback function table of the native application.
  46. * You can set the functions here to your own callbacks. The callbacks
  47. * pointer itself here should not be changed; it is allocated and managed
  48. * for you by the framework.
  49. */
  50. struct ANativeActivityCallbacks* callbacks;
  51. /**
  52. * The global handle on the process's Java VM.
  53. */
  54. JavaVM* vm;
  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.
  59. */
  60. JNIEnv* env;
  61. /**
  62. * The NativeActivity object handle.
  63. *
  64. * IMPORTANT NOTE: This member is mis-named. It should really be named
  65. * 'activity' instead of 'clazz', since it's a reference to the
  66. * NativeActivity instance created by the system for you.
  67. *
  68. * We unfortunately cannot change this without breaking NDK
  69. * source-compatibility.
  70. */
  71. jobject clazz;
  72. /**
  73. * Path to this application's internal data directory.
  74. */
  75. const char* internalDataPath;
  76. /**
  77. * Path to this application's external (removable/mountable) data directory.
  78. */
  79. const char* externalDataPath;
  80. /**
  81. * The platform's SDK version code.
  82. */
  83. int32_t sdkVersion;
  84. /**
  85. * This is the native instance of the application. It is not used by
  86. * the framework, but can be set by the application to its own instance
  87. * state.
  88. */
  89. void* instance;
  90. /**
  91. * Pointer to the Asset Manager instance for the application. The application
  92. * uses this to access binary assets bundled inside its own .apk file.
  93. */
  94. AAssetManager* assetManager;
  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;
  101. } ANativeActivity;
  102. /**
  103. * These are the callbacks the framework makes into a native application.
  104. * All of these callbacks happen on the main thread of the application.
  105. * By default, all callbacks are NULL; set to a pointer to your own function
  106. * to have it called.
  107. */
  108. typedef struct ANativeActivityCallbacks {
  109. /**
  110. * NativeActivity has started. See Java documentation for Activity.onStart()
  111. * for more information.
  112. */
  113. void (*onStart)(ANativeActivity* activity);
  114. /**
  115. * NativeActivity has resumed. See Java documentation for Activity.onResume()
  116. * for more information.
  117. */
  118. void (*onResume)(ANativeActivity* activity);
  119. /**
  120. * Framework is asking NativeActivity to save its current instance state.
  121. * See Java documentation for Activity.onSaveInstanceState() for more
  122. * information. The returned pointer needs to be created with malloc();
  123. * the framework will call free() on it for you. You also must fill in
  124. * outSize with the number of bytes in the allocation. Note that the
  125. * saved state will be persisted, so it can not contain any active
  126. * entities (pointers to memory, file descriptors, etc).
  127. */
  128. void* (*onSaveInstanceState)(ANativeActivity* activity, size_t* outSize);
  129. /**
  130. * NativeActivity has paused. See Java documentation for Activity.onPause()
  131. * for more information.
  132. */
  133. void (*onPause)(ANativeActivity* activity);
  134. /**
  135. * NativeActivity has stopped. See Java documentation for Activity.onStop()
  136. * for more information.
  137. */
  138. void (*onStop)(ANativeActivity* activity);
  139. /**
  140. * NativeActivity is being destroyed. See Java documentation for Activity.onDestroy()
  141. * for more information.
  142. */
  143. void (*onDestroy)(ANativeActivity* activity);
  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. */
  148. void (*onWindowFocusChanged)(ANativeActivity* activity, int hasFocus);
  149. /**
  150. * The drawing window for this native activity has been created. You
  151. * can use the given native window object to start drawing.
  152. */
  153. void (*onNativeWindowCreated)(ANativeActivity* activity, ANativeWindow* window);
  154. /**
  155. * The drawing window for this native activity has been resized. You should
  156. * retrieve the new size from the window and ensure that your rendering in
  157. * it now matches.
  158. */
  159. void (*onNativeWindowResized)(ANativeActivity* activity, ANativeWindow* window);
  160. /**
  161. * The drawing window for this native activity needs to be redrawn. To avoid
  162. * transient artifacts during screen changes (such resizing after rotation),
  163. * applications should not return from this function until they have finished
  164. * drawing their window in its current state.
  165. */
  166. void (*onNativeWindowRedrawNeeded)(ANativeActivity* activity, ANativeWindow* window);
  167. /**
  168. * The drawing window for this native activity is going to be destroyed.
  169. * You MUST ensure that you do not touch the window object after returning
  170. * from this function: in the common case of drawing to the window from
  171. * another thread, that means the implementation of this callback must
  172. * properly synchronize with the other thread to stop its drawing before
  173. * returning from here.
  174. */
  175. void (*onNativeWindowDestroyed)(ANativeActivity* activity, ANativeWindow* window);
  176. /**
  177. * The input queue for this native activity's window has been created.
  178. * You can use the given input queue to start retrieving input events.
  179. */
  180. void (*onInputQueueCreated)(ANativeActivity* activity, AInputQueue* queue);
  181. /**
  182. * The input queue for this native activity's window is being destroyed.
  183. * You should no longer try to reference this object upon returning from this
  184. * function.
  185. */
  186. void (*onInputQueueDestroyed)(ANativeActivity* activity, AInputQueue* queue);
  187. /**
  188. * The rectangle in the window in which content should be placed has changed.
  189. */
  190. void (*onContentRectChanged)(ANativeActivity* activity, const ARect* rect);
  191. /**
  192. * The current device AConfiguration has changed. The new configuration can
  193. * be retrieved from assetManager.
  194. */
  195. void (*onConfigurationChanged)(ANativeActivity* activity);
  196. /**
  197. * The system is running low on memory. Use this callback to release
  198. * resources you do not need, to help the system avoid killing more
  199. * important processes.
  200. */
  201. void (*onLowMemory)(ANativeActivity* activity);
  202. } ANativeActivityCallbacks;
  203. /**
  204. * This is the function that must be in the native code to instantiate the
  205. * application's native activity. It is called with the activity instance (see
  206. * above); if the code is being instantiated from a previously saved instance,
  207. * the savedState will be non-NULL and point to the saved data. You must make
  208. * any copy of this data you need -- it will be released after you return from
  209. * this function.
  210. */
  211. typedef void ANativeActivity_createFunc(ANativeActivity* activity,
  212. void* savedState, size_t savedStateSize);
  213. /**
  214. * The name of the function that NativeInstance looks for when launching its
  215. * native code. This is the default function that is used, you can specify
  216. * "android.app.func_name" string meta-data in your manifest to use a different
  217. * function.
  218. */
  219. extern ANativeActivity_createFunc ANativeActivity_onCreate;
  220. /**
  221. * Finish the given activity. Its finish() method will be called, causing it
  222. * to be stopped and destroyed. Note that this method can be called from
  223. * *any* thread; it will send a message to the main thread of the process
  224. * where the Java finish call will take place.
  225. */
  226. void ANativeActivity_finish(ANativeActivity* activity);
  227. /**
  228. * Change the window format of the given activity. Calls getWindow().setFormat()
  229. * of the given activity. Note that this method can be called from
  230. * *any* thread; it will send a message to the main thread of the process
  231. * where the Java finish call will take place.
  232. */
  233. void ANativeActivity_setWindowFormat(ANativeActivity* activity, int32_t format);
  234. /**
  235. * Change the window flags of the given activity. Calls getWindow().setFlags()
  236. * of the given activity. Note that this method can be called from
  237. * *any* thread; it will send a message to the main thread of the process
  238. * where the Java finish call will take place. See window.h for flag constants.
  239. */
  240. void ANativeActivity_setWindowFlags(ANativeActivity* activity,
  241. uint32_t addFlags, uint32_t removeFlags);
  242. /**
  243. * Flags for ANativeActivity_showSoftInput; see the Java InputMethodManager
  244. * API for documentation.
  245. */
  246. enum {
  247. /**
  248. * Implicit request to show the input window, not as the result
  249. * of a direct request by the user.
  250. */
  251. ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = 0x0001,
  252. /**
  253. * The user has forced the input method open (such as by
  254. * long-pressing menu) so it should not be closed until they
  255. * explicitly do so.
  256. */
  257. ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = 0x0002,
  258. };
  259. /**
  260. * Show the IME while in the given activity. Calls InputMethodManager.showSoftInput()
  261. * for the given activity. Note that this method can be called from
  262. * *any* thread; it will send a message to the main thread of the process
  263. * where the Java finish call will take place.
  264. */
  265. void ANativeActivity_showSoftInput(ANativeActivity* activity, uint32_t flags);
  266. /**
  267. * Flags for ANativeActivity_hideSoftInput; see the Java InputMethodManager
  268. * API for documentation.
  269. */
  270. enum {
  271. /**
  272. * The soft input window should only be hidden if it was not
  273. * explicitly shown by the user.
  274. */
  275. ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = 0x0001,
  276. /**
  277. * The soft input window should normally be hidden, unless it was
  278. * originally shown with {@link ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED}.
  279. */
  280. ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = 0x0002,
  281. };
  282. /**
  283. * Hide the IME while in the given activity. Calls InputMethodManager.hideSoftInput()
  284. * for the given activity. Note that this method can be called from
  285. * *any* thread; it will send a message to the main thread of the process
  286. * where the Java finish call will take place.
  287. */
  288. void ANativeActivity_hideSoftInput(ANativeActivity* activity, uint32_t flags);
  289. #ifdef __cplusplus
  290. };
  291. #endif
  292. #endif // ANDROID_NATIVE_ACTIVITY_H
  293. /** @} */