GNSS 時間偵測

在 Android 12 以上版本中,Android 可選擇使用全球導航衛星系統 (GNSS) 向 time_detector 服務建議 Unix 紀元時間。這項功能在 Android 開放原始碼計畫 (AOSP) 中預設為停用。

啟用 GNSS 時間偵測功能後,gnss_time_update_service 會被動監聽 GNSS 來源的位置更新,並將 GNSS 建議提交至 time_detector 服務。time_detector 服務接著會判斷是否要更新系統時鐘,以符合建議。

對耗電量的影響

AOSP gnss_time_update_service 會被動監聽位置更新。也就是說,這項功能絕不會主動開啟 GPS 或消耗額外電量。這也表示,除非系統中的其他應用程式或服務主動要求位置資訊更新,否則 gnss_time_update_service 不會收到位置資訊更新,也不會建議 GNSS 時間。

實作

如要啟用 GNSS 時間偵測功能,裝置製造商必須在系統伺服器中明確啟用 gnss_time_update_service

如要啟用這項功能,請更新 core/res/res/values/config.xml 檔案中的 config_enableGnssTimeUpdateServiceconfig_autoTimeSourcesPriority 值。將 config_enableGnssTimeUpdateService 的值設為 true,並將 gnss 新增至 config_autoTimeSourcesPriority 的項目清單。優先順序清單中 gnss 的位置,決定了系統給予 GNSS 建議的優先順序,相對於其他來源的建議。

以下是 core/res/res/values/config.xml 檔案範例,其中已啟用 GNSS 時間偵測,且 gnss 在優先順序清單中排在 networktelephony 之後,位居第三。

<!-- Specifies priority of automatic time sources. Suggestions from higher entries in the list
         take precedence over lower ones.
         See com.android.server.timedetector.TimeDetectorStrategy for available sources. -->
    <string-array name="config_autoTimeSourcesPriority">
        <item>network</item>
        <item>telephony</item>
        <item>gnss</item>
    </string-array>

    <!-- Enables the GnssTimeUpdate service. This is the global switch for enabling Gnss time based
         suggestions to TimeDetector service. See also config_autoTimeSourcesPriority. -->
    <bool name="config_enableGnssTimeUpdateService">true</bool>

偵錯和測試

如要測試 GNSS 時間偵測功能,可以使用 adb shell cmd location 指令。 使用這些指令新增測試位置資訊供應器,您可以在其中指定位置資訊和相關聯的 GNSS 時間。gnss_time_update_service 會監聽這些位置更新,並定期提供建議。

以下是 adb shell cmd location 指令的範例:

# Enable Master Location Switch in the foreground user (usually user 10 on automotive). If you just flashed, this can be done through setup wizard.
adb shell cmd location set-location-enabled true --user 10

# Add GPS test provider (This usually fails the first time. Throws a SecurityException with "android from <SOME_UID> not allowed to perform MOCK_LOCATION".)
adb shell cmd location providers add-test-provider gps

# Enable mock location permissions for previous UID
adb shell appops set UID_PRINTED_IN_PREVIOUS_ERROR android:mock_location allow

# Add GPS test provider (Should work with no errors.)
adb shell cmd location providers add-test-provider gps

# Enable GPS test provider
adb shell cmd location providers set-test-provider-enabled gps true

# Set location with time (Time can't be lower than the limit set by the lower bound.)
adb shell cmd location providers set-test-provider-location gps --location LATITUDE,LONGITUDE --time TIME