| 1 |
* |
| 2 |
|
| 3 |
|
| 4 |
ерсия 2.0 ( "Лицензия"); |
| 5 |
о в соответствии с лицензией. |
| 6 |
лучить на веб-сайте |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
м или согласовано в письменном виде, программное обеспечение |
| 11 |
зии на УСЛОВИЯХ "КАК ЕСТЬ", |
| 12 |
о выраженных и подразумеваемых. |
| 13 |
нных разрешениях по использованию языка и |
| 14 |
мках лицензии. |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <jni.h> |
| 19 |
|
| 20 |
#include <errno.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <sys/resource.h> |
| 25 |
|
| 26 |
#include "android_native_app_glue.h" |
| 27 |
#include <android/log.h> |
| 28 |
|
| 29 |
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__)) |
| 30 |
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "threaded_app", __VA_ARGS__)) |
| 31 |
|
| 32 |
чать трассировку отладки в этой библиотеке*/ |
| 33 |
#ifndef NDEBUG |
| 34 |
# define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, "threaded_app", __VA_ARGS__)) |
| 35 |
#else |
| 36 |
# define LOGV(...) ((void)0) |
| 37 |
#endif |
| 38 |
|
| 39 |
static void free_saved_state(struct android_app* android_app) { |
| 40 |
pthread_mutex_lock(&android_app->mutex); |
| 41 |
if (android_app->savedState != NULL) { |
| 42 |
free(android_app->savedState); |
| 43 |
android_app->savedState = NULL; |
| 44 |
android_app->savedStateSize = 0; |
| 45 |
} |
| 46 |
pthread_mutex_unlock(&android_app->mutex); |
| 47 |
} |
| 48 |
|
| 49 |
int8_t android_app_read_cmd(struct android_app* android_app) { |
| 50 |
int8_t cmd; |
| 51 |
if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) { |
| 52 |
switch (cmd) { |
| 53 |
case APP_CMD_SAVE_STATE: |
| 54 |
free_saved_state(android_app); |
| 55 |
break; |
| 56 |
} |
| 57 |
return cmd; |
| 58 |
} else { |
| 59 |
LOGE("No data on command pipe!"); |
| 60 |
} |
| 61 |
return -1; |
| 62 |
} |
| 63 |
|
| 64 |
static void print_cur_config(struct android_app* android_app) { |
| 65 |
char lang[2], country[2]; |
| 66 |
AConfiguration_getLanguage(android_app->config, lang); |
| 67 |
AConfiguration_getCountry(android_app->config, country); |
| 68 |
|
| 69 |
LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d " |
| 70 |
"keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d " |
| 71 |
"modetype=%d modenight=%d", |
| 72 |
AConfiguration_getMcc(android_app->config), |
| 73 |
AConfiguration_getMnc(android_app->config), |
| 74 |
lang[0], lang[1], country[0], country[1], |
| 75 |
AConfiguration_getOrientation(android_app->config), |
| 76 |
AConfiguration_getTouchscreen(android_app->config), |
| 77 |
AConfiguration_getDensity(android_app->config), |
| 78 |
AConfiguration_getKeyboard(android_app->config), |
| 79 |
AConfiguration_getNavigation(android_app->config), |
| 80 |
AConfiguration_getKeysHidden(android_app->config), |
| 81 |
AConfiguration_getNavHidden(android_app->config), |
| 82 |
AConfiguration_getSdkVersion(android_app->config), |
| 83 |
AConfiguration_getScreenSize(android_app->config), |
| 84 |
AConfiguration_getScreenLong(android_app->config), |
| 85 |
AConfiguration_getUiModeType(android_app->config), |
| 86 |
AConfiguration_getUiModeNight(android_app->config)); |
| 87 |
} |
| 88 |
|
| 89 |
void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) { |
| 90 |
switch (cmd) { |
| 91 |
case APP_CMD_INPUT_CHANGED: |
| 92 |
LOGV("APP_CMD_INPUT_CHANGED\n"); |
| 93 |
pthread_mutex_lock(&android_app->mutex); |
| 94 |
if (android_app->inputQueue != NULL) { |
| 95 |
AInputQueue_detachLooper(android_app->inputQueue); |
| 96 |
} |
| 97 |
android_app->inputQueue = android_app->pendingInputQueue; |
| 98 |
if (android_app->inputQueue != NULL) { |
| 99 |
LOGV("Attaching input queue to looper"); |
| 100 |
AInputQueue_attachLooper(android_app->inputQueue, |
| 101 |
android_app->looper, LOOPER_ID_INPUT, NULL, |
| 102 |
&android_app->inputPollSource); |
| 103 |
} |
| 104 |
pthread_cond_broadcast(&android_app->cond); |
| 105 |
pthread_mutex_unlock(&android_app->mutex); |
| 106 |
break; |
| 107 |
|
| 108 |
case APP_CMD_INIT_WINDOW: |
| 109 |
LOGV("APP_CMD_INIT_WINDOW\n"); |
| 110 |
pthread_mutex_lock(&android_app->mutex); |
| 111 |
android_app->window = android_app->pendingWindow; |
| 112 |
pthread_cond_broadcast(&android_app->cond); |
| 113 |
pthread_mutex_unlock(&android_app->mutex); |
| 114 |
break; |
| 115 |
|
| 116 |
case APP_CMD_TERM_WINDOW: |
| 117 |
LOGV("APP_CMD_TERM_WINDOW\n"); |
| 118 |
pthread_cond_broadcast(&android_app->cond); |
| 119 |
break; |
| 120 |
|
| 121 |
case APP_CMD_RESUME: |
| 122 |
case APP_CMD_START: |
| 123 |
case APP_CMD_PAUSE: |
| 124 |
case APP_CMD_STOP: |
| 125 |
LOGV("activityState=%d\n", cmd); |
| 126 |
pthread_mutex_lock(&android_app->mutex); |
| 127 |
android_app->activityState = cmd; |
| 128 |
pthread_cond_broadcast(&android_app->cond); |
| 129 |
pthread_mutex_unlock(&android_app->mutex); |
| 130 |
break; |
| 131 |
|
| 132 |
case APP_CMD_CONFIG_CHANGED: |
| 133 |
LOGV("APP_CMD_CONFIG_CHANGED\n"); |
| 134 |
AConfiguration_fromAssetManager(android_app->config, |
| 135 |
android_app->activity->assetManager); |
| 136 |
print_cur_config(android_app); |
| 137 |
break; |
| 138 |
|
| 139 |
case APP_CMD_DESTROY: |
| 140 |
LOGV("APP_CMD_DESTROY\n"); |
| 141 |
android_app->destroyRequested = 1; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) { |
| 147 |
switch (cmd) { |
| 148 |
case APP_CMD_TERM_WINDOW: |
| 149 |
LOGV("APP_CMD_TERM_WINDOW\n"); |
| 150 |
pthread_mutex_lock(&android_app->mutex); |
| 151 |
android_app->window = NULL; |
| 152 |
pthread_cond_broadcast(&android_app->cond); |
| 153 |
pthread_mutex_unlock(&android_app->mutex); |
| 154 |
break; |
| 155 |
|
| 156 |
case APP_CMD_SAVE_STATE: |
| 157 |
LOGV("APP_CMD_SAVE_STATE\n"); |
| 158 |
pthread_mutex_lock(&android_app->mutex); |
| 159 |
android_app->stateSaved = 1; |
| 160 |
pthread_cond_broadcast(&android_app->cond); |
| 161 |
pthread_mutex_unlock(&android_app->mutex); |
| 162 |
break; |
| 163 |
|
| 164 |
case APP_CMD_RESUME: |
| 165 |
free_saved_state(android_app); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static void android_app_destroy(struct android_app* android_app) { |
| 171 |
LOGV("android_app_destroy!"); |
| 172 |
free_saved_state(android_app); |
| 173 |
pthread_mutex_lock(&android_app->mutex); |
| 174 |
if (android_app->inputQueue != NULL) { |
| 175 |
AInputQueue_detachLooper(android_app->inputQueue); |
| 176 |
} |
| 177 |
AConfiguration_delete(android_app->config); |
| 178 |
android_app->destroyed = 1; |
| 179 |
pthread_cond_broadcast(&android_app->cond); |
| 180 |
pthread_mutex_unlock(&android_app->mutex); |
| 181 |
ть объект android_app. |
| 182 |
} |
| 183 |
|
| 184 |
static void process_input(struct android_app* app, struct android_poll_source* source) { |
| 185 |
AInputEvent* event = NULL; |
| 186 |
while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) { |
| 187 |
LOGV("New input event: type=%d\n", AInputEvent_getType(event)); |
| 188 |
if (AInputQueue_preDispatchEvent(app->inputQueue, event)) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
int32_t handled = 0; |
| 192 |
if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event); |
| 193 |
AInputQueue_finishEvent(app->inputQueue, event, handled); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static void process_cmd(struct android_app* app, struct android_poll_source* source) { |
| 198 |
int8_t cmd = android_app_read_cmd(app); |
| 199 |
android_app_pre_exec_cmd(app, cmd); |
| 200 |
if (app->onAppCmd != NULL) app->onAppCmd(app, cmd); |
| 201 |
android_app_post_exec_cmd(app, cmd); |
| 202 |
} |
| 203 |
|
| 204 |
static void* android_app_entry(void* param) { |
| 205 |
struct android_app* android_app = (struct android_app*)param; |
| 206 |
|
| 207 |
android_app->config = AConfiguration_new(); |
| 208 |
AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager); |
| 209 |
|
| 210 |
print_cur_config(android_app); |
| 211 |
|
| 212 |
android_app->cmdPollSource.id = LOOPER_ID_MAIN; |
| 213 |
android_app->cmdPollSource.app = android_app; |
| 214 |
android_app->cmdPollSource.process = process_cmd; |
| 215 |
android_app->inputPollSource.id = LOOPER_ID_INPUT; |
| 216 |
android_app->inputPollSource.app = android_app; |
| 217 |
android_app->inputPollSource.process = process_input; |
| 218 |
|
| 219 |
ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); |
| 220 |
ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL, |
| 221 |
&android_app->cmdPollSource); |
| 222 |
android_app->looper = looper; |
| 223 |
|
| 224 |
pthread_mutex_lock(&android_app->mutex); |
| 225 |
android_app->running = 1; |
| 226 |
pthread_cond_broadcast(&android_app->cond); |
| 227 |
pthread_mutex_unlock(&android_app->mutex); |
| 228 |
|
| 229 |
android_main(android_app); |
| 230 |
|
| 231 |
android_app_destroy(android_app); |
| 232 |
return NULL; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
е из основного потока) |
| 237 |
|
| 238 |
|
| 239 |
static struct android_app* android_app_create(ANativeActivity* activity, |
| 240 |
void* savedState, size_t savedStateSize) { |
| 241 |
struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app)); |
| 242 |
memset(android_app, 0, sizeof(struct android_app)); |
| 243 |
android_app->activity = activity; |
| 244 |
|
| 245 |
pthread_mutex_init(&android_app->mutex, NULL); |
| 246 |
pthread_cond_init(&android_app->cond, NULL); |
| 247 |
|
| 248 |
if (savedState != NULL) { |
| 249 |
android_app->savedState = malloc(savedStateSize); |
| 250 |
android_app->savedStateSize = savedStateSize; |
| 251 |
memcpy(android_app->savedState, savedState, savedStateSize); |
| 252 |
} |
| 253 |
|
| 254 |
int msgpipe[2]; |
| 255 |
if (pipe(msgpipe)) { |
| 256 |
LOGE("could not create pipe: %s", strerror(errno)); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
android_app->msgread = msgpipe[0]; |
| 260 |
android_app->msgwrite = msgpipe[1]; |
| 261 |
|
| 262 |
pthread_attr_t attr; |
| 263 |
pthread_attr_init(&attr); |
| 264 |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 265 |
pthread_create(&android_app->thread, &attr, android_app_entry, android_app); |
| 266 |
|
| 267 |
уска потока. |
| 268 |
pthread_mutex_lock(&android_app->mutex); |
| 269 |
while (!android_app->running) { |
| 270 |
pthread_cond_wait(&android_app->cond, &android_app->mutex); |
| 271 |
} |
| 272 |
pthread_mutex_unlock(&android_app->mutex); |
| 273 |
|
| 274 |
return android_app; |
| 275 |
} |
| 276 |
|
| 277 |
static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) { |
| 278 |
if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) { |
| 279 |
LOGE("Failure writing android_app cmd: %s\n", strerror(errno)); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) { |
| 284 |
pthread_mutex_lock(&android_app->mutex); |
| 285 |
android_app->pendingInputQueue = inputQueue; |
| 286 |
android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED); |
| 287 |
while (android_app->inputQueue != android_app->pendingInputQueue) { |
| 288 |
pthread_cond_wait(&android_app->cond, &android_app->mutex); |
| 289 |
} |
| 290 |
pthread_mutex_unlock(&android_app->mutex); |
| 291 |
} |
| 292 |
|
| 293 |
static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) { |
| 294 |
pthread_mutex_lock(&android_app->mutex); |
| 295 |
if (android_app->pendingWindow != NULL) { |
| 296 |
android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW); |
| 297 |
} |
| 298 |
android_app->pendingWindow = window; |
| 299 |
if (window != NULL) { |
| 300 |
android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW); |
| 301 |
} |
| 302 |
while (android_app->window != android_app->pendingWindow) { |
| 303 |
pthread_cond_wait(&android_app->cond, &android_app->mutex); |
| 304 |
} |
| 305 |
pthread_mutex_unlock(&android_app->mutex); |
| 306 |
} |
| 307 |
|
| 308 |
static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) { |
| 309 |
pthread_mutex_lock(&android_app->mutex); |
| 310 |
android_app_write_cmd(android_app, cmd); |
| 311 |
while (android_app->activityState != cmd) { |
| 312 |
pthread_cond_wait(&android_app->cond, &android_app->mutex); |
| 313 |
} |
| 314 |
pthread_mutex_unlock(&android_app->mutex); |
| 315 |
} |
| 316 |
|
| 317 |
static void android_app_free(struct android_app* android_app) { |
| 318 |
pthread_mutex_lock(&android_app->mutex); |
| 319 |
android_app_write_cmd(android_app, APP_CMD_DESTROY); |
| 320 |
while (!android_app->destroyed) { |
| 321 |
pthread_cond_wait(&android_app->cond, &android_app->mutex); |
| 322 |
} |
| 323 |
pthread_mutex_unlock(&android_app->mutex); |
| 324 |
|
| 325 |
close(android_app->msgread); |
| 326 |
close(android_app->msgwrite); |
| 327 |
pthread_cond_destroy(&android_app->cond); |
| 328 |
pthread_mutex_destroy(&android_app->mutex); |
| 329 |
free(android_app); |
| 330 |
} |
| 331 |
|
| 332 |
static void onDestroy(ANativeActivity* activity) { |
| 333 |
LOGV("Destroy: %p\n", activity); |
| 334 |
android_app_free((struct android_app*)activity->instance); |
| 335 |
} |
| 336 |
|
| 337 |
static void onStart(ANativeActivity* activity) { |
| 338 |
LOGV("Start: %p\n", activity); |
| 339 |
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START); |
| 340 |
} |
| 341 |
|
| 342 |
static void onResume(ANativeActivity* activity) { |
| 343 |
LOGV("Resume: %p\n", activity); |
| 344 |
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME); |
| 345 |
} |
| 346 |
|
| 347 |
static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) { |
| 348 |
struct android_app* android_app = (struct android_app*)activity->instance; |
| 349 |
void* savedState = NULL; |
| 350 |
|
| 351 |
LOGV("SaveInstanceState: %p\n", activity); |
| 352 |
pthread_mutex_lock(&android_app->mutex); |
| 353 |
android_app->stateSaved = 0; |
| 354 |
android_app_write_cmd(android_app, APP_CMD_SAVE_STATE); |
| 355 |
while (!android_app->stateSaved) { |
| 356 |
pthread_cond_wait(&android_app->cond, &android_app->mutex); |
| 357 |
} |
| 358 |
|
| 359 |
if (android_app->savedState != NULL) { |
| 360 |
savedState = android_app->savedState; |
| 361 |
*outLen = android_app->savedStateSize; |
| 362 |
android_app->savedState = NULL; |
| 363 |
android_app->savedStateSize = 0; |
| 364 |
} |
| 365 |
|
| 366 |
pthread_mutex_unlock(&android_app->mutex); |
| 367 |
|
| 368 |
return savedState; |
| 369 |
} |
| 370 |
|
| 371 |
static void onPause(ANativeActivity* activity) { |
| 372 |
LOGV("Pause: %p\n", activity); |
| 373 |
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE); |
| 374 |
} |
| 375 |
|
| 376 |
static void onStop(ANativeActivity* activity) { |
| 377 |
LOGV("Stop: %p\n", activity); |
| 378 |
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP); |
| 379 |
} |
| 380 |
|
| 381 |
static void onConfigurationChanged(ANativeActivity* activity) { |
| 382 |
struct android_app* android_app = (struct android_app*)activity->instance; |
| 383 |
LOGV("ConfigurationChanged: %p\n", activity); |
| 384 |
android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED); |
| 385 |
} |
| 386 |
|
| 387 |
static void onLowMemory(ANativeActivity* activity) { |
| 388 |
struct android_app* android_app = (struct android_app*)activity->instance; |
| 389 |
LOGV("LowMemory: %p\n", activity); |
| 390 |
android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY); |
| 391 |
} |
| 392 |
|
| 393 |
static void onWindowFocusChanged(ANativeActivity* activity, int focused) { |
| 394 |
LOGV("WindowFocusChanged: %p -- %d\n", activity, focused); |
| 395 |
android_app_write_cmd((struct android_app*)activity->instance, |
| 396 |
focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS); |
| 397 |
} |
| 398 |
|
| 399 |
static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) { |
| 400 |
LOGV("NativeWindowCreated: %p -- %p\n", activity, window); |
| 401 |
android_app_set_window((struct android_app*)activity->instance, window); |
| 402 |
} |
| 403 |
|
| 404 |
static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) { |
| 405 |
LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window); |
| 406 |
android_app_set_window((struct android_app*)activity->instance, NULL); |
| 407 |
} |
| 408 |
|
| 409 |
static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) { |
| 410 |
LOGV("InputQueueCreated: %p -- %p\n", activity, queue); |
| 411 |
android_app_set_input((struct android_app*)activity->instance, queue); |
| 412 |
} |
| 413 |
|
| 414 |
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) { |
| 415 |
LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue); |
| 416 |
android_app_set_input((struct android_app*)activity->instance, NULL); |
| 417 |
} |
| 418 |
|
| 419 |
void ANativeActivity_onCreate(ANativeActivity* activity, |
| 420 |
void* savedState, size_t savedStateSize) { |
| 421 |
LOGV("Creating: %p\n", activity); |
| 422 |
activity->callbacks->onDestroy = onDestroy; |
| 423 |
activity->callbacks->onStart = onStart; |
| 424 |
activity->callbacks->onResume = onResume; |
| 425 |
activity->callbacks->onSaveInstanceState = onSaveInstanceState; |
| 426 |
activity->callbacks->onPause = onPause; |
| 427 |
activity->callbacks->onStop = onStop; |
| 428 |
activity->callbacks->onConfigurationChanged = onConfigurationChanged; |
| 429 |
activity->callbacks->onLowMemory = onLowMemory; |
| 430 |
activity->callbacks->onWindowFocusChanged = onWindowFocusChanged; |
| 431 |
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated; |
| 432 |
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed; |
| 433 |
activity->callbacks->onInputQueueCreated = onInputQueueCreated; |
| 434 |
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed; |
| 435 |
|
| 436 |
activity->instance = android_app_create(activity, savedState, savedStateSize); |
| 437 |
} |