GNSS 時間の検出

Android 12 以降では、必要に応じて全球測位衛星システム(GNSS)を使用して、time_detector サービスに Unix エポック時刻を提案できます。これは、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 を明示的に有効にする必要があります。

この機能を有効にするには、core/res/res/values/config.xml ファイル内の config_enableGnssTimeUpdateServiceconfig_autoTimeSourcesPriority の両方の値を更新する必要があります。config_enableGnssTimeUpdateService の値を true に設定し、gnssconfig_autoTimeSourcesPriority の項目リストに追加します。優先度リスト内の gnss の位置によって、他のソースに対する GNSS の提案の優先度が決まります。

次の core/res/res/values/config.xml ファイルの例では、GNSS 時刻の検出が有効で、gnss は優先度リストの 3 番目(networktelephony の後)にあります。

<!-- 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