Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrueTime 4.0 : Kotlin & coroutines #129

Merged
merged 86 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 72 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
f3a0f45
Convert sample to kt + androidx
Jawnnypoo Apr 12, 2019
6b77436
Convert rx class to kt
Jawnnypoo Apr 12, 2019
e28e515
Convert main library module to kt
Jawnnypoo Apr 12, 2019
9837d29
Binary compatibility cleanup
Jawnnypoo Apr 12, 2019
2e5b035
Java compat
Jawnnypoo Apr 12, 2019
b674f8e
Remove Collections references
Jawnnypoo Apr 12, 2019
cab1e02
Neat app icon
Jawnnypoo Apr 12, 2019
eee0134
Add way to view base implementation from Rx implementation
Jawnnypoo Apr 12, 2019
a560af3
chore: upgrade kotlin + gradle versions
kaushikgopal Nov 25, 2020
12c0457
dep: add coroutines
kaushikgopal Nov 25, 2020
59d2d9d
skip build config generation
kaushikgopal Nov 25, 2020
43b3525
fix: update non Rx activity title
kaushikgopal Nov 25, 2020
5a43240
add TrueTime2 interface
kaushikgopal Nov 25, 2020
843f9cc
simplify Rx in TrueTimeRx
kaushikgopal Nov 25, 2020
d330482
wip: write out algorithm for coroutine implementation
kaushikgopal Nov 25, 2020
28d0f47
wip: ntp algo with todo methods
kaushikgopal Nov 25, 2020
7e9683c
reogranize sntip client package
kaushikgopal Nov 25, 2020
9cf3f45
inject sntp client into TrueTime
kaushikgopal Nov 25, 2020
90ad99a
implementation logic
kaushikgopal Nov 25, 2020
fcd3857
feat: flush out implementation for TrueTimeImpl
kaushikgopal Nov 26, 2020
94b15cc
test figures for TrueTimeImpl
kaushikgopal Nov 26, 2020
de1045d
hack to test coroutines TT
kaushikgopal Nov 26, 2020
26a3260
app example : initialize TrueTime2
kaushikgopal Nov 26, 2020
2b8cd73
fix: revert TrueTime.kt -> .java
kaushikgopal Nov 26, 2020
dda83ef
ref: reorganize packages
kaushikgopal Nov 26, 2020
be815e1
--wip: sntp duplicate
kaushikgopal Nov 26, 2020
38449d4
upgrade kotlin + coroutines gradle
kaushikgopal Dec 28, 2020
43fab29
ref: move to legacy package
kaushikgopal Dec 29, 2020
6d19a1b
feat: Sntp add devicetime api
kaushikgopal Dec 29, 2020
6036da1
feat: TrueTime2 add more usable apis
kaushikgopal Dec 29, 2020
13342c3
feat: cleanup sample app
kaushikgopal Dec 29, 2020
c60ccaa
feat: add repeat count parameter
kaushikgopal Dec 29, 2020
ef8c0fe
fix: add equals + hashcode for TruetimeResult
kaushikgopal Dec 29, 2020
9243133
store and reuse ntpResult
kaushikgopal Dec 29, 2020
053875c
ref: extract TimeKeeper logic
kaushikgopal Dec 29, 2020
7e32a99
fix: cleanup SntpImpl
kaushikgopal Dec 29, 2020
0c028a6
feat: add logger interface
kaushikgopal Dec 29, 2020
dd37470
fix: logging levels
kaushikgopal Dec 30, 2020
61a307a
ref: remove TrueTime dependencies from Sntp
kaushikgopal Dec 30, 2020
e78471e
feat: add sync through coroutines
kaushikgopal Dec 31, 2020
0a3c94c
ref: clean up api given sync present
kaushikgopal Dec 31, 2020
f1c9150
fix: truetime failure shouldn't crash
kaushikgopal Dec 31, 2020
c78f9eb
wip
kaushikgopal Nov 1, 2021
b18f9ea
package rename
kaushikgopal Aug 14, 2022
742506c
api changes
kaushikgopal Aug 14, 2022
a33593f
wip: upgrade gradle plugin
kaushikgopal Aug 14, 2022
bedee05
refactor(sntp): move from logger to event listener
kaushikgopal Aug 14, 2022
567af93
SntpImpl: move from logger → EventListener
kaushikgopal Aug 23, 2022
04dcfa2
move from logging → event listener
kaushikgopal Aug 23, 2022
2f0c346
ref: move TrueTimeParameters from data class → Builder pattern
kaushikgopal Aug 24, 2022
e5517b6
ref: remove initialize method; sync is the way to go
kaushikgopal Aug 24, 2022
a515b93
ref: event listener + sample app log listener
kaushikgopal Aug 24, 2022
051b53e
fix app based on changes
kaushikgopal Aug 24, 2022
7ec3ade
app tweaks
kaushikgopal Aug 24, 2022
0900ee6
update README
kaushikgopal Aug 24, 2022
6cd1516
update README
kaushikgopal Aug 24, 2022
03e24b5
update REAME
kaushikgopal Aug 24, 2022
b0f16e6
update REAME
kaushikgopal Aug 24, 2022
44b5772
update REAME
kaushikgopal Aug 24, 2022
fe1be4a
cleanup
kaushikgopal Aug 24, 2022
51767b9
prepare for release
kaushikgopal Aug 24, 2022
6b452d3
update jitpack instructions
kaushikgopal Aug 24, 2022
80f0a4d
fix: compileSdkVersion to 33
kaushikgopal Aug 24, 2022
dd1207a
fix: compiler warning for resolveNtpHostToIPs
kaushikgopal Aug 24, 2022
bd113cd
fix: sample app using actual truetime
kaushikgopal Aug 24, 2022
abde7e2
ref: move from String → InetAddress object
kaushikgopal Aug 24, 2022
8a0a04b
ref: change SNTP call to address vs ntphost
kaushikgopal Aug 25, 2022
8c7dcf5
feat: keep device on when using TrueTime app
kaushikgopal Aug 25, 2022
3a07875
feat: add feature to filter ipv6 addresses
kaushikgopal Aug 25, 2022
0d13df9
feat: add strict mode flag
kaushikgopal Aug 25, 2022
369e428
fix: sample app api
kaushikgopal Aug 25, 2022
5b7d498
ref: improve TrueTime Parameters naming
kaushikgopal Aug 25, 2022
e1145a6
update gitignore
kaushikgopal Feb 25, 2023
b08dc3c
rename serverResponseDelayMax → InMillis
kaushikgopal Feb 25, 2023
296ef71
improve docs
kaushikgopal Feb 25, 2023
f9174e7
use lifecyclescope in sample app
kaushikgopal Feb 25, 2023
aaadff8
fix glaring documentation bug on strictNtpMode
kaushikgopal Feb 25, 2023
72e790d
dispose activity syncer
kaushikgopal Feb 25, 2023
fe2f761
upgrade gradle
kaushikgopal Feb 25, 2023
4b115c8
reorder todos based on priority
kaushikgopal Feb 25, 2023
1b44fed
upgrade kotlin 1.8.0 coroutines 1.6.4
kaushikgopal Feb 25, 2023
3e581be
increase server response delay
kaushikgopal Feb 25, 2023
024cac8
switching to time.apple for example
kaushikgopal Feb 25, 2023
d7a1490
truetime will one day be pure kotlin, so this is pointless
kaushikgopal Feb 25, 2023
c1a5d38
agp 8 warning
kaushikgopal Feb 25, 2023
464d93d
target sdk 33 + move sample app out of instacart.library package
kaushikgopal Feb 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,68 @@

![TrueTime](truetime.png "TrueTime for Android")

*⚠️ See work in progress section below. TrueTime is undergoing some changes*

----------------------------------------

*Make sure to check out our counterpart too: [TrueTime](https://github.com/instacart/TrueTime.swift), an NTP library for Swift.*

NTP client for Android. Calculate the date and time "now" impervious to manual changes to device clock time.

In certain applications it becomes important to get the real or "true" date and time. On most devices, if the clock has been changed manually, then a `new Date()` instance gives you a time impacted by local settings.
In certain applications it becomes important to get the real or "true" date and time. On most devices, if the clock has been changed manually, then a `Date()` instance gives you a time impacted by local settings.

Users may do this for a variety of reasons, like being in different timezones, trying to be punctual by setting their clocks 5 – 10 minutes early, etc. Your application or service may want a date that is unaffected by these changes and reliable as a source of truth. TrueTime gives you that.

You can read more about the use case in our [blog post](https://tech.instacart.com/truetime/).

In a [recent conference talk](https://vimeo.com/190922794), we explained how the full NTP implementation works with Rx. Check the [video](https://vimeo.com/190922794) and [slides](https://speakerdeck.com/kaushikgopal/learning-rx-by-example-2?slide=31) out for implementation details.
In a [conference talk](https://vimeo.com/190922794), we explained how the full NTP implementation works (with Rx). Check the [video](https://vimeo.com/190922794#t=1466s) and [slides](https://speakerdeck.com/kaushikgopal/learning-rx-by-example-2?slide=31) out for implementation details.


# Work in Progress 4.0

With the move to Kotlin & Coroutines TrueTime 4 was a [major overhaul](https://github.com/instacart/truetime-android/pull/129). We still haven't ported some of the additional bells & whistles. This section keeps track of those features (that will come in the near future). TrueTime is completely functional without these additional features, so feel free to start using it.

- [ ] Fix Jitpack import

I may have busted the jitpack import. Got to fix this first.

- [ ] Introduce a Cache provider

* Add an `interface CacheProvider` so folks can inject in their preferred caching mechanisms
* Provide a default cache implementation (probably using the non-android version of [DataStore](https://developer.android.com/topic/libraries/architecture/datastore#kts))
* ? Provide example of using this with a Database like Realm

- [ ] BootCompletedBroadcastReceiver sample
kaushikgopal marked this conversation as resolved.
Show resolved Hide resolved

Also, once we have this information it's valid until the next time you boot your device. This means if you enable the disk caching feature, after a single successful NTP request you can use the information on disk directly without ever making another network request. This applies even across application kills which can happen frequently if your users have a memory starved device.
Everytime a device is rebooted, the Truetime info is invalid. Previous libraries included an actual `BroadcastReceiver` but this is better handled by the application than the library. For safe measure, I'll include an example of how this can be done in case folks are curious.

- [ ] Utilize all ntp pool addresses from `TrueTimeParameters.ntpHostPool`

We currently only take the first ntp host pool address from the supplied parameters. In the future, it would be nice to provide multiple ntp "pool" addresses like `time.google.com`, `time.apple.com` and utilize all of those to get the "best" value.

- [ ] Algorithmic improvements

There are some exciting improvements that we have planned and use internally. Will have to upstream these changes (with a cleaner api + implementation)

# Installation

[See the wiki instructions](https://github.com/instacart/truetime-android/wiki/How-to-use-this-library).
*⚠️ The Wiki has not been updated to the latest version of TrueTime*

[See the wiki for full instructions](https://github.com/instacart/truetime-android/wiki/How-to-use-this-library).

## Usage

```kt
val trueTime = TrueTimeImpl()
trueTime.sync()
trueTime.now()
```

💥

# Wiki has a lot of useful information

*⚠️ The Wiki has not been updated to the latest version of TrueTime*
Take a look at [the wiki sidebar](https://github.com/instacart/truetime-android/wiki) which should have a lot of useful information.

# License
Expand Down
37 changes: 24 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion rootProject.ext.compileSdkVersion

kaushikgopal marked this conversation as resolved.
Show resolved Hide resolved
defaultConfig {
applicationId "com.instacart.library.truetime"
applicationId "com.instacart.truetime"
minSdkVersion 14
targetSdkVersion rootProject.ext.targetSdkVersion

versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

buildFeatures {
viewBinding true
}
}

repositories {
maven {
url "https://jitpack.io"
}
mavenCentral()
}

ext {
butterknifeVersion = '8.8.1'
}
dependencies {
implementation project(path: ':library')
implementation project(path: ':library-extension-rx')
// compile 'com.github.instacart.truetime-android:library-extension-rx:master-SNAPSHOT'

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "com.jakewharton:butterknife:$butterknifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
// implementation project(path: ':library-extension-rx')
implementation 'com.github.instacart.truetime-android:library-extension-rx:3.5'

implementation "org.jetbrains.kotlin:kotlin-stdlib"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed, it gets added by default when you apply the kotlin plugin

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 AS is complaining when i remove this. I'm guessing i've stripped/messed with the dependencies during the kotlin migration. i'm going to leave this one as is. once we have it merged, happy to try again and clean this up.

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
}
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
android:name=".App"
android:theme="@style/AppTheme">

<activity android:name=".Sample2Activity">
<activity android:name=".SampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
</manifest>
Binary file added app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 0 additions & 81 deletions app/src/main/java/com/instacart/library/sample/App.java

This file was deleted.

19 changes: 19 additions & 0 deletions app/src/main/java/com/instacart/library/sample/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.instacart.library.sample

import android.app.Application
import com.instacart.truetime.time.TrueTime
import com.instacart.truetime.time.TrueTimeImpl
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

@Suppress("unused")
class App : Application() {

// val trueTime = TrueTimeImpl()

override fun onCreate() {
super.onCreate()
// trueTime.sync()
}

}

This file was deleted.

61 changes: 0 additions & 61 deletions app/src/main/java/com/instacart/library/sample/SampleActivity.java

This file was deleted.

Loading