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

Fix doFirstTimeUse on Android so that it completes when all underlying operations are completed #494 #515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,10 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}

override fun doFirstTimeUse(identifier: String, ftuConfig: PolarFirstTimeUseConfig): Completable {
return Completable.create { emitter ->
return Completable.defer {
try {
BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): started")

val session = sessionPsFtpClientReady(identifier)
val client = session.fetchClient(BlePsFtpUtils.RFC77_PFTP_SERVICE) as BlePsFtpClient?
?: throw PolarServiceNotAvailable()
Expand All @@ -1537,7 +1539,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}

val ftuInputStream = ByteArrayInputStream(ftuData)
val disposable = client.write(ftuBuilder.build().toByteArray(), ftuInputStream)
return@defer client.write(ftuBuilder.build().toByteArray(), ftuInputStream)
.concatWith(
Completable.defer {
try {
Expand All @@ -1554,6 +1556,8 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
baos.toByteArray()
}

BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): write user identifier")

val userIdInputStream = ByteArrayInputStream(userIdData)
client.write(userIdBuilder.build().toByteArray(), userIdInputStream)
.ignoreElements()
Expand All @@ -1568,26 +1572,27 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
throw IllegalArgumentException("Invalid deviceTime format: ${ftuConfig.deviceTime}", e)
}
}
BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): set local time")
setLocalTime(identifier, calendar)
}
)
} catch (error: Throwable) {
BleLogger.e(TAG, "writeUserIdentifier() error: $error")
emitter.onError(error)
Completable.complete()
BleLogger.e(TAG, "doFirstTimeUse(identifier: $identifier): write user identifier error: $error")
Completable.error(error)
}
}
)
.ignoreElements()
.doOnComplete {
BleLogger.d(TAG, "doFirstTimeUse(identifier: $identifier): completed")
sendTerminateAndStopSyncNotifications(client)
}
.subscribe(
{ emitter.onComplete() },
{ error -> emitter.onError(error) }
)
.doOnError { error ->
BleLogger.e(TAG, "doFirstTimeUse(identifier: $identifier): error $error")
}
} catch (error: Throwable) {
BleLogger.e(TAG, "doConfig() error: $error")
emitter.onError(error)
BleLogger.e(TAG, "doFirstTimeUse(identifier: $identifier): error $error")
return@defer Completable.error(error)
}
}
}
Expand Down