-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathUpdater.java
More file actions
212 lines (184 loc) · 6.76 KB
/
Updater.java
File metadata and controls
212 lines (184 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package nodebox.versioncheck;
import nodebox.client.SwingUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
/**
* Main class. Checks for updates.
*/
public class Updater {
public static final String LAST_UPDATE_CHECK = "NBLastUpdateCheck";
public static final long UPDATE_INTERVAL = 1000 * 60 * 60 * 24; // 1000 milliseconds * 60 seconds * 60 minutes * 24 hours = Every day
private final Host host;
private Icon hostIcon;
private boolean automaticCheck;
private Preferences preferences;
private UpdateChecker updateChecker;
private UpdateDelegate delegate;
private UpdateCheckDialog updateCheckDialog;
public Updater(Host host) {
this.host = host;
automaticCheck = true;
Class hostClass = host.getClass();
this.preferences = Preferences.userNodeForPackage(hostClass);
}
public Host getHost() {
return host;
}
public Icon getHostIcon() {
if (hostIcon != null) return hostIcon;
BufferedImage img = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
Icon icon = new ImageIcon(host.getIconFile());
float factor = 64f / icon.getIconHeight();
g.scale(factor, factor);
icon.paintIcon(null, g, 0, 0);
hostIcon = new ImageIcon(img);
return hostIcon;
}
public UpdateDelegate getDelegate() {
return delegate;
}
public void setDelegate(UpdateDelegate delegate) {
this.delegate = delegate;
}
public UpdateChecker getUpdateChecker() {
return updateChecker;
}
/**
* Notification sent from the application that the updater can do its thing.
*/
public void applicationDidFinishLaunching() {
checkForUpdatesInBackground();
}
public boolean isAutomaticCheck() {
return automaticCheck;
}
/**
* This method is initialized by a user action.
* <p/>
* It will check for updates and reports its findings in a user dialog.
*/
public void checkForUpdates() {
checkForUpdates(true);
}
/**
* This method is initialized by a user action.
* <p/>
* It will check for updates and reports its findings in a user dialog.
*
* @param showProgressWindow if true, shows the progress window.
*/
public void checkForUpdates(boolean showProgressWindow) {
if (updateChecker != null && updateChecker.isAlive()) return;
if (showProgressWindow) {
updateCheckDialog = new UpdateCheckDialog(null, this);
Window win = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
SwingUtils.centerOnScreen(updateCheckDialog, win);
updateCheckDialog.setVisible(true);
}
updateChecker = new UpdateChecker(this, false);
updateChecker.start();
}
/**
* This method gets run when automatically checking for updates.
* <p/>
* If no updates are found, results are silently discarded. If an update is found, the update dialog will show.
*/
public void checkForUpdatesInBackground() {
if (shouldCheckForUpdate()) {
updateChecker = new UpdateChecker(this, true);
updateChecker.start();
}
}
/**
* Determines if the last check was far enough in the past to justify a new check.
*
* @return if the enough time has passed since the last check.
*/
public boolean shouldCheckForUpdate() {
long lastTime = getPreferences().getLong(LAST_UPDATE_CHECK, 0);
long deltaTime = System.currentTimeMillis() - lastTime;
return deltaTime > UPDATE_INTERVAL;
}
public Preferences getPreferences() {
return preferences;
}
/**
* The update checker completed the check without error.
*
* @param checker the update checker
* @param appcast the appcast
*/
public void checkCompleted(UpdateChecker checker, Appcast appcast) {
hideUpdateCheckDialog();
// Delegate method.
if (delegate != null)
if (delegate.checkCompleted(checker, appcast))
return;
// Store last check update check time.
getPreferences().putLong(LAST_UPDATE_CHECK, System.currentTimeMillis());
try {
getPreferences().flush();
} catch (BackingStoreException e) {
throw new RuntimeException("Error while storing preferences", e);
}
}
public void checkerFoundValidUpdate(UpdateChecker checker, Appcast appcast) {
// Delegate method.
if (delegate != null)
if (delegate.checkerFoundValidUpdate(checker, appcast))
return;
UpdateAlert alert = new UpdateAlert(this, appcast);
Window win = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
SwingUtils.centerOnScreen(alert, win);
alert.setVisible(true);
}
public void checkerDetectedLatestVersion(UpdateChecker checker, Appcast appcast) {
// Delegate method.
if (delegate != null)
if (delegate.checkerDetectedLatestVersion(checker, appcast))
return;
// Show a message that you're using the latest version if you've explicitly checked for updates.
if (!checker.isSilent()) {
JOptionPane.showMessageDialog(null, "<html><b>You're up to date!</b><br>\n" + host.getName() + " " + host.getVersion() + " is the latest version available.", "", JOptionPane.INFORMATION_MESSAGE, getHostIcon());
}
}
public void checkerEncounteredError(UpdateChecker checker, Throwable t) {
hideUpdateCheckDialog();
// Delegate method.
if (delegate != null)
if (delegate.checkerEncounteredError(checker, t))
return;
if (checker.isSilent()) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Update checker encountered error.", t);
} else {
throw new RuntimeException(t);
}
}
public void waitForCheck(int timeoutMillis) {
try {
updateChecker.join(timeoutMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (updateChecker.isAlive()) {
updateChecker.interrupt();
}
}
public void cancelUpdateCheck() {
if (updateChecker != null && updateChecker.isAlive()) {
updateChecker.interrupt();
}
}
private void hideUpdateCheckDialog() {
if (updateCheckDialog != null) {
updateCheckDialog.setVisible(false);
updateCheckDialog = null;
}
}
}