IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android11 GPS 流程代码走读 -> 正文阅读

[移动开发]Android11 GPS 流程代码走读

Android11 GPS 流程代码走读


程序员是一块砖,哪里需要哪里搬,蓝牙看完看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              // Location toggling disabled by user restriction. Read the current location mode to
112              // update the location master switch.
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      /*
2399       * This must be set before calling into the HAL library.
2400       */
2401      if (!mCallbacksObj)
2402          mCallbacksObj = env->NewGlobalRef(obj);
2403  
2404      /*
2405       * Fail if the main interface fails to initialize
2406       */
2407      if (gnssHal == nullptr) {
2408          ALOGE("Unable to initialize GNSS HAL.");
2409          return JNI_FALSE;
2410      }
2411  
2412      Return<bool> result = false;
2413  
2414      // Set top level IGnss.hal callback.
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      // Set IGnssXtra.hal callback.
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      // Set IAGnss.hal callback.
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      // Set IGnssGeofencing.hal callback.
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      // Set IGnssNi.hal callback.
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      // Set IAGnssRil.hal callback.
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      // Set IGnssVisibilityControl.hal callback.
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      // Set IMeasurementCorrections.hal callback.
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 /*cookie*/);
411  
412      // If this was received in the past, send it up again to refresh caller.
413      // mGnssIface will override after init() is called below, if needed
414      // (though it's unlikely the gps.h capabilities or system info will change.)
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流程比蓝牙看起来简单很多,不需要涉及协议相关内容,需要计算的部分由芯片或者厂商提供的驱动提供.

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:53:18  更:2022-10-08 20:55:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 2:54:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码