自動時間偵測從各種來源接收時間建議,選擇最佳選項,然後設定 Android 中的系統時鐘以匹配。先前的 Android 版本提供了兩種設定日期和時間的方法,可以為每個用戶手動設置,也可以透過自動時間檢測設置,並透過以下選項之一進行設定:
-
telephony
使用網路身分和時區 (NITZ) 電話訊號。 -
network
使用網路時間協定 (NTP) 時間伺服器。
每個選項都需要連接到外部網絡,這在 Android Automotive 中並不總是可用。例如,在某些國家/地區,某些汽車可能沒有內建電話。因此,我們提供全球衛星導航系統 (GNSS)時間作為系統時間來源,供您在網路連線不可用時使用。
即將推出的 Android 版本提供了另外兩個選項來自動偵測和設定時間:
啟用自動時間偵測
若要啟用自動時間偵測,請務必選擇設定>日期和時間>自動日期和時間:
圖 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>
在此範例中,在自動時間檢測中考慮telephony
和network
,並且telephony
時間建議優先於network
時間建議。
一般來說,如果建議無效或太舊,則來自較高優先順序來源的建議將被忽略。此外,如果最高優先順序的有效建議與裝置的當前系統時鐘時間相符在幾秒鐘之內(預設值為兩 (2) 秒),則時間不會變更。
較低的時間限制
Android 12 提供了驗證時間建議時所使用的新的下限時間。在此功能之前,自動時間偵測不會驗證建議的傳入 UTC 時間。使用此功能,下限之前經過的時間將被丟棄。
下限值根據建置時間戳派生的日期決定。其工作原理是在建置系統映像之前不能出現有效時間。 Android 不強制規定上限。
GNSS 時間建議
gnss
時間源是 Android 12 中的新增功能,由 GPS 訊號提供。當telephony
和network
不可用時,這是可靠的時間來源。此選項被加入到 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>
若要啟用此功能:
- 更新
config_enableGnssTimeUpdateService
。config_enableGnssTimeUpdateService
的值必須設為true
。 - 更新
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 日 (UTC) 以來經過的毫秒數。它的值可以傳遞給 AndroidTimeManager
以建議新的系統時間。在下面的參考實作中提供了更新此屬性的範例 VHAL 實作。 - 系統 API。 TimeManager 中提供了一個名為
suggestExternalTime()
的新方法,可為系統提供外部時間建議。如果系統配置為考慮外部時間建議(在設定檔中使用config_autoTimeSourcesPriority
),且沒有較高優先順序的時間建議可用,則傳遞給此方法的時間戳記將用於設定係統時間。
您可以實施外部時間解決方案,如下所述:
- 更新資源設定檔 (
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 屬性。 - 供應商提供的應用程式會讀取此屬性並呼叫
TimeManager.suggestExternal()
。然後,Android 可以使用提供的時間戳記作為新的系統時鐘值。