From Android 12, Android can optionally use a Global Navigation Satellite System (GNSS) to suggest Unix epoch times to the time_detector service. This isn't enabled by default in AOSP.
When GNSS time detection is enabled, the gnss_time_update_service
passively
listens to location updates from GNSS sources and submits GNSS suggestions to
the time_detector
service. The time_detector
service then determines whether
to update the system clock to match the suggestion.
Impact on power usage
The AOSP gnss_time_update_service
passively listens to location updates. This
means that it never actively turns on the GPS or consumes additional power. This
also means that unless another app or service in the system is actively
requesting location updates, the gnss_time_update_service
won't get a location
update and suggest a GNSS time.
Implementation
To enable GNSS time detection, device manufacturers must explicitly enable the
gnss_time_update_service
in the system server.
Both the config_enableGnssTimeUpdateService
and
config_autoTimeSourcesPriority
values in the core/res/res/values/config.xml
file must be updated to enable this feature. Set the value for
config_enableGnssTimeUpdateService
to true
and add gnss
to the item list
for config_autoTimeSourcesPriority
. The position of gnss
in the priority
list determines the priority given to GNSS suggestions with respect to those
from other sources.
The following is an example core/res/res/values/config.xml
file where GNSS
time detection is enabled and gnss
is third in the priority list after
network
and 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>
Debugging and testing
To test GNSS time detection, you can use adb shell cmd location
commands.
Use these commands to add test location providers where you can specify a
location and the associated GNSS time. The gnss_time_update_service
listens to
these location updates, and makes suggestions periodically.
The following shows examples of adb shell cmd location
commands:
# 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 UIDadb 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 provideradb 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