Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jpackage/apps/image/Hello.java
41153 views
1
/*
2
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.awt.AWTError;
25
import java.awt.Desktop;
26
import java.awt.GraphicsEnvironment;
27
import java.awt.desktop.OpenFilesEvent;
28
import java.awt.desktop.OpenFilesHandler;
29
import java.io.File;
30
import java.io.IOException;
31
import java.io.PrintWriter;
32
import java.io.StringWriter;
33
import java.nio.file.Path;
34
import java.nio.file.Files;
35
import java.util.stream.Collectors;
36
import java.util.List;
37
import java.util.ArrayList;
38
import java.util.stream.Stream;
39
import java.util.Collections;
40
41
public class Hello implements OpenFilesHandler {
42
43
public static void main(String[] args) throws IOException, InterruptedException {
44
var faFiles = getFaFiles();
45
if (faFiles != null) {
46
// Some files got opened through fa mechanizm.
47
// They are the arguments then.
48
args = faFiles.toArray(String[]::new);
49
}
50
51
var lines = printArgs(args);
52
53
Stream.of(args).forEach(arg -> System.out.println(
54
arg.codePoints()
55
.mapToObj(codePoint -> String.format("0x%04x", codePoint))
56
.collect(Collectors.joining(",", "[", "]"))));
57
58
lines.forEach(System.out::println);
59
60
var outputFile = getOutputFile(args);
61
trace(String.format("Output file: [%s]", outputFile));
62
Files.write(outputFile, lines);
63
}
64
65
private static List<String> printArgs(String[] args) {
66
List<String> lines = new ArrayList<>();
67
lines.add(MSG);
68
69
lines.add("args.length: " + args.length);
70
71
for (String arg : args) {
72
if (arg.startsWith("jpackage.app")) {
73
lines.add(arg + "=" + System.getProperty(arg));
74
} else {
75
lines.add(arg);
76
}
77
}
78
79
for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {
80
String value = System.getProperty("param" + index);
81
if (value != null) {
82
lines.add("-Dparam" + index + "=" + value);
83
}
84
}
85
86
return lines;
87
}
88
89
private static Path getOutputFile(String[] args) {
90
Path outputFilePath = Path.of("appOutput.txt");
91
92
// If first arg is a file (most likely from fa), then put output in the same folder as
93
// the file from fa.
94
if (args.length >= 1) {
95
Path faPath = Path.of(args[0]);
96
if (Files.exists(faPath)) {
97
return faPath.toAbsolutePath().getParent().resolve(outputFilePath);
98
}
99
}
100
101
try {
102
// Try writing in the default output file.
103
Files.write(outputFilePath, Collections.emptyList());
104
return outputFilePath;
105
} catch (IOException ex) {
106
// Log reason of a failure.
107
StringWriter errors = new StringWriter();
108
ex.printStackTrace(new PrintWriter(errors));
109
Stream.of(errors.toString().split("\\R")).forEachOrdered(Hello::trace);
110
}
111
112
return Path.of(System.getProperty("user.home")).resolve(outputFilePath);
113
}
114
115
@Override
116
public void openFiles(OpenFilesEvent e) {
117
synchronized(lock) {
118
trace("openFiles");
119
files = e.getFiles().stream()
120
.map(File::toString)
121
.collect(Collectors.toList());
122
123
lock.notifyAll();
124
}
125
}
126
127
private static List<String> getFaFiles() throws InterruptedException {
128
if (openFilesHandler == null) {
129
return null;
130
}
131
132
synchronized(openFilesHandler.lock) {
133
trace("getFaFiles: wait");
134
openFilesHandler.lock.wait(1000);
135
if (openFilesHandler.files == null) {
136
trace(String.format("getFaFiles: no files"));
137
return null;
138
}
139
// Return copy of `files` to keep access to `files` field synchronized.
140
trace(String.format("getFaFiles: file count %d",
141
openFilesHandler.files.size()));
142
return new ArrayList<>(openFilesHandler.files);
143
}
144
}
145
146
private List<String> files;
147
private final Object lock = new Object();
148
private final static Hello openFilesHandler = createInstance();
149
150
private static Hello createInstance() {
151
if (GraphicsEnvironment.isHeadless()) {
152
return null;
153
}
154
155
trace("Environment supports a display");
156
157
if (!Desktop.isDesktopSupported()) {
158
return null;
159
}
160
161
trace("Environment supports a desktop");
162
163
try {
164
// Disable JAB.
165
// Needed to suppress error:
166
// Exception in thread "main" java.awt.AWTError: Assistive Technology not found: com.sun.java.accessibility.AccessBridge
167
System.setProperty("javax.accessibility.assistive_technologies", "");
168
} catch (SecurityException ex) {
169
ex.printStackTrace();
170
}
171
172
try {
173
var desktop = Desktop.getDesktop();
174
if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) {
175
trace("Set file handler");
176
Hello instance = new Hello();
177
desktop.setOpenFileHandler(instance);
178
return instance;
179
}
180
} catch (AWTError ex) {
181
trace("Set file handler failed");
182
ex.printStackTrace();
183
}
184
185
return null;
186
}
187
188
private static final String MSG = "jpackage test application";
189
private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 1
190
191
private static void trace(String msg) {
192
System.out.println("hello: " + msg);
193
}
194
}
195
196