native-update-capacitor 0.0.0-stage → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI-INTEGRATION-GUIDE.md +578 -0
- package/LICENSE +25 -0
- package/NativeUpdate.podspec +19 -0
- package/README.md +574 -2
- package/android/build.gradle +125 -0
- package/android/consumer-rules.pro +9 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/android/gradle.properties +17 -0
- package/android/manifest-additions.xml +28 -0
- package/android/proguard-rules.pro +26 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +13 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppReviewPlugin.kt +159 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppUpdatePlugin.kt +314 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +406 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +48 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +343 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +236 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/JsonOptional.kt +26 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +1249 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +404 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NotificationActionReceiver.kt +107 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +240 -0
- package/android/variables.gradle +5 -0
- package/cli/commands/backend-create.js +623 -0
- package/cli/commands/bundle-create.js +145 -0
- package/cli/commands/bundle-sign.js +64 -0
- package/cli/commands/bundle-verify.js +58 -0
- package/cli/commands/init.js +146 -0
- package/cli/commands/keys-generate.js +58 -0
- package/cli/commands/monitor.js +89 -0
- package/cli/commands/remote-shared.js +262 -0
- package/cli/commands/remote.js +622 -0
- package/cli/commands/server-start.js +96 -0
- package/cli/index.js +605 -0
- package/cli/package.json +21 -0
- package/dist/esm/app-review/app-review-manager.d.ts +24 -0
- package/dist/esm/app-review/app-review-manager.js +194 -0
- package/dist/esm/app-review/index.d.ts +5 -0
- package/dist/esm/app-review/index.js +5 -0
- package/dist/esm/app-review/platform-review-handler.d.ts +20 -0
- package/dist/esm/app-review/platform-review-handler.js +137 -0
- package/dist/esm/app-review/review-conditions-checker.d.ts +22 -0
- package/dist/esm/app-review/review-conditions-checker.js +154 -0
- package/dist/esm/app-review/review-rate-limiter.d.ts +23 -0
- package/dist/esm/app-review/review-rate-limiter.js +163 -0
- package/dist/esm/app-review/types.d.ts +41 -0
- package/dist/esm/app-review/types.js +1 -0
- package/dist/esm/app-update/app-update-checker.d.ts +58 -0
- package/dist/esm/app-update/app-update-checker.js +227 -0
- package/dist/esm/app-update/app-update-installer.d.ts +20 -0
- package/dist/esm/app-update/app-update-installer.js +134 -0
- package/dist/esm/app-update/app-update-manager.d.ts +28 -0
- package/dist/esm/app-update/app-update-manager.js +198 -0
- package/dist/esm/app-update/app-update-notifier.d.ts +14 -0
- package/dist/esm/app-update/app-update-notifier.js +99 -0
- package/dist/esm/app-update/index.d.ts +6 -0
- package/dist/esm/app-update/index.js +6 -0
- package/dist/esm/app-update/platform-app-update.d.ts +19 -0
- package/dist/esm/app-update/platform-app-update.js +138 -0
- package/dist/esm/app-update/types.d.ts +58 -0
- package/dist/esm/app-update/types.js +11 -0
- package/dist/esm/background-update/background-scheduler.d.ts +63 -0
- package/dist/esm/background-update/background-scheduler.js +332 -0
- package/dist/esm/background-update/index.d.ts +3 -0
- package/dist/esm/background-update/index.js +2 -0
- package/dist/esm/background-update/notification-manager.d.ts +29 -0
- package/dist/esm/background-update/notification-manager.js +88 -0
- package/dist/esm/config/support.d.ts +9 -0
- package/dist/esm/config/support.js +9 -0
- package/dist/esm/core/analytics.d.ts +83 -0
- package/dist/esm/core/analytics.js +151 -0
- package/dist/esm/core/cache-manager.d.ts +72 -0
- package/dist/esm/core/cache-manager.js +274 -0
- package/dist/esm/core/config.d.ts +69 -0
- package/dist/esm/core/config.js +107 -0
- package/dist/esm/core/errors.d.ts +56 -0
- package/dist/esm/core/errors.js +85 -0
- package/dist/esm/core/event-emitter.d.ts +30 -0
- package/dist/esm/core/event-emitter.js +80 -0
- package/dist/esm/core/logger.d.ts +21 -0
- package/dist/esm/core/logger.js +108 -0
- package/dist/esm/core/performance.d.ts +53 -0
- package/dist/esm/core/performance.js +153 -0
- package/dist/esm/core/plugin-manager.d.ts +88 -0
- package/dist/esm/core/plugin-manager.js +224 -0
- package/dist/esm/core/security.d.ts +123 -0
- package/dist/esm/core/security.js +361 -0
- package/dist/esm/core/update-check.d.ts +121 -0
- package/dist/esm/core/update-check.js +127 -0
- package/dist/esm/definitions.d.ts +872 -0
- package/dist/esm/definitions.js +104 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/live-update/bundle-manager.d.ts +102 -0
- package/dist/esm/live-update/bundle-manager.js +344 -0
- package/dist/esm/live-update/certificate-pinning.d.ts +38 -0
- package/dist/esm/live-update/certificate-pinning.js +77 -0
- package/dist/esm/live-update/delta-processor.d.ts +84 -0
- package/dist/esm/live-update/delta-processor.js +257 -0
- package/dist/esm/live-update/download-manager.d.ts +106 -0
- package/dist/esm/live-update/download-manager.js +412 -0
- package/dist/esm/live-update/rollout-checker.d.ts +86 -0
- package/dist/esm/live-update/rollout-checker.js +304 -0
- package/dist/esm/live-update/update-manager.d.ts +63 -0
- package/dist/esm/live-update/update-manager.js +321 -0
- package/dist/esm/live-update/version-manager.d.ts +93 -0
- package/dist/esm/live-update/version-manager.js +390 -0
- package/dist/esm/plugin.d.ts +3 -0
- package/dist/esm/plugin.js +848 -0
- package/dist/esm/security/crypto.d.ts +88 -0
- package/dist/esm/security/crypto.js +228 -0
- package/dist/esm/security/validator.d.ts +60 -0
- package/dist/esm/security/validator.js +142 -0
- package/dist/esm/types/app-identity.d.ts +36 -0
- package/dist/esm/types/app-identity.js +1 -0
- package/dist/esm/types/rollout.d.ts +91 -0
- package/dist/esm/types/rollout.js +25 -0
- package/dist/esm/web.d.ts +130 -0
- package/dist/esm/web.js +981 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.esm.js +1 -0
- package/dist/plugin.js +2 -0
- package/docs/CHANGELOG.md +993 -0
- package/docs/KNOWN_LIMITATIONS.md +235 -0
- package/docs/MIGRATION.md +391 -0
- package/docs/protocol/CLIENT-PROTOCOL.md +178 -0
- package/docs/server-requirements.md +211 -0
- package/ios/Plugin/AppReview/AppReviewPlugin.swift +158 -0
- package/ios/Plugin/AppUpdate/AppUpdatePlugin.swift +249 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +333 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +464 -0
- package/ios/Plugin/Info.plist +43 -0
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +1286 -0
- package/ios/Plugin/LiveUpdate/WebViewConfiguration.swift +45 -0
- package/ios/Plugin/NativeUpdatePlugin.m +53 -0
- package/ios/Plugin/NativeUpdatePlugin.swift +315 -0
- package/ios/Plugin/Security/SecurityManager.swift +289 -0
- package/package.json +135 -4
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
package com.aoneahsan.nativeupdate
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import androidx.work.*
|
|
5
|
+
import com.getcapacitor.JSObject
|
|
6
|
+
import com.getcapacitor.PluginCall
|
|
7
|
+
import java.util.concurrent.TimeUnit
|
|
8
|
+
import org.json.JSONObject
|
|
9
|
+
import org.json.JSONArray
|
|
10
|
+
|
|
11
|
+
class BackgroundUpdatePlugin(private val context: Context) {
|
|
12
|
+
|
|
13
|
+
private lateinit var notificationManager: BackgroundNotificationManager
|
|
14
|
+
private var backgroundUpdateConfig: BackgroundUpdateConfig? = null
|
|
15
|
+
private var backgroundUpdateStatus: BackgroundUpdateStatus = BackgroundUpdateStatus()
|
|
16
|
+
private var eventListener: ((String, JSObject) -> Unit)? = null
|
|
17
|
+
|
|
18
|
+
companion object {
|
|
19
|
+
private const val WORK_NAME = "background_update_work"
|
|
20
|
+
private const val WORK_TAG = "capacitor_native_update"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
init {
|
|
24
|
+
notificationManager = BackgroundNotificationManager(context) { event, data ->
|
|
25
|
+
eventListener?.invoke(event, data)
|
|
26
|
+
}
|
|
27
|
+
notificationManager.createNotificationChannel()
|
|
28
|
+
BackgroundUpdateManager.registerBackgroundUpdatePlugin(this)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fun setEventListener(listener: (String, JSObject) -> Unit) {
|
|
32
|
+
eventListener = listener
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fun notifyBackgroundUpdateListeners(event: String, data: JSObject) {
|
|
36
|
+
eventListener?.invoke(event, data)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fun enableBackgroundUpdates(call: PluginCall) {
|
|
40
|
+
try {
|
|
41
|
+
val configData = call.data
|
|
42
|
+
val config = BackgroundUpdateConfig.fromJSObject(configData)
|
|
43
|
+
|
|
44
|
+
backgroundUpdateConfig = config
|
|
45
|
+
backgroundUpdateStatus.enabled = config.enabled
|
|
46
|
+
|
|
47
|
+
if (config.enabled) {
|
|
48
|
+
scheduleBackgroundWork(config.checkInterval.toLong())
|
|
49
|
+
} else {
|
|
50
|
+
cancelAllBackgroundWork()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
call.resolve()
|
|
54
|
+
} catch (e: Exception) {
|
|
55
|
+
call.reject("Invalid configuration: ${e.message}")
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fun disableBackgroundUpdates(call: PluginCall) {
|
|
60
|
+
cancelAllBackgroundWork()
|
|
61
|
+
call.resolve()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fun getBackgroundUpdateStatus(call: PluginCall) {
|
|
65
|
+
call.resolve(backgroundUpdateStatus.toJSObject())
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fun scheduleBackgroundCheck(call: PluginCall) {
|
|
69
|
+
val interval = call.getLong("interval") ?: (24 * 60 * 60 * 1000L) // Default 24 hours
|
|
70
|
+
scheduleBackgroundWork(interval)
|
|
71
|
+
call.resolve()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fun triggerBackgroundCheck(call: PluginCall) {
|
|
75
|
+
val workRequest = OneTimeWorkRequestBuilder<BackgroundUpdateWorker>()
|
|
76
|
+
.setInputData(createWorkData())
|
|
77
|
+
.addTag(WORK_TAG)
|
|
78
|
+
// Exponential backoff starting at 30s. WorkManager's default
|
|
79
|
+
// is 10s which hits a failing server too fast and burns
|
|
80
|
+
// battery on mobile. 30s base, doubled each retry, caps
|
|
81
|
+
// automatically at WorkManager's internal max (~5 hours).
|
|
82
|
+
.setBackoffCriteria(
|
|
83
|
+
BackoffPolicy.EXPONENTIAL,
|
|
84
|
+
30,
|
|
85
|
+
TimeUnit.SECONDS
|
|
86
|
+
)
|
|
87
|
+
.build()
|
|
88
|
+
|
|
89
|
+
WorkManager.getInstance(context)
|
|
90
|
+
.enqueueUniqueWork(
|
|
91
|
+
"${WORK_NAME}_manual",
|
|
92
|
+
ExistingWorkPolicy.REPLACE,
|
|
93
|
+
workRequest
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
// Return immediately - actual result will be sent via listeners
|
|
97
|
+
call.resolve(JSObject().put("triggered", true))
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fun setNotificationPreferences(call: PluginCall) {
|
|
101
|
+
val preferences = call.data
|
|
102
|
+
notificationManager.setPreferences(preferences)
|
|
103
|
+
call.resolve()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
fun getNotificationPermissions(call: PluginCall) {
|
|
107
|
+
val status = notificationManager.getPermissionStatus()
|
|
108
|
+
call.resolve(status.toJSObject())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fun requestNotificationPermissions(call: PluginCall) {
|
|
112
|
+
val granted = notificationManager.requestPermissions()
|
|
113
|
+
call.resolve(JSObject().put("granted", granted))
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
internal fun cancelAllBackgroundWork() {
|
|
117
|
+
backgroundUpdateStatus.enabled = false
|
|
118
|
+
backgroundUpdateStatus.isRunning = false
|
|
119
|
+
backgroundUpdateStatus.currentTaskId = null
|
|
120
|
+
|
|
121
|
+
WorkManager.getInstance(context).cancelAllWorkByTag(WORK_TAG)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private fun scheduleBackgroundWork(intervalMs: Long) {
|
|
125
|
+
val constraints = Constraints.Builder()
|
|
126
|
+
.setRequiredNetworkType(
|
|
127
|
+
if (backgroundUpdateConfig?.requireWifi == true)
|
|
128
|
+
NetworkType.UNMETERED
|
|
129
|
+
else
|
|
130
|
+
NetworkType.CONNECTED
|
|
131
|
+
)
|
|
132
|
+
.setRequiresBatteryNotLow(backgroundUpdateConfig?.respectBatteryOptimization != false)
|
|
133
|
+
.build()
|
|
134
|
+
|
|
135
|
+
val workRequest = PeriodicWorkRequestBuilder<BackgroundUpdateWorker>(
|
|
136
|
+
intervalMs,
|
|
137
|
+
TimeUnit.MILLISECONDS,
|
|
138
|
+
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS,
|
|
139
|
+
TimeUnit.MILLISECONDS
|
|
140
|
+
)
|
|
141
|
+
.setConstraints(constraints)
|
|
142
|
+
.setInputData(createWorkData())
|
|
143
|
+
.addTag(WORK_TAG)
|
|
144
|
+
// See triggerBackgroundCheck — same 30s exponential base.
|
|
145
|
+
.setBackoffCriteria(
|
|
146
|
+
BackoffPolicy.EXPONENTIAL,
|
|
147
|
+
30,
|
|
148
|
+
TimeUnit.SECONDS
|
|
149
|
+
)
|
|
150
|
+
.build()
|
|
151
|
+
|
|
152
|
+
WorkManager.getInstance(context)
|
|
153
|
+
.enqueueUniquePeriodicWork(
|
|
154
|
+
WORK_NAME,
|
|
155
|
+
ExistingPeriodicWorkPolicy.REPLACE,
|
|
156
|
+
workRequest
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
backgroundUpdateStatus.nextCheckTime = System.currentTimeMillis() + intervalMs
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private fun createWorkData(): Data {
|
|
163
|
+
val configJson = backgroundUpdateConfig?.toJSONObject()?.toString() ?: "{}"
|
|
164
|
+
return Data.Builder()
|
|
165
|
+
.putString("config", configJson)
|
|
166
|
+
.build()
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fun updateBackgroundStatus(status: BackgroundUpdateStatus) {
|
|
170
|
+
backgroundUpdateStatus = status
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fun getBackgroundConfig(): BackgroundUpdateConfig? {
|
|
174
|
+
return backgroundUpdateConfig
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fun getNotificationManager(): BackgroundNotificationManager {
|
|
178
|
+
return notificationManager
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
data class BackgroundUpdateConfig(
|
|
183
|
+
val enabled: Boolean,
|
|
184
|
+
val checkInterval: Int,
|
|
185
|
+
val updateTypes: List<BackgroundUpdateType>,
|
|
186
|
+
val autoInstall: Boolean = false,
|
|
187
|
+
val notificationPreferences: NotificationPreferences? = null,
|
|
188
|
+
val respectBatteryOptimization: Boolean = true,
|
|
189
|
+
val allowMeteredConnection: Boolean = false,
|
|
190
|
+
val minimumBatteryLevel: Int = 20,
|
|
191
|
+
val requireWifi: Boolean = false,
|
|
192
|
+
val maxRetries: Int = 3,
|
|
193
|
+
val retryDelay: Int = 5000,
|
|
194
|
+
val taskIdentifier: String? = null
|
|
195
|
+
) {
|
|
196
|
+
companion object {
|
|
197
|
+
fun fromJSObject(obj: JSObject): BackgroundUpdateConfig {
|
|
198
|
+
val enabled = obj.boolOrNull("enabled") ?: false
|
|
199
|
+
val checkInterval = obj.intOrNull("checkInterval") ?: (24 * 60 * 60 * 1000)
|
|
200
|
+
val updateTypesArray = obj.arrayOrNull("updateTypes") ?: org.json.JSONArray()
|
|
201
|
+
val updateTypes = mutableListOf<BackgroundUpdateType>()
|
|
202
|
+
|
|
203
|
+
for (i in 0 until updateTypesArray.length()) {
|
|
204
|
+
val typeString = updateTypesArray.getString(i)
|
|
205
|
+
BackgroundUpdateType.fromString(typeString)?.let { updateTypes.add(it) }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
val notificationPrefs = obj.getJSObject("notificationPreferences")
|
|
209
|
+
|
|
210
|
+
return BackgroundUpdateConfig(
|
|
211
|
+
enabled = enabled,
|
|
212
|
+
checkInterval = checkInterval,
|
|
213
|
+
updateTypes = updateTypes,
|
|
214
|
+
autoInstall = obj.boolOrNull("autoInstall") ?: false,
|
|
215
|
+
notificationPreferences = notificationPrefs?.let { NotificationPreferences.fromJSObject(it) },
|
|
216
|
+
respectBatteryOptimization = obj.boolOrNull("respectBatteryOptimization") ?: true,
|
|
217
|
+
allowMeteredConnection = obj.boolOrNull("allowMeteredConnection") ?: false,
|
|
218
|
+
minimumBatteryLevel = obj.intOrNull("minimumBatteryLevel") ?: 20,
|
|
219
|
+
requireWifi = obj.boolOrNull("requireWifi") ?: false,
|
|
220
|
+
maxRetries = obj.intOrNull("maxRetries") ?: 3,
|
|
221
|
+
retryDelay = obj.intOrNull("retryDelay") ?: 5000,
|
|
222
|
+
taskIdentifier = obj.getString("taskIdentifier")
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fun toJSONObject(): JSONObject {
|
|
228
|
+
val obj = JSONObject()
|
|
229
|
+
obj.put("enabled", enabled)
|
|
230
|
+
obj.put("checkInterval", checkInterval)
|
|
231
|
+
obj.put("updateTypes", JSONArray(updateTypes.map { it.value }))
|
|
232
|
+
obj.put("autoInstall", autoInstall)
|
|
233
|
+
obj.put("respectBatteryOptimization", respectBatteryOptimization)
|
|
234
|
+
obj.put("allowMeteredConnection", allowMeteredConnection)
|
|
235
|
+
obj.put("minimumBatteryLevel", minimumBatteryLevel)
|
|
236
|
+
obj.put("requireWifi", requireWifi)
|
|
237
|
+
obj.put("maxRetries", maxRetries)
|
|
238
|
+
obj.put("retryDelay", retryDelay)
|
|
239
|
+
taskIdentifier?.let { obj.put("taskIdentifier", it) }
|
|
240
|
+
return obj
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
enum class BackgroundUpdateType(val value: String) {
|
|
245
|
+
APP_UPDATE("app_update"),
|
|
246
|
+
LIVE_UPDATE("live_update"),
|
|
247
|
+
BOTH("both");
|
|
248
|
+
|
|
249
|
+
companion object {
|
|
250
|
+
fun fromString(value: String): BackgroundUpdateType? {
|
|
251
|
+
return values().find { it.value == value }
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
data class BackgroundUpdateStatus(
|
|
257
|
+
var enabled: Boolean = false,
|
|
258
|
+
var lastCheckTime: Long? = null,
|
|
259
|
+
var nextCheckTime: Long? = null,
|
|
260
|
+
var lastUpdateTime: Long? = null,
|
|
261
|
+
var currentTaskId: String? = null,
|
|
262
|
+
var isRunning: Boolean = false,
|
|
263
|
+
var checkCount: Int = 0,
|
|
264
|
+
var failureCount: Int = 0,
|
|
265
|
+
var lastError: UpdateError? = null
|
|
266
|
+
) {
|
|
267
|
+
fun toJSObject(): JSObject {
|
|
268
|
+
val obj = JSObject()
|
|
269
|
+
obj.put("enabled", enabled)
|
|
270
|
+
obj.put("isRunning", isRunning)
|
|
271
|
+
obj.put("checkCount", checkCount)
|
|
272
|
+
obj.put("failureCount", failureCount)
|
|
273
|
+
|
|
274
|
+
lastCheckTime?.let { obj.put("lastCheckTime", it) }
|
|
275
|
+
nextCheckTime?.let { obj.put("nextCheckTime", it) }
|
|
276
|
+
lastUpdateTime?.let { obj.put("lastUpdateTime", it) }
|
|
277
|
+
currentTaskId?.let { obj.put("currentTaskId", it) }
|
|
278
|
+
lastError?.let { obj.put("lastError", it.toJSObject()) }
|
|
279
|
+
|
|
280
|
+
return obj
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
data class BackgroundCheckResult(
|
|
285
|
+
val success: Boolean,
|
|
286
|
+
val updatesFound: Boolean,
|
|
287
|
+
val appUpdate: AppUpdateStatus? = null,
|
|
288
|
+
val liveUpdate: LatestVersion? = null,
|
|
289
|
+
val notificationSent: Boolean = false,
|
|
290
|
+
val error: UpdateError? = null
|
|
291
|
+
) {
|
|
292
|
+
fun toJSObject(): JSObject {
|
|
293
|
+
val obj = JSObject()
|
|
294
|
+
obj.put("success", success)
|
|
295
|
+
obj.put("updatesFound", updatesFound)
|
|
296
|
+
obj.put("notificationSent", notificationSent)
|
|
297
|
+
|
|
298
|
+
appUpdate?.let { obj.put("appUpdate", it.toJSObject()) }
|
|
299
|
+
liveUpdate?.let { obj.put("liveUpdate", it.toJSObject()) }
|
|
300
|
+
error?.let { obj.put("error", it.toJSObject()) }
|
|
301
|
+
|
|
302
|
+
return obj
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
data class UpdateError(
|
|
307
|
+
val code: String,
|
|
308
|
+
val message: String
|
|
309
|
+
) {
|
|
310
|
+
fun toJSObject(): JSObject {
|
|
311
|
+
val obj = JSObject()
|
|
312
|
+
obj.put("code", code)
|
|
313
|
+
obj.put("message", message)
|
|
314
|
+
return obj
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Placeholder data classes - would be defined in other plugins
|
|
319
|
+
data class AppUpdateStatus(
|
|
320
|
+
val updateAvailable: Boolean,
|
|
321
|
+
val currentVersion: String,
|
|
322
|
+
val availableVersion: String? = null
|
|
323
|
+
) {
|
|
324
|
+
fun toJSObject(): JSObject {
|
|
325
|
+
val obj = JSObject()
|
|
326
|
+
obj.put("updateAvailable", updateAvailable)
|
|
327
|
+
obj.put("currentVersion", currentVersion)
|
|
328
|
+
availableVersion?.let { obj.put("availableVersion", it) }
|
|
329
|
+
return obj
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
data class LatestVersion(
|
|
334
|
+
val available: Boolean,
|
|
335
|
+
val version: String? = null
|
|
336
|
+
) {
|
|
337
|
+
fun toJSObject(): JSObject {
|
|
338
|
+
val obj = JSObject()
|
|
339
|
+
obj.put("available", available)
|
|
340
|
+
version?.let { obj.put("version", it) }
|
|
341
|
+
return obj
|
|
342
|
+
}
|
|
343
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
package com.aoneahsan.nativeupdate
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import androidx.work.*
|
|
5
|
+
import com.getcapacitor.JSObject
|
|
6
|
+
import com.google.gson.Gson
|
|
7
|
+
import kotlinx.coroutines.Dispatchers
|
|
8
|
+
import kotlinx.coroutines.withContext
|
|
9
|
+
import org.json.JSONObject
|
|
10
|
+
|
|
11
|
+
class BackgroundUpdateWorker(
|
|
12
|
+
private val context: Context,
|
|
13
|
+
workerParams: WorkerParameters
|
|
14
|
+
) : CoroutineWorker(context, workerParams) {
|
|
15
|
+
|
|
16
|
+
companion object {
|
|
17
|
+
private const val TAG = "BackgroundUpdateWorker"
|
|
18
|
+
private const val NOTIFICATION_ID = 1001
|
|
19
|
+
// Cap the number of automatic retries for a transient failure
|
|
20
|
+
// (network error, backend 5xx, etc). WorkManager does not cap
|
|
21
|
+
// retries natively for our periodic request — without this guard
|
|
22
|
+
// a persistently-failing server would burn battery indefinitely.
|
|
23
|
+
// The schedule (with exponential backoff starting at 30s) takes
|
|
24
|
+
// roughly 30+60+120+240+480 = ~15 minutes across 5 retries, after
|
|
25
|
+
// which we give up until the next periodic run.
|
|
26
|
+
private const val MAX_RETRY_ATTEMPTS = 5
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override suspend fun doWork(): Result {
|
|
30
|
+
return withContext(Dispatchers.IO) {
|
|
31
|
+
try {
|
|
32
|
+
if (runAttemptCount >= MAX_RETRY_ATTEMPTS) {
|
|
33
|
+
android.util.Log.w(
|
|
34
|
+
TAG,
|
|
35
|
+
"Giving up after $runAttemptCount retries; will try again on next periodic run"
|
|
36
|
+
)
|
|
37
|
+
return@withContext Result.failure()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
val configJson = inputData.getString("config")
|
|
41
|
+
val config = parseConfig(configJson)
|
|
42
|
+
|
|
43
|
+
if (config == null || !config.enabled) {
|
|
44
|
+
return@withContext Result.failure()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
val result = performBackgroundCheck(config)
|
|
48
|
+
|
|
49
|
+
// Update plugin status
|
|
50
|
+
updatePluginStatus(result)
|
|
51
|
+
|
|
52
|
+
// Send notification if updates found
|
|
53
|
+
if (result.updatesFound) {
|
|
54
|
+
sendNotification(result)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Notify listeners
|
|
58
|
+
notifyListeners(result)
|
|
59
|
+
|
|
60
|
+
if (result.success) Result.success() else Result.retry()
|
|
61
|
+
} catch (e: Exception) {
|
|
62
|
+
android.util.Log.e(TAG, "Background update failed", e)
|
|
63
|
+
Result.failure()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private fun parseConfig(configJson: String?): BackgroundUpdateConfig? {
|
|
69
|
+
return try {
|
|
70
|
+
configJson?.let {
|
|
71
|
+
val jsonObject = JSONObject(it)
|
|
72
|
+
val jsObject = JSObject.fromJSONObject(jsonObject)
|
|
73
|
+
BackgroundUpdateConfig.fromJSObject(jsObject)
|
|
74
|
+
}
|
|
75
|
+
} catch (e: Exception) {
|
|
76
|
+
android.util.Log.e(TAG, "Failed to parse config", e)
|
|
77
|
+
null
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private suspend fun performBackgroundCheck(config: BackgroundUpdateConfig): BackgroundCheckResult {
|
|
82
|
+
try {
|
|
83
|
+
var appUpdate: AppUpdateStatus? = null
|
|
84
|
+
var liveUpdate: LatestVersion? = null
|
|
85
|
+
|
|
86
|
+
// Check for app updates
|
|
87
|
+
if (config.updateTypes.contains(BackgroundUpdateType.APP_UPDATE) ||
|
|
88
|
+
config.updateTypes.contains(BackgroundUpdateType.BOTH)) {
|
|
89
|
+
appUpdate = checkForAppUpdate()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Check for live updates
|
|
93
|
+
if (config.updateTypes.contains(BackgroundUpdateType.LIVE_UPDATE) ||
|
|
94
|
+
config.updateTypes.contains(BackgroundUpdateType.BOTH)) {
|
|
95
|
+
liveUpdate = checkForLiveUpdate()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
val updatesFound = (appUpdate?.updateAvailable == true) || (liveUpdate?.available == true)
|
|
99
|
+
|
|
100
|
+
return BackgroundCheckResult(
|
|
101
|
+
success = true,
|
|
102
|
+
updatesFound = updatesFound,
|
|
103
|
+
appUpdate = appUpdate,
|
|
104
|
+
liveUpdate = liveUpdate,
|
|
105
|
+
notificationSent = false // Will be updated after notification is sent
|
|
106
|
+
)
|
|
107
|
+
} catch (e: Exception) {
|
|
108
|
+
android.util.Log.e(TAG, "Background check failed", e)
|
|
109
|
+
return BackgroundCheckResult(
|
|
110
|
+
success = false,
|
|
111
|
+
updatesFound = false,
|
|
112
|
+
error = UpdateError("UNKNOWN_ERROR", e.message ?: "Unknown error")
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private suspend fun checkForAppUpdate(): AppUpdateStatus? {
|
|
118
|
+
return withContext(Dispatchers.IO) {
|
|
119
|
+
try {
|
|
120
|
+
val appUpdatePlugin = BackgroundUpdateManager.getAppUpdatePlugin()
|
|
121
|
+
if (appUpdatePlugin != null) {
|
|
122
|
+
// Use actual plugin to check for updates
|
|
123
|
+
appUpdatePlugin.getAppUpdateInfoAsync()
|
|
124
|
+
} else null
|
|
125
|
+
} catch (e: Exception) {
|
|
126
|
+
android.util.Log.e(TAG, "App update check failed", e)
|
|
127
|
+
null
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private suspend fun checkForLiveUpdate(): LatestVersion? {
|
|
133
|
+
return withContext(Dispatchers.IO) {
|
|
134
|
+
try {
|
|
135
|
+
val liveUpdatePlugin = BackgroundUpdateManager.getLiveUpdatePlugin()
|
|
136
|
+
if (liveUpdatePlugin != null) {
|
|
137
|
+
// Use actual plugin to check for updates
|
|
138
|
+
liveUpdatePlugin.getLatestVersionAsync()
|
|
139
|
+
} else null
|
|
140
|
+
} catch (e: Exception) {
|
|
141
|
+
android.util.Log.e(TAG, "Live update check failed", e)
|
|
142
|
+
null
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private fun getCurrentAppVersion(): String {
|
|
148
|
+
return try {
|
|
149
|
+
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
|
|
150
|
+
packageInfo.versionName ?: "1.0.0"
|
|
151
|
+
} catch (e: Exception) {
|
|
152
|
+
"1.0.0"
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private fun sendNotification(result: BackgroundCheckResult) {
|
|
157
|
+
try {
|
|
158
|
+
val plugin = getBackgroundUpdatePlugin()
|
|
159
|
+
val notificationManager = plugin?.getNotificationManager()
|
|
160
|
+
|
|
161
|
+
if (notificationManager != null) {
|
|
162
|
+
val sent = notificationManager.sendUpdateNotification(
|
|
163
|
+
result.appUpdate,
|
|
164
|
+
result.liveUpdate
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
// Update result
|
|
168
|
+
result.copy(notificationSent = sent)
|
|
169
|
+
}
|
|
170
|
+
} catch (e: Exception) {
|
|
171
|
+
android.util.Log.e(TAG, "Failed to send notification", e)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private fun updatePluginStatus(result: BackgroundCheckResult) {
|
|
176
|
+
try {
|
|
177
|
+
val plugin = getBackgroundUpdatePlugin()
|
|
178
|
+
val currentStatus = plugin?.getBackgroundConfig()?.let { BackgroundUpdateStatus() } ?: return
|
|
179
|
+
|
|
180
|
+
currentStatus.apply {
|
|
181
|
+
isRunning = false
|
|
182
|
+
checkCount++
|
|
183
|
+
lastCheckTime = System.currentTimeMillis()
|
|
184
|
+
|
|
185
|
+
if (result.success) {
|
|
186
|
+
lastError = null
|
|
187
|
+
if (result.updatesFound) {
|
|
188
|
+
lastUpdateTime = System.currentTimeMillis()
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
failureCount++
|
|
192
|
+
lastError = result.error
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
plugin.updateBackgroundStatus(currentStatus)
|
|
197
|
+
} catch (e: Exception) {
|
|
198
|
+
android.util.Log.e(TAG, "Failed to update plugin status", e)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private fun notifyListeners(result: BackgroundCheckResult) {
|
|
203
|
+
try {
|
|
204
|
+
val plugin = getBackgroundUpdatePlugin()
|
|
205
|
+
|
|
206
|
+
// Notify about progress
|
|
207
|
+
val progressData = JSObject()
|
|
208
|
+
progressData.put("type", if (result.appUpdate != null) "app_update" else "live_update")
|
|
209
|
+
progressData.put("status", if (result.success) "completed" else "failed")
|
|
210
|
+
progressData.put("percent", 100)
|
|
211
|
+
|
|
212
|
+
plugin?.notifyBackgroundUpdateListeners("backgroundUpdateProgress", progressData)
|
|
213
|
+
|
|
214
|
+
// Notify about notification if sent
|
|
215
|
+
if (result.notificationSent) {
|
|
216
|
+
val notificationData = JSObject()
|
|
217
|
+
notificationData.put("type", if (result.appUpdate != null) "app_update" else "live_update")
|
|
218
|
+
notificationData.put("updateAvailable", result.updatesFound)
|
|
219
|
+
notificationData.put("action", "shown")
|
|
220
|
+
|
|
221
|
+
plugin?.notifyBackgroundUpdateListeners("backgroundUpdateNotification", notificationData)
|
|
222
|
+
}
|
|
223
|
+
} catch (e: Exception) {
|
|
224
|
+
android.util.Log.e(TAG, "Failed to notify listeners", e)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private fun getBackgroundUpdatePlugin(): BackgroundUpdatePlugin? {
|
|
229
|
+
return try {
|
|
230
|
+
BackgroundUpdateManager.getBackgroundUpdatePlugin()
|
|
231
|
+
} catch (e: Exception) {
|
|
232
|
+
android.util.Log.e(TAG, "Failed to get plugin", e)
|
|
233
|
+
null
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
package com.aoneahsan.nativeupdate
|
|
2
|
+
|
|
3
|
+
import org.json.JSONArray
|
|
4
|
+
import org.json.JSONObject
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Null-safe reads for OPTIONAL configuration fields.
|
|
8
|
+
*
|
|
9
|
+
* Capacitor's JSObject overrides getString/getInteger/getBool/getJSObject to return null for a missing key, but
|
|
10
|
+
* getLong, getInt, getBoolean and getJSONArray are org.json's and THROW JSONException instead. A `?.let` or `?:`
|
|
11
|
+
* after them compiles and reads as optional, yet a consumer that simply omits the field gets a failed configure()
|
|
12
|
+
* and OTA switched off (ISSUE-14: every flat-config consumer that left out maxBundleSize). Optional fields are read
|
|
13
|
+
* only through these helpers.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The field as a Long, or null when it is absent, JSON null or not a number. */
|
|
17
|
+
internal fun JSONObject.longOrNull(name: String): Long? = (opt(name) as? Number)?.toLong()
|
|
18
|
+
|
|
19
|
+
/** The field as an Int, or null when it is absent, JSON null or not a number. */
|
|
20
|
+
internal fun JSONObject.intOrNull(name: String): Int? = (opt(name) as? Number)?.toInt()
|
|
21
|
+
|
|
22
|
+
/** The field as a Boolean, or null when it is absent, JSON null or not a boolean. */
|
|
23
|
+
internal fun JSONObject.boolOrNull(name: String): Boolean? = opt(name) as? Boolean
|
|
24
|
+
|
|
25
|
+
/** The field as a JSONArray, or null when it is absent or not an array. */
|
|
26
|
+
internal fun JSONObject.arrayOrNull(name: String): JSONArray? = optJSONArray(name)
|