forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevToolsUrl.java
More file actions
258 lines (225 loc) · 7.15 KB
/
DevToolsUrl.java
File metadata and controls
258 lines (225 loc) · 7.15 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
/*
* Copyright 2021 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.devtools;
import com.intellij.openapi.application.ApplicationManager;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.sdk.FlutterSdkUtil;
import io.flutter.sdk.FlutterSdkVersion;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.application.ApplicationInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class DevToolsUrl {
public static final String UNKNOWN_INTELLIJ_NAME = "IntelliJ - Unknown";
private String devToolsHost;
private int devToolsPort;
public String vmServiceUri;
private String page;
private boolean embed;
public String colorHexCode;
public Boolean isBright;
public String widgetId;
public String hide;
private final FlutterSdkVersion flutterSdkVersion;
private final FlutterSdkUtil sdkUtil;
private final boolean canUseMultiEmbed;
public final DevToolsIdeFeature ideFeature;
private final String ideName;
@NotNull private final DevToolsUtils devToolsUtils;
public static class Builder {
private @Nullable String devToolsHost;
private int devToolsPort;
private @Nullable String vmServiceUri;
private String page;
private Boolean embed;
private String widgetId;
private String hide;
private @Nullable FlutterSdkVersion flutterSdkVersion;
private WorkspaceCache workspaceCache;
private DevToolsIdeFeature ideFeature;
private String ideName;
private DevToolsUtils devToolsUtils;
private FlutterSdkUtil flutterSdkUtil;
public Builder() {
}
@NotNull
public Builder setDevToolsHost(@Nullable String devToolsHost) {
this.devToolsHost = devToolsHost;
return this;
}
@NotNull
public Builder setDevToolsPort(int devToolsPort) {
this.devToolsPort = devToolsPort;
return this;
}
@NotNull
public Builder setVmServiceUri(@Nullable String vmServiceUri) {
this.vmServiceUri = vmServiceUri;
return this;
}
@NotNull
public Builder setPage(String page) {
this.page = page;
return this;
}
@NotNull
public Builder setEmbed(Boolean embed) {
this.embed = embed;
return this;
}
@NotNull
public Builder setWidgetId(String widgetId) {
this.widgetId = widgetId;
return this;
}
@NotNull
public Builder setHide(String hide) {
this.hide = hide;
return this;
}
@NotNull
public Builder setDevToolsUtils(DevToolsUtils devToolsUtils) {
this.devToolsUtils = devToolsUtils;
return this;
}
@NotNull
public Builder setFlutterSdkVersion(@Nullable FlutterSdkVersion sdkVersion) {
this.flutterSdkVersion = sdkVersion;
return this;
}
@NotNull
public Builder setWorkspaceCache(WorkspaceCache workspaceCache) {
this.workspaceCache = workspaceCache;
return this;
}
@NotNull
public Builder setIdeFeature(DevToolsIdeFeature ideFeature) {
this.ideFeature = ideFeature;
return this;
}
@NotNull
public Builder setFlutterSdkUtil(FlutterSdkUtil flutterSdkUtil) {
this.flutterSdkUtil = flutterSdkUtil;
return this;
}
@NotNull
public Builder setIdeName(String ideName) {
this.ideName = ideName;
return this;
}
@NotNull
public DevToolsUrl build() {
if (devToolsUtils == null) {
devToolsUtils = new DevToolsUtils();
}
if (flutterSdkUtil == null) {
flutterSdkUtil = new FlutterSdkUtil();
}
if (embed == null) {
embed = false;
}
return new DevToolsUrl(this);
}
}
private DevToolsUrl(Builder builder) {
this.devToolsHost = builder.devToolsHost;
this.devToolsPort = builder.devToolsPort;
this.vmServiceUri = builder.vmServiceUri;
this.page = builder.page;
this.embed = builder.embed;
this.devToolsUtils = builder.devToolsUtils;
if (builder.embed) {
this.colorHexCode = builder.devToolsUtils.getColorHexCode();
this.isBright = builder.devToolsUtils.getIsBackgroundBright();
}
this.hide = builder.hide;
this.widgetId = builder.widgetId;
this.flutterSdkVersion = builder.flutterSdkVersion;
this.ideFeature = builder.ideFeature;
this.sdkUtil = builder.flutterSdkUtil;
this.ideName = builder.ideName != null ? builder.ideName : getIdeName();
if (builder.workspaceCache != null && builder.workspaceCache.isBazel()) {
this.canUseMultiEmbed = true;
}
else if (flutterSdkVersion != null) {
this.canUseMultiEmbed = flutterSdkVersion.canUseDevToolsMultiEmbed();
}
else {
this.canUseMultiEmbed = false;
}
}
@SuppressWarnings("HttpUrlsUsage")
@NotNull
public String getUrlString() {
final List<String> params = new ArrayList<>();
String ideValue = sdkUtil.getFlutterHostEnvValue();
String ideParamValue = ideValue == null ? UNKNOWN_INTELLIJ_NAME : ideValue;
params.add("ide=" + URLEncoder.encode(ideParamValue, StandardCharsets.UTF_8));
params.add("dashTool=intellij-plugins");
params.add("dashIdeName=" + URLEncoder.encode(this.ideName, StandardCharsets.UTF_8));
if (colorHexCode != null) {
params.add("backgroundColor=" + colorHexCode);
}
if (isBright != null) {
params.add("theme=" + (isBright ? "light" : "dark"));
}
if (embed) {
if (!this.canUseMultiEmbed) {
// This is for older versions of DevTools that do not support embed= one vs. many.
params.add("embed=true");
}
else {
if (hide != null) {
// If we are using the hide param, we can assume that we are trying to embed multiple tabs.
params.add("embedMode=many");
params.add("hide=" + hide);
}
else {
params.add("embedMode=one");
}
}
}
if (ideFeature != null) {
params.add("ideFeature=" + ideFeature.value);
}
if (vmServiceUri != null) {
final String urlParam = URLEncoder.encode(vmServiceUri, StandardCharsets.UTF_8);
params.add("uri=" + urlParam);
}
if (widgetId != null) {
params.add("inspectorRef=" + widgetId);
}
return "http://" + devToolsHost + ":" + devToolsPort + "/" + (page != null ? page : "") + "?"
+ StringUtil.join(params, "&");
}
private @NotNull String getIdeName() {
if (ApplicationManager.getApplication() == null) {
return UNKNOWN_INTELLIJ_NAME;
}
ApplicationInfo appInfo = ApplicationInfo.getInstance();
if (appInfo != null) {
String versionName = appInfo.getVersionName();
if (versionName != null) {
return versionName;
}
}
return UNKNOWN_INTELLIJ_NAME;
}
public boolean maybeUpdateColor() {
final String newColor = devToolsUtils.getColorHexCode();
if (Objects.equals(colorHexCode, newColor)) {
return false;
}
colorHexCode = newColor;
isBright = devToolsUtils.getIsBackgroundBright();
return true;
}
}