自動偵測時間

「自動偵測時間」會接收來自各種來源的時間建議,選取最佳選項,然後設定 Android 系統時鐘以符合該時間。先前的 Android 版本提供兩種設定日期和時間的方式:手動為每位使用者設定,或自動偵測時間,並透過下列其中一種方式設定:

  • telephony 使用網路身分識別和時區 (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 時間來源是 Android 12 的新功能,由 GPS 訊號提供。如果無法使用 telephonynetwork,這就是可靠的時間來源。這個選項會新增至 SystemServer 中的新 GnssTimeUpdateService,被動監聽位置資訊更新。收到有效位置後,GnssTimeUpdateService 會向 TimeDetectorService 提出建議,然後由 TimeDetectorService 判斷是否應更新系統時鐘。

根據預設,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_autoTimeSourcesPriority」。gnss 必須新增至 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 隨後可將提供的值做為新的系統時鐘值。