Skip to content

Commit 7a2ac55

Browse files
committed
WelcomeService: Show a specific welcome message only once
We should avoid pestering users with the same message over and over again. Better to show it once, remember the SHA-1 checksum of the displayed message, and then only show the welcome message again when the application version number *and* the welcome message changes. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 7e4d0ef commit 7a2ac55

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/main/java/org/scijava/welcome/DefaultWelcomeService.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
import java.io.File;
3535
import java.io.IOException;
36+
import java.io.UnsupportedEncodingException;
37+
import java.security.MessageDigest;
38+
import java.security.NoSuchAlgorithmException;
3639

3740
import org.scijava.app.AppService;
3841
import org.scijava.command.CommandService;
@@ -61,6 +64,7 @@ public class DefaultWelcomeService extends AbstractService implements
6164
{
6265

6366
private final static String WELCOME_FILE = "WELCOME.md";
67+
private final static String CHECKSUM_PREFS_KEY = "checksum";
6468

6569
@Parameter
6670
private LogService log;
@@ -89,6 +93,10 @@ public void displayWelcome() {
8993
try {
9094
if (welcomeFile.exists()) {
9195
final String welcomeText = textService.asHTML(welcomeFile);
96+
final String checksum = getChecksum(welcomeText);
97+
final String previousChecksum = Prefs.get(getClass(), CHECKSUM_PREFS_KEY);
98+
if (checksum.equals(previousChecksum)) return;
99+
Prefs.put(getClass(), CHECKSUM_PREFS_KEY, checksum);
92100
displayService.createDisplay(welcomeText);
93101
}
94102
}
@@ -126,4 +134,32 @@ private String firstRunPrefKey() {
126134
return "firstRun-" + appService.getApp().getVersion();
127135
}
128136

137+
// get digest of the file as according to fullPath
138+
private String getChecksum(final String text)
139+
{
140+
try {
141+
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
142+
digest.update(text.getBytes("UTF-8"));
143+
return toHex(digest.digest());
144+
}
145+
catch (NoSuchAlgorithmException e) {
146+
return "" + text.hashCode();
147+
}
148+
catch (UnsupportedEncodingException e) {
149+
return "" + text.hashCode();
150+
}
151+
}
152+
153+
private final static char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7',
154+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
155+
156+
private String toHex(final byte[] bytes) {
157+
final char[] buffer = new char[bytes.length * 2];
158+
for (int i = 0; i < bytes.length; i++) {
159+
buffer[i * 2] = hex[(bytes[i] & 0xf0) >> 4];
160+
buffer[i * 2 + 1] = hex[bytes[i] & 0xf];
161+
}
162+
return new String(buffer);
163+
}
164+
129165
}

0 commit comments

Comments
 (0)