@capgo/capacitor-updater 6.51.18 → 6.51.21
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/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +27 -29
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +302 -316
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +227 -64
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +12 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +12 -16
- package/dist/esm/history.js +7 -7
- package/dist/esm/history.js.map +1 -1
- package/dist/plugin.cjs.js +7 -7
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +7 -7
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +157 -54
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +13 -3
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +30 -37
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +16 -41
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +2 -2
- package/package.json +6 -8
|
@@ -1768,12 +1768,10 @@ public class CapgoUpdater {
|
|
|
1768
1768
|
}
|
|
1769
1769
|
|
|
1770
1770
|
void restoreResetState(final ResetState state) {
|
|
1771
|
-
final String currentBundlePath =
|
|
1772
|
-
? "public"
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
? BundleInfo.ID_BUILTIN
|
|
1776
|
-
: state.fallbackBundleId;
|
|
1771
|
+
final String currentBundlePath =
|
|
1772
|
+
state.currentBundlePath == null || state.currentBundlePath.trim().isEmpty() ? "public" : state.currentBundlePath;
|
|
1773
|
+
final String fallbackBundleId =
|
|
1774
|
+
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
1777
1775
|
|
|
1778
1776
|
this.editor.putString(this.CAP_SERVER_PATH, currentBundlePath);
|
|
1779
1777
|
this.editor.putString(FALLBACK_VERSION, fallbackBundleId);
|
|
@@ -2208,32 +2206,30 @@ public class CapgoUpdater {
|
|
|
2208
2206
|
.build();
|
|
2209
2207
|
|
|
2210
2208
|
// User-Agent header is automatically added by DownloadService.sharedClient interceptor
|
|
2211
|
-
DownloadService.sharedClient
|
|
2212
|
-
.
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
logger.debug("Error: " + e.getMessage());
|
|
2220
|
-
}
|
|
2209
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2210
|
+
new okhttp3.Callback() {
|
|
2211
|
+
@Override
|
|
2212
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2213
|
+
releaseRateLimitStatisticClaim();
|
|
2214
|
+
logger.error("Failed to send rate limit statistic");
|
|
2215
|
+
logger.debug("Error: " + e.getMessage());
|
|
2216
|
+
}
|
|
2221
2217
|
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
}
|
|
2218
|
+
@Override
|
|
2219
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) {
|
|
2220
|
+
// The body is unused here; closing the Response closes it.
|
|
2221
|
+
try (response) {
|
|
2222
|
+
if (response.isSuccessful()) {
|
|
2223
|
+
logger.info("Rate limit statistic sent");
|
|
2224
|
+
} else {
|
|
2225
|
+
releaseRateLimitStatisticClaim();
|
|
2226
|
+
logger.error("Error sending rate limit statistic");
|
|
2227
|
+
logger.debug("Response code: " + response.code());
|
|
2233
2228
|
}
|
|
2234
2229
|
}
|
|
2235
2230
|
}
|
|
2236
|
-
|
|
2231
|
+
}
|
|
2232
|
+
);
|
|
2237
2233
|
} catch (final Exception e) {
|
|
2238
2234
|
releaseRateLimitStatisticClaim();
|
|
2239
2235
|
logger.error("Failed to send rate limit statistic");
|
|
@@ -2247,70 +2243,47 @@ public class CapgoUpdater {
|
|
|
2247
2243
|
|
|
2248
2244
|
Request request = new Request.Builder().url(url).post(body).build();
|
|
2249
2245
|
|
|
2250
|
-
DownloadService.sharedClient
|
|
2251
|
-
.
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
callback.callback(retError);
|
|
2261
|
-
}
|
|
2246
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2247
|
+
new okhttp3.Callback() {
|
|
2248
|
+
@Override
|
|
2249
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2250
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2251
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
2252
|
+
retError.put("error", "network_error");
|
|
2253
|
+
retError.put("kind", "failed");
|
|
2254
|
+
callback.callback(retError);
|
|
2255
|
+
}
|
|
2262
2256
|
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
}
|
|
2257
|
+
@Override
|
|
2258
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2259
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2260
|
+
final int statusCode = response.code();
|
|
2261
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
2262
|
+
JSONObject jsonResponse = null;
|
|
2263
|
+
if (!responseData.isEmpty()) {
|
|
2264
|
+
try {
|
|
2265
|
+
jsonResponse = new JSONObject(responseData);
|
|
2266
|
+
} catch (JSONException ignored) {
|
|
2267
|
+
// Non-JSON responses are handled as response or parse errors below.
|
|
2275
2268
|
}
|
|
2269
|
+
}
|
|
2276
2270
|
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2281
|
-
retError.put(
|
|
2282
|
-
"error",
|
|
2283
|
-
rateLimit.error.isEmpty() ? jsonResponse.optString("error", "too_many_requests") : rateLimit.error
|
|
2284
|
-
);
|
|
2285
|
-
retError.put(
|
|
2286
|
-
"message",
|
|
2287
|
-
rateLimit.message.isEmpty()
|
|
2288
|
-
? jsonResponse.optString("message", "Too many requests")
|
|
2289
|
-
: rateLimit.message
|
|
2290
|
-
);
|
|
2291
|
-
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
2292
|
-
retError.put("kind", jsonResponse.getString("kind"));
|
|
2293
|
-
} else {
|
|
2294
|
-
retError.put("kind", "failed");
|
|
2295
|
-
}
|
|
2296
|
-
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
2297
|
-
retError.put("version", jsonResponse.getString("version"));
|
|
2298
|
-
}
|
|
2299
|
-
retError.put("statusCode", statusCode);
|
|
2300
|
-
callback.callback(retError);
|
|
2301
|
-
return;
|
|
2302
|
-
}
|
|
2271
|
+
if (jsonResponse != null && (jsonResponse.has("error") || jsonResponse.has("kind"))) {
|
|
2272
|
+
if (statusCode == 429) {
|
|
2273
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2303
2274
|
Map<String, Object> retError = new HashMap<>();
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2275
|
+
retError.put(
|
|
2276
|
+
"error",
|
|
2277
|
+
rateLimit.error.isEmpty() ? jsonResponse.optString("error", "too_many_requests") : rateLimit.error
|
|
2278
|
+
);
|
|
2279
|
+
retError.put(
|
|
2280
|
+
"message",
|
|
2281
|
+
rateLimit.message.isEmpty() ? jsonResponse.optString("message", "Too many requests") : rateLimit.message
|
|
2282
|
+
);
|
|
2307
2283
|
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
2308
2284
|
retError.put("kind", jsonResponse.getString("kind"));
|
|
2309
|
-
}
|
|
2310
|
-
if (jsonResponse.has("message") && !jsonResponse.isNull("message")) {
|
|
2311
|
-
retError.put("message", jsonResponse.getString("message"));
|
|
2312
2285
|
} else {
|
|
2313
|
-
retError.put("
|
|
2286
|
+
retError.put("kind", "failed");
|
|
2314
2287
|
}
|
|
2315
2288
|
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
2316
2289
|
retError.put("version", jsonResponse.getString("version"));
|
|
@@ -2319,58 +2292,77 @@ public class CapgoUpdater {
|
|
|
2319
2292
|
callback.callback(retError);
|
|
2320
2293
|
return;
|
|
2321
2294
|
}
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
if (rateLimit.blocked) {
|
|
2326
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2327
|
-
retError.put("message", rateLimit.message);
|
|
2328
|
-
retError.put("error", rateLimit.error);
|
|
2329
|
-
retError.put("kind", "failed");
|
|
2330
|
-
retError.put("statusCode", statusCode);
|
|
2331
|
-
callback.callback(retError);
|
|
2332
|
-
return;
|
|
2295
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2296
|
+
if (jsonResponse.has("error") && !jsonResponse.isNull("error")) {
|
|
2297
|
+
retError.put("error", jsonResponse.getString("error"));
|
|
2333
2298
|
}
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2337
|
-
retError.put("message", "Server error: " + response.code());
|
|
2338
|
-
retError.put("error", "response_error");
|
|
2339
|
-
retError.put("kind", "failed");
|
|
2340
|
-
retError.put("statusCode", statusCode);
|
|
2341
|
-
callback.callback(retError);
|
|
2342
|
-
return;
|
|
2299
|
+
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
2300
|
+
retError.put("kind", jsonResponse.getString("kind"));
|
|
2343
2301
|
}
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2302
|
+
if (jsonResponse.has("message") && !jsonResponse.isNull("message")) {
|
|
2303
|
+
retError.put("message", jsonResponse.getString("message"));
|
|
2304
|
+
} else {
|
|
2305
|
+
retError.put("message", "server did not provide a message");
|
|
2306
|
+
}
|
|
2307
|
+
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
2308
|
+
retError.put("version", jsonResponse.getString("version"));
|
|
2347
2309
|
}
|
|
2310
|
+
retError.put("statusCode", statusCode);
|
|
2311
|
+
callback.callback(retError);
|
|
2312
|
+
return;
|
|
2313
|
+
}
|
|
2348
2314
|
|
|
2349
|
-
|
|
2350
|
-
|
|
2315
|
+
// Check for 429 rate limit without JSON body
|
|
2316
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2317
|
+
if (rateLimit.blocked) {
|
|
2318
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2319
|
+
retError.put("message", rateLimit.message);
|
|
2320
|
+
retError.put("error", rateLimit.error);
|
|
2321
|
+
retError.put("kind", "failed");
|
|
2322
|
+
retError.put("statusCode", statusCode);
|
|
2323
|
+
callback.callback(retError);
|
|
2324
|
+
return;
|
|
2325
|
+
}
|
|
2351
2326
|
|
|
2352
|
-
|
|
2353
|
-
while (keys.hasNext()) {
|
|
2354
|
-
String key = keys.next();
|
|
2355
|
-
if (jsonResponse.has(key)) {
|
|
2356
|
-
if ("session_key".equals(key)) {
|
|
2357
|
-
ret.put("sessionKey", jsonResponse.get(key));
|
|
2358
|
-
} else {
|
|
2359
|
-
ret.put(key, jsonResponse.get(key));
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
|
-
}
|
|
2363
|
-
callback.callback(ret);
|
|
2364
|
-
} catch (JSONException e) {
|
|
2327
|
+
if (!response.isSuccessful()) {
|
|
2365
2328
|
Map<String, Object> retError = new HashMap<>();
|
|
2366
|
-
retError.put("message", "
|
|
2367
|
-
retError.put("error", "
|
|
2329
|
+
retError.put("message", "Server error: " + response.code());
|
|
2330
|
+
retError.put("error", "response_error");
|
|
2368
2331
|
retError.put("kind", "failed");
|
|
2332
|
+
retError.put("statusCode", statusCode);
|
|
2369
2333
|
callback.callback(retError);
|
|
2334
|
+
return;
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
if (jsonResponse == null) {
|
|
2338
|
+
throw new JSONException("Response is not a JSON object");
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
Map<String, Object> ret = new HashMap<>();
|
|
2342
|
+
ret.put("statusCode", statusCode);
|
|
2343
|
+
|
|
2344
|
+
Iterator<String> keys = jsonResponse.keys();
|
|
2345
|
+
while (keys.hasNext()) {
|
|
2346
|
+
String key = keys.next();
|
|
2347
|
+
if (jsonResponse.has(key)) {
|
|
2348
|
+
if ("session_key".equals(key)) {
|
|
2349
|
+
ret.put("sessionKey", jsonResponse.get(key));
|
|
2350
|
+
} else {
|
|
2351
|
+
ret.put(key, jsonResponse.get(key));
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2370
2354
|
}
|
|
2355
|
+
callback.callback(ret);
|
|
2356
|
+
} catch (JSONException e) {
|
|
2357
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2358
|
+
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
2359
|
+
retError.put("error", "parse_error");
|
|
2360
|
+
retError.put("kind", "failed");
|
|
2361
|
+
callback.callback(retError);
|
|
2371
2362
|
}
|
|
2372
2363
|
}
|
|
2373
|
-
|
|
2364
|
+
}
|
|
2365
|
+
);
|
|
2374
2366
|
}
|
|
2375
2367
|
|
|
2376
2368
|
public void getLatest(final String updateUrl, final String channel, final Callback callback) {
|
|
@@ -2553,93 +2545,91 @@ public class CapgoUpdater {
|
|
|
2553
2545
|
.put(RequestBody.create(json.toString(), MediaType.get("application/json")))
|
|
2554
2546
|
.build();
|
|
2555
2547
|
|
|
2556
|
-
DownloadService.sharedClient
|
|
2557
|
-
.
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
callback.callback(retError);
|
|
2566
|
-
}
|
|
2567
|
-
|
|
2568
|
-
@Override
|
|
2569
|
-
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2570
|
-
try (ResponseBody responseBody = response.body()) {
|
|
2571
|
-
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
2572
|
-
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2573
|
-
if (rateLimit.blocked) {
|
|
2574
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2575
|
-
retError.put("message", rateLimit.message);
|
|
2576
|
-
retError.put("error", rateLimit.error);
|
|
2577
|
-
callback.callback(retError);
|
|
2578
|
-
return;
|
|
2579
|
-
}
|
|
2548
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2549
|
+
new okhttp3.Callback() {
|
|
2550
|
+
@Override
|
|
2551
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2552
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2553
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
2554
|
+
retError.put("error", "network_error");
|
|
2555
|
+
callback.callback(retError);
|
|
2556
|
+
}
|
|
2580
2557
|
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2558
|
+
@Override
|
|
2559
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2560
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2561
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
2562
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, responseData);
|
|
2563
|
+
if (rateLimit.blocked) {
|
|
2564
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2565
|
+
retError.put("message", rateLimit.message);
|
|
2566
|
+
retError.put("error", rateLimit.error);
|
|
2567
|
+
callback.callback(retError);
|
|
2568
|
+
return;
|
|
2569
|
+
}
|
|
2591
2570
|
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2571
|
+
if (response.code() == 400) {
|
|
2572
|
+
if (responseData.contains("channel_not_found") && !defaultChannel.isEmpty()) {
|
|
2573
|
+
Map<String, Object> ret = new HashMap<>();
|
|
2574
|
+
ret.put("channel", defaultChannel);
|
|
2575
|
+
ret.put("status", "default");
|
|
2576
|
+
logger.info("Channel get to \"" + ret);
|
|
2577
|
+
callback.callback(ret);
|
|
2597
2578
|
return;
|
|
2598
2579
|
}
|
|
2580
|
+
}
|
|
2599
2581
|
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
JSONObject jsonResponse = new JSONObject(responseData);
|
|
2582
|
+
if (!response.isSuccessful()) {
|
|
2583
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2584
|
+
retError.put("message", "Server error: " + response.code());
|
|
2585
|
+
retError.put("error", "response_error");
|
|
2586
|
+
callback.callback(retError);
|
|
2587
|
+
return;
|
|
2588
|
+
}
|
|
2608
2589
|
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2590
|
+
if (responseData.isEmpty()) {
|
|
2591
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2592
|
+
retError.put("message", "Empty response body");
|
|
2593
|
+
retError.put("error", "no_response_body");
|
|
2594
|
+
callback.callback(retError);
|
|
2595
|
+
return;
|
|
2596
|
+
}
|
|
2597
|
+
JSONObject jsonResponse = new JSONObject(responseData);
|
|
2598
|
+
|
|
2599
|
+
// Check for server-side errors first
|
|
2600
|
+
if (jsonResponse.has("error")) {
|
|
2601
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2602
|
+
retError.put("error", jsonResponse.getString("error"));
|
|
2603
|
+
if (jsonResponse.has("message")) {
|
|
2604
|
+
retError.put("message", jsonResponse.getString("message"));
|
|
2605
|
+
} else {
|
|
2606
|
+
retError.put("message", "server did not provide a message");
|
|
2620
2607
|
}
|
|
2608
|
+
callback.callback(retError);
|
|
2609
|
+
return;
|
|
2610
|
+
}
|
|
2621
2611
|
|
|
2622
|
-
|
|
2612
|
+
Map<String, Object> ret = new HashMap<>();
|
|
2623
2613
|
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
}
|
|
2614
|
+
Iterator<String> keys = jsonResponse.keys();
|
|
2615
|
+
while (keys.hasNext()) {
|
|
2616
|
+
String key = keys.next();
|
|
2617
|
+
if (jsonResponse.has(key)) {
|
|
2618
|
+
ret.put(key, jsonResponse.get(key));
|
|
2630
2619
|
}
|
|
2631
|
-
persistDefaultChannelFromResponse(ret.get("channel"), editor, defaultChannelKey);
|
|
2632
|
-
logger.info("Channel get to \"" + ret);
|
|
2633
|
-
callback.callback(ret);
|
|
2634
|
-
} catch (JSONException e) {
|
|
2635
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2636
|
-
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
2637
|
-
retError.put("error", "parse_error");
|
|
2638
|
-
callback.callback(retError);
|
|
2639
2620
|
}
|
|
2621
|
+
persistDefaultChannelFromResponse(ret.get("channel"), editor, defaultChannelKey);
|
|
2622
|
+
logger.info("Channel get to \"" + ret);
|
|
2623
|
+
callback.callback(ret);
|
|
2624
|
+
} catch (JSONException e) {
|
|
2625
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2626
|
+
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
2627
|
+
retError.put("error", "parse_error");
|
|
2628
|
+
callback.callback(retError);
|
|
2640
2629
|
}
|
|
2641
2630
|
}
|
|
2642
|
-
|
|
2631
|
+
}
|
|
2632
|
+
);
|
|
2643
2633
|
}
|
|
2644
2634
|
|
|
2645
2635
|
void persistDefaultChannelFromResponse(final Object channel, final SharedPreferences.Editor editor, final String defaultChannelKey) {
|
|
@@ -2712,85 +2702,83 @@ public class CapgoUpdater {
|
|
|
2712
2702
|
|
|
2713
2703
|
Request request = new Request.Builder().url(urlBuilder.build()).get().build();
|
|
2714
2704
|
|
|
2715
|
-
DownloadService.sharedClient
|
|
2716
|
-
.
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
callback.callback(retError);
|
|
2725
|
-
}
|
|
2705
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2706
|
+
new okhttp3.Callback() {
|
|
2707
|
+
@Override
|
|
2708
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2709
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2710
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
2711
|
+
retError.put("error", "network_error");
|
|
2712
|
+
callback.callback(retError);
|
|
2713
|
+
}
|
|
2726
2714
|
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2715
|
+
@Override
|
|
2716
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2717
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2718
|
+
final String data = responseBody != null ? responseBody.string() : "";
|
|
2719
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, data);
|
|
2720
|
+
if (rateLimit.blocked) {
|
|
2721
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2722
|
+
retError.put("message", rateLimit.message);
|
|
2723
|
+
retError.put("error", rateLimit.error);
|
|
2724
|
+
callback.callback(retError);
|
|
2725
|
+
return;
|
|
2726
|
+
}
|
|
2739
2727
|
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2728
|
+
if (!response.isSuccessful()) {
|
|
2729
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2730
|
+
retError.put("message", "Server error: " + response.code());
|
|
2731
|
+
retError.put("error", "response_error");
|
|
2732
|
+
callback.callback(retError);
|
|
2733
|
+
return;
|
|
2734
|
+
}
|
|
2747
2735
|
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2736
|
+
if (data.isEmpty()) {
|
|
2737
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2738
|
+
retError.put("message", "Empty response body");
|
|
2739
|
+
retError.put("error", "no_response_body");
|
|
2740
|
+
callback.callback(retError);
|
|
2741
|
+
return;
|
|
2742
|
+
}
|
|
2755
2743
|
|
|
2756
|
-
|
|
2757
|
-
|
|
2744
|
+
try {
|
|
2745
|
+
Map<String, Object> ret = parseListChannelsResponse(data);
|
|
2758
2746
|
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2767
|
-
retError.put("error", json.getString("error"));
|
|
2768
|
-
if (json.has("message")) {
|
|
2769
|
-
retError.put("message", json.getString("message"));
|
|
2770
|
-
} else {
|
|
2771
|
-
retError.put("message", "server did not provide a message");
|
|
2772
|
-
}
|
|
2773
|
-
callback.callback(retError);
|
|
2774
|
-
return;
|
|
2775
|
-
}
|
|
2747
|
+
logger.info("Channels listed successfully");
|
|
2748
|
+
callback.callback(ret);
|
|
2749
|
+
} catch (JSONException arrayException) {
|
|
2750
|
+
// If not an array, try to parse as error object
|
|
2751
|
+
try {
|
|
2752
|
+
JSONObject json = new JSONObject(data);
|
|
2753
|
+
if (json.has("error")) {
|
|
2776
2754
|
Map<String, Object> retError = new HashMap<>();
|
|
2777
|
-
retError.put("
|
|
2778
|
-
|
|
2755
|
+
retError.put("error", json.getString("error"));
|
|
2756
|
+
if (json.has("message")) {
|
|
2757
|
+
retError.put("message", json.getString("message"));
|
|
2758
|
+
} else {
|
|
2759
|
+
retError.put("message", "server did not provide a message");
|
|
2760
|
+
}
|
|
2779
2761
|
callback.callback(retError);
|
|
2780
2762
|
return;
|
|
2781
|
-
} catch (JSONException objException) {
|
|
2782
|
-
// If neither array nor object, throw parse error
|
|
2783
|
-
arrayException.addSuppressed(objException);
|
|
2784
|
-
Map<String, Object> retError = new HashMap<>();
|
|
2785
|
-
retError.put("message", "JSON parse error: " + arrayException.getMessage());
|
|
2786
|
-
retError.put("error", "parse_error");
|
|
2787
|
-
callback.callback(retError);
|
|
2788
2763
|
}
|
|
2764
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2765
|
+
retError.put("message", "Unexpected channels response format");
|
|
2766
|
+
retError.put("error", "parse_error");
|
|
2767
|
+
callback.callback(retError);
|
|
2768
|
+
return;
|
|
2769
|
+
} catch (JSONException objException) {
|
|
2770
|
+
// If neither array nor object, throw parse error
|
|
2771
|
+
arrayException.addSuppressed(objException);
|
|
2772
|
+
Map<String, Object> retError = new HashMap<>();
|
|
2773
|
+
retError.put("message", "JSON parse error: " + arrayException.getMessage());
|
|
2774
|
+
retError.put("error", "parse_error");
|
|
2775
|
+
callback.callback(retError);
|
|
2789
2776
|
}
|
|
2790
2777
|
}
|
|
2791
2778
|
}
|
|
2792
2779
|
}
|
|
2793
|
-
|
|
2780
|
+
}
|
|
2781
|
+
);
|
|
2794
2782
|
}
|
|
2795
2783
|
|
|
2796
2784
|
static Map<String, Object> parseListChannelsResponse(final String data) throws JSONException {
|
|
@@ -3112,67 +3100,65 @@ public class CapgoUpdater {
|
|
|
3112
3100
|
.build();
|
|
3113
3101
|
|
|
3114
3102
|
final int eventCount = eventsToSend.size();
|
|
3115
|
-
DownloadService.sharedClient
|
|
3116
|
-
.
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3103
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
3104
|
+
new okhttp3.Callback() {
|
|
3105
|
+
@Override
|
|
3106
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
3107
|
+
if (abandonStoppedStatsFlush()) {
|
|
3108
|
+
return;
|
|
3109
|
+
}
|
|
3110
|
+
requeueStatsEvents(eventsToSend);
|
|
3111
|
+
if (logger != null) {
|
|
3112
|
+
logger.error("Failed to send stats batch");
|
|
3113
|
+
logger.debug("Error: " + e.getMessage());
|
|
3114
|
+
}
|
|
3115
|
+
statsFlushInFlight.set(false);
|
|
3116
|
+
}
|
|
3117
|
+
|
|
3118
|
+
@Override
|
|
3119
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
3120
|
+
try (ResponseBody responseBody = response.body()) {
|
|
3121
3121
|
if (abandonStoppedStatsFlush()) {
|
|
3122
3122
|
return;
|
|
3123
3123
|
}
|
|
3124
|
-
|
|
3125
|
-
if (
|
|
3126
|
-
|
|
3127
|
-
|
|
3124
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
3125
|
+
if (checkAndHandleRateLimitResponse(response, responseData).blocked) {
|
|
3126
|
+
requeueStatsEvents(eventsToSend);
|
|
3127
|
+
return;
|
|
3128
3128
|
}
|
|
3129
|
-
statsFlushInFlight.set(false);
|
|
3130
|
-
}
|
|
3131
3129
|
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
if (abandonStoppedStatsFlush()) {
|
|
3136
|
-
return;
|
|
3130
|
+
if (response.isSuccessful()) {
|
|
3131
|
+
synchronized (statsQueue) {
|
|
3132
|
+
statsInFlight.clear();
|
|
3137
3133
|
}
|
|
3138
|
-
|
|
3139
|
-
if (
|
|
3140
|
-
|
|
3141
|
-
|
|
3134
|
+
persistStatsQueue();
|
|
3135
|
+
if (logger != null) {
|
|
3136
|
+
logger.info("Stats batch sent successfully");
|
|
3137
|
+
logger.debug("Sent " + eventCount + " events");
|
|
3142
3138
|
}
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
logger.debug("Retrying later, response code: " + response.code());
|
|
3159
|
-
}
|
|
3160
|
-
} else {
|
|
3161
|
-
synchronized (statsQueue) {
|
|
3162
|
-
statsInFlight.clear();
|
|
3163
|
-
}
|
|
3164
|
-
persistStatsQueue();
|
|
3165
|
-
if (logger != null) {
|
|
3166
|
-
logger.error("Dropping stats batch after permanent error");
|
|
3167
|
-
logger.debug("Response code: " + response.code());
|
|
3168
|
-
}
|
|
3139
|
+
runStatsCallbacks(eventsToSend);
|
|
3140
|
+
} else if (isTransientStatsFailure(response.code())) {
|
|
3141
|
+
requeueStatsEvents(eventsToSend);
|
|
3142
|
+
if (logger != null) {
|
|
3143
|
+
logger.error("Error sending stats batch");
|
|
3144
|
+
logger.debug("Retrying later, response code: " + response.code());
|
|
3145
|
+
}
|
|
3146
|
+
} else {
|
|
3147
|
+
synchronized (statsQueue) {
|
|
3148
|
+
statsInFlight.clear();
|
|
3149
|
+
}
|
|
3150
|
+
persistStatsQueue();
|
|
3151
|
+
if (logger != null) {
|
|
3152
|
+
logger.error("Dropping stats batch after permanent error");
|
|
3153
|
+
logger.debug("Response code: " + response.code());
|
|
3169
3154
|
}
|
|
3170
|
-
} finally {
|
|
3171
|
-
statsFlushInFlight.set(false);
|
|
3172
3155
|
}
|
|
3156
|
+
} finally {
|
|
3157
|
+
statsFlushInFlight.set(false);
|
|
3173
3158
|
}
|
|
3174
3159
|
}
|
|
3175
|
-
|
|
3160
|
+
}
|
|
3161
|
+
);
|
|
3176
3162
|
}
|
|
3177
3163
|
|
|
3178
3164
|
private boolean abandonStoppedStatsFlush() {
|