自動時間檢測

自動時間檢測從各種來源接收時間建議,選擇最佳選項,然後設置 Android 中的系統時鐘以匹配。以前的 Android 版本提供了兩種設置日期和時間的方法——根據用戶手動設置或通過自動時間檢測,由以下兩個選項之一設置:

  • 電話使用網絡身份和時區 (NITZ) 電話信號。
  • 網絡使用網絡時間協議 (NTP) 時間服務器。

每個選項都需要連接到外部網絡,而這在 Android Automotive 中並不總是可用。例如,在某些國家/地區,某些汽車可能沒有內置電話。因此,現在還提供全球衛星導航系統 (GNSS)時間作為系統時間來源,供您在網絡連接不可用時使用。

這個即將發布的 Android 版本提供了另外兩個選項來自動檢測和設置時間:

  • gnss使用全球衛星導航系統 (GNSS)。
  • external使用 VHAL 屬性或系統 API。

啟用自動時間檢測

要啟用自動時間檢測,請務必選擇設置>日期和時間>自動日期和時間

圖 1.選擇自動日期和時間

配置時間源

要指定在自動時間檢測中包含哪些時間源,以及應考慮這些時間源的優先級,您必須修改設備的資源配置文件core/res/res/values/config.xml

<!-- 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>telephony</item>
    <item>network</item>
</string-array>

在此示例中,在自動時間檢測中考慮telephonynetwork ,並且telephony時間建議優先於network時間建議。

一般來說,如果建議無效或建議太舊,則忽略來自更高優先級來源的建議。此外,如果最高優先級的有效建議與設備的當前系統時鐘時間匹配在幾秒內(默認值為兩 (2) 秒),則不會更改時間。

下限時間

Android 12 在驗證時間建議時提供了一個新的較低時間限制。在此功能之前,自動時間檢測不會驗證建議的傳入 UTC 時間。使用此功能,在下限之前經過的時間將被丟棄。

下限值由從構建時間戳派生的日期確定。這樣做的原理是在構建系統映像之前不能出現有效時間。 Android 不強制設置上限。

GNSS 時間建議

gnss時間源是 Android 12 的新功能,由 GPS 信號提供。當電話和網絡不可用時,這被認為是可靠的時間來源。此選項被添加到 SystemServer 中的新GnssTimeUpdateService中,它被動地偵聽位置更新。當接收到有效位置時, GnssTimeUpdateServiceTimeDetectorService提出建議,然後確定是否應更新系統時鐘。

默認情況下,AOSP 中未啟用gnss時間源,因此必須由合作夥伴啟用:

<!-- 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>telephony</item>
    <item>network</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>

要啟用此功能:

  1. 更新 config_enableGnssTimeUpdateService。 config_enableGnssTimeUpdateService的值必須設置為true
  2. 更新config_autoTimeSourcesPrioritygnss必須添加到config_autoTimeSourcesPriority的項目列表中。 gnss在優先級列表中的位置決定了 GNSS 建議相對於其他來源的值的優先級。

對電源的影響

GnssTimeUpdateService被動地監聽位置更新,這意味著它永遠不會主動打開 GPS 來消耗額外的電量。因此,啟用 GNSS 源時消耗的功率可以忽略不計。這也意味著除非系統中的另一個應用程序或服務主動請求位置更新, GnssTimeUpdateService不會獲得位置更新並建議 GNSS 時間。

測試

兼容性測試套件 (CTS)

提供 CTS 測試以驗證 GNSS 提供的時間是否可用。有關詳細信息,請參閱LocationManagerCoarseTest.java

單元測試

請參閱以下文件中的基本單元測試:

atest frameworks/base/services/tests/servicestests/src/com/android/server/timedetector/GnssTimeUpdateServiceTest.java

手動測試

為了測試此功能,已將新命令添加到LocationShellCommand.java中。使用這些命令添加測試提供程序,您可以使用它們指定位置和關聯的 GNSS 時間。 GnssTimeUpdateService監聽這些位置更新,並定期提出建議。

注意:這些命令的界面可能會因版本而異。

# 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 and will throw 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 earlier than the limit set by the lower bound.)
adb shell cmd location providers set-test-provider-location gps --location <LATITUDE>,<LONGITUDE> --time <TIME>

外部時間建議

外部時間建議是向 Android 提供自動時間建議的另一種方式。這個新選項使您能夠向 Android 提供完全定制的時間建議,這些建議可以源自各種 ECU,而這些 ECU 又可以使用實時時鐘、GNSS、NITZ 或任何其他時間源的組合。

Android 12 現在可以將以下建議視為external時間建議:

  • VHAL 屬性。提供了一個名為EPOCH_TIME的新VHAL 屬性。此屬性表示自 1970 年 1 月 1 日 UTC 以來經過的毫秒數。它的值可以傳遞給 Android TimeManager以建議新的系統時間。下面的參考實現中提供了更新此屬性的示例 VHAL 實現。
  • 系統 API。 TimeManager 中現在提供了一個名為suggestExternalTime()的新方法,可以為系統提供外部時間建議。如果系統被配置為考慮外部時間建議(在配置文件中使用config_autoTimeSourcesPriority ),如果沒有更高優先級的時間建議可用,則使用傳遞給此方法的時間戳來設置系統時間。

您可以實現如下所述的外部時間解決方案:

  1. 更新資源配置文件( core/res/res/values/config.xml ),然後將值添加到config_autoTimeSourcesPriority external
    <string-array name="config_autoTimeSourcesPriority>
            <item>external</item>
            <item>gnss</item>
    </string-array>
    

    這樣做會指示 Android 在設置系統時鐘時為外部時間建議提供最高優先級。車輛上的硬件將時間戳建議寫入新的EPOCH_TIME VHAL 屬性

  2. 供應商提供的應用程序讀取此屬性並調用TimeManager.suggestExternal() 。然後,Android 可以使用提供的時間戳作為新的系統時鐘值。