Android GPS架构分析
Daniel Wood 20101222
转载时请注明出处和作者
文章出处:http://danielwood.cublog.cn
作者:Daniel Wood
------------------------------------------------------------
initialize函数
LocationManagerService.java[frameworks\base\services\java\com\android\server]
private void initialize() { // Create a wake lock, needs to be done before calling loadProviders() below PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY); // Load providers loadProviders();
...
|
initialize函数中最重要的就是loadProviders函数了,该函数调用loadProvidersLocked,然后loadProvidersLocked函数又调用_loadProvidersLocked函数。为什么要这么折腾呢?
先来看一部分的_loadProvidersLocked函数:
private void _loadProvidersLocked() { // Attempt to load "real" providers first if (GpsLocationProvider.isSupported()) { // Create a gps location provider GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this); mGpsStatusProvider = gpsProvider.getGpsStatusProvider(); mNetInitiatedListener = gpsProvider.getNetInitiatedListener(); addProvider(gpsProvider); mGpsLocationProvider = gpsProvider; }
...
|
注意这个if语句,狠重要,因为在这个语句中得到了HAL层的GPS接口GpsInterface。就是通过调用GpsLocationProvider的isSupported()函数才调用到gps.cpp[hardware/libhardware_legacy/gps]中的gps_get_interface()。这个isSupported函数才是第一个吃螃蟹的人。(而不是JNI层的init函数,这个下面会提到)。
GpsLocationProvider.cpp [frameworks\base\location\java\com\android\internal\location]
public static boolean isSupported() { return native_is_supported(); }
|
然而isSupported只有一句话,果然是高手,一击必中。然后就调用native方法,也就是JNI层定义的方法。native_is_supported函数对于JNI层是android_location_GpsLocationProvider_is_supported方法。
android_location_GpsLocationProvider.cpp [frameworks\base\core\jni]
static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz) { if (!sGpsInterface) sGpsInterface = gps_get_interface(); return (sGpsInterface != NULL); }
|
前面已经提到JNI起到承上启下的作用,gps_get_interface函数属于HAL层的调用,在文件gps.cpp中。
gps.cpp [hardware\libhardware_legacy\gps]
const GpsInterface* gps_get_interface() { if (sGpsInterface == NULL) gps_find_hardware(); return sGpsInterface; }
|
然后通过gps_find_hardware函数去得到gps接口,下面只模拟器中的gpsinterface。
static void gps_find_hardware( void ) { #ifdef HAVE_QEMU_GPS_HARDWARE if (qemu_check()) { sGpsInterface = gps_get_qemu_interface(); if (sGpsInterface) { LOGD("using QEMU GPS Hardware emulation\n"); return; } } #endif #ifdef HAVE_GPS_HARDWARE sGpsInterface = gps_get_hardware_interface(); #endif if (!sGpsInterface) LOGD("no GPS hardware on this device\n"); }
|
gps_qemu.c [hardware\libhardware_legacy\gps]
const GpsInterface* gps_get_qemu_interface() { return &qemuGpsInterface; }
|
qemuGpsInterface的整体实现就在文件gps_qemu.c中。
static const GpsInterface qemuGpsInterface = { qemu_gps_init, qemu_gps_start, qemu_gps_stop, qemu_gps_cleanup, qemu_gps_inject_time, qemu_gps_inject_location, qemu_gps_delete_aiding_data, qemu_gps_set_position_mode, qemu_gps_get_extension, };
|
阅读(3841) | 评论(0) | 转发(1) |