Deteksi waktu GNSS

Di Android 12 dan yang lebih tinggi, Android dapat secara opsional menggunakan Global Navigation Satellite System (GNSS) untuk menyarankan waktu epoch Unix ke layanan time_detector. Fitur ini tidak diaktifkan secara default di AOSP.

Jika deteksi waktu GNSS diaktifkan, gnss_time_update_service akan secara pasif memproses update lokasi dari sumber GNSS dan mengirimkan saran GNSS ke layanan time_detector. Layanan time_detector kemudian menentukan apakah akan mengupdate jam sistem agar sesuai dengan saran.

Dampak pada penggunaan daya

AOSP gnss_time_update_service secara pasif memproses pembaruan lokasi. Artinya, aplikasi tidak pernah mengaktifkan GPS secara aktif atau menggunakan daya tambahan. Hal ini juga berarti bahwa kecuali jika aplikasi atau layanan lain di sistem secara aktif meminta update lokasi, gnss_time_update_service tidak akan mendapatkan update lokasi dan menyarankan waktu GNSS.

Implementasi

Untuk mengaktifkan deteksi waktu GNSS, produsen perangkat harus mengaktifkan gnss_time_update_service secara eksplisit di server sistem.

Nilai config_enableGnssTimeUpdateService dan config_autoTimeSourcesPriority dalam file core/res/res/values/config.xml harus diperbarui untuk mengaktifkan fitur ini. Tetapkan nilai untuk config_enableGnssTimeUpdateService ke true dan tambahkan gnss ke daftar item untuk config_autoTimeSourcesPriority. Posisi gnss dalam daftar prioritas menentukan prioritas yang diberikan pada saran GNSS sehubungan dengan saran dari sumber lain.

Berikut adalah contoh file core/res/res/values/config.xml dengan deteksi waktu GNSS diaktifkan dan gnss berada di urutan ketiga dalam daftar prioritas setelah network dan 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>

Proses debug dan pengujian

Untuk menguji deteksi waktu GNSS, Anda dapat menggunakan perintah adb shell cmd location. Gunakan perintah ini untuk menambahkan penyedia lokasi pengujian tempat Anda dapat menentukan lokasi dan waktu GNSS terkait. gnss_time_update_service mendengarkan pembaruan lokasi ini, dan memberikan saran secara berkala.

Berikut menunjukkan contoh perintah 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. Throws 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