### Usage Example Source: https://github.com/instacart/truetime-android/blob/master/README.md A concise example demonstrating TrueTime initialization, sync, and time retrieval. ```kotlin val trueTime = TrueTimeImpl() trueTime.sync() trueTime.now() ``` -------------------------------- ### Accessing TrueTime Source: https://github.com/instacart/truetime-android/wiki/How-to-use-this-library Example of how to get the current time after TrueTime has synced. ```kotlin (application as App).trueTime.now() ``` -------------------------------- ### Application Class Initialization Source: https://github.com/instacart/truetime-android/wiki/How-to-use-this-library Initialize and sync TrueTime in your application class's onCreate method. ```kotlin // App.kt class App : Application() { val trueTime = TrueTimeImpl() override fun onCreate() { super.onCreate() trueTime.sync() } } ``` -------------------------------- ### Gradle Build Configuration Source: https://github.com/instacart/truetime-android/wiki/How-to-use-this-library Add JitPack repository and TrueTime dependency to your application's build.gradle file. ```groovy repositories { maven { url "https://jitpack.io" } } dependencies { // ... implementation 'com.github.instacart:truetime-android:' } ``` -------------------------------- ### TrueTime Syncer Background Service Source: https://github.com/instacart/truetime-android/wiki/Caveats---Gotchas Sample pseudo-code demonstrating how to run the TrueTime syncer in the background using Flowable.interval to re-spawn an Android service at regular intervals. ```kotlin // inside MyApplication class override fun onCreate() { // ... runTrueTimeSyncerInBackground(this) // ... } /** * This should be run only once. * calling this method again will exponentially increase calling rates */ fun runTrueTimeSyncerInBackground(app: ISApplication): Disposable { return Flowable.interval(0L, 5, TimeUnit.MINUTES) .filter { !app.isOffline } .filter { !app.isInBackground } .doOnNext { app.startService(Intent(app, TrueTimeSyncService::class.java)) } .subscribeOn(Schedulers.io()) .subscribe( { Timber.d("TrueTime Syncer called") }, { Timber.w(it, "Error when running TrueTime Syncer") } ) } ``` -------------------------------- ### TrueTimeSyncService Implementation Source: https://github.com/instacart/truetime-android/wiki/Caveats---Gotchas Implementation of the Android Service that initiates the TrueTime NTP initialization. It includes logic to prevent multiple jobs from running concurrently. ```kotlin /** * Background Android Service object that sends the request to initialize TrueTime * * Calling the start service multiple times should not have any negative effect * as we filter these out if a current job is already running * */ class ISTrueTimeSyncService : Service() { private var jobCurrentlyRunning = false // ... override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { // ^ Warning: By default executed on main thread if (jobCurrentlyRunning) return Service.START_STICKY Single .fromCallable { jobCurrentlyRunning = true Timber.d("Running TrueTime init now") } .flatMap { TrueTimeRx.initializeNtp("time.google.com") } .doFinally { jobCurrentlyRunning = false } .subscribeOn(Schedulers.io()) .subscribe( { Timber.v("TrueTime was initialized at ${it!!.contentToString()}") }, { Timber.v("TrueTime init fail ${it}") } ) return Service.START_STICKY } // ... } ``` -------------------------------- ### Rx-ified Library Implementation Source: https://github.com/instacart/truetime-android/wiki/Caveats---Gotchas This dependency declaration shows how to include the Rx-ified version of the TrueTime library, which handles retries and other complexities automatically. ```gradle implementation 'com.github.instacart.truetime-android:library-extension-rx:' ``` -------------------------------- ### Apache License 2.0 Source: https://github.com/instacart/truetime-android/blob/master/README.md The license under which the Truetime Android project is distributed. ```text Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` -------------------------------- ### BootCompletedBroadcastReceiver Source: https://github.com/instacart/truetime-android/wiki/How-TrueTime-works The BootCompletedBroadcastReceiver class is responsible for invalidating cached TrueTime information upon device boot. ```java com.instacart.library.truetime.BootCompletedBroadcastReceiver ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.