程序员是一块砖,哪里需要哪里搬,蓝牙看完看wifi,wifi看完看gps.
我们从最上层的应用settings开始:
1.settings
packages/apps/Settings/src/com/android/settings/location/LocationSwitchBarController.java
96 public void onSwitchChanged(Switch switchView, boolean isChecked) {
97 mLocationEnabler.setLocationEnabled(isChecked);
98 }
packages/apps/Settings/src/com/android/settings/location/LocationEnabler.java
106 void setLocationEnabled(boolean enabled) {
107 final int currentMode = Settings.Secure.getInt(mContext.getContentResolver(),
108 Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
109
110 if (isRestricted()) {
111
112
113 if (Log.isLoggable(TAG, Log.INFO)) {
114 Log.i(TAG, "Restricted user, not setting location mode");
115 }
116 if (mListener != null) {
117 mListener.onLocationModeChanged(currentMode, true);
118 }
119 return;
120 }
121 updateLocationEnabled(mContext, enabled, UserHandle.myUserId(),
122 Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS);
123 refreshLocationMode();
124 }
2.framework
frameworks/base/location/java/android/location/LocationManager.java
519 public void setLocationEnabledForUser(boolean enabled, @NonNull UserHandle userHandle) {
520 try {
521 mService.setLocationEnabledForUser(enabled, userHandle.getIdentifier());
522 } catch (RemoteException e) {
523 throw e.rethrowFromSystemServer();
524 }
525 }
frameworks/base/services/core/java/com/android/server/location/LocationManagerService.java
2363 public void setLocationEnabledForUser(boolean enabled, int userId) {
2364 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
2365 userId, false, false, "setLocationEnabledForUser", null);
2366
2367 mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS, null);
2368
2369 LocationManager.invalidateLocalLocationEnabledCaches();
2370 mSettingsHelper.setLocationEnabled(enabled, userId);
2371 }
这里我们需要关注的重点其实是最后一个:setlocationEnabled,因为它是靠一个`provider监听配置项来启动的,而非靠启动service或者binder调用
frameworks/base/services/core/java/com/android/server/location/SettingsHelper.java
144 public void setLocationEnabled(boolean enabled, int userId) {
145 long identity = Binder.clearCallingIdentity();
146 try {
147 Settings.Secure.putIntForUser(
148 mContext.getContentResolver(),
149 Settings.Secure.LOCATION_MODE,
150 enabled
151 ? Settings.Secure.LOCATION_MODE_ON
152 : Settings.Secure.LOCATION_MODE_OFF,
153 userId);
154 } finally {
155 Binder.restoreCallingIdentity(identity);
156 }
157 }
我们需要找的就是监听LOCATION_MODE 这个配置项的位置: frameworks/base/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
734 mContext.getContentResolver().registerContentObserver(
735 Settings.Secure.getUriFor(Settings.Secure.LOCATION_MODE),
736 true,
737 new ContentObserver(mHandler) {
738 @Override
739 public void onChange(boolean selfChange) {
740 updateEnabled();
741 }
742 }, UserHandle.USER_ALL);
到这里framework就快走完了,后面基本就跟着代码走就行了,到gnssbatchingprovider时,进入native方法 updateEnabled() -> handleEnable() -> mGnssBatchingProvider.enable()
3.native
frameworks/base/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java
static jboolean android_location_GnssLocationProvider_init(JNIEnv* env, jobject obj) {
2398
2401 if (!mCallbacksObj)
2402 mCallbacksObj = env->NewGlobalRef(obj);
2403
2404
2407 if (gnssHal == nullptr) {
2408 ALOGE("Unable to initialize GNSS HAL.");
2409 return JNI_FALSE;
2410 }
2411
2412 Return<bool> result = false;
2413
2414
2415 sp<IGnssCallback_V2_1> gnssCbIface = new GnssCallback();
2416 if (gnssHal_V2_1 != nullptr) {
2417 result = gnssHal_V2_1->setCallback_2_1(gnssCbIface);
2418 } else if (gnssHal_V2_0 != nullptr) {
2419 result = gnssHal_V2_0->setCallback_2_0(gnssCbIface);
2420 } else if (gnssHal_V1_1 != nullptr) {
2421 result = gnssHal_V1_1->setCallback_1_1(gnssCbIface);
2422 } else {
2423 result = gnssHal->setCallback(gnssCbIface);
2424 }
2425
2426 if (!checkHidlReturn(result, "IGnss setCallback() failed.")) {
2427 return JNI_FALSE;
2428 }
2429
2430
2431 if (gnssXtraIface == nullptr) {
2432 ALOGI("Unable to initialize IGnssXtra interface.");
2433 } else {
2434 sp<IGnssXtraCallback> gnssXtraCbIface = new GnssXtraCallback();
2435 result = gnssXtraIface->setCallback(gnssXtraCbIface);
2436 if (!checkHidlReturn(result, "IGnssXtra setCallback() failed.")) {
2437 gnssXtraIface = nullptr;
2438 }
2439 }
2440
2441
2442 if (agnssIface_V2_0 != nullptr) {
2443 sp<IAGnssCallback_V2_0> aGnssCbIface = new AGnssCallback_V2_0();
2444 auto agnssStatus = agnssIface_V2_0->setCallback(aGnssCbIface);
2445 checkHidlReturn(agnssStatus, "IAGnss 2.0 setCallback() failed.");
2446 } else if (agnssIface != nullptr) {
2447 sp<IAGnssCallback_V1_0> aGnssCbIface = new AGnssCallback_V1_0();
2448 auto agnssStatus = agnssIface->setCallback(aGnssCbIface);
2449 checkHidlReturn(agnssStatus, "IAGnss setCallback() failed.");
2450 } else {
2451 ALOGI("Unable to initialize IAGnss interface.");
2452 }
2453
2454
2455 sp<IGnssGeofenceCallback> gnssGeofencingCbIface = new GnssGeofenceCallback();
2456 if (gnssGeofencingIface != nullptr) {
2457 auto status = gnssGeofencingIface->setCallback(gnssGeofencingCbIface);
2458 checkHidlReturn(status, "IGnssGeofencing setCallback() failed.");
2459 } else {
2460 ALOGI("Unable to initialize IGnssGeofencing interface.");
2461 }
2462
2463
2464 sp<IGnssNiCallback> gnssNiCbIface = new GnssNiCallback();
2465 if (gnssNiIface != nullptr) {
2466 auto status = gnssNiIface->setCallback(gnssNiCbIface);
2467 checkHidlReturn(status, "IGnssNi setCallback() failed.");
2468 } else {
2469 ALOGI("Unable to initialize IGnssNi interface.");
2470 }
2471
2472
2473 sp<IAGnssRilCallback> aGnssRilCbIface = new AGnssRilCallback();
2474 if (agnssRilIface != nullptr) {
2475 auto status = agnssRilIface->setCallback(aGnssRilCbIface);
2476 checkHidlReturn(status, "IAGnssRil setCallback() failed.");
2477 } else {
2478 ALOGI("Unable to initialize IAGnssRil interface.");
2479 }
2480
2481
2482 if (gnssVisibilityControlIface != nullptr) {
2483 sp<IGnssVisibilityControlCallback> gnssVisibilityControlCbIface =
2484 new GnssVisibilityControlCallback();
2485 result = gnssVisibilityControlIface->setCallback(gnssVisibilityControlCbIface);
2486 checkHidlReturn(result, "IGnssVisibilityControl setCallback() failed.");
2487 }
2488
2489
2490 if (gnssCorrectionsIface_V1_1 != nullptr) {
2491 sp<IMeasurementCorrectionsCallback> gnssCorrectionsIfaceCbIface =
2492 new MeasurementCorrectionsCallback();
2493 result = gnssCorrectionsIface_V1_1->setCallback(gnssCorrectionsIfaceCbIface);
2494 checkHidlReturn(result, "IMeasurementCorrections 1.1 setCallback() failed.");
2495 } else if (gnssCorrectionsIface_V1_0 != nullptr) {
2496 sp<IMeasurementCorrectionsCallback> gnssCorrectionsIfaceCbIface =
2497 new MeasurementCorrectionsCallback();
2498 result = gnssCorrectionsIface_V1_0->setCallback(gnssCorrectionsIfaceCbIface);
2499 checkHidlReturn(result, "IMeasurementCorrections 1.0 setCallback() failed.");
2500 } else {
2501 ALOGI("Unable to find IMeasurementCorrections.");
2502 }
2503
2504 return JNI_TRUE;
2505 }
4.HAL
gnssHal->setCallback(gnssCbIface);
393 Return<bool> Gnss::setCallback(const sp<IGnssCallback>& callback) {
394 if (mGnssIface == nullptr) {
395 ALOGE("%s: Gnss interface is unavailable", __func__);
396 return false;
397 }
398
399 if (callback == nullptr) {
400 ALOGE("%s: Null callback ignored", __func__);
401 return false;
402 }
403
404 if (sGnssCbIface != NULL) {
405 ALOGW("%s called more than once. Unexpected unless test.", __func__);
406 sGnssCbIface->unlinkToDeath(mDeathRecipient);
407 }
408
409 sGnssCbIface = callback;
410 callback->linkToDeath(mDeathRecipient, 0 );
411
412
413
414
415 if (sCapabilitiesCached != 0) {
416 setCapabilitiesCb(sCapabilitiesCached);
417 }
418 if (sYearOfHwCached != 0) {
419 LegacyGnssSystemInfo info;
420 info.year_of_hw = sYearOfHwCached;
421 setSystemInfoCb(&info);
422 }
423
424 return (mGnssIface->init(&sGnssCb) == 0);
425 }
这往下就是gps.h内容,需要厂家去实现了
总体来说android的gps流程比蓝牙看起来简单很多,不需要涉及协议相关内容,需要计算的部分由芯片或者厂商提供的驱动提供.
|