Android GPS学习笔记(6)—GpsLP初始化

目录:

frameworks\base\services\core\java\com\android\server\location


GpsLocationProvider本身有一段初始化代码,如下所示:

[java]  view plain copy
  1. //GpsLP定义了一些native函数,此处的class_init_native将初始化相关JNI方法  
  2. static { class_init_native(); }  

下面看一下GpsLocationProvider的构造函数:

[java]  view plain copy
  1. public GpsLocationProvider(Context context, ILocationManager ilocationManager,  
  2.             Looper looper) {  
  3.         mContext = context;  
  4.         /** 
  5.         NTP即Network Time Protocol,它是一种用来同步计算机时间的协议。该时间的源是UTC。在下面这行代码中,GpsLP 
  6. 将创建一个NtpTrustedTime对象,该对象采用SNTP协议来和指定的NTP服务器通信以获取准确的时间。 
  7.         **/  
  8.         mNtpTime = NtpTrustedTime.getInstance(context);  
  9.         mILocationManager = ilocationManager;  
  10.   
  11.         mLocation.setExtras(mLocationExtras);  
  12.   
  13.         // Create a wake lock  
  14.         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);  
  15.         mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);  
  16.         mWakeLock.setReferenceCounted(true);  
  17.   
  18.         mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);  
  19.         mWakeupIntent = PendingIntent.getBroadcast(mContext, 0new Intent(ALARM_WAKEUP), 0);  
  20.         mTimeoutIntent = PendingIntent.getBroadcast(mContext, 0new Intent(ALARM_TIMEOUT), 0);  
  21.   
  22.         mConnMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  23.   
  24.         // App ops service to keep track of who is accessing the GPS  
  25.         mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(  
  26.                 Context.APP_OPS_SERVICE));  
  27.   
  28.         // Battery statistics service to be notified when GPS turns on or off  
  29.         mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(  
  30.                 BatteryStats.SERVICE_NAME));  
  31.   
  32.         // 加载GPS配置文件,下文详细介绍函数reloadGpsProperties  
  33.         mProperties = new Properties();  
  34.         reloadGpsProperties(mContext, mProperties);  
  35.   
  36.         // 主要处理来自GPS HAL层通知的NI事件  
  37.         mNIHandler = new GpsNetInitiatedHandler(context,  
  38.                                                 mNetInitiatedListener,  
  39.                                                 mSuplEsEnabled);  
  40.   
  41.         // construct handler, listen for events  
  42.         mHandler = new ProviderHandler(looper);  
  43.         //SUPL的初始化可以由两种特殊的短信触发,下文将简单介绍listenForBroadcasts函数  
  44.         listenForBroadcasts();  
  45.   
  46.         // 对PASSIVE_PROVIDER添加监听  
  47.         mHandler.post(new Runnable() {  
  48.             @Override  
  49.             public void run() {  
  50.                 LocationManager locManager =  
  51.                         (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);  
  52.                 final long minTime = 0;  
  53.                 final float minDistance = 0;  
  54.                 final boolean oneShot = false;  
  55.                 LocationRequest request = LocationRequest.createFromDeprecatedProvider(  
  56.                         LocationManager.PASSIVE_PROVIDER,  
  57.                         minTime,  
  58.                         minDistance,  
  59.                         oneShot);  
  60.   
  61.                 request.setHideFromAppOps(true);  
  62.                 //接收来自NetworkLP的位置更新通知  
  63.                 //当GpsLP收到来自NetworkLP的位置信息后,将把他们传给GPS HAL层去处理  
  64.                 locManager.requestLocationUpdates(  
  65.                         request,  
  66.                         new NetworkLocationListener(),  
  67.                         mHandler.getLooper());  
  68.             }  
  69.         });  
  70.     }  
函数reloadGpsProperties从文件etc/gps.conf中加载了GPS相关参数包括SUPL服务器地址、端口号等,代码如下:

[java]  view plain copy
  1. private void reloadGpsProperties(Context context, Properties properties) {  
  2.     Log.d(TAG, "Reset GPS properties, previous size = " + properties.size());  
  3.     loadPropertiesFromResource(context, properties);  
  4.     boolean isPropertiesLoadedFromFile = false;  
  5.     final String gpsHardware = SystemProperties.get("ro.hardware.gps");  
  6.     if (!TextUtils.isEmpty(gpsHardware)) {  
  7.         final String propFilename =  
  8.                 PROPERTIES_FILE_PREFIX + "." + gpsHardware + PROPERTIES_FILE_SUFFIX;  
  9.         isPropertiesLoadedFromFile =  
  10.                 loadPropertiesFromFile(propFilename, properties);  
  11.     }  
  12.     //加载文件中的GPS属性信息  
  13.     if (!isPropertiesLoadedFromFile) {  
  14.         loadPropertiesFromFile(DEFAULT_PROPERTIES_FILE, properties);  
  15.     }  
  16.     Log.d(TAG, "GPS properties reloaded, size = " + properties.size());  
  17.   
  18.     // 读取配置文件中的属性信息,通过JNI设置到HAL层  
  19.     setSuplHostPort(properties.getProperty("SUPL_HOST"),  
  20.                     properties.getProperty("SUPL_PORT"));  
  21.     //C2K是CDMA2000的缩写,C2K_HOST和C2K_PORT主要用于GPS模块的测试,对用户来说没有太大意义  
  22.     mC2KServerHost = properties.getProperty("C2K_HOST");  
  23.     String portString = properties.getProperty("C2K_PORT");  
  24.     if (mC2KServerHost != null && portString != null) {  
  25.         try {  
  26.             mC2KServerPort = Integer.parseInt(portString);  
  27.         } catch (NumberFormatException e) {  
  28.             Log.e(TAG, "unable to parse C2K_PORT: " + portString);  
  29.         }  
  30.     }  
  31.   
  32.     try {  
  33.         // Convert properties to string contents and send it to HAL.  
  34.         ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);  
  35.         properties.store(baos, null);  
  36.         native_configuration_update(baos.toString());  
  37.         Log.d(TAG, "final config = " + baos.toString());  
  38.     } catch (IOException ex) {  
  39.         Log.w(TAG, "failed to dump properties contents");  
  40.     }  
  41.   
  42.     // SUPL_ES configuration.  
  43.     String suplESProperty = mProperties.getProperty("SUPL_ES");  
  44.     if (suplESProperty != null) {  
  45.         try {  
  46.             mSuplEsEnabled = (Integer.parseInt(suplESProperty) == 1);  
  47.         } catch (NumberFormatException e) {  
  48.             Log.e(TAG, "unable to parse SUPL_ES: " + suplESProperty);  
  49.         }  
  50.     }  
  51. }  
下面我们来看一下listenForBroadcasts函数,其内容如下:

[java]  view plain copy
  1. private void listenForBroadcasts() {  
  2.         IntentFilter intentFilter = new IntentFilter();  
  3.         //SUPL INIT流程可以由一条特殊的数据短信触发。数据短信与文本短信不同,下面的这个IntentFilter将接收发往  
  4.         //127.0.0.1的数据短信。7275为OMA-SUPL使用的端口号  
  5.         intentFilter.addAction(Intents.DATA_SMS_RECEIVED_ACTION);  
  6.         intentFilter.addDataScheme("sms");  
  7.         intentFilter.addDataAuthority("localhost","7275");  
  8.         mContext.registerReceiver(mBroadcastReceiver, intentFilter, null, mHandler);  
  9.           
  10.         //SUPL INIT也可由WAP推送短信触发,该短信包含的数据类型为MIME中的"application/vnd.omaloc-supl-init"  
  11.         intentFilter = new IntentFilter();  
  12.         intentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION);  
  13.         try {  
  14.             intentFilter.addDataType("application/vnd.omaloc-supl-init");  
  15.         } catch (IntentFilter.MalformedMimeTypeException e) {  
  16.             Log.w(TAG, "Malformed SUPL init mime type");  
  17.         }  
  18.         mContext.registerReceiver(mBroadcastReceiver, intentFilter, null, mHandler);  
  19.   
  20.         //监听ALARM事件、网络事件等  
  21.         intentFilter = new IntentFilter();  
  22.         intentFilter.addAction(ALARM_WAKEUP);  
  23.         intentFilter.addAction(ALARM_TIMEOUT);  
  24.         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
  25.         intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);  
  26.         intentFilter.addAction(Intent.ACTION_SCREEN_OFF);  
  27.         intentFilter.addAction(Intent.ACTION_SCREEN_ON);  
  28.         intentFilter.addAction(SIM_STATE_CHANGED);  
  29.         // TODO: remove the use TelephonyIntents. We are using it because SIM_STATE_CHANGED  
  30.         // is not reliable at the moment.  
  31.         intentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_CONTENT_CHANGE);  
  32.         intentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED);  
  33.         mContext.registerReceiver(mBroadcastReceiver, intentFilter, null, mHandler);  
  34.     }  
当GpsLP收到指定的数据短信或WAP推送短信后,checkSmsSuplInit或checkWapSuplInit函数将被调用。这两个函数的功能比较简单,就是将短信的内容传递到GPS HAL层,下面是它们的代码:

[java]  view plain copy
  1. private void checkSmsSuplInit(Intent intent) {  
  2.     SmsMessage[] messages = Intents.getMessagesFromIntent(intent);  
  3.     for (int i=0; i <messages.length; i++) {  
  4.         byte[] supl_init = messages[i].getUserData();  
  5.         native_agps_ni_message(supl_init,supl_init.length);  
  6.     }  
  7. }  
  8.   
  9. private void checkWapSuplInit(Intent intent) {  
  10.     byte[] supl_init = (byte[]) intent.getExtra("data");  
  11.     native_agps_ni_message(supl_init,supl_init.length);  
  12. }  
GpsLP初始化完毕。

参考文献:《深入理解Android:WiFi、NFC和GPS卷》


原文:http://blog.csdn.net/dreamback1987/article/details/46789903

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值