/******************************************************************************* * I.MX6 Android frameworks services 文件架构 * 说明: * 这部分内容后续可能需要多给一点注意力了,因为这部分在做系统移植的时候经常 * 需要通过这部分来进行调试。 * 2016-3-19 深圳 南山平山村 曾剑锋 ******************************************************************************/一、cat frameworks/base/services/jni/Android.mk # 获取当前路径 LOCAL_PATH:= $(call my-dir) # 清理变量,但是不会清理LOCAL_PATH变量的值 include $(CLEAR_VARS) # 指定jni cpp文件 LOCAL_SRC_FILES:= \ com_android_server_AlarmManagerService.cpp \ com_android_server_BatteryService.cpp \ com_android_server_input_InputApplicationHandle.cpp \ com_android_server_input_InputManagerService.cpp \ com_android_server_input_InputWindowHandle.cpp \ com_android_server_LightsService.cpp \ com_android_server_power_PowerManagerService.cpp \ com_android_server_SerialService.cpp \ com_android_server_SystemServer.cpp \ com_android_server_UsbDeviceManager.cpp \ com_android_server_UsbHostManager.cpp \ com_android_server_VibratorService.cpp \ com_android_server_location_GpsLocationProvider.cpp \ com_android_server_connectivity_Vpn.cpp \ onload.cpp -----------------------------+ | # 指定jni cpp依赖的头文件 | LOCAL_C_INCLUDES += \ | $(JNI_H_INCLUDE) \ | frameworks/base/services \ | frameworks/base/core/jni \ | external/skia/include/core \ | libcore/include \ | libcore/include/libsuspend \ | $(call include-path-for, libhardware)/hardware \ | $(call include-path-for, libhardware_legacy)/hardware_legacy \ | | # 指定依赖的库文件 | LOCAL_SHARED_LIBRARIES := \ | libandroid_runtime \ | libandroidfw \ | libcutils \ | libhardware \ | libhardware_legacy \ | libnativehelper \ | libsystem_server \ | libutils \ | libui \ | libinput \ | libskia \ | libgui \ | libusbhost \ | libsuspend | | ifeq ($(WITH_MALLOC_LEAK_CHECK),true) | LOCAL_CFLAGS += -DMALLOC_LEAK_CHECK | endif | | # 生成的模块的名字 | LOCAL_MODULE:= libandroid_servers | | # 这里相当于开始编译 | include $(BUILD_SHARED_LIBRARY) | |二、cat frameworks/base/services/jni/onload.cpp <--------------+ /* * Copyright (C) 2009 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 * * http://www.apache.org/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 "JNIHelp.h" #include "jni.h" #include "utils/Log.h" #include "utils/misc.h" // 声明接下来的需要用到android命名空间中的jni函数 namespace android { int register_android_server_AlarmManagerService(JNIEnv* env); int register_android_server_BatteryService(JNIEnv* env); int register_android_server_InputApplicationHandle(JNIEnv* env); int register_android_server_InputWindowHandle(JNIEnv* env); int register_android_server_InputManager(JNIEnv* env); int register_android_server_LightsService(JNIEnv* env); int register_android_server_PowerManagerService(JNIEnv* env); int register_android_server_SerialService(JNIEnv* env); int register_android_server_UsbDeviceManager(JNIEnv* env); int register_android_server_UsbHostManager(JNIEnv* env); int register_android_server_VibratorService(JNIEnv* env); int register_android_server_SystemServer(JNIEnv* env); int register_android_server_location_GpsLocationProvider(JNIEnv* env); int register_android_server_connectivity_Vpn(JNIEnv* env); }; // 使用命名空间 using namespace android; // C语言JNI加载函数,用于调用前面的android命名空间总的函数,这些函数 // 主要是对JNI进行注册。 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { ALOGE("GetEnv failed!"); return result; } ALOG_ASSERT(env, "Could not retrieve the env!"); register_android_server_PowerManagerService(env); ---------------+ register_android_server_SerialService(env); | register_android_server_InputApplicationHandle(env); | register_android_server_InputWindowHandle(env); | register_android_server_InputManager(env); | register_android_server_LightsService(env); | register_android_server_AlarmManagerService(env); | register_android_server_BatteryService(env); | register_android_server_UsbDeviceManager(env); | register_android_server_UsbHostManager(env); | register_android_server_VibratorService(env); | register_android_server_SystemServer(env); | register_android_server_location_GpsLocationProvider(env); | register_android_server_connectivity_Vpn(env); | +-------------------------+ return JNI_VERSION_1_4; | 间接加载 } | v三、cat frameworks/base/services/jni/com_android_server_BatteryService.cpp ...... namespace android { #define POWER_SUPPLY_PATH "/sys/class/power_supply" static void android_server_BatteryService_update(JNIEnv* env, jobject obj) { setBooleanField(env, obj, gPaths.acOnlinePath, gFieldIds.mAcOnline); setBooleanField(env, obj, gPaths.usbOnlinePath, gFieldIds.mUsbOnline); setBooleanField(env, obj, gPaths.wirelessOnlinePath, gFieldIds.mWirelessOnline); setBooleanField(env, obj, gPaths.batteryPresentPath, gFieldIds.mBatteryPresent); setIntField(env, obj, gPaths.batteryCapacityPath, gFieldIds.mBatteryLevel); setVoltageField(env, obj, gPaths.batteryVoltagePath, gFieldIds.mBatteryVoltage); setIntField(env, obj, gPaths.batteryTemperaturePath, gFieldIds.mBatteryTemperature); char prop[5]; // always report AC plug-in and capacity 100% if emulated.battery is set to 1 property_get("sys.emulated.battery", prop, "0"); if (!strcmp(prop, "1")){ env->SetBooleanField(obj, gFieldIds.mAcOnline, true); env->SetIntField(obj, gFieldIds.mBatteryLevel, 100); } const int SIZE = 128; char buf[SIZE]; if (readFromFile(gPaths.batteryStatusPath, buf, SIZE) > 0) env->SetIntField(obj, gFieldIds.mBatteryStatus, getBatteryStatus(buf)); else env->SetIntField(obj, gFieldIds.mBatteryStatus, gConstants.statusUnknown); if (readFromFile(gPaths.batteryHealthPath, buf, SIZE) > 0) env->SetIntField(obj, gFieldIds.mBatteryHealth, getBatteryHealth(buf)); if (readFromFile(gPaths.batteryTechnologyPath, buf, SIZE) > 0) env->SetObjectField(obj, gFieldIds.mBatteryTechnology, env->NewStringUTF(buf)); } static JNINativeMethod sMethods[] = { /* name, signature, funcPtr */ { "native_update", "()V", (void*)android_server_BatteryService_update}, }; int register_android_server_BatteryService(JNIEnv* env) { ...... return jniRegisterNativeMethods(env, "com/android/server/BatteryService", sMethods, NELEM(sMethods)); } | | } /* namespace android */ | V四、cat frameworks/base/services/java/com/android/server/BatteryService.java ...... public final class BatteryService extends Binder { ...... private native void native_update(); ...... private void updateLocked() { if (!mUpdatesStopped) { // Update the values of mAcOnline, et. all. native_update(); // Process the new values. processValuesLocked(); } } ...... }