天天看點

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.         mNtpTime = NtpTrustedTime.getInstance(context);  
  5.         mILocationManager = ilocationManager;  
  6.         mLocation.setExtras(mLocationExtras);  
  7.         // Create a wake lock  
  8.         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);  
  9.         mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY);  
  10.         mWakeLock.setReferenceCounted(true);  
  11.         mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);  
  12.         mWakeupIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_WAKEUP), 0);  
  13.         mTimeoutIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_TIMEOUT), 0);  
  14.         mConnMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  15.         // App ops service to keep track of who is accessing the GPS  
  16.         mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService(  
  17.                 Context.APP_OPS_SERVICE));  
  18.         // Battery statistics service to be notified when GPS turns on or off  
  19.         mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(  
  20.                 BatteryStats.SERVICE_NAME));  
  21.         // 加載GPS配置檔案,下文詳細介紹函數reloadGpsProperties  
  22.         mProperties = new Properties();  
  23.         reloadGpsProperties(mContext, mProperties);  
  24.         // 主要處理來自GPS HAL層通知的NI事件  
  25.         mNIHandler = new GpsNetInitiatedHandler(context,  
  26.                                                 mNetInitiatedListener,  
  27.                                                 mSuplEsEnabled);  
  28.         // construct handler, listen for events  
  29.         mHandler = new ProviderHandler(looper);  
  30.         //SUPL的初始化可以由兩種特殊的短信觸發,下文将簡單介紹listenForBroadcasts函數  
  31.         listenForBroadcasts();  
  32.         // 對PASSIVE_PROVIDER添加監聽  
  33.         mHandler.post(new Runnable() {  
  34.             @Override  
  35.             public void run() {  
  36.                 LocationManager locManager =  
  37.                         (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);  
  38.                 final long minTime = 0;  
  39.                 final float minDistance = 0;  
  40.                 final boolean oneShot = false;  
  41.                 LocationRequest request = LocationRequest.createFromDeprecatedProvider(  
  42.                         LocationManager.PASSIVE_PROVIDER,  
  43.                         minTime,  
  44.                         minDistance,  
  45.                         oneShot);  
  46.                 request.setHideFromAppOps(true);  
  47.                 //接收來自NetworkLP的位置更新通知  
  48.                 //當GpsLP收到來自NetworkLP的位置資訊後,将把他們傳給GPS HAL層去處理  
  49.                 locManager.requestLocationUpdates(  
  50.                         request,  
  51.                         new NetworkLocationListener(),  
  52.                         mHandler.getLooper());  
  53.             }  
  54.         });  
  55.     }  

函數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.     // 讀取配置檔案中的屬性資訊,通過JNI設定到HAL層  
  18.     setSuplHostPort(properties.getProperty("SUPL_HOST"),  
  19.                     properties.getProperty("SUPL_PORT"));  
  20.     //C2K是CDMA2000的縮寫,C2K_HOST和C2K_PORT主要用于GPS子產品的測試,對使用者來說沒有太大意義  
  21.     mC2KServerHost = properties.getProperty("C2K_HOST");  
  22.     String portString = properties.getProperty("C2K_PORT");  
  23.     if (mC2KServerHost != null && portString != null) {  
  24.         try {  
  25.             mC2KServerPort = Integer.parseInt(portString);  
  26.         } catch (NumberFormatException e) {  
  27.             Log.e(TAG, "unable to parse C2K_PORT: " + portString);  
  28.         }  
  29.     }  
  30.     try {  
  31.         // Convert properties to string contents and send it to HAL.  
  32.         ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);  
  33.         properties.store(baos, null);  
  34.         native_configuration_update(baos.toString());  
  35.         Log.d(TAG, "final config = " + baos.toString());  
  36.     } catch (IOException ex) {  
  37.         Log.w(TAG, "failed to dump properties contents");  
  38.     }  
  39.     // SUPL_ES configuration.  
  40.     String suplESProperty = mProperties.getProperty("SUPL_ES");  
  41.     if (suplESProperty != null) {  
  42.         try {  
  43.             mSuplEsEnabled = (Integer.parseInt(suplESProperty) == 1);  
  44.         } catch (NumberFormatException e) {  
  45.             Log.e(TAG, "unable to parse SUPL_ES: " + suplESProperty);  
  46.         }  
  47.     }  
  48. }  

下面我們來看一下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.         //SUPL INIT也可由WAP推送短信觸發,該短信包含的資料類型為MIME中的"application/vnd.omaloc-supl-init"  
  10.         intentFilter = new IntentFilter();  
  11.         intentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION);  
  12.         try {  
  13.             intentFilter.addDataType("application/vnd.omaloc-supl-init");  
  14.         } catch (IntentFilter.MalformedMimeTypeException e) {  
  15.             Log.w(TAG, "Malformed SUPL init mime type");  
  16.         }  
  17.         mContext.registerReceiver(mBroadcastReceiver, intentFilter, null, mHandler);  
  18.         //監聽ALARM事件、網絡事件等  
  19.         intentFilter = new IntentFilter();  
  20.         intentFilter.addAction(ALARM_WAKEUP);  
  21.         intentFilter.addAction(ALARM_TIMEOUT);  
  22.         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
  23.         intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);  
  24.         intentFilter.addAction(Intent.ACTION_SCREEN_OFF);  
  25.         intentFilter.addAction(Intent.ACTION_SCREEN_ON);  
  26.         intentFilter.addAction(SIM_STATE_CHANGED);  
  27.         // TODO: remove the use TelephonyIntents. We are using it because SIM_STATE_CHANGED  
  28.         // is not reliable at the moment.  
  29.         intentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_CONTENT_CHANGE);  
  30.         intentFilter.addAction(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED);  
  31.         mContext.registerReceiver(mBroadcastReceiver, intentFilter, null, mHandler);  
  32.     }  

當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. private void checkWapSuplInit(Intent intent) {  
  9.     byte[] supl_init = (byte[]) intent.getExtra("data");  
  10.     native_agps_ni_message(supl_init,supl_init.length);  
  11. }  

GpsLP初始化完畢。

參考文獻:《深入了解Android:WiFi、NFC和GPS卷》

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