源码地址:
base\services\core\jni\com_android_server_lights_LightsService.cpp
功能:
定义JNI接口函数供Framework调用,主要设置init_native和setLight_native
init_native:获得HAL层不同类别的light device,保存在devices中并返回
setLight_native:不同light设备的set_light方法
源码分析:
申明JNI接口函数,关注init_native和setLight_native
static JNINativeMethod method_table[] = { { "init_native", "()J", (void*)init_native }, { "finalize_native", "(J)V", (void*)finalize_native }, { "setLight_native", "(JIIIIII)V", (void*)setLight_native }, };
先暂时关注LIGHT_ID_BACKLIGHT,去除不相关code。
init_native:
1:load HAL的so文件
2:依次查找支持的light设备,并且保存在devices->lights[]中
devices->lights[]中存储了HAL不同lights设备对应的light_device_t(其中包含了set_light方法)
3:返回devices
static jlong init_native(JNIEnv *env, jobject clazz) { devices = (Devices*)malloc(sizeof(Devices)); hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
// devices->lights[]中存储了HAL不同lights设备对应的light_device_t devices->lights[LIGHT_INDEX_BACKLIGHT] = get_device(module, LIGHT_ID_BACKLIGHT);
module->methods->open(module, name, &device); // 将会call HAL的open_lights,返回HAL的light_device_t,其中包含set_light方法
return (light_device_t*)device;
return (jlong)devices; }
setLight_native:
配置不同light设备的set_light方法
static void setLight_native(JNIEnv *env, jobject clazz, jlong ptr, jint light, jint colorARGB, jint flashMode, jint onMS, jint offMS, jint brightnessMode) { Devices* devices = (Devices*)ptr; light_state_t state; // 配置state memset(&state, 0, sizeof(light_state_t)); state.color = colorARGB; state.flashMode = flashMode; state.flashOnMS = onMS; state.flashOffMS = offMS; state.brightnessMode = brightnessMode; devices->lights[light]->set_light(devices->lights[light], &state); }