@capgo/capacitor-updater 8.47.5 → 8.47.7
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/README.md +13 -20
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +380 -92
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +279 -259
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +23 -26
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +111 -62
- package/dist/docs.json +5 -5
- package/dist/esm/definitions.d.ts +16 -24
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +343 -46
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +25 -8
- package/package.json +1 -1
|
@@ -82,7 +82,7 @@ public class CapgoUpdater {
|
|
|
82
82
|
public String channelUrl = "";
|
|
83
83
|
public String defaultChannel = "";
|
|
84
84
|
public String appId = "";
|
|
85
|
-
public boolean previewSession = false;
|
|
85
|
+
public volatile boolean previewSession = false;
|
|
86
86
|
public String publicKey = "";
|
|
87
87
|
public String deviceID = "";
|
|
88
88
|
public int timeout = 20000;
|
|
@@ -742,11 +742,15 @@ public class CapgoUpdater {
|
|
|
742
742
|
CapgoUpdater.this.notifyListeners("updateAvailable", ret);
|
|
743
743
|
logger.info("setNext: " + setNext);
|
|
744
744
|
if (setNext) {
|
|
745
|
-
|
|
746
|
-
|
|
745
|
+
if (this.previewSession) {
|
|
746
|
+
logger.info("Preview session is active, skipping automatic install of downloaded bundle");
|
|
747
|
+
this.directUpdate = false;
|
|
748
|
+
} else if (this.directUpdate) {
|
|
749
|
+
logger.info("directUpdate: " + this.directUpdate);
|
|
747
750
|
CapgoUpdater.this.directUpdateFinish(next);
|
|
748
751
|
this.directUpdate = false;
|
|
749
752
|
} else {
|
|
753
|
+
logger.info("directUpdate: " + this.directUpdate);
|
|
750
754
|
this.setNextBundle(next.getId());
|
|
751
755
|
}
|
|
752
756
|
}
|
|
@@ -1187,12 +1191,10 @@ public class CapgoUpdater {
|
|
|
1187
1191
|
}
|
|
1188
1192
|
|
|
1189
1193
|
void restoreResetState(final ResetState state) {
|
|
1190
|
-
final String currentBundlePath =
|
|
1191
|
-
? "public"
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
? BundleInfo.ID_BUILTIN
|
|
1195
|
-
: state.fallbackBundleId;
|
|
1194
|
+
final String currentBundlePath =
|
|
1195
|
+
state.currentBundlePath == null || state.currentBundlePath.trim().isEmpty() ? "public" : state.currentBundlePath;
|
|
1196
|
+
final String fallbackBundleId =
|
|
1197
|
+
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
1196
1198
|
|
|
1197
1199
|
this.editor.putString(this.CAP_SERVER_PATH, currentBundlePath);
|
|
1198
1200
|
this.editor.putString(FALLBACK_VERSION, fallbackBundleId);
|
|
@@ -1459,107 +1461,105 @@ public class CapgoUpdater {
|
|
|
1459
1461
|
|
|
1460
1462
|
Request request = new Request.Builder().url(url).post(body).build();
|
|
1461
1463
|
|
|
1462
|
-
DownloadService.sharedClient
|
|
1463
|
-
.
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
callback.callback(retError);
|
|
1473
|
-
}
|
|
1464
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
1465
|
+
new okhttp3.Callback() {
|
|
1466
|
+
@Override
|
|
1467
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
1468
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1469
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
1470
|
+
retError.put("error", "network_error");
|
|
1471
|
+
retError.put("kind", "failed");
|
|
1472
|
+
callback.callback(retError);
|
|
1473
|
+
}
|
|
1474
1474
|
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
}
|
|
1475
|
+
@Override
|
|
1476
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
1477
|
+
try (ResponseBody responseBody = response.body()) {
|
|
1478
|
+
final int statusCode = response.code();
|
|
1479
|
+
final String responseData = responseBody != null ? responseBody.string() : "";
|
|
1480
|
+
JSONObject jsonResponse = null;
|
|
1481
|
+
if (!responseData.isEmpty()) {
|
|
1482
|
+
try {
|
|
1483
|
+
jsonResponse = new JSONObject(responseData);
|
|
1484
|
+
} catch (JSONException ignored) {
|
|
1485
|
+
// Non-JSON responses are handled as response or parse errors below.
|
|
1487
1486
|
}
|
|
1487
|
+
}
|
|
1488
1488
|
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
}
|
|
1493
|
-
Map<String, Object> retError = new HashMap<>();
|
|
1494
|
-
if (jsonResponse.has("error") && !jsonResponse.isNull("error")) {
|
|
1495
|
-
retError.put("error", jsonResponse.getString("error"));
|
|
1496
|
-
}
|
|
1497
|
-
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
1498
|
-
retError.put("kind", jsonResponse.getString("kind"));
|
|
1499
|
-
}
|
|
1500
|
-
if (jsonResponse.has("message") && !jsonResponse.isNull("message")) {
|
|
1501
|
-
retError.put("message", jsonResponse.getString("message"));
|
|
1502
|
-
} else {
|
|
1503
|
-
retError.put("message", "server did not provide a message");
|
|
1504
|
-
}
|
|
1505
|
-
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
1506
|
-
retError.put("version", jsonResponse.getString("version"));
|
|
1507
|
-
}
|
|
1508
|
-
retError.put("statusCode", statusCode);
|
|
1509
|
-
callback.callback(retError);
|
|
1510
|
-
return;
|
|
1489
|
+
if (jsonResponse != null && (jsonResponse.has("error") || jsonResponse.has("kind"))) {
|
|
1490
|
+
if (statusCode == 429) {
|
|
1491
|
+
checkAndHandleRateLimitResponse(response);
|
|
1511
1492
|
}
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
Map<String, Object> retError = new HashMap<>();
|
|
1516
|
-
retError.put("message", "Rate limit exceeded");
|
|
1517
|
-
retError.put("error", "rate_limit_exceeded");
|
|
1518
|
-
retError.put("kind", "failed");
|
|
1519
|
-
retError.put("statusCode", statusCode);
|
|
1520
|
-
callback.callback(retError);
|
|
1521
|
-
return;
|
|
1493
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1494
|
+
if (jsonResponse.has("error") && !jsonResponse.isNull("error")) {
|
|
1495
|
+
retError.put("error", jsonResponse.getString("error"));
|
|
1522
1496
|
}
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
Map<String, Object> retError = new HashMap<>();
|
|
1526
|
-
retError.put("message", "Server error: " + response.code());
|
|
1527
|
-
retError.put("error", "response_error");
|
|
1528
|
-
retError.put("kind", "failed");
|
|
1529
|
-
retError.put("statusCode", statusCode);
|
|
1530
|
-
callback.callback(retError);
|
|
1531
|
-
return;
|
|
1497
|
+
if (jsonResponse.has("kind") && !jsonResponse.isNull("kind")) {
|
|
1498
|
+
retError.put("kind", jsonResponse.getString("kind"));
|
|
1532
1499
|
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1500
|
+
if (jsonResponse.has("message") && !jsonResponse.isNull("message")) {
|
|
1501
|
+
retError.put("message", jsonResponse.getString("message"));
|
|
1502
|
+
} else {
|
|
1503
|
+
retError.put("message", "server did not provide a message");
|
|
1536
1504
|
}
|
|
1505
|
+
if (jsonResponse.has("version") && !jsonResponse.isNull("version")) {
|
|
1506
|
+
retError.put("version", jsonResponse.getString("version"));
|
|
1507
|
+
}
|
|
1508
|
+
retError.put("statusCode", statusCode);
|
|
1509
|
+
callback.callback(retError);
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1537
1512
|
|
|
1538
|
-
|
|
1539
|
-
|
|
1513
|
+
// Check for 429 rate limit
|
|
1514
|
+
if (checkAndHandleRateLimitResponse(response)) {
|
|
1515
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1516
|
+
retError.put("message", "Rate limit exceeded");
|
|
1517
|
+
retError.put("error", "rate_limit_exceeded");
|
|
1518
|
+
retError.put("kind", "failed");
|
|
1519
|
+
retError.put("statusCode", statusCode);
|
|
1520
|
+
callback.callback(retError);
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1540
1523
|
|
|
1541
|
-
|
|
1542
|
-
while (keys.hasNext()) {
|
|
1543
|
-
String key = keys.next();
|
|
1544
|
-
if (jsonResponse.has(key)) {
|
|
1545
|
-
if ("session_key".equals(key)) {
|
|
1546
|
-
ret.put("sessionKey", jsonResponse.get(key));
|
|
1547
|
-
} else {
|
|
1548
|
-
ret.put(key, jsonResponse.get(key));
|
|
1549
|
-
}
|
|
1550
|
-
}
|
|
1551
|
-
}
|
|
1552
|
-
callback.callback(ret);
|
|
1553
|
-
} catch (JSONException e) {
|
|
1524
|
+
if (!response.isSuccessful()) {
|
|
1554
1525
|
Map<String, Object> retError = new HashMap<>();
|
|
1555
|
-
retError.put("message", "
|
|
1556
|
-
retError.put("error", "
|
|
1526
|
+
retError.put("message", "Server error: " + response.code());
|
|
1527
|
+
retError.put("error", "response_error");
|
|
1557
1528
|
retError.put("kind", "failed");
|
|
1529
|
+
retError.put("statusCode", statusCode);
|
|
1558
1530
|
callback.callback(retError);
|
|
1531
|
+
return;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
if (jsonResponse == null) {
|
|
1535
|
+
throw new JSONException("Response is not a JSON object");
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
Map<String, Object> ret = new HashMap<>();
|
|
1539
|
+
ret.put("statusCode", statusCode);
|
|
1540
|
+
|
|
1541
|
+
Iterator<String> keys = jsonResponse.keys();
|
|
1542
|
+
while (keys.hasNext()) {
|
|
1543
|
+
String key = keys.next();
|
|
1544
|
+
if (jsonResponse.has(key)) {
|
|
1545
|
+
if ("session_key".equals(key)) {
|
|
1546
|
+
ret.put("sessionKey", jsonResponse.get(key));
|
|
1547
|
+
} else {
|
|
1548
|
+
ret.put(key, jsonResponse.get(key));
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1559
1551
|
}
|
|
1552
|
+
callback.callback(ret);
|
|
1553
|
+
} catch (JSONException e) {
|
|
1554
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1555
|
+
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
1556
|
+
retError.put("error", "parse_error");
|
|
1557
|
+
retError.put("kind", "failed");
|
|
1558
|
+
callback.callback(retError);
|
|
1560
1559
|
}
|
|
1561
1560
|
}
|
|
1562
|
-
|
|
1561
|
+
}
|
|
1562
|
+
);
|
|
1563
1563
|
}
|
|
1564
1564
|
|
|
1565
1565
|
public void getLatest(final String updateUrl, final String channel, final Callback callback) {
|
|
@@ -1717,88 +1717,98 @@ public class CapgoUpdater {
|
|
|
1717
1717
|
.put(RequestBody.create(json.toString(), MediaType.get("application/json")))
|
|
1718
1718
|
.build();
|
|
1719
1719
|
|
|
1720
|
-
DownloadService.sharedClient
|
|
1721
|
-
.
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1720
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
1721
|
+
new okhttp3.Callback() {
|
|
1722
|
+
@Override
|
|
1723
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
1724
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1725
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
1726
|
+
retError.put("error", "network_error");
|
|
1727
|
+
callback.callback(retError);
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
@Override
|
|
1731
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
1732
|
+
try (ResponseBody responseBody = response.body()) {
|
|
1733
|
+
// Check for 429 rate limit
|
|
1734
|
+
if (checkAndHandleRateLimitResponse(response)) {
|
|
1735
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1736
|
+
retError.put("message", "Rate limit exceeded");
|
|
1737
|
+
retError.put("error", "rate_limit_exceeded");
|
|
1738
|
+
callback.callback(retError);
|
|
1739
|
+
return;
|
|
1740
|
+
}
|
|
1731
1741
|
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
try (ResponseBody responseBody = response.body()) {
|
|
1735
|
-
// Check for 429 rate limit
|
|
1736
|
-
if (checkAndHandleRateLimitResponse(response)) {
|
|
1742
|
+
if (response.code() == 400) {
|
|
1743
|
+
if (responseBody == null) {
|
|
1737
1744
|
Map<String, Object> retError = new HashMap<>();
|
|
1738
|
-
retError.put("message", "
|
|
1739
|
-
retError.put("error", "
|
|
1745
|
+
retError.put("message", "Empty response body");
|
|
1746
|
+
retError.put("error", "no_response_body");
|
|
1740
1747
|
callback.callback(retError);
|
|
1741
1748
|
return;
|
|
1742
1749
|
}
|
|
1743
|
-
|
|
1744
|
-
if (
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
ret.put("status", "default");
|
|
1751
|
-
logger.info("Channel get to \"" + ret);
|
|
1752
|
-
callback.callback(ret);
|
|
1753
|
-
return;
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
|
|
1757
|
-
if (!response.isSuccessful()) {
|
|
1758
|
-
Map<String, Object> retError = new HashMap<>();
|
|
1759
|
-
retError.put("message", "Server error: " + response.code());
|
|
1760
|
-
retError.put("error", "response_error");
|
|
1761
|
-
callback.callback(retError);
|
|
1750
|
+
String data = responseBody.string();
|
|
1751
|
+
if (data.contains("channel_not_found") && !defaultChannel.isEmpty()) {
|
|
1752
|
+
Map<String, Object> ret = new HashMap<>();
|
|
1753
|
+
ret.put("channel", defaultChannel);
|
|
1754
|
+
ret.put("status", "default");
|
|
1755
|
+
logger.info("Channel get to \"" + ret);
|
|
1756
|
+
callback.callback(ret);
|
|
1762
1757
|
return;
|
|
1763
1758
|
}
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
if (!response.isSuccessful()) {
|
|
1762
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1763
|
+
retError.put("message", "Server error: " + response.code());
|
|
1764
|
+
retError.put("error", "response_error");
|
|
1765
|
+
callback.callback(retError);
|
|
1766
|
+
return;
|
|
1767
|
+
}
|
|
1764
1768
|
|
|
1765
|
-
|
|
1766
|
-
String
|
|
1767
|
-
|
|
1769
|
+
if (responseBody == null) {
|
|
1770
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1771
|
+
retError.put("message", "Empty response body");
|
|
1772
|
+
retError.put("error", "no_response_body");
|
|
1773
|
+
callback.callback(retError);
|
|
1774
|
+
return;
|
|
1775
|
+
}
|
|
1776
|
+
String responseData = responseBody.string();
|
|
1777
|
+
JSONObject jsonResponse = new JSONObject(responseData);
|
|
1768
1778
|
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
}
|
|
1778
|
-
callback.callback(retError);
|
|
1779
|
-
return;
|
|
1779
|
+
// Check for server-side errors first
|
|
1780
|
+
if (jsonResponse.has("error")) {
|
|
1781
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1782
|
+
retError.put("error", jsonResponse.getString("error"));
|
|
1783
|
+
if (jsonResponse.has("message")) {
|
|
1784
|
+
retError.put("message", jsonResponse.getString("message"));
|
|
1785
|
+
} else {
|
|
1786
|
+
retError.put("message", "server did not provide a message");
|
|
1780
1787
|
}
|
|
1788
|
+
callback.callback(retError);
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1781
1791
|
|
|
1782
|
-
|
|
1792
|
+
Map<String, Object> ret = new HashMap<>();
|
|
1783
1793
|
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
}
|
|
1794
|
+
Iterator<String> keys = jsonResponse.keys();
|
|
1795
|
+
while (keys.hasNext()) {
|
|
1796
|
+
String key = keys.next();
|
|
1797
|
+
if (jsonResponse.has(key)) {
|
|
1798
|
+
ret.put(key, jsonResponse.get(key));
|
|
1790
1799
|
}
|
|
1791
|
-
logger.info("Channel get to \"" + ret);
|
|
1792
|
-
callback.callback(ret);
|
|
1793
|
-
} catch (JSONException e) {
|
|
1794
|
-
Map<String, Object> retError = new HashMap<>();
|
|
1795
|
-
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
1796
|
-
retError.put("error", "parse_error");
|
|
1797
|
-
callback.callback(retError);
|
|
1798
1800
|
}
|
|
1801
|
+
logger.info("Channel get to \"" + ret);
|
|
1802
|
+
callback.callback(ret);
|
|
1803
|
+
} catch (JSONException e) {
|
|
1804
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1805
|
+
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
1806
|
+
retError.put("error", "parse_error");
|
|
1807
|
+
callback.callback(retError);
|
|
1799
1808
|
}
|
|
1800
1809
|
}
|
|
1801
|
-
|
|
1810
|
+
}
|
|
1811
|
+
);
|
|
1802
1812
|
}
|
|
1803
1813
|
|
|
1804
1814
|
public void listChannels(final Callback callback) {
|
|
@@ -1853,94 +1863,106 @@ public class CapgoUpdater {
|
|
|
1853
1863
|
|
|
1854
1864
|
Request request = new Request.Builder().url(urlBuilder.build()).get().build();
|
|
1855
1865
|
|
|
1856
|
-
DownloadService.sharedClient
|
|
1857
|
-
.
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
callback.callback(retError);
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
@Override
|
|
1869
|
-
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
1870
|
-
try (ResponseBody responseBody = response.body()) {
|
|
1871
|
-
// Check for 429 rate limit
|
|
1872
|
-
if (checkAndHandleRateLimitResponse(response)) {
|
|
1873
|
-
Map<String, Object> retError = new HashMap<>();
|
|
1874
|
-
retError.put("message", "Rate limit exceeded");
|
|
1875
|
-
retError.put("error", "rate_limit_exceeded");
|
|
1876
|
-
callback.callback(retError);
|
|
1877
|
-
return;
|
|
1878
|
-
}
|
|
1866
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
1867
|
+
new okhttp3.Callback() {
|
|
1868
|
+
@Override
|
|
1869
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
1870
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1871
|
+
retError.put("message", "Request failed: " + e.getMessage());
|
|
1872
|
+
retError.put("error", "network_error");
|
|
1873
|
+
callback.callback(retError);
|
|
1874
|
+
}
|
|
1879
1875
|
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1876
|
+
@Override
|
|
1877
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
1878
|
+
try (ResponseBody responseBody = response.body()) {
|
|
1879
|
+
// Check for 429 rate limit
|
|
1880
|
+
if (checkAndHandleRateLimitResponse(response)) {
|
|
1881
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1882
|
+
retError.put("message", "Rate limit exceeded");
|
|
1883
|
+
retError.put("error", "rate_limit_exceeded");
|
|
1884
|
+
callback.callback(retError);
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
1887
|
|
|
1888
|
-
|
|
1889
|
-
String
|
|
1888
|
+
if (!response.isSuccessful()) {
|
|
1889
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1890
|
+
retError.put("message", "Server error: " + response.code());
|
|
1891
|
+
retError.put("error", "response_error");
|
|
1892
|
+
callback.callback(retError);
|
|
1893
|
+
return;
|
|
1894
|
+
}
|
|
1890
1895
|
|
|
1891
|
-
|
|
1892
|
-
|
|
1896
|
+
if (responseBody == null) {
|
|
1897
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1898
|
+
retError.put("message", "Empty response body");
|
|
1899
|
+
retError.put("error", "no_response_body");
|
|
1900
|
+
callback.callback(retError);
|
|
1901
|
+
return;
|
|
1902
|
+
}
|
|
1903
|
+
String data = responseBody.string();
|
|
1893
1904
|
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
JSONArray channelsJson = new JSONArray(data);
|
|
1897
|
-
List<Map<String, Object>> channelsList = new ArrayList<>();
|
|
1898
|
-
|
|
1899
|
-
for (int i = 0; i < channelsJson.length(); i++) {
|
|
1900
|
-
JSONObject channelJson = channelsJson.getJSONObject(i);
|
|
1901
|
-
Map<String, Object> channel = new HashMap<>();
|
|
1902
|
-
channel.put("id", channelJson.optString("id", ""));
|
|
1903
|
-
channel.put("name", channelJson.optString("name", ""));
|
|
1904
|
-
channel.put("public", channelJson.optBoolean("public", false));
|
|
1905
|
-
channel.put("allow_self_set", channelJson.optBoolean("allow_self_set", false));
|
|
1906
|
-
channelsList.add(channel);
|
|
1907
|
-
}
|
|
1905
|
+
try {
|
|
1906
|
+
Map<String, Object> ret = parseListChannelsResponse(data);
|
|
1908
1907
|
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
retError.put("message", json.getString("message"));
|
|
1923
|
-
} else {
|
|
1924
|
-
retError.put("message", "server did not provide a message");
|
|
1925
|
-
}
|
|
1926
|
-
callback.callback(retError);
|
|
1927
|
-
return;
|
|
1928
|
-
}
|
|
1929
|
-
} catch (JSONException objException) {
|
|
1930
|
-
// If neither array nor object, throw parse error
|
|
1931
|
-
throw arrayException;
|
|
1908
|
+
logger.info("Channels listed successfully");
|
|
1909
|
+
callback.callback(ret);
|
|
1910
|
+
} catch (JSONException arrayException) {
|
|
1911
|
+
// If not an array, try to parse as error object
|
|
1912
|
+
try {
|
|
1913
|
+
JSONObject json = new JSONObject(data);
|
|
1914
|
+
if (json.has("error")) {
|
|
1915
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1916
|
+
retError.put("error", json.getString("error"));
|
|
1917
|
+
if (json.has("message")) {
|
|
1918
|
+
retError.put("message", json.getString("message"));
|
|
1919
|
+
} else {
|
|
1920
|
+
retError.put("message", "server did not provide a message");
|
|
1932
1921
|
}
|
|
1922
|
+
callback.callback(retError);
|
|
1923
|
+
return;
|
|
1933
1924
|
}
|
|
1934
|
-
} catch (JSONException e) {
|
|
1935
1925
|
Map<String, Object> retError = new HashMap<>();
|
|
1936
|
-
retError.put("message", "
|
|
1926
|
+
retError.put("message", "Unexpected channels response format");
|
|
1927
|
+
retError.put("error", "parse_error");
|
|
1928
|
+
callback.callback(retError);
|
|
1929
|
+
return;
|
|
1930
|
+
} catch (JSONException objException) {
|
|
1931
|
+
// If neither array nor object, throw parse error
|
|
1932
|
+
arrayException.addSuppressed(objException);
|
|
1933
|
+
Map<String, Object> retError = new HashMap<>();
|
|
1934
|
+
retError.put("message", "JSON parse error: " + arrayException.getMessage());
|
|
1937
1935
|
retError.put("error", "parse_error");
|
|
1938
1936
|
callback.callback(retError);
|
|
1939
1937
|
}
|
|
1940
1938
|
}
|
|
1941
1939
|
}
|
|
1942
1940
|
}
|
|
1943
|
-
|
|
1941
|
+
}
|
|
1942
|
+
);
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
static Map<String, Object> parseListChannelsResponse(final String data) throws JSONException {
|
|
1946
|
+
JSONArray channelsJson = new JSONArray(data);
|
|
1947
|
+
List<Map<String, Object>> channelsList = new ArrayList<>();
|
|
1948
|
+
|
|
1949
|
+
for (int i = 0; i < channelsJson.length(); i++) {
|
|
1950
|
+
JSONObject channelJson = channelsJson.getJSONObject(i);
|
|
1951
|
+
Object channelId = channelJson.get("id");
|
|
1952
|
+
if (!(channelId instanceof Number)) {
|
|
1953
|
+
throw new JSONException("Channel id must be a number");
|
|
1954
|
+
}
|
|
1955
|
+
Map<String, Object> channel = new HashMap<>();
|
|
1956
|
+
channel.put("id", channelId);
|
|
1957
|
+
channel.put("name", channelJson.optString("name", ""));
|
|
1958
|
+
channel.put("public", channelJson.optBoolean("public", false));
|
|
1959
|
+
channel.put("allow_self_set", channelJson.optBoolean("allow_self_set", false));
|
|
1960
|
+
channelsList.add(channel);
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
Map<String, Object> ret = new HashMap<>();
|
|
1964
|
+
ret.put("channels", channelsList);
|
|
1965
|
+
return ret;
|
|
1944
1966
|
}
|
|
1945
1967
|
|
|
1946
1968
|
public void sendStats(final String action) {
|
|
@@ -2037,35 +2059,33 @@ public class CapgoUpdater {
|
|
|
2037
2059
|
.build();
|
|
2038
2060
|
|
|
2039
2061
|
final int eventCount = eventsToSend.size();
|
|
2040
|
-
DownloadService.sharedClient
|
|
2041
|
-
.
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
logger.debug("Error: " + e.getMessage());
|
|
2048
|
-
}
|
|
2062
|
+
DownloadService.sharedClient.newCall(request).enqueue(
|
|
2063
|
+
new okhttp3.Callback() {
|
|
2064
|
+
@Override
|
|
2065
|
+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
2066
|
+
logger.error("Failed to send stats batch");
|
|
2067
|
+
logger.debug("Error: " + e.getMessage());
|
|
2068
|
+
}
|
|
2049
2069
|
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2070
|
+
@Override
|
|
2071
|
+
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
2072
|
+
try (ResponseBody responseBody = response.body()) {
|
|
2073
|
+
// Check for 429 rate limit
|
|
2074
|
+
if (checkAndHandleRateLimitResponse(response)) {
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2057
2077
|
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
}
|
|
2078
|
+
if (response.isSuccessful()) {
|
|
2079
|
+
logger.info("Stats batch sent successfully");
|
|
2080
|
+
logger.debug("Sent " + eventCount + " events");
|
|
2081
|
+
} else {
|
|
2082
|
+
logger.error("Error sending stats batch");
|
|
2083
|
+
logger.debug("Response code: " + response.code());
|
|
2065
2084
|
}
|
|
2066
2085
|
}
|
|
2067
2086
|
}
|
|
2068
|
-
|
|
2087
|
+
}
|
|
2088
|
+
);
|
|
2069
2089
|
}
|
|
2070
2090
|
|
|
2071
2091
|
public BundleInfo getBundleInfo(final String id) {
|