已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include #include #include #include #include #include android_native_app_glue.h#include #define LOGI(.) (void)_android_log_print(ANDROID_LOG_INFO, threaded_app, _VA_ARGS_)#define LOGE(.) (void)_android_log_print(ANDROID_LOG_ERROR, threaded_app, _VA_ARGS_)/* For debug builds, always enable the debug traces in this library */#ifndef NDEBUG# define LOGV(.) (void)_android_log_print(ANDROID_LOG_VERBOSE, threaded_app, _VA_ARGS_)#else# define LOGV(.) (void)0)#endifstatic void free_saved_state(struct android_app* android_app) pthread_mutex_lock(&android_app-mutex); if (android_app-savedState != NULL) free(android_app-savedState); android_app-savedState = NULL; android_app-savedStateSize = 0; pthread_mutex_unlock(&android_app-mutex);int8_t android_app_read_cmd(struct android_app* android_app) int8_t cmd; if (read(android_app-msgread, &cmd, sizeof(cmd) = sizeof(cmd) switch (cmd) case APP_CMD_SAVE_STATE: free_saved_state(android_app); break; return cmd; else LOGE(No data on command pipe!); return -1;static void print_cur_config(struct android_app* android_app) char lang2, country2; AConfiguration_getLanguage(android_app-config, lang); AConfiguration_getCountry(android_app-config, country); LOGV(Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d modetype=%d modenight=%d, AConfiguration_getMcc(android_app-config), AConfiguration_getMnc(android_app-config), lang0, lang1, country0, country1, AConfiguration_getOrientation(android_app-config), AConfiguration_getTouchscreen(android_app-config), AConfiguration_getDensity(android_app-config), AConfiguration_getKeyboard(android_app-config), AConfiguration_getNavigation(android_app-config), AConfiguration_getKeysHidden(android_app-config), AConfiguration_getNavHidden(android_app-config), AConfiguration_getSdkVersion(android_app-config), AConfiguration_getScreenSize(android_app-config), AConfiguration_getScreenLong(android_app-config), AConfiguration_getUiModeType(android_app-config), AConfiguration_getUiModeNight(android_app-config);void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) switch (cmd) case APP_CMD_INPUT_CHANGED: LOGV(APP_CMD_INPUT_CHANGEDn); pthread_mutex_lock(&android_app-mutex); if (android_app-inputQueue != NULL) AInputQueue_detachLooper(android_app-inputQueue); android_app-inputQueue = android_app-pendingInputQueue; if (android_app-inputQueue != NULL) LOGV(Attaching input queue to looper); AInputQueue_attachLooper(android_app-inputQueue, android_app-looper, LOOPER_ID_INPUT, NULL, &android_app-inputPollSource); pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); break; case APP_CMD_INIT_WINDOW: LOGV(APP_CMD_INIT_WINDOWn); pthread_mutex_lock(&android_app-mutex); android_app-window = android_app-pendingWindow; pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); break; case APP_CMD_TERM_WINDOW: LOGV(APP_CMD_TERM_WINDOWn); pthread_cond_broadcast(&android_app-cond); break; case APP_CMD_RESUME: case APP_CMD_START: case APP_CMD_PAUSE: case APP_CMD_STOP: LOGV(activityState=%dn, cmd); pthread_mutex_lock(&android_app-mutex); android_app-activityState = cmd; pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); break; case APP_CMD_CONFIG_CHANGED: LOGV(APP_CMD_CONFIG_CHANGEDn); AConfiguration_fromAssetManager(android_app-config, android_app-activity-assetManager); print_cur_config(android_app); break; case APP_CMD_DESTROY: LOGV(APP_CMD_DESTROYn); android_app-destroyRequested = 1; break; void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) switch (cmd) case APP_CMD_TERM_WINDOW: LOGV(APP_CMD_TERM_WINDOWn); pthread_mutex_lock(&android_app-mutex); android_app-window = NULL; pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); break; case APP_CMD_SAVE_STATE: LOGV(APP_CMD_SAVE_STATEn); pthread_mutex_lock(&android_app-mutex); android_app-stateSaved = 1; pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); break; case APP_CMD_RESUME: free_saved_state(android_app); break; void app_dummy() static void android_app_destroy(struct android_app* android_app) LOGV(android_app_destroy!); free_saved_state(android_app); pthread_mutex_lock(&android_app-mutex); if (android_app-inputQueue != NULL) AInputQueue_detachLooper(android_app-inputQueue); AConfiguration_delete(android_app-config); android_app-destroyed = 1; pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); / Cant touch android_app object after this.static void process_input(struct android_app* app, struct android_poll_source* source) AInputEvent* event = NULL; int processed = 0; while (AInputQueue_getEvent(app-inputQueue, &event) = 0) LOGV(New input event: type=%dn, AInputEvent_getType(event); if (AInputQueue_preDispatchEvent(app-inputQueue, event) continue; int32_t handled = 0; if (app-onInputEvent != NULL) handled = app-onInputEvent(app, event); AInputQueue_finishEvent(app-inputQueue, event, handled); processed = 1; if (processed = 0) LOGE(Failure reading next input event: %sn, strerror(errno); static void process_cmd(struct android_app* app, struct android_poll_source* source) int8_t cmd = android_app_read_cmd(app); android_app_pre_exec_cmd(app, cmd); if (app-onAppCmd != NULL) app-onAppCmd(app, cmd); android_app_post_exec_cmd(app, cmd);static void* android_app_entry(void* param) struct android_app* android_app = (struct android_app*)param; android_app-config = AConfiguration_new(); AConfiguration_fromAssetManager(android_app-config, android_app-activity-assetManager); print_cur_config(android_app); android_app-cmdPollSource.id = LOOPER_ID_MAIN; android_app-cmdPollSource.app = android_app; android_app-cmdPollScess = process_cmd; android_app-inputPollSource.id = LOOPER_ID_INPUT; android_app-inputPollSource.app = android_app; android_app-inputPollScess = process_input; ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); ALooper_addFd(looper, android_app-msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL, &android_app-cmdPollSource); android_app-looper = looper; pthread_mutex_lock(&android_app-mutex); android_app-running = 1; pthread_cond_broadcast(&android_app-cond); pthread_mutex_unlock(&android_app-mutex); android_main(android_app); android_app_destroy(android_app); return NULL;/ -/ Native activity interaction (called from main thread)/ -static struct android_app* android_app_create(ANativeActivity* activity, void* savedState, size_t savedStateSize) struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app); memset(android_app, 0, sizeof(struct android_app); android_app-activity = activity; pthread_mutex_init(&android_app-mutex, NULL); pthread_cond_init(&android_app-cond, NULL); if (savedState != NULL) android_app-savedState = malloc(savedStateSize); android_app-savedStateSize = savedStateSize; memcpy(android_app-savedState, savedState, savedStateSize); int msgpipe2; if (pipe(msgpipe) LOGE(could not create pipe: %s, strerror(errno); return NULL; android_app-msgread = msgpipe0; android_app-msgwrite = msgpipe1; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&android_app-thread, &attr, android_app_entry, android_app); / Wait for thread to start. pthread_mutex_lock(&android_app-mutex); while (!android_app-running) pthread_cond_wait(&android_app-cond, &android_app-mutex); pthread_mutex_unlock(&android_app-mutex); return android_app;static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) if (write(android_app-msgwrite, &cmd, sizeof(cmd) != sizeof(cmd) LOGE(Failure writing android_app cmd: %sn, strerror(errno); static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) pthread_mutex_lock(&android_app-mutex); android_app-pendingInputQueue = inputQueue; android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED); while (android_app-inputQueue != android_app-pendingInputQueue) pthread_cond_wait(&android_app-cond, &android_app-mutex); pthread_mutex_unlock(&android_app-mutex);static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) pthread_mutex_lock(&android_app-mutex); if (android_app-pendingWindow != NULL) android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW); android_app-pendingWindow = window; if (window != NULL) android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW); while (android_app-window != android_app-pendingWindow) pthread_cond_wait(&android_app-cond, &android_app-mutex); pthread_mutex_unlock(&android_app-mutex);static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) pthread_mutex_lock(&android_app-mutex); android_app_write_cmd(android_app, cmd); while (android_app-activityState != cmd) pthread_cond_wait(&android_app-cond, &android_app-mutex); pthread_mutex_unlock(&android_app-mutex);static void android_app_free(struct android_app* android_app) pthread_mutex_lock(&android_app-mutex); android_app_write_cmd(android_app, APP_CMD_DESTROY); while (!android_app-destroyed) pthread_cond_wait(&android_app-cond, &android_app-mutex); pthread_mutex_unlock(&android_app-mutex); close(android_app-msgread); close(android_app-msgwrite); pthread_cond_destroy(&android_app-cond); pthread_mutex_destroy(&android_app-mutex); free(android_app);static void onDestroy(ANativeActivity* activity) LOGV(Destroy: %pn, activity); android_app_free(struct android_app*)activity-instance);static void onStart(ANativeActivity* activity) LOGV(Start: %pn, activity); android_app_set_activity_state(struct android_app*)activity-instance, APP_CMD_START);static void onResume(ANativeActivity* activity) LOGV(Resume: %pn, activity); android_app_set_activity_state(struct android_app*)activity-instance, APP_CMD_RESUME);static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) struct android_app* android_app = (struct android_app*)activity-instance; void* savedState = NULL; LOGV(SaveInstanceState: %pn, activity); pthread_mutex_lock(&android_app-mutex); android_app-stateSaved = 0; android_app_write_cmd(android_app, APP_CMD_SAVE_STATE); while (!android_app-stateSaved) pthread_cond_wait(&android_app-cond, &android_app-mutex); if (android_app-savedState != NULL) savedState = android_app-savedState; *outLen = android_app-savedStateSize; android_app-savedState = NULL; android_app-savedStateSize = 0; pthread_mutex_unlock(&android_app-mutex); return savedState;static void onPause(ANativeActivity* activity) LOGV(Pause: %pn, activity); android_app_set_activity_state(struct android_app*)activity-instance, APP_CMD_PAUSE);static void onStop(ANativeActivity* activity) LOGV(Stop: %pn, activity); android_app_set_activity_state(struct android_app*)activity-instance, APP_CMD_STOP);static void onConfigurationChanged(ANativeActivity* activity) struct android_app* android_app = (struct android_app*)activity-instance; LOGV(ConfigurationChanged: %pn, activity); android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);static void onLowMemory(ANativeActivity* activity) struct android_app* android_app = (struct android_app*)activity-instance; LOGV(LowMemory: %pn, activity); android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);static void onWindowFocusChanged(ANativeActivity* activity, int focused) LOGV(WindowFocusChanged: %p - %dn, activity, focused); android_app_write_cmd(struct android_app*)activity-instance, focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) LOGV(NativeWindowCreated: %p - %pn, activity, window); android_app_set_window(struct android_app*)activity-instance, window);static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) LOGV(NativeWindowDestroyed: %p - %pn, act
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 6044-2025指针式石英手表
- 【正版授权】 ISO 13956:2025 EN Plastics pipes and fittings - Decohesion test of polyethylene (PE) saddle fusion joints - Evaluation of ductility of fusion joint interface by tear test
- 医疗拒收红包协议书
- 医疗废物转运协议书
- 出海推广协议书范本
- 江苏连云港经济技术开发区事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 江苏射阳县2025事业单位招聘笔试易考易错模拟试题(共500题)试卷后附参考答案
- 武汉供销集团限公司出资企业2025年下半年下半年招聘【28人】易考易错模拟试题(共500题)试卷后附参考答案
- 拉开差距826事业单位联考最后一套卷0元易考易错模拟试题(共500题)试卷后附参考答案
- 广州铁路(集团)公司招聘2025高校毕业生500人易考易错模拟试题(共500题)试卷后附参考答案
- 2025河北秦皇岛县(区)总工会招聘工会社工工作人员16人考试笔试备考试题及答案解析
- 草鱼养殖技术与鱼塘管理
- 2025-2026学年北京市101中学八年级(上)期中英语试卷
- 2026年中国PHM项目经营分析报告
- 深静脉血栓的预防与护理
- 高职院校实习基地建设方案及管理办法
- 中职人力资源管理考核模拟试题
- 药事管理促进合理用药
- 2025年天津市高考英语试卷(含答案及解析)
- 【自主招生】小升初数学试卷(含解析)2025年重庆市渝北区重庆皇冠实验中学
- 肾内科慢性肾病患者的运动指导
评论
0/150
提交评论