-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava_picker.dart
More file actions
414 lines (360 loc) · 12.5 KB
/
java_picker.dart
File metadata and controls
414 lines (360 loc) · 12.5 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import "dart:async";
import "dart:ffi";
import "dart:io";
import "package:archive/archive_io.dart";
import "package:file_picker/file_picker.dart";
import "package:flutter/material.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:path/path.dart" as p;
import "package:path_provider/path_provider.dart";
import "../../../prefs.dart";
import "../../../utils.dart";
import "../../../versions.dart";
import "check_java_version.dart";
enum _SystemRadioState { loading, success, errored }
enum _ManagedRadioState { empty, downloading, hashing, unpacking, success, errored }
enum _CustomRadioState { empty, success, errored }
class JavaPicker extends ConsumerStatefulWidget {
const JavaPicker({super.key});
@override
ConsumerState<JavaPicker> createState() => _JavaPickerState();
}
class _JavaPickerState extends ConsumerState<JavaPicker> {
// I don't want these for providers; too long
// ignore: specify_nonobvious_property_types
final _javaPickerModeProvider = javaPathProvider.select((javaPath) => javaPath?.type);
///disables all RadioListTiles whenever something is in progress, so it cannot be interrupted
bool inProgress = false;
_SystemRadioState systemRadioState = .loading;
int? systemJavaVersion;
String? systemError;
_ManagedRadioState managedRadioState = .empty;
late final Uri? managedDownloadLink;
late final String? managedHash;
String? managedError;
double? managedProgress;
void initManagedDownloadLink() {
Uri createLink(String platform, String architecture) {
return Uri.https(
"api.adoptium.net",
"/v3/binary/version/$javaManagedVersion/$platform/$architecture/jre/hotspot/normal/eclipse",
);
}
// Hashes are SHA256
switch (Abi.current()) {
case Abi.linuxX64:
managedDownloadLink = createLink("linux", "x64");
managedHash = javaManagedLinuxX64Hash;
case Abi.windowsX64:
managedDownloadLink = createLink("windows", "x64");
managedHash = javaManagedWindowsX64Hash;
}
}
_CustomRadioState customRadioState = .empty;
int? customJavaVersion;
String? customJavaPath;
String? customError;
@override
void initState() {
super.initState();
initManagedDownloadLink();
// System
unawaited(
checkJavaVersion(JavaPath(.system, "java")).then(
(javaVersion) {
setState(() {
systemRadioState = .success;
systemJavaVersion = javaVersion;
systemError = null;
});
},
onError: (Object e) {
setState(() {
systemRadioState = .errored;
systemJavaVersion = null;
systemError = e is JavaVersionCheckException ? e.message : e.toString();
});
},
),
);
// Custom
if (ref.read(_javaPickerModeProvider) == .custom) {
final JavaPath javaPath = ref.read(javaPathProvider)!;
customRadioState = .success;
customJavaVersion = 0;
customJavaPath = javaPath.path;
customError = null;
unawaited(
checkJavaVersion(javaPath).then(
(int javaVersion) {
setState(() {
customJavaVersion = javaVersion;
});
},
onError: (Object e) {
setState(() {
customRadioState = .errored;
customJavaVersion = null;
customError = e is JavaVersionCheckException ? e.message : e.toString();
});
},
),
);
}
}
@override
Widget build(BuildContext context) {
final JavaPathMode javaPickerMode = ref.watch(_javaPickerModeProvider) ?? .unset;
const TextStyle red = TextStyle(color: Colors.red);
return RadioGroup(
groupValue: javaPickerMode,
onChanged: (JavaPathMode? newJavaPickerMode) async {
switch (newJavaPickerMode) {
case .unset:
onUnset();
case .system:
onSystem();
case .managed:
setState(() => inProgress = true);
await onManaged();
setState(() => inProgress = false);
case .custom:
setState(() => inProgress = true);
await onCustom();
setState(() => inProgress = false);
case null:
break;
}
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
RadioListTile(
value: JavaPathMode.unset,
title: Text(JavaPathMode.unset.name.capitalize()),
subtitle: const Text("No Java selected."),
enabled: !inProgress,
),
RadioListTile(
value: JavaPathMode.system,
title: Text(JavaPathMode.system.name.capitalize()),
subtitle: switch (systemRadioState) {
.loading => const Text("Checking System Java version..."),
.success => Text("Detected System Java version: $systemJavaVersion"),
.errored => Text("$systemError", style: red),
},
enabled: systemRadioState == .success && !inProgress,
),
RadioListTile(
value: JavaPathMode.managed,
title: Text(JavaPathMode.managed.name.capitalize()),
subtitle: managedDownloadLink == null
? const Text(
"Your computer is incompatible with the automatic download. Please try the Custom mode below.",
style: red,
)
: Column(
crossAxisAlignment: .start,
mainAxisSize: .min,
children: [
switch (managedRadioState) {
.empty => const Text(
"Automatically download Java. Use this if you don't have a working System Installation, and you don't want to use a custom one either.",
),
.downloading => const Text("Downloading..."),
.hashing => const Text("Verifying..."),
.unpacking => const Text("Unpacking..."),
.success => const Text(
"Successfully downloaded! You are ready to go!",
),
.errored => Text("$managedError", style: red),
},
if (managedRadioState == .downloading ||
managedRadioState == .hashing ||
managedRadioState == .unpacking)
LinearProgressIndicator(value: managedProgress),
],
),
enabled: managedDownloadLink != null && !inProgress,
),
RadioListTile(
value: JavaPathMode.custom,
title: Text(JavaPathMode.custom.name.capitalize()),
subtitle: switch (customRadioState) {
.empty => const Text("Select a custom Java executable manually."),
.success => Text(
"Detected Java version: $customJavaVersion ( $customJavaPath )",
),
.errored => Text("$customError ( $customJavaPath )", style: red),
},
enabled: !inProgress,
),
],
),
);
}
void onUnset() {
ref.read(javaPathProvider.notifier).clearJavaPath();
}
void onSystem() {
ref.read(javaPathProvider.notifier).setJavaPath(JavaPath(.system, "java"));
}
Future<File?> findJavaExecutableInDirectory(Directory dir) async {
final File javaExe = File(p.join(dir.path, "java.exe"));
if (javaExe.existsSync()) return javaExe;
final File java = File(p.join(dir.path, "java"));
if (java.existsSync()) return java;
final FileSystemEntity fallback = await dir.list().firstWhere(
(file) => p.basename(file.path).startsWith("java"),
);
if (fallback is File) {
return fallback;
}
return null;
}
Future<void> onManaged() async {
final Uri? downloadLink = managedDownloadLink;
final String? hash = managedHash;
if (downloadLink == null || hash == null) return;
setState(() {
managedRadioState = .downloading;
managedProgress = null;
});
final Directory supportDir = await getApplicationSupportDirectory();
final Directory javaManagedDirectory = Directory(p.join(supportDir.path, "java"));
// Delete old Java Manageds(s)
if (javaManagedDirectory.existsSync()) {
try {
await javaManagedDirectory.delete(recursive: true);
} on FileSystemException catch (e) {
setState(() {
managedRadioState = .errored;
managedError = e.toString();
managedProgress = null;
});
return;
}
}
final NonHashedFile susManagedArchive;
try {
susManagedArchive = await downloadFile(
uri: downloadLink,
outputFileGenerator: (response) {
final String filename = response.redirects.first.location.getFileName();
return File(p.join(supportDir.path, filename));
},
onProgress: (double progress) {
setState(() {
managedProgress = progress;
});
},
);
} on IOException catch (e) {
setState(() {
managedRadioState = .errored;
managedError = e.toString();
managedProgress = null;
});
return;
}
setState(() {
managedRadioState = .hashing;
managedProgress = null;
});
final File? hashedManagedArchive = await susManagedArchive.hashFile(hash);
if (hashedManagedArchive == null) {
setState(() {
managedRadioState = .errored;
managedError =
"Could not verify the downloaded Java archive's integrity!\n"
"The hash of the downloaded file does not match the expected hash.";
managedProgress = null;
});
return;
}
final File javaManagedArchive = hashedManagedArchive;
setState(() {
managedRadioState = .unpacking;
});
try {
await extractFileToDisk(javaManagedArchive.path, javaManagedDirectory.path);
} on IOException catch (e) {
setState(() {
managedRadioState = .errored;
managedError = e.toString();
managedProgress = null;
});
return;
}
// Delete the archive; it is not needed anymore.
try {
await javaManagedArchive.delete(recursive: true);
} on FileSystemException catch (e) {
setState(() {
managedRadioState = .errored;
managedError = e.toString();
managedProgress = null;
});
return;
}
final Directory binDir = Directory(
p.join((await javaManagedDirectory.list().first).path, "bin"),
);
final File? javaExecutable = await findJavaExecutableInDirectory(binDir);
if (javaExecutable == null) {
setState(() {
managedRadioState = .errored;
managedError = "Could not find Java Executable in the download.";
managedProgress = null;
});
return;
}
setState(() {
managedRadioState = .success;
managedError = null;
managedProgress = null;
ref
.read(javaPathProvider.notifier)
.setJavaPath(JavaPath(.managed, javaExecutable.path));
});
}
Future<void> onCustom() async {
final FilePickerResult? browsed = await FilePicker.pickFiles(
dialogTitle: "Select Java executable",
// Specifically mention this, because we can't use FileType.custom, which would be expected here.
// This is because it doesn't support files with no extension, which is the case for executables on linux.
// ignore: avoid_redundant_argument_values
type: FileType.any,
);
if (browsed == null) {
return; // User canceled the browser
}
final String? javaPath = browsed.files.single.path;
if (javaPath == null) {
setState(() {
customRadioState = .errored;
customJavaVersion = null;
customJavaPath = null;
customError = "Path is null";
});
return;
}
customJavaPath = javaPath;
try {
final JavaPath potentialJavaPath = JavaPath(.custom, javaPath);
final int javaVersion = await checkJavaVersion(potentialJavaPath);
setState(() {
customRadioState = .success;
customJavaVersion = javaVersion;
customError = null;
ref.read(javaPathProvider.notifier).setJavaPath(potentialJavaPath);
});
} on JavaVersionCheckException catch (e) {
setState(() {
customRadioState = .errored;
customJavaVersion = null;
customError = e.message;
});
}
}
}