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

Get bundle info #64

Merged
merged 4 commits into from
Jun 12, 2024
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
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Savva Mikhalevski
Copyright (c) 2024 Savva Mikhalevski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
31 changes: 13 additions & 18 deletions android/example/src/main/java/com/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ import org.racehorse.ProxyPathHandler
import org.racehorse.StaticPathHandler
import org.racehorse.evergreen.BundleReadyEvent
import org.racehorse.evergreen.EvergreenPlugin
import org.racehorse.evergreen.StartEvent
import org.racehorse.evergreen.UpdateMode
import org.racehorse.webview.RacehorseDownloadListener
import org.racehorse.webview.RacehorseWebChromeClient
import org.racehorse.webview.RacehorseWebViewClient
import java.io.File
import java.net.URL
import java.util.Date

@SuppressLint("SetJavaScriptEnabled")
class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -100,7 +102,10 @@ class MainActivity : AppCompatActivity() {
eventBus.register(PermissionsPlugin(this))
eventBus.register(NotificationsPlugin(this))
eventBus.register(GoogleSignInPlugin(this))
eventBus.register(FacebookLoginPlugin(this))
eventBus.register(FacebookLoginPlugin(this).also {
@Suppress("DEPRECATION")
FacebookSdk.sdkInitialize(this)
})
eventBus.register(FacebookSharePlugin(this))
eventBus.register(BiometricPlugin(this))
eventBus.register(BiometricEncryptedStoragePlugin(this, File(filesDir, "biometric_storage")))
Expand All @@ -112,13 +117,8 @@ class MainActivity : AppCompatActivity() {
baseLocalUrl = "https://example.com/fs"
)
)

// From the example app
eventBus.register(ToastPlugin(this))

@Suppress("DEPRECATION")
FacebookSdk.sdkInitialize(this)

// 🟡 Run `npm run watch` in `<racehorse>/web/example` to build the web app and start the server.

if (BuildConfig.DEBUG) {
Expand All @@ -145,19 +145,14 @@ class MainActivity : AppCompatActivity() {
// attribute to `AndroidManifest.xml/manifest/application`. `BundleReadyEvent` is emitted after bundle is
// successfully downloaded, see `onBundleReady` below.

val evergreenPlugin = EvergreenPlugin(File(filesDir, "app"))

eventBus.register(EvergreenPlugin(File(filesDir, "app")))
eventBus.register(this)
eventBus.register(evergreenPlugin)

// Download the bundle in the background thread.
Thread {
// The update bundle is downloaded if there's no bundle available, or if the provided version differs
// from the version of previously downloaded bundle.
evergreenPlugin.start("0.0.0", UpdateMode.MANDATORY) {
URL("http://10.0.2.2:10001/bundle.zip").openConnection()
}
}.start()

// The update bundle is downloaded if there's no bundle available, or if the available version differs
// from the version of previously downloaded bundle.
eventBus.post(StartEvent(version = "0.0.0+" + Date().time.toString(), UpdateMode.MANDATORY) {
URL("http://10.0.2.2:10001/bundle.zip").openConnection()
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ enum class UpdateMode {
*/
open class Bootstrapper(private val bundlesDir: File) {

val masterVersion get() = masterVersionFile.takeIf(File::exists)?.readText()
val updateVersion get() = updateVersionFile.takeIf(File::exists)?.readText()
val masterVersion get() = runCatching(masterVersionFile::readText).getOrNull()
val updateVersion get() = runCatching(updateVersionFile::readText).getOrNull()

val isMasterReady get() = masterDir.exists()
val isUpdateReady get() = updateDir.exists() && updateDownload == null

private var masterDir = File(bundlesDir, "master")
private var masterVersionFile = File(bundlesDir, "master.version")
val masterDir = File(bundlesDir, "master")
val updateDir = File(bundlesDir, "update")

private var updateDir = File(bundlesDir, "update")
private var updateVersionFile = File(bundlesDir, "update.version")
private val masterVersionFile = File(bundlesDir, "master.version")
private val updateVersionFile = File(bundlesDir, "update.version")

private var updateDownload: BundleDownload? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.racehorse.evergreen

import android.net.Uri
import androidx.core.net.toUri
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
Expand All @@ -8,6 +10,7 @@ import org.racehorse.RequestEvent
import org.racehorse.ResponseEvent
import java.io.File
import java.io.Serializable
import java.net.URLConnection

/**
* The status of the update bundle.
Expand All @@ -16,6 +19,7 @@ import java.io.Serializable
* @param isReady `true` if the update is fully downloaded and ready to be applied, or `false` if update is being
* downloaded.
*/
@Deprecated("Use GetBundleInfoEvent")
class UpdateStatus(val version: String, val isReady: Boolean) : Serializable

/**
Expand Down Expand Up @@ -51,6 +55,7 @@ class UpdateProgressEvent(val contentLength: Int, val readLength: Long) : Notice
/**
* Get the version of the available master bundle.
*/
@Deprecated("Use GetBundleInfoEvent")
class GetMasterVersionEvent : RequestEvent() {

/**
Expand All @@ -62,6 +67,7 @@ class GetMasterVersionEvent : RequestEvent() {
/**
* Get the version of the update bundle that would be applied on the next app restart.
*/
@Deprecated("Use GetBundleInfoEvent")
class GetUpdateStatusEvent : RequestEvent() {

/**
Expand All @@ -70,6 +76,29 @@ class GetUpdateStatusEvent : RequestEvent() {
class ResultEvent(val status: UpdateStatus?) : ResponseEvent()
}

/**
* Get the info about the current bundle status.
*/
class GetBundleInfoEvent : RequestEvent() {
class ResultEvent(
val masterVersion: String?,
val updateVersion: String?,
val isMasterReady: Boolean,
val isUpdateReady: Boolean,
val masterDir: Uri,
val updateDir: Uri
) : ResponseEvent()
}

/**
* Starts/restarts the bundle provisioning process.
*
* @param version The expected version of the app bundle.
* @param updateMode The mode of how update is applied.
* @param openConnection Returns connection that downloads the bundle ZIP archive.
*/
class StartEvent(val version: String, val updateMode: UpdateMode, val openConnection: () -> URLConnection)

/**
* Applies the available update bundle to master, see [UpdateStatus.isReady].
*/
Expand Down Expand Up @@ -113,15 +142,36 @@ open class EvergreenPlugin(
}

@Subscribe
@Deprecated("Use GetBundleInfoEvent")
open fun onGetMasterVersion(event: GetMasterVersionEvent) {
event.respond(GetMasterVersionEvent.ResultEvent(masterVersion))
}

@Subscribe
@Deprecated("Use GetBundleInfoEvent")
open fun onGetUpdateStatus(event: GetUpdateStatusEvent) {
event.respond(GetUpdateStatusEvent.ResultEvent(updateVersion?.let { UpdateStatus(it, isUpdateReady) }))
}

@Subscribe
open fun onGetBundleInfo(event: GetBundleInfoEvent) {
event.respond(
GetBundleInfoEvent.ResultEvent(
masterVersion = masterVersion,
updateVersion = updateVersion,
isMasterReady = isMasterReady,
isUpdateReady = isUpdateReady,
masterDir = masterDir.toUri(),
updateDir = updateDir.toUri(),
)
)
}

@Subscribe(threadMode = ThreadMode.ASYNC)
open fun onStartEvent(event: StartEvent) {
start(event.version, event.updateMode, event.openConnection)
}

@Subscribe(threadMode = ThreadMode.ASYNC)
open fun onApplyUpdate(event: ApplyUpdateEvent) {
event.respond(ApplyUpdateEvent.ResultEvent(if (applyUpdate()) masterVersion else null))
Expand Down
Loading