27
27
import io .flutter .utils .FileUtils ;
28
28
import io .flutter .utils .JxBrowserUtils ;
29
29
import org .jetbrains .annotations .NotNull ;
30
+ import org .jetbrains .annotations .Nullable ;
30
31
31
32
import java .io .File ;
32
33
import java .io .FileNotFoundException ;
50
51
public class JxBrowserManager {
51
52
private static JxBrowserManager manager ;
52
53
54
+ @ NotNull
53
55
protected static final String DOWNLOAD_PATH =
54
56
PathManager .getPluginsPath () + File .separatorChar + "flutter-intellij" + File .separatorChar + "jxbrowser" ;
57
+ @ NotNull
55
58
private static final AtomicReference <JxBrowserStatus > status = new AtomicReference <>(JxBrowserStatus .NOT_INSTALLED );
59
+ @ NotNull
56
60
private static final AtomicBoolean listeningForSetting = new AtomicBoolean (false );
61
+ @ NotNull
57
62
private static final Logger LOG = Logger .getInstance (JxBrowserManager .class );
63
+ @ NotNull
58
64
private static CompletableFuture <JxBrowserStatus > installation = new CompletableFuture <>();
65
+ @ NotNull
59
66
public static final String ANALYTICS_CATEGORY = "jxbrowser" ;
60
67
private static InstallationFailedReason latestFailureReason ;
61
68
private final JxBrowserUtils jxBrowserUtils ;
62
69
private final Analytics analytics ;
63
70
private final FileUtils fileUtils ;
64
71
65
72
@ VisibleForTesting
66
- protected JxBrowserManager (JxBrowserUtils jxBrowserUtils , Analytics analytics , FileUtils fileUtils ) {
73
+ protected JxBrowserManager (@ NotNull JxBrowserUtils jxBrowserUtils , @ NotNull Analytics analytics , @ NotNull FileUtils fileUtils ) {
67
74
this .jxBrowserUtils = jxBrowserUtils ;
68
75
this .analytics = analytics ;
69
76
this .fileUtils = fileUtils ;
@@ -72,6 +79,7 @@ protected JxBrowserManager(JxBrowserUtils jxBrowserUtils, Analytics analytics, F
72
79
@ NotNull
73
80
public static JxBrowserManager getInstance () {
74
81
if (manager == null ) {
82
+ //noinspection ConstantConditions
75
83
manager = new JxBrowserManager (new JxBrowserUtils (), FlutterInitializer .getAnalytics (), FileUtils .getInstance ());
76
84
}
77
85
return manager ;
@@ -82,17 +90,21 @@ protected static void resetForTest() {
82
90
status .set (JxBrowserStatus .NOT_INSTALLED );
83
91
}
84
92
93
+ @ NotNull
85
94
public JxBrowserStatus getStatus () {
95
+ //noinspection ConstantConditions
86
96
return status .get ();
87
97
}
88
98
99
+ @ Nullable
89
100
public InstallationFailedReason getLatestFailureReason () {
90
101
return latestFailureReason ;
91
102
}
92
103
93
104
/**
94
105
* Call {@link #setUp} before this function to ensure that an installation has started.
95
106
*/
107
+ @ Nullable
96
108
public JxBrowserStatus waitForInstallation (int seconds ) throws TimeoutException {
97
109
try {
98
110
return installation .get (seconds , TimeUnit .SECONDS );
@@ -114,7 +126,7 @@ public void retryFromFailed(@NotNull Project project) {
114
126
private class SettingsListener implements FlutterSettings .Listener {
115
127
final Project project ;
116
128
117
- public SettingsListener (Project project ) {
129
+ public SettingsListener (@ NotNull Project project ) {
118
130
this .project = project ;
119
131
}
120
132
@@ -123,18 +135,19 @@ public void settingsChanged() {
123
135
final FlutterSettings settings = FlutterSettings .getInstance ();
124
136
125
137
// Set up JxBrowser files if the embedded inspector option has been turned on and the files aren't already loaded.
138
+ //noinspection ConstantConditions
126
139
if (settings .isEnableEmbeddedBrowsers () && getStatus ().equals (JxBrowserStatus .NOT_INSTALLED )) {
127
140
setUp (project );
128
141
}
129
142
}
130
143
}
131
144
132
- private void setStatusFailed (InstallationFailedReason reason ) {
145
+ private void setStatusFailed (@ NotNull InstallationFailedReason reason ) {
133
146
setStatusFailed (reason , null );
134
147
}
135
148
136
- private void setStatusFailed (InstallationFailedReason reason , Long time ) {
137
- StringBuilder eventName = new StringBuilder ();
149
+ private void setStatusFailed (@ NotNull InstallationFailedReason reason , @ Nullable Long time ) {
150
+ final StringBuilder eventName = new StringBuilder ();
138
151
eventName .append ("installationFailed-" );
139
152
eventName .append (reason .failureType );
140
153
if (reason .detail != null ) {
@@ -159,6 +172,7 @@ public void listenForSettingChanges(@NotNull Project project) {
159
172
return ;
160
173
}
161
174
175
+ //noinspection ConstantConditions
162
176
FlutterSettings .getInstance ().addListener (new SettingsListener (project ));
163
177
}
164
178
@@ -221,6 +235,7 @@ public void setUp(@NotNull Project project) {
221
235
final String [] fileNames = {platformFileName , jxBrowserUtils .getApiFileName (), jxBrowserUtils .getSwingFileName ()};
222
236
boolean allDownloaded = true ;
223
237
for (String fileName : fileNames ) {
238
+ assert fileName != null ;
224
239
if (!fileUtils .fileExists (getFilePath (fileName ))) {
225
240
allDownloaded = false ;
226
241
break ;
@@ -236,6 +251,7 @@ public void setUp(@NotNull Project project) {
236
251
// Delete any already existing files.
237
252
// TODO(helin24): Handle if files cannot be deleted.
238
253
for (String fileName : fileNames ) {
254
+ assert fileName != null ;
239
255
final String filePath = getFilePath (fileName );
240
256
if (!fileUtils .deleteFile (filePath )) {
241
257
LOG .info (project .getName () + ": Existing file could not be deleted - " + filePath );
@@ -245,12 +261,14 @@ public void setUp(@NotNull Project project) {
245
261
downloadJxBrowser (project , fileNames );
246
262
}
247
263
248
- protected void downloadJxBrowser (Project project , String [] fileNames ) {
264
+ protected void downloadJxBrowser (@ NotNull Project project , @ NotNull String [] fileNames ) {
249
265
// The FileDownloader API is used by other plugins - e.g.
250
266
// https://github.com/JetBrains/intellij-community/blob/b09f8151e0d189d70363266c3bb6edb5f6bfeca4/plugins/markdown/src/org/intellij/plugins/markdown/ui/preview/javafx/JavaFXInstallator.java#L48
251
267
final List <FileDownloader > fileDownloaders = new ArrayList <>();
252
268
final DownloadableFileService service = DownloadableFileService .getInstance ();
269
+ assert service != null ;
253
270
for (String fileName : fileNames ) {
271
+ assert fileName != null ;
254
272
final DownloadableFileDescription
255
273
description = service .createFileDescription (jxBrowserUtils .getDistributionLink (fileName ), fileName );
256
274
fileDownloaders .add (service .createDownloader (Collections .singletonList (description ), fileName ));
@@ -264,6 +282,7 @@ public void run(@NotNull ProgressIndicator indicator) {
264
282
try {
265
283
for (int i = 0 ; i < fileDownloaders .size (); i ++) {
266
284
final FileDownloader downloader = fileDownloaders .get (i );
285
+ assert downloader != null ;
267
286
currentFileName = fileNames [i ];
268
287
final Pair <File , DownloadableFileDescription > download =
269
288
ContainerUtil .getFirstItem (downloader .download (new File (DOWNLOAD_PATH )));
@@ -288,11 +307,13 @@ public void run(@NotNull ProgressIndicator indicator) {
288
307
ProgressManager .getInstance ().runProcessWithProgressAsynchronously (task , processIndicator );
289
308
}
290
309
291
- private void loadClasses (String [] fileNames ) {
310
+ private void loadClasses (@ NotNull String [] fileNames ) {
292
311
for (String fileName : fileNames ) {
312
+ assert fileName != null ;
293
313
final String fullPath = getFilePath (fileName );
294
314
295
315
try {
316
+ //noinspection ConstantConditions
296
317
fileUtils .loadClass (this .getClass ().getClassLoader (), fullPath );
297
318
} catch (Exception ex ) {
298
319
LOG .info ("Failed to load JxBrowser file" , ex );
@@ -303,6 +324,7 @@ private void loadClasses(String[] fileNames) {
303
324
LOG .info ("Loaded JxBrowser file successfully: " + fullPath );
304
325
}
305
326
try {
327
+ //noinspection ThrowableNotThrown
306
328
final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException (RenderingMode .HARDWARE_ACCELERATED );
307
329
} catch (NoClassDefFoundError e ) {
308
330
LOG .info ("Failed to find JxBrowser class" );
@@ -314,13 +336,15 @@ private void loadClasses(String[] fileNames) {
314
336
installation .complete (JxBrowserStatus .INSTALLED );
315
337
}
316
338
317
- private void loadClasses2021 (String [] fileNames ) {
318
- List <Path > paths = new ArrayList <>();
339
+ private void loadClasses2021 (@ NotNull String [] fileNames ) {
340
+ final List <Path > paths = new ArrayList <>();
319
341
320
342
try {
321
343
for (String fileName : fileNames ) {
344
+ assert fileName != null ;
322
345
paths .add (Paths .get (getFilePath (fileName )));
323
346
}
347
+ //noinspection ConstantConditions
324
348
fileUtils .loadPaths (this .getClass ().getClassLoader (), paths );
325
349
} catch (Exception ex ) {
326
350
LOG .info ("Failed to load JxBrowser file" , ex );
@@ -329,6 +353,7 @@ private void loadClasses2021(String[] fileNames) {
329
353
}
330
354
331
355
try {
356
+ //noinspection ThrowableNotThrown
332
357
final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException (RenderingMode .HARDWARE_ACCELERATED );
333
358
} catch (NoClassDefFoundError e ) {
334
359
LOG .info ("Failed to find JxBrowser class" );
@@ -340,7 +365,8 @@ private void loadClasses2021(String[] fileNames) {
340
365
installation .complete (JxBrowserStatus .INSTALLED );
341
366
}
342
367
343
- private String getFilePath (String fileName ) {
368
+ @ NotNull
369
+ private String getFilePath (@ NotNull String fileName ) {
344
370
return DOWNLOAD_PATH + File .separatorChar + fileName ;
345
371
}
346
372
}
0 commit comments