自動偵測時間

自動時間偵測功能會接收來自不同來源的時間建議,選取最佳選項,然後將 Android 中的系統時鐘設為相符的時間。先前的 Android 版本提供兩種設定日期和時間的方式,可為每位使用者手動設定,或透過自動時間偵測功能設定,並透過下列任一選項設定:

  • telephony 會使用網路 ID 和時區 (NITZ) 電話信號。
  • network 會使用網路時間通訊協定 (NTP) 時間伺服器。

每個選項都需要連線至外部網路,但 Android Automotive 不一定提供這類網路。舉例來說,在部分國家/地區,部分車輛可能沒有內建電話功能。因此,當無法連上網路時,系統會提供 全球衛星導航系統 (GNSS) 時間做為系統時間來源。

即將推出的 Android 版本提供兩個額外的選項,可自動偵測及設定時間:

  • gnss 使用全球衛星導航系統 (GNSS)。
  • external 會使用 VHAL 屬性或 System 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 提供新的較低時間限制,可用於驗證時間建議。在推出這項功能之前,自動時間偵測功能不會驗證建議的傳入世界標準時間。使用這項功能時,系統會捨棄低限值前經過的時間。

下限值是根據建構時間戳記衍生的日期決定。這項功能的運作原理是,在建構系統映像檔之前,系統無法產生有效時間。Android 不會強制執行上限。

GNSS 時間建議

gnss 時間來源是 Android 12 的新功能,由 GPS 訊號提供。在 telephonynetwork 無法使用時,這是可靠的時間來源。這個選項已新增至 SystemServer 中的新 GnssTimeUpdateService,可被動監聽位置更新。收到有效位置後,GnssTimeUpdateService 會向 TimeDetectorService 提出建議,後者再決定是否應更新系統時鐘。

根據預設,gnss 時間來源在 AOSP 中未啟用,因此必須由合作夥伴啟用:

<!-- 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_enableGnssTimeUpdateServiceconfig_enableGnssTimeUpdateService 的值必須設為 true
  2. 更新 config_autoTimeSourcesPrioritygnss 必須加入 config_autoTimeSourcesPriority 的項目清單。gnss 在優先清單中的順序,會決定 GNSS 建議相對於其他來源值的優先順序。

對電力的影響

GnssTimeUpdateService 會被動監聽位置更新,也就是說,它不會主動開啟 GPS 耗用額外電力。因此,啟用 GNSS 來源時的耗電量微乎其微。這也表示,除非系統中的其他應用程式或服務主動要求位置更新,否則 GnssTimeUpdateService 不會取得位置更新,也不會建議 GNSS 時間。

測試

相容性測試套件 (CTS)

我們提供 CTS 測試,以便驗證 GNSS 提供的時間是否可用。詳情請參閱 LocationShellCommand.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 日世界標準時間起經過的毫秒數。其值可傳遞至 Android TimeManager,以建議新的系統時間。如需更新這項屬性的 VHAL 實作範例,請參閱下方的參考實作
  • 系統 API:TimeManager 中提供名為 suggestExternalTime() 的新方法,可為系統提供外部時間建議。如果系統已設定為考量外部時間建議 (使用設定檔中的 config_autoTimeSourcesPriority),則系統會在沒有更高優先順序時間建議可用時,使用傳遞至此方法的時間戳記來設定系統時間。

您可以按照下列說明,實作外部時間解決方案:

  1. 更新資源設定檔 (core/res/res/values/config.xml),然後將 external 值新增至 config_autoTimeSourcesPriority
    <string-array name="config_autoTimeSourcesPriority>
            <item>external</item>
            <item>gnss</item>
    </string-array>

    這樣做可指示 Android 在設定系統時鐘時,將外部時間建議設為最高優先順序。車輛上的硬體會將時間戳記建議寫入新的 EPOCH_TIME VHAL 屬性

  2. 供應商提供的應用程式會讀取這項屬性並呼叫 TimeManager.suggestExternal()。接著,Android 可以使用提供的時間戳記做為新的系統時鐘值。