http://blog.csdn.net/zhangjie201412/article/details/7340143
前面我们已经从linux kernel一直介绍到了android jni,现在我们来看看更上层的封装,代码位置:
/frameworks/base/services/java/com/android/server/LightsService.java
这里其实就是封装了一个setLight函数,我们来看下,首先是load我们jni中的接口:
- private static native int init_native();
- private static native void finalize_native(int ptr);
- private static native void setLight_native(int ptr, int light, int color, int mode,
- int onMS, int offMS, int brightnessMode);
- private final Context mContext;
- private int mNativePointer;
private static native int init_native();
private static native void finalize_native(int ptr);
private static native void setLight_native(int ptr, int light, int color, int mode,
int onMS, int offMS, int brightnessMode);
private final Context mContext;
private int mNativePointer;
这些函数就是核心,对于java代码,我不是很擅长分析,不过我喜欢先看构造函数,应该是做一些初始化的动作,
- LightsService(Context context) {
- mNativePointer = init_native();
- mContext = context;
- ServiceManager.addService("hardware", mLegacyFlashlightHack);
- for (int i = 0; i < LIGHT_ID_COUNT; i++) {
- mLights[i] = new Light(i);
- }
- }
LightsService(Context context) {
mNativePointer = init_native();
mContext = context;
ServiceManager.addService("hardware", mLegacyFlashlightHack);
for (int i = 0; i < LIGHT_ID_COUNT; i++) {
mLights[i] = new Light(i);
}
}
这里只是把闪光灯的服务加到了系统的service中,我们这里没有,只有backlight,所以我们不关心,关心的之后下面这个for循环,这里只是new了一个light对象而已,其中当然包括我们的背光了。
我们来跟踪下我们的native 函数在哪被调用了,查看了下只有一个地方:
- private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
- if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {
- mColor = color;
- mMode = mode;
- mOnMS = onMS;
- mOffMS = offMS;
- setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
- }
- }
private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {
mColor = color;
mMode = mode;
mOnMS = onMS;
mOffMS = offMS;
setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
}
}
就是这个函数,这个setLightLocked函数可以说是这边的一个接口吧,在上层的都调用了这个函数,来设置背光的,下面的可以自己分析。