@capgo/capacitor-updater 7.2.21 → 7.4.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.
@@ -1,7 +1,6 @@
1
1
  package ee.forgr.capacitor_updater;
2
2
 
3
3
  import android.content.SharedPreferences;
4
- import android.util.Log;
5
4
  import com.google.common.reflect.TypeToken;
6
5
  import com.google.gson.Gson;
7
6
  import io.github.g00fy2.versioncompare.Version;
@@ -12,6 +11,8 @@ import java.util.Date;
12
11
 
13
12
  public class DelayUpdateUtils {
14
13
 
14
+ private final Logger logger;
15
+
15
16
  public static final String DELAY_CONDITION_PREFERENCES = "DELAY_CONDITION_PREFERENCES_CAPGO";
16
17
  public static final String BACKGROUND_TIMESTAMP_KEY = "BACKGROUND_TIMESTAMP_KEY_CAPGO";
17
18
 
@@ -20,11 +21,18 @@ public class DelayUpdateUtils {
20
21
  private final Version currentVersionNative;
21
22
  private final Runnable installNext;
22
23
 
23
- public DelayUpdateUtils(SharedPreferences prefs, SharedPreferences.Editor editor, Version currentVersionNative, Runnable installNext) {
24
+ public DelayUpdateUtils(
25
+ SharedPreferences prefs,
26
+ SharedPreferences.Editor editor,
27
+ Version currentVersionNative,
28
+ Runnable installNext,
29
+ Logger logger
30
+ ) {
24
31
  this.prefs = prefs;
25
32
  this.editor = editor;
26
33
  this.currentVersionNative = currentVersionNative;
27
34
  this.installNext = installNext;
35
+ this.logger = logger;
28
36
  }
29
37
 
30
38
  public enum CancelDelaySource {
@@ -54,8 +62,7 @@ public class DelayUpdateUtils {
54
62
  try {
55
63
  longValue = Long.parseLong(value);
56
64
  } catch (NumberFormatException e) {
57
- Log.e(
58
- CapgoUpdater.TAG,
65
+ logger.error(
59
66
  "Background condition (value: " +
60
67
  value +
61
68
  ") had an invalid value at index " +
@@ -65,8 +72,7 @@ public class DelayUpdateUtils {
65
72
  }
66
73
 
67
74
  if (delta > longValue) {
68
- Log.i(
69
- CapgoUpdater.TAG,
75
+ logger.info(
70
76
  "Background condition (value: " +
71
77
  value +
72
78
  ") deleted at index " +
@@ -79,8 +85,7 @@ public class DelayUpdateUtils {
79
85
  }
80
86
  } else {
81
87
  delayConditionListToKeep.add(condition);
82
- Log.i(
83
- CapgoUpdater.TAG,
88
+ logger.info(
84
89
  "Background delay (value: " +
85
90
  value +
86
91
  ") condition kept at index " +
@@ -96,8 +101,7 @@ public class DelayUpdateUtils {
96
101
  this.installNext.run();
97
102
  } else {
98
103
  delayConditionListToKeep.add(condition);
99
- Log.i(
100
- CapgoUpdater.TAG,
104
+ logger.info(
101
105
  "Kill delay (value: " + value + ") condition kept at index " + index + " (source: " + source.toString() + ")"
102
106
  );
103
107
  }
@@ -109,23 +113,23 @@ public class DelayUpdateUtils {
109
113
  Date date = sdf.parse(value);
110
114
  assert date != null;
111
115
  if (new Date().compareTo(date) > 0) {
112
- Log.i(
113
- CapgoUpdater.TAG,
114
- "Date delay (value: " + value + ") condition removed due to expired date at index " + index
115
- );
116
+ logger.info("Date delay (value: " + value + ") condition removed due to expired date at index " + index);
116
117
  } else {
117
118
  delayConditionListToKeep.add(condition);
118
- Log.i(CapgoUpdater.TAG, "Date delay (value: " + value + ") condition kept at index " + index);
119
+ logger.info("Date delay (value: " + value + ") condition kept at index " + index);
119
120
  }
120
121
  } catch (final Exception e) {
121
- Log.e(
122
- CapgoUpdater.TAG,
123
- "Date delay (value: " + value + ") condition removed due to parsing issue at index " + index,
124
- e
122
+ logger.error(
123
+ "Date delay (value: " +
124
+ value +
125
+ ") condition removed due to parsing issue at index " +
126
+ index +
127
+ " " +
128
+ e.getMessage()
125
129
  );
126
130
  }
127
131
  } else {
128
- Log.d(CapgoUpdater.TAG, "Date delay (value: " + value + ") condition removed due to empty value at index " + index);
132
+ logger.debug("Date delay (value: " + value + ") condition removed due to empty value at index " + index);
129
133
  }
130
134
  break;
131
135
  case DelayUntilNext.nativeVersion:
@@ -133,26 +137,25 @@ public class DelayUpdateUtils {
133
137
  try {
134
138
  final Version versionLimit = new Version(value);
135
139
  if (this.currentVersionNative.isAtLeast(versionLimit)) {
136
- Log.i(
137
- CapgoUpdater.TAG,
140
+ logger.info(
138
141
  "Native version delay (value: " + value + ") condition removed due to above limit at index " + index
139
142
  );
140
143
  } else {
141
144
  delayConditionListToKeep.add(condition);
142
- Log.i(CapgoUpdater.TAG, "Native version delay (value: " + value + ") condition kept at index " + index);
145
+ logger.info("Native version delay (value: " + value + ") condition kept at index " + index);
143
146
  }
144
147
  } catch (final Exception e) {
145
- Log.e(
146
- CapgoUpdater.TAG,
147
- "Native version delay (value: " + value + ") condition removed due to parsing issue at index " + index,
148
- e
148
+ logger.error(
149
+ "Native version delay (value: " +
150
+ value +
151
+ ") condition removed due to parsing issue at index " +
152
+ index +
153
+ " " +
154
+ e.getMessage()
149
155
  );
150
156
  }
151
157
  } else {
152
- Log.d(
153
- CapgoUpdater.TAG,
154
- "Native version delay (value: " + value + ") condition removed due to empty value at index " + index
155
- );
158
+ logger.debug("Native version delay (value: " + value + ") condition removed due to empty value at index " + index);
156
159
  }
157
160
  break;
158
161
  }
@@ -168,10 +171,10 @@ public class DelayUpdateUtils {
168
171
  try {
169
172
  this.editor.putString(DELAY_CONDITION_PREFERENCES, delayConditions);
170
173
  this.editor.commit();
171
- Log.i(CapgoUpdater.TAG, "Delay update saved");
174
+ logger.info("Delay update saved");
172
175
  return true;
173
176
  } catch (final Exception e) {
174
- Log.e(CapgoUpdater.TAG, "Failed to delay update, [Error calling '_setMultiDelay()']", e);
177
+ logger.error("Failed to delay update, [Error calling '_setMultiDelay()'] " + e.getMessage());
175
178
  return false;
176
179
  }
177
180
  }
@@ -180,9 +183,9 @@ public class DelayUpdateUtils {
180
183
  try {
181
184
  this.editor.putLong(BACKGROUND_TIMESTAMP_KEY, backgroundTimestamp);
182
185
  this.editor.commit();
183
- Log.i(CapgoUpdater.TAG, "Delay update saved");
186
+ logger.info("Delay update saved");
184
187
  } catch (final Exception e) {
185
- Log.e(CapgoUpdater.TAG, "Failed to delay update, [Error calling '_setBackgroundTimestamp()']", e);
188
+ logger.error("Failed to delay update, [Error calling '_setBackgroundTimestamp()'] " + e.getMessage());
186
189
  }
187
190
  }
188
191
 
@@ -190,9 +193,9 @@ public class DelayUpdateUtils {
190
193
  try {
191
194
  this.editor.remove(BACKGROUND_TIMESTAMP_KEY);
192
195
  this.editor.commit();
193
- Log.i(CapgoUpdater.TAG, "Delay update saved");
196
+ logger.info("Delay update saved");
194
197
  } catch (final Exception e) {
195
- Log.e(CapgoUpdater.TAG, "Failed to delay update, [Error calling '_unsetBackgroundTimestamp()']", e);
198
+ logger.error("Failed to delay update, [Error calling '_unsetBackgroundTimestamp()'] " + e.getMessage());
196
199
  }
197
200
  }
198
201
 
@@ -200,7 +203,7 @@ public class DelayUpdateUtils {
200
203
  try {
201
204
  return this.prefs.getLong(BACKGROUND_TIMESTAMP_KEY, 0);
202
205
  } catch (final Exception e) {
203
- Log.e(CapgoUpdater.TAG, "Failed to delay update, [Error calling '_getBackgroundTimestamp()']", e);
206
+ logger.error("Failed to delay update, [Error calling '_getBackgroundTimestamp()'] " + e.getMessage());
204
207
  return 0;
205
208
  }
206
209
  }
@@ -209,10 +212,10 @@ public class DelayUpdateUtils {
209
212
  try {
210
213
  this.editor.remove(DELAY_CONDITION_PREFERENCES);
211
214
  this.editor.commit();
212
- Log.i(CapgoUpdater.TAG, "All delays canceled from " + source);
215
+ logger.info("All delays canceled from " + source);
213
216
  return true;
214
217
  } catch (final Exception e) {
215
- Log.e(CapgoUpdater.TAG, "Failed to cancel update delay", e);
218
+ logger.error("Failed to cancel update delay " + e.getMessage());
216
219
  return false;
217
220
  }
218
221
  }
@@ -6,7 +6,6 @@
6
6
  package ee.forgr.capacitor_updater;
7
7
 
8
8
  import android.content.Context;
9
- import android.util.Log;
10
9
  import androidx.annotation.NonNull;
11
10
  import androidx.work.Data;
12
11
  import androidx.work.Worker;
@@ -39,7 +38,12 @@ import org.json.JSONObject;
39
38
 
40
39
  public class DownloadService extends Worker {
41
40
 
42
- public static final String TAG = "Capacitor-updater";
41
+ private static Logger logger;
42
+
43
+ public static void setLogger(Logger loggerInstance) {
44
+ logger = loggerInstance;
45
+ }
46
+
43
47
  public static final String URL = "URL";
44
48
  public static final String ID = "id";
45
49
  public static final String PERCENT = "percent";
@@ -94,7 +98,7 @@ public class DownloadService extends Worker {
94
98
  String publicKey = getInputData().getString(PUBLIC_KEY);
95
99
  boolean isManifest = getInputData().getBoolean(IS_MANIFEST, false);
96
100
 
97
- Log.d(TAG, "doWork isManifest: " + isManifest);
101
+ logger.debug("doWork isManifest: " + isManifest);
98
102
 
99
103
  if (isManifest) {
100
104
  JSONArray manifest = DataManager.getInstance().getAndClearManifest();
@@ -102,7 +106,7 @@ public class DownloadService extends Worker {
102
106
  handleManifestDownload(id, documentsDir, dest, version, sessionKey, publicKey, manifest.toString());
103
107
  return createSuccessResult(dest, version, sessionKey, checksum, true);
104
108
  } else {
105
- Log.e(TAG, "Manifest is null");
109
+ logger.error("Manifest is null");
106
110
  return createFailureResult("Manifest is null");
107
111
  }
108
112
  } else {
@@ -134,7 +138,7 @@ public class DownloadService extends Worker {
134
138
  String manifestString
135
139
  ) {
136
140
  try {
137
- Log.d(TAG, "handleManifestDownload");
141
+ logger.debug("handleManifestDownload");
138
142
  JSONArray manifest = new JSONArray(manifestString);
139
143
  File destFolder = new File(documentsDir, dest);
140
144
  File cacheFolder = new File(getApplicationContext().getCacheDir(), "capgo_downloads");
@@ -167,7 +171,7 @@ public class DownloadService extends Worker {
167
171
  try {
168
172
  fileHash = CryptoCipherV2.decryptChecksum(fileHash, publicKey);
169
173
  } catch (Exception e) {
170
- Log.e(TAG, "Error decrypting checksum for " + fileName + "fileHash: " + fileHash);
174
+ logger.error("Error decrypting checksum for " + fileName + "fileHash: " + fileHash);
171
175
  hasError.set(true);
172
176
  continue;
173
177
  }
@@ -180,7 +184,7 @@ public class DownloadService extends Worker {
180
184
 
181
185
  // Ensure parent directories of the target file exist
182
186
  if (!Objects.requireNonNull(targetFile.getParentFile()).exists() && !targetFile.getParentFile().mkdirs()) {
183
- Log.e(TAG, "Failed to create parent directory for: " + targetFile.getAbsolutePath());
187
+ logger.error("Failed to create parent directory for: " + targetFile.getAbsolutePath());
184
188
  hasError.set(true);
185
189
  continue;
186
190
  }
@@ -189,10 +193,10 @@ public class DownloadService extends Worker {
189
193
  try {
190
194
  if (builtinFile.exists() && verifyChecksum(builtinFile, finalFileHash)) {
191
195
  copyFile(builtinFile, targetFile);
192
- Log.d(TAG, "using builtin file " + fileName);
196
+ logger.debug("using builtin file " + fileName);
193
197
  } else if (cacheFile.exists() && verifyChecksum(cacheFile, finalFileHash)) {
194
198
  copyFile(cacheFile, targetFile);
195
- Log.d(TAG, "already cached " + fileName);
199
+ logger.debug("already cached " + fileName);
196
200
  } else {
197
201
  downloadAndVerify(downloadUrl, targetFile, cacheFile, finalFileHash, sessionKey, publicKey);
198
202
  }
@@ -201,7 +205,7 @@ public class DownloadService extends Worker {
201
205
  int percent = calcTotalPercent(completed, totalFiles);
202
206
  setProgress(percent);
203
207
  } catch (Exception e) {
204
- Log.e(TAG, "Error processing file: " + fileName, e);
208
+ logger.error("Error processing file: " + fileName + " " + e.getMessage());
205
209
  hasError.set(true);
206
210
  }
207
211
  });
@@ -213,7 +217,7 @@ public class DownloadService extends Worker {
213
217
  try {
214
218
  future.get();
215
219
  } catch (Exception e) {
216
- Log.e(TAG, "Error waiting for download", e);
220
+ logger.error("Error waiting for download " + e.getMessage());
217
221
  hasError.set(true);
218
222
  }
219
223
  }
@@ -229,11 +233,11 @@ public class DownloadService extends Worker {
229
233
  }
230
234
 
231
235
  if (hasError.get()) {
232
- Log.e(TAG, "One or more files failed to download");
236
+ logger.error("One or more files failed to download");
233
237
  throw new IOException("One or more files failed to download");
234
238
  }
235
239
  } catch (Exception e) {
236
- Log.e(TAG, "Error in handleManifestDownload", e);
240
+ logger.error("Error in handleManifestDownload " + e.getMessage());
237
241
  throw new RuntimeException(e.getLocalizedMessage());
238
242
  }
239
243
  }
@@ -347,7 +351,7 @@ public class DownloadService extends Worker {
347
351
  infoFile.createNewFile();
348
352
  tempFile.createNewFile();
349
353
  } catch (IOException e) {
350
- Log.e(TAG, "Error in clearDownloadData", e);
354
+ logger.error("Error in clearDownloadData " + e.getMessage());
351
355
  // not a fatal error, so we don't throw an exception
352
356
  }
353
357
  }
@@ -373,7 +377,7 @@ public class DownloadService extends Worker {
373
377
  String sessionKey,
374
378
  String publicKey
375
379
  ) throws Exception {
376
- Log.d(TAG, "downloadAndVerify " + downloadUrl);
380
+ logger.debug("downloadAndVerify " + downloadUrl);
377
381
 
378
382
  Request request = new Request.Builder().url(downloadUrl).build();
379
383
 
@@ -409,7 +413,7 @@ public class DownloadService extends Worker {
409
413
  }
410
414
 
411
415
  if (!publicKey.isEmpty() && sessionKey != null && !sessionKey.isEmpty()) {
412
- Log.d(CapgoUpdater.TAG + " DLSrv", "Decrypting file " + targetFile.getName());
416
+ logger.debug("Decrypting file " + targetFile.getName());
413
417
  CryptoCipherV2.decryptFile(compressedFile, publicKey, sessionKey);
414
418
  }
415
419
 
@@ -482,7 +486,7 @@ public class DownloadService extends Worker {
482
486
  private byte[] decompressBrotli(byte[] data, String fileName) throws IOException {
483
487
  // Validate input
484
488
  if (data == null) {
485
- Log.e(TAG, "Error: Null data received for " + fileName);
489
+ logger.error("Error: Null data received for " + fileName);
486
490
  throw new IOException("Null data received");
487
491
  }
488
492
 
@@ -509,7 +513,7 @@ public class DownloadService extends Worker {
509
513
  return Arrays.copyOfRange(data, 3, data.length - 1);
510
514
  }
511
515
  } catch (ArrayIndexOutOfBoundsException e) {
512
- Log.e(TAG, "Error: Malformed data for " + fileName);
516
+ logger.error("Error: Malformed data for " + fileName);
513
517
  throw new IOException("Malformed data structure");
514
518
  }
515
519
  }
@@ -527,13 +531,13 @@ public class DownloadService extends Worker {
527
531
  }
528
532
  return bos.toByteArray();
529
533
  } catch (IOException e) {
530
- Log.e(TAG, "Error: Brotli process failed for " + fileName + ". Status: " + e.getMessage());
534
+ logger.error("Error: Brotli process failed for " + fileName + ". Status: " + e.getMessage());
531
535
  // Add hex dump for debugging
532
536
  StringBuilder hexDump = new StringBuilder();
533
537
  for (int i = 0; i < Math.min(32, data.length); i++) {
534
538
  hexDump.append(String.format("%02x ", data[i]));
535
539
  }
536
- Log.e(TAG, "Error: Raw data (" + fileName + "): " + hexDump.toString());
540
+ logger.error("Error: Raw data (" + fileName + "): " + hexDump.toString());
537
541
  throw e;
538
542
  }
539
543
  }
@@ -1,7 +1,6 @@
1
1
  package ee.forgr.capacitor_updater;
2
2
 
3
3
  import android.content.Context;
4
- import android.util.Log;
5
4
  import androidx.work.BackoffPolicy;
6
5
  import androidx.work.Configuration;
7
6
  import androidx.work.Constraints;
@@ -16,7 +15,12 @@ import java.util.concurrent.TimeUnit;
16
15
 
17
16
  public class DownloadWorkerManager {
18
17
 
19
- private static final String TAG = "DownloadWorkerManager";
18
+ private static Logger logger;
19
+
20
+ public static void setLogger(Logger loggerInstance) {
21
+ logger = loggerInstance;
22
+ }
23
+
20
24
  private static volatile boolean isInitialized = false;
21
25
  private static final Set<String> activeVersions = new HashSet<>();
22
26
 
@@ -53,7 +57,7 @@ public class DownloadWorkerManager {
53
57
 
54
58
  // If version is already downloading, don't start another one
55
59
  if (isVersionDownloading(version)) {
56
- Log.i(TAG, "Version " + version + " is already downloading");
60
+ logger.info("Version " + version + " is already downloading");
57
61
  return;
58
62
  }
59
63
  activeVersions.add(version);
@@ -74,7 +78,7 @@ public class DownloadWorkerManager {
74
78
  // Create network constraints - be more lenient on emulators
75
79
  Constraints.Builder constraintsBuilder = new Constraints.Builder();
76
80
  if (isEmulator) {
77
- Log.i(TAG, "Emulator detected - using lenient network constraints");
81
+ logger.info("Emulator detected - using lenient network constraints");
78
82
  // On emulators, use NOT_REQUIRED to avoid background network issues
79
83
  constraintsBuilder.setRequiredNetworkType(NetworkType.NOT_REQUIRED);
80
84
  } else {