Skip to content

Commit dc93bb9

Browse files
author
Federico Fissore
committed
ProgressListener is now a method arg (was an instance arg)
1 parent b3baa27 commit dc93bb9

13 files changed

+86
-107
lines changed

app/src/cc/arduino/contributions/ContributionsSelfCheck.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ContributionsSelfCheck extends TimerTask {
2424
private final ContributionInstaller contributionInstaller;
2525
private final LibrariesIndexer librariesIndexer;
2626
private final LibraryInstaller libraryInstaller;
27+
private final ProgressListener progressListener;
2728

2829
public ContributionsSelfCheck(Base base, HyperlinkListener hyperlinkListener, ContributionsIndexer contributionsIndexer, ContributionInstaller contributionInstaller, LibrariesIndexer librariesIndexer, LibraryInstaller libraryInstaller) {
2930
this.base = base;
@@ -32,6 +33,7 @@ public ContributionsSelfCheck(Base base, HyperlinkListener hyperlinkListener, Co
3233
this.contributionInstaller = contributionInstaller;
3334
this.librariesIndexer = librariesIndexer;
3435
this.libraryInstaller = libraryInstaller;
36+
this.progressListener = new NoopProgressListener();
3537
}
3638

3739
@Override
@@ -67,15 +69,15 @@ public void run() {
6769

6870
private void updateLibrariesIndex() {
6971
try {
70-
libraryInstaller.updateIndex();
72+
libraryInstaller.updateIndex(progressListener);
7173
} catch (Exception e) {
7274
// ignore
7375
}
7476
}
7577

7678
private void updateContributionIndex() {
7779
try {
78-
contributionInstaller.updateIndex();
80+
contributionInstaller.updateIndex(progressListener);
7981
} catch (Exception e) {
8082
// ignore
8183
}

app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,7 @@ public void setIndexer(LibrariesIndexer indexer) {
183183
filterField.setEnabled(contribModel.getRowCount() > 0);
184184

185185
// Create LibrariesInstaller tied with the provided index
186-
installer = new LibraryInstaller(indexer, platform) {
187-
@Override
188-
public void onProgress(Progress progress) {
189-
setProgress(progress);
190-
}
191-
};
186+
installer = new LibraryInstaller(indexer, platform);
192187
}
193188

194189
public void selectDropdownItemByClassName(String dropdownItem) {
@@ -216,7 +211,7 @@ protected void onUpdatePressed() {
216211
installerThread = new Thread(() -> {
217212
try {
218213
setProgressVisible(true, "");
219-
installer.updateIndex();
214+
installer.updateIndex(this::setProgress);
220215
onIndexesUpdated();
221216
} catch (Exception e) {
222217
throw new RuntimeException(e);
@@ -233,7 +228,7 @@ public void onInstallPressed(final ContributedLibrary lib, final ContributedLibr
233228
installerThread = new Thread(() -> {
234229
try {
235230
setProgressVisible(true, tr("Installing..."));
236-
installer.install(lib, replaced);
231+
installer.install(lib, replaced, this::setProgress);
237232
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
238233
//getContribModel().updateLibrary(lib);
239234
} catch (Exception e) {
@@ -260,7 +255,7 @@ public void onRemovePressed(final ContributedLibrary lib) {
260255
installerThread = new Thread(() -> {
261256
try {
262257
setProgressVisible(true, tr("Removing..."));
263-
installer.remove(lib);
258+
installer.remove(lib, this::setProgress);
264259
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
265260
//getContribModel().updateLibrary(lib);
266261
} catch (Exception e) {

app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void setIndexer(ContributionsIndexer indexer) {
118118
}
119119

120120
// Create ConstributionInstaller tied with the provided index
121-
installer = new ContributionInstaller(indexer, platform, new GPGDetachedSignatureVerifier(), this::setProgress);
121+
installer = new ContributionInstaller(indexer, platform, new GPGDetachedSignatureVerifier());
122122
}
123123

124124
public void setProgress(Progress progress) {
@@ -146,7 +146,7 @@ public void onUpdatePressed() {
146146
installerThread = new Thread(() -> {
147147
try {
148148
setProgressVisible(true, "");
149-
List<String> downloadedPackageIndexFiles = installer.updateIndex();
149+
List<String> downloadedPackageIndexFiles = installer.updateIndex(this::setProgress);
150150
installer.deleteUnknownFiles(downloadedPackageIndexFiles);
151151
onIndexesUpdated();
152152
} catch (Exception e) {
@@ -165,7 +165,7 @@ public void onInstallPressed(final ContributedPlatform platformToInstall, final
165165
List<String> errors = new LinkedList<>();
166166
try {
167167
setProgressVisible(true, tr("Installing..."));
168-
errors.addAll(installer.install(platformToInstall));
168+
errors.addAll(installer.install(platformToInstall, this::setProgress));
169169
if (platformToRemove != null && !platformToRemove.isReadOnly()) {
170170
errors.addAll(installer.remove(platformToRemove));
171171
}

app/src/processing/app/Base.java

+9-28
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import cc.arduino.contributions.packages.ui.ContributionManagerUI;
3434
import cc.arduino.files.DeleteFilesOnShutdown;
3535
import cc.arduino.packages.DiscoveryManager;
36-
import cc.arduino.utils.Progress;
3736
import cc.arduino.view.Event;
3837
import cc.arduino.view.JMenuUtils;
3938
import cc.arduino.view.SplashScreenHelper;
@@ -341,19 +340,10 @@ public Base(String[] args) throws Exception {
341340

342341
if (parser.isInstallBoard()) {
343342
ContributionsIndexer indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
344-
ContributionInstaller installer = new ContributionInstaller(indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier(), new ProgressListener() {
345-
private String lastStatus = "";
343+
ProgressListener progressListener = new ConsoleProgressListener();
344+
ContributionInstaller installer = new ContributionInstaller(indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
346345

347-
@Override
348-
public void onProgress(Progress progress) {
349-
if (!lastStatus.equals(progress.getStatus())) {
350-
System.out.println(progress.getStatus());
351-
}
352-
lastStatus = progress.getStatus();
353-
}
354-
});
355-
356-
List<String> downloadedPackageIndexFiles = installer.updateIndex();
346+
List<String> downloadedPackageIndexFiles = installer.updateIndex(progressListener);
357347
installer.deleteUnknownFiles(downloadedPackageIndexFiles);
358348
indexer.parseIndex();
359349
indexer.syncWithFilesystem(BaseNoGui.getHardwareFolder());
@@ -378,7 +368,7 @@ public void onProgress(Progress progress) {
378368
ContributedPlatform installed = indexer.getInstalled(boardToInstallParts[0], boardToInstallParts[1]);
379369

380370
if (!selected.isReadOnly()) {
381-
installer.install(selected);
371+
installer.install(selected, progressListener);
382372
}
383373

384374
if (installed != null && !installed.isReadOnly()) {
@@ -389,22 +379,13 @@ public void onProgress(Progress progress) {
389379

390380
} else if (parser.isInstallLibrary()) {
391381
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier()));
392-
LibraryInstaller installer = new LibraryInstaller(indexer, BaseNoGui.getPlatform()) {
393-
private String lastStatus = "";
394-
395-
@Override
396-
protected void onProgress(Progress progress) {
397-
if (!lastStatus.equals(progress.getStatus())) {
398-
System.out.println(progress.getStatus());
399-
}
400-
lastStatus = progress.getStatus();
401-
}
402-
};
382+
ProgressListener progressListener = new ConsoleProgressListener();
383+
LibraryInstaller installer = new LibraryInstaller(indexer, BaseNoGui.getPlatform());
403384
indexer.parseIndex();
404385
BaseNoGui.onBoardOrPortChange();
405386
indexer.setSketchbookLibrariesFolder(BaseNoGui.getSketchbookLibrariesFolder());
406387
indexer.setLibrariesFolders(BaseNoGui.getLibrariesPath());
407-
installer.updateIndex();
388+
installer.updateIndex(progressListener);
408389

409390
for (String library : parser.getLibraryToInstall().split(",")) {
410391
String[] libraryToInstallParts = library.split(":");
@@ -426,9 +407,9 @@ protected void onProgress(Progress progress) {
426407

427408
ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
428409
if (selected.isReadOnly()) {
429-
installer.remove(installed);
410+
installer.remove(installed, progressListener);
430411
} else {
431-
installer.install(selected, installed);
412+
installer.install(selected, installed, progressListener);
432413
}
433414
}
434415

app/test/cc/arduino/contributions/GzippedJsonDownloaderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void tearDown() throws Exception {
3838

3939
@Test
4040
public void testJsonDownload() throws Exception {
41-
new GZippedJsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json"), new URL("http://downloads.arduino.cc/libraries/library_index.json.gz")).download(tempFile, new MultiStepProgress(1), "");
41+
new GZippedJsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json"), new URL("http://downloads.arduino.cc/libraries/library_index.json.gz")).download(tempFile, new MultiStepProgress(1), "", new NoopProgressListener());
4242

4343
InputStream indexIn = new FileInputStream(tempFile);
4444
ObjectMapper mapper = new ObjectMapper();

app/test/cc/arduino/contributions/JsonDownloaderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void tearDown() throws Exception {
3838

3939
@Test
4040
public void testJsonDownload() throws Exception {
41-
new JsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json")).download(tempFile, new MultiStepProgress(1), "");
41+
new JsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json")).download(tempFile, new MultiStepProgress(1), "", new NoopProgressListener());
4242

4343
InputStream indexIn = new FileInputStream(tempFile);
4444
ObjectMapper mapper = new ObjectMapper();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cc.arduino.contributions;
2+
3+
import cc.arduino.utils.Progress;
4+
5+
public class ConsoleProgressListener implements ProgressListener {
6+
private String lastStatus = "";
7+
8+
@Override
9+
public void onProgress(Progress progress) {
10+
if (!lastStatus.equals(progress.getStatus())) {
11+
System.out.println(progress.getStatus());
12+
}
13+
lastStatus = progress.getStatus();
14+
}
15+
}

arduino-core/src/cc/arduino/contributions/DownloadableContributionsDownloader.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public DownloadableContributionsDownloader(File _stagingFolder) {
5151
stagingFolder = _stagingFolder;
5252
}
5353

54-
public File download(DownloadableContribution contribution, Progress progress, final String statusText) throws Exception {
54+
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener) throws Exception {
5555
URL url = new URL(contribution.getUrl());
5656
Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName());
5757

@@ -64,12 +64,12 @@ public File download(DownloadableContribution contribution, Progress progress, f
6464

6565
// Need to download or resume downloading?
6666
if (!Files.isRegularFile(outputFile, LinkOption.NOFOLLOW_LINKS) || (Files.size(outputFile) < contribution.getSize())) {
67-
download(url, outputFile.toFile(), progress, statusText);
67+
download(url, outputFile.toFile(), progress, statusText, progressListener);
6868
}
6969

7070
// Test checksum
7171
progress.setStatus(tr("Verifying archive integrity..."));
72-
onProgress(progress);
72+
progressListener.onProgress(progress);
7373
String checksum = contribution.getChecksum();
7474
if (hasChecksum(contribution)) {
7575
String algo = checksum.split(":")[0];
@@ -94,7 +94,7 @@ private boolean hasChecksum(DownloadableContribution contribution) {
9494
return algo != null && !algo.isEmpty();
9595
}
9696

97-
public void download(URL url, File tmpFile, Progress progress, String statusText) throws Exception {
97+
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
9898
FileDownloader downloader = new FileDownloader(url, tmpFile);
9999
downloader.addObserver((o, arg) -> {
100100
FileDownloader me = (FileDownloader) o;
@@ -106,16 +106,12 @@ public void download(URL url, File tmpFile, Progress progress, String statusText
106106
}
107107
progress.setStatus(statusText + " " + msg);
108108
progress.setProgress(me.getProgress());
109-
onProgress(progress);
109+
progressListener.onProgress(progress);
110110
});
111111
downloader.download();
112112
if (!downloader.isCompleted()) {
113113
throw new Exception(format(tr("Error downloading {0}"), url), downloader.getError());
114114
}
115115
}
116116

117-
protected void onProgress(Progress progress) {
118-
// Empty
119-
}
120-
121117
}

arduino-core/src/cc/arduino/contributions/GZippedJsonDownloader.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public GZippedJsonDownloader(DownloadableContributionsDownloader downloader, URL
4949
this.gzippedUrl = gzippedUrl;
5050
}
5151

52-
public void download(File tmpFile, Progress progress, String statusText) throws Exception {
52+
public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
5353
try {
54-
new JsonDownloader(downloader, gzippedUrl).download(tmpFile, progress, statusText);
54+
new JsonDownloader(downloader, gzippedUrl).download(tmpFile, progress, statusText, progressListener);
5555
File gzipTmpFile = new File(tmpFile.getParentFile(), GzipUtils.getCompressedFilename(tmpFile.getName()));
5656
tmpFile.renameTo(gzipTmpFile);
5757
decompress(gzipTmpFile, tmpFile);
5858
} catch (Exception e) {
59-
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText);
59+
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText, progressListener);
6060
}
6161
}
6262

arduino-core/src/cc/arduino/contributions/JsonDownloader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public JsonDownloader(DownloadableContributionsDownloader downloader, URL url) {
4444
this.url = url;
4545
}
4646

47-
public void download(File tmpFile, Progress progress, String statusText) throws Exception {
47+
public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
4848
try {
49-
downloader.download(url, tmpFile, progress, statusText);
49+
downloader.download(url, tmpFile, progress, statusText, progressListener);
5050
} catch (InterruptedException e) {
5151
// Download interrupted... just exit
5252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cc.arduino.contributions;
2+
3+
import cc.arduino.utils.Progress;
4+
5+
public class NoopProgressListener implements ProgressListener {
6+
7+
@Override
8+
public void onProgress(Progress progress) {
9+
10+
}
11+
12+
}

0 commit comments

Comments
 (0)