การตรวจจับเวลา GNSS

จาก Android 12 นั้น Android สามารถเลือกใช้ระบบดาวเทียมนำทางทั่วโลก (GNSS) เพื่อแนะนำเวลายุค Unix ไปยังบริการ time_detector สิ่งนี้ไม่ได้เปิดใช้งานตามค่าเริ่มต้นใน 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 ในเซิร์ฟเวอร์ระบบอย่างชัดเจน

ทั้งค่า config_enableGnssTimeUpdateService และ config_autoTimeSourcesPriority ในไฟล์ core/res/res/values/config.xml ต้องได้รับการอัปเดตเพื่อเปิดใช้งานคุณลักษณะนี้ ตั้งค่าสำหรับ config_enableGnssTimeUpdateService เป็น true และเพิ่ม gnss ในรายการรายการสำหรับ config_autoTimeSourcesPriority ตำแหน่งของ gnss ในรายการลำดับความสำคัญจะกำหนดลำดับความสำคัญที่กำหนดให้กับข้อเสนอแนะ GNSS เทียบกับลำดับความสำคัญจากแหล่งอื่น

ต่อไปนี้คือตัวอย่างไฟล์ core/res/res/values/config.xml ที่เปิดใช้งานการตรวจจับเวลา GNSS และ gnss อยู่ในอันดับที่สามในรายการลำดับความสำคัญรองจาก network และ telephony

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