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

RealmInstant.now implementation on Android uses API 26 > minSDK 16 #1572

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Support for experimental K2-compilation with `kotlin.experimental.tryK2=true`. (Issue [#1483](https://github.com/realm/realm-kotlin/issues/1483))

### Fixed
* `RealmInstant.now` used an API (`java.time.Clock.systemUTC().instant()`) introduced in API 26, current minSDK is 16. (Issue [#1564](https://github.com/realm/realm-kotlin/issues/1564))
* Fix compiler crash caused by a change in Kotlin 1.9.20 ((toIrConst moved under common IrUtils)[https://github.com/JetBrains/kotlin/commit/ca8db7d0b83f6dfd6afcea7a5fe7556d38f325d8]). (Issue [#1566](https://github.com/realm/realm-kotlin/issues/1566))

### Compatibility
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.realm.kotlin.internal.platform

import android.os.Build
import io.realm.kotlin.internal.RealmInitializer
import io.realm.kotlin.internal.RealmInstantImpl
import io.realm.kotlin.internal.interop.SyncConnectionParams
import io.realm.kotlin.internal.util.Exceptions
import io.realm.kotlin.log.LogLevel
import io.realm.kotlin.log.RealmLogger
import io.realm.kotlin.types.RealmInstant
import java.io.FileNotFoundException
import java.io.InputStream

Expand Down Expand Up @@ -36,3 +39,12 @@ public actual fun assetFileAsStream(assetFilename: String): InputStream = try {
// Returns the default logger for the platform
public actual fun createDefaultSystemLogger(tag: String, logLevel: LogLevel): RealmLogger =
LogCatLogger(tag, logLevel)

public actual fun currentTime(): RealmInstant {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should document that there is some precision loss depending on the SDK version used.

val jtInstant = java.time.Clock.systemUTC().instant()
RealmInstantImpl(jtInstant.epochSecond, jtInstant.nano)
} else {
RealmInstantImpl(System.currentTimeMillis(), 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public expect fun epochInSeconds(): Long

/**
* Returns a RealmInstant representing the time that has passed since the Unix epoch.
* For Android targets < API 26 there's no nano seconds precision.
*/
public expect fun currentTime(): RealmInstant

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.realm.kotlin.internal.platform

import io.realm.kotlin.internal.Constants.FILE_COPY_BUFFER_SIZE
import io.realm.kotlin.internal.RealmInstantImpl
import io.realm.kotlin.internal.util.Exceptions
import io.realm.kotlin.types.RealmInstant
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.security.DigestInputStream
import java.security.MessageDigest
import java.time.Clock.systemUTC
import java.util.concurrent.TimeUnit
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KType
Expand All @@ -24,15 +21,6 @@ public actual fun threadId(): ULong {
public actual fun epochInSeconds(): Long =
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())

/**
* Since internalNow() should only logically return a value after the Unix epoch, it is safe to create a RealmInstant
* without considering having to pass negative nanoseconds.
*/
public actual fun currentTime(): RealmInstant {
val jtInstant = systemUTC().instant()
return RealmInstantImpl(jtInstant.epochSecond, jtInstant.nano)
}

public actual fun fileExists(path: String): Boolean =
File(path).let { it.exists() && it.isFile }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.realm.kotlin.internal.platform

import io.realm.kotlin.Realm
import io.realm.kotlin.internal.RealmInstantImpl
import io.realm.kotlin.internal.interop.SyncConnectionParams
import io.realm.kotlin.internal.util.Exceptions
import io.realm.kotlin.log.LogLevel
import io.realm.kotlin.log.RealmLogger
import io.realm.kotlin.types.RealmInstant
import java.io.InputStream
import java.net.URL
import java.time.Clock

public actual val RUNTIME: SyncConnectionParams.Runtime = SyncConnectionParams.Runtime.JVM
public actual val RUNTIME_VERSION: String = System.getProperty("java.version")
Expand All @@ -27,3 +30,13 @@ public actual fun assetFileAsStream(assetFilename: String): InputStream {

public actual fun createDefaultSystemLogger(tag: String, logLevel: LogLevel): RealmLogger =
StdOutLogger(tag, logLevel)

/**
* Since internalNow() should only logically return a value after the Unix epoch, it is safe to create a RealmInstant
* without considering having to pass negative nanoseconds.
*/
@Suppress("NewApi") // The implementation in SystemUtilsAndroid has a guard to only use systemUTC on API >= 26
public actual fun currentTime(): RealmInstant {
val jtInstant = Clock.systemUTC().instant()
return RealmInstantImpl(jtInstant.epochSecond, jtInstant.nano)
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal class RealmSyncInitializer : Initializer<Context> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /* 21 */) {
val request = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /* 23 */) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M /* 23 */) {
request.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}
RealmLog.info("Register ConnectivityManager network callbacks")
Expand Down