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