Android4.4 GPS框架分析【转】
本文转载自:http://blog.csdn.net/junzhang1122/article/details/46674569
GPS HAL层代码在目录trunk/Android/hardware/xxx/gps/skytraqskytraq_gsp.c,向下与硬件驱动通讯,向上提供接口
GPS JNI层层代码在目录trunk/Android/frameworks/base/services/jni/com_android_server_location_GpsLocationProvider.cpp C与java转换,给java层提供接口
GPS framework层代码目录Android/frameworks/base/services/java/com/android/server下,主要文件是location服务对内部的封装并提供provider服务
Android/frameworks/base/location/java/com/android/internal/location这个是framework对location服务内部的实现 启动GPS服务代码流程如下
public static void main(String[] args)
-->ServerThread thr = new ServerThread();
-->location = new LocationManagerService(context);
-->ServiceManager.addService(Context.LOCATION_SERVICE, location);
-->locationF.systemRunning()
-->systemRunning()
-->loadProvidersLocked();
-->GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this, mLocationHandler.getLooper());
-->class_init_native();
-->android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz)
-->err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
-->pen_gps(const struct hw_module_t *module, char const *name, struct hw_device_t **device)
-->sGpsInterface = gps_device->get_gps_interface(gps_device);
-->const GpsInterface *gps__get_gps_interface(struct gps_device_t *dev)
-->return &skytraqGpsInterface;
-->GpsLocationProvider.isSupported()
-->native_is_supported()
-->android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz)
-->return (sGpsInterface != NULL);
-->updateProvidersLocked
--> updateProviderListenersLocked
--> applyRequirementsLocked(provider);
--> p.setRequest(providerRequest, worksource);
--> public void setRequest(ProviderRequest request, WorkSource source)
--> sendMessage(SET_REQUEST, , new GpsRequest(request, source));
-->public void handleMessage(Message msg)
-->handleSetRequest(gpsRequest.request, gpsRequest.source);
-->startNavigating(singleShot);
-->native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC,interval, , )
-->native_start() Android系统运行的第一个进程就是init进程,在该进程中创建的第一个虚拟机就是zygote,zygote孵化的第一个虚拟机就是Dalvik,该进程就是SystemServer,
对于的SystemServer代码是framework\base\services\java\com\android\server\SystemServer.java
该SystemServer对于的第一个运行函数就是main public static void main(String[] args) //SystemServer运行的第一个函数
ServerThread thr = new ServerThread(); //初始化一个server线程
class ServerThread
{
public void initAndLoop()
{
if (!disableLocation) //开始的时候这里的 disableLocation = false
{
try{
Slog.i(TAG, "Location Manager");
<> location = new LocationManagerService(context); //实例化一个LocationManagerService服务,该服务主要用来管理GPS地理位置的变化
ServiceManager.addService(Context.LOCATION_SERVICE, location); //将这个service添加到SystemManager中
}
catch (Throwable e)
{
reportWtf("starting Location Manager", e);
} try {
Slog.i(TAG, "Country Detector");
countryDetector = new CountryDetectorService(context);//实例化一个CountryDetectorService服务,用来探测不同国家,也就是不同地区和时区
ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
} catch (Throwable e) {
reportWtf("starting Country Detector", e);
}
} ActivityManagerService.self().systemReady(new Runnable()
{
public void run()
{
try
{
<> if (locationF != null) locationF.systemRunning();//在<1>的位置已给locationF赋值,在这里调用LocationManagerService的systemRunning()函数
}
catch(Throwable e)
{
reportWtf("Notifying Location Service running", e);
}
}
} }
} 接下来分析LocationManagerService(context)
/**framework\base\services\java\com\android\server\LocationManagerService.java**/
public class LocationManagerService extends ILocationManager.Stub
{
public void systemRunning() //该函数有<2>处调用
{
synchronized (mLock)
{
/*GPS启动到运行及监听都在这两个函数里*/
<> loadProvidersLocked(); //在这里加载各种provider
<> updateProvidersLocked(); //加载后更新
}
}
} /**framework\base\services\java\com\android\server\LocationManagerService.java**/
private void loadProvidersLocked()
{
// create a passive location provider, which is always enabled
PassiveProvider passiveProvider = new PassiveProvider(this);
addProviderLocked(passiveProvider);
mEnabledProviders.add(passiveProvider.getName());
mPassiveProvider = passiveProvider;
// Create a gps location provider
<> GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
mLocationHandler.getLooper()); <> if (GpsLocationProvider.isSupported()) //这里调用JNI的函数接口
{
mGpsStatusProvider = gpsProvider.getGpsStatusProvider(); //获取gps状态provider
mNetInitiatedListener = gpsProvider.getNetInitiatedListener(); //初始网络监听
addProviderLocked(gpsProvider); //将gpsProvider添加到mProviders中
mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
}
} /**分析 <5> ***/
public class GpsLocationProvider implements LocationProviderInterface
{
static { class_init_native(); } //在这里调用JNI的android_location_GpsLocationProvider_class_init_native函数
} static void android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz)
{
err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);//打开GPS设备文件,对应trunk/Android/hardware/xxx/gps/skytraq/skytraq_gps.c
-->static int open_gps(const struct hw_module_t *module, char const *name, struct hw_device_t **device) //获取gpsInterface接口,该函数接口在trunk/Android/hardware/xxx/gps/skytraq/skytraq_gps.c中,对应的函数是gps__get_gps_interface
sGpsInterface = gps_device->get_gps_interface(gps_device);
} const GpsInterface *gps__get_gps_interface(struct gps_device_t *dev)
{
return &skytraqGpsInterface;
} /**分析 <6> **/
public static boolean GpsLocationProvider.isSupported()
{
return native_is_supported(); //该函数对应JNI的函数如下
} static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz)
{
return (sGpsInterface != NULL);
}
GpsLocationProvider.isSupported()返回true,所以会走if{}里面的。在网上查阅资料,都是基于2.3的系统,.3的系统里是直接在isSupported()函数里来获取接口,没有在GpsLocationProvider类中实现调用class_init_native()来获取接口 /**分析 <4> **/
/**framework\base\services\java\com\android\server\LocationManagerService.java**/
private void updateProvidersLocked()
{
for (int i = mProviders.size() - ; i >= ; i--)
{
boolean isEnabled = p.isEnabled(); //开始的时候返回false
boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name); //在loadProvidersLocked()函数就说了,一直是enabled
所以会走else
else if (!isEnabled && shouldBeEnabled)
{
<> updateProviderListenersLocked(name, true, mCurrentUserId);
changesMade = true;
}
}
/*发送广播,app和framework都可以接受该广播*/
if (changesMade) {
mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
UserHandle.ALL);
mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
UserHandle.ALL);
}
} /**分析 <7> framework\base\services\java\com\android\server\locationManagerService.java **/
private void updateProviderListenersLocked(String provider, boolean enabled, int userId)
{
if (records != null)
{
//这里主要是从UserHandle里解析UserId 是否等于 mCurrentUserId,如果是则发送notification给receiver
}
if (enabled) //这里的 enabled == true
{
<> p.enable();
if (listeners > )
{
<> applyRequirementsLocked(provider);
}
}
else
{
p.disable();
} } /**分析 <8> framework\base\services\java\com\android\server\location\gpsLocationProvider.java**/
public void enable()
{
synchronized (mLock)
{
if (mEnabled) return;//开始的时候这里是false
mEnabled = true;
}
<> sendMessage(ENABLE, , null);//发送消息
}
/**分析 <10> base\services\java\com\android\server\location\gpsLocationProvider.java**/
private void sendMessage(int message, int arg, Object obj)
{
// hold a wake lock until this message is delivered
// note that this assumes the message will not be removed from the queue before
// it is handled (otherwise the wake lock would be leaked).
mWakeLock.acquire();
<> mHandler.obtainMessage(message, arg, , obj).sendToTarget(); //通过handler机制来发送消息
} /**分析 <11> base\services\java\com\android\server\location\gpsLocationProvider.java**/
private final class ProviderHandler extends Handler
{
public void handleMessage(Message msg)
{
switch (message)
{
case ENABLE: //有<10>可知,message == ENABLE , msg.arg1 == 1
if (msg.arg1 == ) {
<> handleEnable(); //所以会调用这个函数
} else {
handleDisable();
}
break;
.....
}
}
} /**分析 <12> framework\base\services\java\com\android\server\location\gpsLocationProvider.java**/
private void handleEnable()
{
<> boolean enabled = native_init(); //调用JNI中的 static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
if (enabled)
{
mSupportsXtra = native_supports_xtra(); //native_supports_xtra()直接返回true,在class_init_native()已经赋值 if (mSuplServerHost != null)
{
/*这里没做任何事,因为sAGpsInterface为空,代码流程如下*/
native_set_agps_server(AGPS_TYPE_SUPL, mSuplServerHost, mSuplServerPort);
-->static void android_location_GpsLocationProvider_set_agps_server(JNIEnv* env, jobject obj, jint type, jstring hostname, jint port)
-->if (!sAGpsInterface) return;
-->sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE);
-->static const void *skytraq_gps_get_extension(const char* name)
{
ALOGD("%s: called", __FUNCTION__);
return NULL;
} }
if (mC2KServerHost != null) { //同理这里也直接返回了
native_set_agps_server(AGPS_TYPE_C2K, mC2KServerHost, mC2KServerPort);
}
}else{
synchronized (mLock) {
mEnabled = false;
}
Log.w(TAG, "Failed to enable location provider");
} } /**分析 <13> framework\base\services\jni\com_android_server_location_GpsLocationProvider.cpp **/
static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
{
<> if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != )//sGpsInterface不为null,在一开始的就已赋值,所以会调用init函数
return false;
.....
} /**分析 <14> hardware\xxx\gps\skytraq\Skytraq_gps.c **/
static int skytraq_gps_init(GpsCallbacks* callbacks) //callbacks回调函数时有JNI传下来的,在JNI中实现的
{
GpsState* s = &_gps_state; //该结构图数组初始化的时候都为0
if (!s->init)
<> gps_state_init(s, callbacks);
} /**分析 <15> hardware\xxx\gps\skytraq\Skytraq_gps.c **/
static void gps_state_init( GpsState* state, GpsCallbacks* callbacks )
{
state->fd = gps_channel_open(GPS_CHANNEL_NAME);
--> fd = open("/dev/skytraq_gps_ipc", O_RDWR | O_SYNC); //打开串口
int ret = ioctl(state->fd, GPS_IPC_SET, &gps_connect_info); //调用驱动函数ioctl设置gps
state->thread = callbacks->create_thread_cb( "gps_state_thread", gps_state_thread, state );//创建线程,用来监听发送的命令和监听数据上报,
} /**分析<9> frwmework\base\services\java\com\android\server\LocationManagerService.java
private void applyRequirementsLocked(String provider)
{
//设置worksource和providerRequest
.....
<16> p.setRequest(providerRequest, worksource); //location provider 发送请求
} /**分析<16> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
public void setRequest(ProviderRequest request, WorkSource source)
{
<> sendMessage(SET_REQUEST, , new GpsRequest(request, source)); //发送消息,SET_REQUEST == 3
} /**分析<17> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
public void handleMessage(Message msg)
{
...
case SET_REQUEST:
GpsRequest gpsRequest = (GpsRequest) msg.obj;
<> handleSetRequest(gpsRequest.request, gpsRequest.source);
break;
} /**分析<18> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
private void handleSetRequest(ProviderRequest request, WorkSource source)
{
...
<> startNavigating(singleShot);//开始导航
} /**分析<19> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
private void startNavigating(boolean singleShot)
{
<> native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC, interval, , )//该函数在HAL层直接返回了,没做任何事
<> native_start()
} /**分析<20> framework\base\services\jni\com_android_server_location_GpsLocationProvider.cpp**/
static jboolean android_location_GpsLocationProvider_set_position_mode(JNIEnv* env, jobject obj,
jint mode, jint recurrence, jint min_interval, jint preferred_accuracy, jint preferred_time)
{
if (sGpsInterface)
<> return (sGpsInterface->set_position_mode(mode, recurrence, min_interval, preferred_accuracy,
preferred_time) == ); //调用HAL层接口
} /**分析<22> xxx\gps\skytraq\skytraq.gps.c**/
static int skytraq_gps_set_position_mode(GpsPositionMode mode,
GpsPositionRecurrence recurrence,
uint32_t min_interval,
uint32_t preferred_accuracy,
uint32_t preferred_time)
{
ALOGD("%s: called", __FUNCTION__);
return ;
}
/**分析<21> framework\base\services\jni\com_android_server_location_GpsLocationProvider.cpp**/
static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj)
{
if (sGpsInterface)
<> return (sGpsInterface->start() == ); //调用HAL 层的static int skytraq_gps_start()
} /**分析<23> xxx\gps\skytraq\skytraq.gps.c**/
static int skytraq_gps_start()
{
gps_state_start(s);
-->s->status = CMD_START;
/*在<15>线程函数gps_state_thread()中while(1)中等该命令,进入获取数据*/
}
此时GPS就已运行
- GPS HAL层代码在目录trunk/Android/hardware/xxx/gps/skytraqskytraq_gsp.c,向下与硬件驱动通讯,向上提供接口
- GPS JNI层层代码在目录trunk/Android/frameworks/base/services/jni/com_android_server_location_GpsLocationProvider.cpp C与java转换,给java层提供接口
- GPS framework层代码目录Android/frameworks/base/services/java/com/android/server下,主要文件是location服务对内部的封装并提供provider服务
- Android/frameworks/base/location/java/com/android/internal/location这个是framework对location服务内部的实现
- 启动GPS服务代码流程如下
- public static void main(String[] args)
- -->ServerThread thr = new ServerThread();
- -->location = new LocationManagerService(context);
- -->ServiceManager.addService(Context.LOCATION_SERVICE, location);
- -->locationF.systemRunning()
- -->systemRunning()
- -->loadProvidersLocked();
- -->GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this, mLocationHandler.getLooper());
- -->class_init_native();
- -->android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz)
- -->err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
- -->pen_gps(const struct hw_module_t *module, char const *name, struct hw_device_t **device)
- -->sGpsInterface = gps_device->get_gps_interface(gps_device);
- -->const GpsInterface *gps__get_gps_interface(struct gps_device_t *dev)
- -->return &skytraqGpsInterface;
- -->GpsLocationProvider.isSupported()
- -->native_is_supported()
- -->android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz)
- -->return (sGpsInterface != NULL);
- -->updateProvidersLocked
- --> updateProviderListenersLocked
- --> applyRequirementsLocked(provider);
- --> p.setRequest(providerRequest, worksource);
- --> public void setRequest(ProviderRequest request, WorkSource source)
- --> sendMessage(SET_REQUEST, 0, new GpsRequest(request, source));
- -->public void handleMessage(Message msg)
- -->handleSetRequest(gpsRequest.request, gpsRequest.source);
- -->startNavigating(singleShot);
- -->native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC,interval, 0, 0)
- -->native_start()
- Android系统运行的第一个进程就是init进程,在该进程中创建的第一个虚拟机就是zygote,zygote孵化的第一个虚拟机就是Dalvik,该进程就是SystemServer,
- 对于的SystemServer代码是framework\base\services\java\com\android\server\SystemServer.java
- 该SystemServer对于的第一个运行函数就是main
- public static void main(String[] args) //SystemServer运行的第一个函数
- ServerThread thr = new ServerThread(); //初始化一个server线程
- class ServerThread
- {
- public void initAndLoop()
- {
- if (!disableLocation) //开始的时候这里的 disableLocation = false
- {
- try{
- Slog.i(TAG, "Location Manager");
- <1> location = new LocationManagerService(context); //实例化一个LocationManagerService服务,该服务主要用来管理GPS地理位置的变化
- ServiceManager.addService(Context.LOCATION_SERVICE, location); //将这个service添加到SystemManager中
- }
- catch (Throwable e)
- {
- reportWtf("starting Location Manager", e);
- }
- try {
- Slog.i(TAG, "Country Detector");
- countryDetector = new CountryDetectorService(context);//实例化一个CountryDetectorService服务,用来探测不同国家,也就是不同地区和时区
- ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
- } catch (Throwable e) {
- reportWtf("starting Country Detector", e);
- }
- }
- ActivityManagerService.self().systemReady(new Runnable()
- {
- public void run()
- {
- try
- {
- <2> if (locationF != null) locationF.systemRunning();//在<1>的位置已给locationF赋值,在这里调用LocationManagerService的systemRunning()函数
- }
- catch(Throwable e)
- {
- reportWtf("Notifying Location Service running", e);
- }
- }
- }
- }
- }
- 接下来分析LocationManagerService(context)
- /**framework\base\services\java\com\android\server\LocationManagerService.java**/
- public class LocationManagerService extends ILocationManager.Stub
- {
- public void systemRunning() //该函数有<2>处调用
- {
- synchronized (mLock)
- {
- /*GPS启动到运行及监听都在这两个函数里*/
- <3> loadProvidersLocked(); //在这里加载各种provider
- <4> updateProvidersLocked(); //加载后更新
- }
- }
- }
- /**framework\base\services\java\com\android\server\LocationManagerService.java**/
- private void loadProvidersLocked()
- {
- // create a passive location provider, which is always enabled
- PassiveProvider passiveProvider = new PassiveProvider(this);
- addProviderLocked(passiveProvider);
- mEnabledProviders.add(passiveProvider.getName());
- mPassiveProvider = passiveProvider;
- // Create a gps location provider
- <5> GpsLocationProvider gpsProvider = new GpsLocationProvider(mContext, this,
- mLocationHandler.getLooper());
- <6> if (GpsLocationProvider.isSupported()) //这里调用JNI的函数接口
- {
- mGpsStatusProvider = gpsProvider.getGpsStatusProvider(); //获取gps状态provider
- mNetInitiatedListener = gpsProvider.getNetInitiatedListener(); //初始网络监听
- addProviderLocked(gpsProvider); //将gpsProvider添加到mProviders中
- mRealProviders.put(LocationManager.GPS_PROVIDER, gpsProvider);
- }
- }
- /**分析 <5> ***/
- public class GpsLocationProvider implements LocationProviderInterface
- {
- static { class_init_native(); } //在这里调用JNI的android_location_GpsLocationProvider_class_init_native函数
- }
- static void android_location_GpsLocationProvider_class_init_native(JNIEnv* env, jclass clazz)
- {
- err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);//打开GPS设备文件,对应trunk/Android/hardware/xxx/gps/skytraq/skytraq_gps.c
- -->static int open_gps(const struct hw_module_t *module, char const *name, struct hw_device_t **device)
- //获取gpsInterface接口,该函数接口在trunk/Android/hardware/xxx/gps/skytraq/skytraq_gps.c中,对应的函数是gps__get_gps_interface
- sGpsInterface = gps_device->get_gps_interface(gps_device);
- }
- const GpsInterface *gps__get_gps_interface(struct gps_device_t *dev)
- {
- return &skytraqGpsInterface;
- }
- /**分析 <6> **/
- public static boolean GpsLocationProvider.isSupported()
- {
- return native_is_supported(); //该函数对应JNI的函数如下
- }
- static jboolean android_location_GpsLocationProvider_is_supported(JNIEnv* env, jclass clazz)
- {
- return (sGpsInterface != NULL);
- }
- GpsLocationProvider.isSupported()返回true,所以会走if{}里面的。在网上查阅资料,都是基于2.3的系统,2.3的系统里是直接在isSupported()函数里来获取接口,没有在GpsLocationProvider类中实现调用class_init_native()来获取接口
- /**分析 <4> **/
- /**framework\base\services\java\com\android\server\LocationManagerService.java**/
- private void updateProvidersLocked()
- {
- for (int i = mProviders.size() - 1; i >= 0; i--)
- {
- boolean isEnabled = p.isEnabled(); //开始的时候返回false
- boolean shouldBeEnabled = isAllowedByCurrentUserSettingsLocked(name); //在loadProvidersLocked()函数就说了,一直是enabled
- 所以会走else
- else if (!isEnabled && shouldBeEnabled)
- {
- <7> updateProviderListenersLocked(name, true, mCurrentUserId);
- changesMade = true;
- }
- }
- /*发送广播,app和framework都可以接受该广播*/
- if (changesMade) {
- mContext.sendBroadcastAsUser(new Intent(LocationManager.PROVIDERS_CHANGED_ACTION),
- UserHandle.ALL);
- mContext.sendBroadcastAsUser(new Intent(LocationManager.MODE_CHANGED_ACTION),
- UserHandle.ALL);
- }
- }
- /**分析 <7> framework\base\services\java\com\android\server\locationManagerService.java **/
- private void updateProviderListenersLocked(String provider, boolean enabled, int userId)
- {
- if (records != null)
- {
- //这里主要是从UserHandle里解析UserId 是否等于 mCurrentUserId,如果是则发送notification给receiver
- }
- if (enabled) //这里的 enabled == true
- {
- <8> p.enable();
- if (listeners > 0)
- {
- <9> applyRequirementsLocked(provider);
- }
- }
- else
- {
- p.disable();
- }
- }
- /**分析 <8> framework\base\services\java\com\android\server\location\gpsLocationProvider.java**/
- public void enable()
- {
- synchronized (mLock)
- {
- if (mEnabled) return;//开始的时候这里是false
- mEnabled = true;
- }
- <10> sendMessage(ENABLE, 1, null);//发送消息
- }
- /**分析 <10> base\services\java\com\android\server\location\gpsLocationProvider.java**/
- private void sendMessage(int message, int arg, Object obj)
- {
- // hold a wake lock until this message is delivered
- // note that this assumes the message will not be removed from the queue before
- // it is handled (otherwise the wake lock would be leaked).
- mWakeLock.acquire();
- <11> mHandler.obtainMessage(message, arg, 1, obj).sendToTarget(); //通过handler机制来发送消息
- }
- /**分析 <11> base\services\java\com\android\server\location\gpsLocationProvider.java**/
- private final class ProviderHandler extends Handler
- {
- public void handleMessage(Message msg)
- {
- switch (message)
- {
- case ENABLE: //有<10>可知,message == ENABLE , msg.arg1 == 1
- if (msg.arg1 == 1) {
- <12> handleEnable(); //所以会调用这个函数
- } else {
- handleDisable();
- }
- break;
- .....
- }
- }
- }
- /**分析 <12> framework\base\services\java\com\android\server\location\gpsLocationProvider.java**/
- private void handleEnable()
- {
- <13> boolean enabled = native_init(); //调用JNI中的 static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
- if (enabled)
- {
- mSupportsXtra = native_supports_xtra(); //native_supports_xtra()直接返回true,在class_init_native()已经赋值
- if (mSuplServerHost != null)
- {
- /*这里没做任何事,因为sAGpsInterface为空,代码流程如下*/
- native_set_agps_server(AGPS_TYPE_SUPL, mSuplServerHost, mSuplServerPort);
- -->static void android_location_GpsLocationProvider_set_agps_server(JNIEnv* env, jobject obj, jint type, jstring hostname, jint port)
- -->if (!sAGpsInterface) return;
- -->sAGpsInterface = (const AGpsInterface*)sGpsInterface->get_extension(AGPS_INTERFACE);
- -->static const void *skytraq_gps_get_extension(const char* name)
- {
- ALOGD("%s: called", __FUNCTION__);
- return NULL;
- }
- }
- if (mC2KServerHost != null) { //同理这里也直接返回了
- native_set_agps_server(AGPS_TYPE_C2K, mC2KServerHost, mC2KServerPort);
- }
- }else{
- synchronized (mLock) {
- mEnabled = false;
- }
- Log.w(TAG, "Failed to enable location provider");
- }
- }
- /**分析 <13> framework\base\services\jni\com_android_server_location_GpsLocationProvider.cpp **/
- static jboolean android_location_GpsLocationProvider_init(JNIEnv* env, jobject obj)
- {
- <14> if (!sGpsInterface || sGpsInterface->init(&sGpsCallbacks) != 0)//sGpsInterface不为null,在一开始的就已赋值,所以会调用init函数
- return false;
- .....
- }
- /**分析 <14> hardware\xxx\gps\skytraq\Skytraq_gps.c **/
- static int skytraq_gps_init(GpsCallbacks* callbacks) //callbacks回调函数时有JNI传下来的,在JNI中实现的
- {
- GpsState* s = &_gps_state; //该结构图数组初始化的时候都为0
- if (!s->init)
- <15> gps_state_init(s, callbacks);
- }
- /**分析 <15> hardware\xxx\gps\skytraq\Skytraq_gps.c **/
- static void gps_state_init( GpsState* state, GpsCallbacks* callbacks )
- {
- state->fd = gps_channel_open(GPS_CHANNEL_NAME);
- --> fd = open("/dev/skytraq_gps_ipc", O_RDWR | O_SYNC); //打开串口
- int ret = ioctl(state->fd, GPS_IPC_SET, &gps_connect_info); //调用驱动函数ioctl设置gps
- state->thread = callbacks->create_thread_cb( "gps_state_thread", gps_state_thread, state );//创建线程,用来监听发送的命令和监听数据上报,
- }
- /**分析<9> frwmework\base\services\java\com\android\server\LocationManagerService.java
- private void applyRequirementsLocked(String provider)
- {
- //设置worksource和providerRequest
- .....
- <16> p.setRequest(providerRequest, worksource); //location provider 发送请求
- }
- /**分析<16> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
- public void setRequest(ProviderRequest request, WorkSource source)
- {
- <17> sendMessage(SET_REQUEST, 0, new GpsRequest(request, source)); //发送消息,SET_REQUEST == 3
- }
- /**分析<17> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
- public void handleMessage(Message msg)
- {
- ...
- case SET_REQUEST:
- GpsRequest gpsRequest = (GpsRequest) msg.obj;
- <18> handleSetRequest(gpsRequest.request, gpsRequest.source);
- break;
- }
- /**分析<18> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
- private void handleSetRequest(ProviderRequest request, WorkSource source)
- {
- ...
- <19> startNavigating(singleShot);//开始导航
- }
- /**分析<19> frwmework\base\services\java\com\android\server\location\GpsLocationProvider.java**/
- private void startNavigating(boolean singleShot)
- {
- <20> native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC, interval, 0, 0)//该函数在HAL层直接返回了,没做任何事
- <21> native_start()
- }
- /**分析<20> framework\base\services\jni\com_android_server_location_GpsLocationProvider.cpp**/
- static jboolean android_location_GpsLocationProvider_set_position_mode(JNIEnv* env, jobject obj,
- jint mode, jint recurrence, jint min_interval, jint preferred_accuracy, jint preferred_time)
- {
- if (sGpsInterface)
- <22> return (sGpsInterface->set_position_mode(mode, recurrence, min_interval, preferred_accuracy,
- preferred_time) == 0); //调用HAL层接口
- }
- /**分析<22> xxx\gps\skytraq\skytraq.gps.c**/
- static int skytraq_gps_set_position_mode(GpsPositionMode mode,
- GpsPositionRecurrence recurrence,
- uint32_t min_interval,
- uint32_t preferred_accuracy,
- uint32_t preferred_time)
- {
- ALOGD("%s: called", __FUNCTION__);
- return 0;
- }
- /**分析<21> framework\base\services\jni\com_android_server_location_GpsLocationProvider.cpp**/
- static jboolean android_location_GpsLocationProvider_start(JNIEnv* env, jobject obj)
- {
- if (sGpsInterface)
- <23> return (sGpsInterface->start() == 0); //调用HAL 层的static int skytraq_gps_start()
- }
- /**分析<23> xxx\gps\skytraq\skytraq.gps.c**/
- static int skytraq_gps_start()
- {
- gps_state_start(s);
- -->s->status = CMD_START;
- /*在<15>线程函数gps_state_thread()中while(1)中等该命令,进入获取数据*/
- }
- 此时GPS就已运行
Android4.4 GPS框架分析【转】的更多相关文章
- Android/Linux下CGroup框架分析及其使用
1 cgroup介绍 CGroup是control group的简称,它为Linux kernel提供一种任务聚集和划分的机制,可以限制.记录.隔离进程组(process groups)所使用的资源( ...
- 几款开源的hybird移动app框架分析
几款开源的Hybrid移动app框架分析 Ionic Onsen UI 与 ionic 相比 jQuery Mobile Mobile Angular UI 结论 很多移动开发者喜欢使用原生代码开发, ...
- 深入浅出 - Android系统移植与平台开发(十一) - Sensor HAL框架分析之一
作者:唐老师,华清远见嵌入式学院讲师. 1. Sensor的概念 Sensor即传感器,在当前智能手机上大量存在:G-Sensor.LightsSensor. ProximitySensor.Temp ...
- 深入浅出 - Android系统移植与平台开发(八)- HAL Stub框架分析
作者:唐老师,华清远见嵌入式学院讲师. 1. HAL Stub框架分析 HAL stub的框架比较简单,三个结构体.两个常量.一个函数,简称321架构,它的定义在:@hardware/libhardw ...
- android4.3 Bluetooth(le)分析之startLeScan分析
BluetoothAdapter.java中有low enery(le)的一些方法,android提供了这些方法,但源码中并未找到这些方法的调用之处.本文档主要分析这类方法的执行流程,来了解下le到底 ...
- openwrt: Makefile 框架分析
openwrt: Makefile 框架分析 原文链接:blog.chinaunix.net/uid-26675482-id-4704952.html 本篇的主要目的是想通过分析Makefile,了解 ...
- Android 核心分析 之六 IPC框架分析 Binder,Service,Service manager
IPC框架分析 Binder,Service,Service manager 我首先从宏观的角度观察Binder,Service,Service Manager,并阐述各自的概念.从Linux的概念空 ...
- VS2010/MFC编程入门之四(MFC应用程序框架分析)
VS2010/MFC编程入门之四(MFC应用程序框架分析)-软件开发-鸡啄米 http://www.jizhuomi.com/software/145.html 上一讲鸡啄米讲的是VS2010应用 ...
- Yii PHP 框架分析(二)
Yii PHP 框架分析(二)作者:wdy http://hi.baidu.com/delphiss/blog/item/54597af595085ad3f3d38552.html Yii是基于组件( ...
随机推荐
- NormalMap 贴图 [转]
转载: http://www.zwqxin.com/archives/shaderglsl/review-normal-map-bump-map.html 说起Normal Map(法线贴图),就 ...
- 线段树专题—ZOJ1610 Count the Colors
题意:给一个n,代表n次操作,接下来每次操作表示把[l.r]区间的线段涂成k的颜色当中,l,r,k的范围都是0到8000 分析:事实上就是拿线段树维护一段区间的颜色,整体用到的是线段树的区间更新把,可 ...
- MySQL ERROR 1044 (42000) 解决方法
在Terminal中输入 mysql 进入到数据库命令行,然后直接: CREATE DATABASE IF NOT EXISTS yuntu; 结果出现如下错误: ERROR 1044 (42000) ...
- Unique Binary Search Trees I&II——给定n有多少种BST可能、DP
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Oracle 查询一个表的所有字段
select * from user_tab_columns where table_name = 'T_B_CLIENT_MSG'
- Hibernate学习五----------组件属性
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- dede频道标签channel和频道内容标签channelartlist的调用栏目名的不同方式,如果错误使用标签会发生错误
频道标签 [field:typename/] 频道内容标签 {dede:field name='typename'/}
- ubuntu 安装时遇到 hash sum mismatch 处理方法
ubuntu安装大软件时,下载经常容易出错,hash sum mismatch是其中一种,说到底还是网络不好,重试很多遍都是这个错误,最后的解决方案是把mismatch说的那个链接用firefox打开 ...
- checkStyle使用具体解释
简单介绍 checkStyle是一款代码格式检查工具.它依据设置好的编码规则来自己主动检查代码.比方命名规范,文件长度.代码行长度等等.代码检查工具是保证项目代码质量.统一编码风格的一种重要途径.本篇 ...
- golang中字符串的查找方法小结
1)func Contains(s, substr string) bool这个函数是查找某个字符是否在这个字符串中存在,存在返回true 示例如下: import ( "fmt" ...