forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlutterInitializer.java
318 lines (268 loc) · 11.4 KB
/
FlutterInitializer.java
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
/*
* Copyright 2016 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;
import com.google.common.annotations.VisibleForTesting;
import com.intellij.ProjectTopics;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.notification.*;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.ModuleListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.startup.StartupActivity;
import io.flutter.analytics.Analytics;
import io.flutter.analytics.ToolWindowTracker;
import io.flutter.android.IntelliJAndroidSdk;
import io.flutter.bazel.WorkspaceCache;
import io.flutter.editor.FlutterSaveActionsManager;
import io.flutter.logging.FlutterConsoleLogManager;
import io.flutter.perf.FlutterWidgetPerfManager;
import io.flutter.performance.FlutterPerformanceViewFactory;
import io.flutter.preview.PreviewViewFactory;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRoots;
import io.flutter.run.FlutterReloadManager;
import io.flutter.run.FlutterRunNotifications;
import io.flutter.run.daemon.DevToolsService;
import io.flutter.run.daemon.DeviceService;
import io.flutter.sdk.FlutterPluginsLibraryManager;
import io.flutter.settings.FlutterSettings;
import io.flutter.survey.FlutterSurveyNotifications;
import io.flutter.utils.FlutterModuleUtils;
import io.flutter.view.FlutterViewFactory;
import org.jetbrains.annotations.NotNull;
import javax.swing.event.HyperlinkEvent;
import java.util.UUID;
/**
* Runs actions after the project has started up and the index is up to date.
*
* @see ProjectOpenActivity for actions that run earlier.
* @see io.flutter.project.FlutterProjectOpenProcessor for additional actions that
* may run when a project is being imported.
*/
public class FlutterInitializer implements StartupActivity {
private static final String analyticsClientIdKey = "io.flutter.analytics.clientId";
private static final String analyticsOptOutKey = "io.flutter.analytics.optOut";
private static final String analyticsToastShown = "io.flutter.analytics.toastShown";
private static Analytics analytics;
private boolean toolWindowsInitialized = false;
@Override
public void runActivity(@NotNull Project project) {
// Convert all modules of deprecated type FlutterModuleType.
if (FlutterModuleUtils.convertFromDeprecatedModuleType(project)) {
// If any modules were converted over, create a notification
FlutterMessages.showInfo(
FlutterBundle.message("flutter.initializer.module.converted.title"),
"Converted from '" + FlutterModuleUtils.DEPRECATED_FLUTTER_MODULE_TYPE_ID +
"' to '" + FlutterModuleUtils.getModuleTypeIDForFlutter() + "'.",
project);
}
// Disable the 'Migrate Project to Gradle' notification.
FlutterUtils.disableGradleProjectMigrationNotification(project);
// Start watching for devices.
DeviceService.getInstance(project);
// Start a DevTools server
DevToolsService.getInstance(project);
// If the project declares a Flutter dependency, do some extra initialization.
boolean hasFlutterModule = false;
for (Module module : ModuleManager.getInstance(project).getModules()) {
final boolean declaresFlutter = FlutterModuleUtils.declaresFlutter(module);
hasFlutterModule = hasFlutterModule || declaresFlutter;
if (!declaresFlutter) {
continue;
}
// Ensure SDKs are configured; needed for clean module import.
FlutterModuleUtils.enableDartSDK(module);
for (PubRoot root : PubRoots.forModule(module)) {
// Set Android SDK.
if (root.hasAndroidModule(project)) {
ensureAndroidSdk(project);
}
// Setup a default run configuration for 'main.dart' (if it's not there already and the file exists).
FlutterModuleUtils.autoCreateRunConfig(project, root);
// If there are no open editors, show main.
if (FileEditorManager.getInstance(project).getOpenFiles().length == 0) {
FlutterModuleUtils.autoShowMain(project, root);
}
}
}
if (hasFlutterModule || WorkspaceCache.getInstance(project).isBazel()) {
initializeToolWindows(project);
} else {
project.getMessageBus().connect().subscribe(ProjectTopics.MODULES, new ModuleListener() {
@Override
public void moduleAdded(@NotNull Project project, @NotNull Module module) {
if (!toolWindowsInitialized && FlutterModuleUtils.isFlutterModule(module)) {
initializeToolWindows(project);
}
}
});
}
if (hasFlutterModule) {
// Ensure a run config is selected and ready to go.
FlutterModuleUtils.ensureRunConfigSelected(project);
}
if (hasFlutterModule) {
// Check to see if we're on a supported version of Android Studio; warn otherwise.
performAndroidStudioCanaryCheck(project);
}
FlutterRunNotifications.init(project);
// Start watching for survey triggers.
FlutterSurveyNotifications.init(project);
// Start the widget perf manager.
FlutterWidgetPerfManager.init(project);
// Watch save actions for reload on save.
FlutterReloadManager.init(project);
// Watch save actions for format on save.
FlutterSaveActionsManager.init(project);
// Start watching for project structure and .packages file changes.
final FlutterPluginsLibraryManager libraryManager = new FlutterPluginsLibraryManager(project);
libraryManager.startWatching();
// Initialize the analytics notification group.
NotificationsConfiguration.getNotificationsConfiguration().register(
Analytics.GROUP_DISPLAY_ID,
NotificationDisplayType.STICKY_BALLOON,
false);
// Set our preferred settings for the run console.
FlutterConsoleLogManager.initConsolePreferences();
// Initialize analytics.
final PropertiesComponent properties = PropertiesComponent.getInstance();
if (!properties.getBoolean(analyticsToastShown)) {
properties.setValue(analyticsToastShown, true);
final Notification notification = new Notification(
Analytics.GROUP_DISPLAY_ID,
"Welcome to Flutter!",
FlutterBundle.message("flutter.analytics.notification.content"),
NotificationType.INFORMATION,
(notification1, event) -> {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if ("url".equals(event.getDescription())) {
BrowserUtil.browse("https://www.google.com/policies/privacy/");
}
}
});
//noinspection DialogTitleCapitalization
notification.addAction(new AnAction("Sounds good!") {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
notification.expire();
final Analytics analytics = getAnalytics();
// We only track for flutter projects.
if (FlutterModuleUtils.declaresFlutter(project)) {
ToolWindowTracker.track(project, analytics);
}
}
});
//noinspection DialogTitleCapitalization
notification.addAction(new AnAction("No thanks") {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
notification.expire();
setCanReportAnalytics(false);
}
});
Notifications.Bus.notify(notification, project);
}
else {
// We only track for flutter projects.
if (FlutterModuleUtils.declaresFlutter(project)) {
ToolWindowTracker.track(project, getAnalytics());
}
}
}
private void initializeToolWindows(@NotNull Project project) {
// Start watching for Flutter debug active events.
FlutterViewFactory.init(project);
FlutterPerformanceViewFactory.init(project);
PreviewViewFactory.init(project);
toolWindowsInitialized = true;
}
/**
* Automatically set Android SDK based on ANDROID_HOME.
*/
private void ensureAndroidSdk(@NotNull Project project) {
if (ProjectRootManager.getInstance(project).getProjectSdk() != null) {
return; // Don't override user's settings.
}
final IntelliJAndroidSdk wanted = IntelliJAndroidSdk.fromEnvironment();
if (wanted == null) {
return; // ANDROID_HOME not set or Android SDK not created in IDEA; not clear what to do.
}
ApplicationManager.getApplication().runWriteAction(() -> wanted.setCurrent(project));
}
/**
* This method is only used for testing.
*/
@VisibleForTesting
public static void setAnalytics(Analytics inAnalytics) {
analytics = inAnalytics;
}
@NotNull
public static Analytics getAnalytics() {
if (analytics == null) {
final PropertiesComponent properties = PropertiesComponent.getInstance();
String clientId = properties.getValue(analyticsClientIdKey);
if (clientId == null) {
clientId = UUID.randomUUID().toString();
properties.setValue(analyticsClientIdKey, clientId);
}
final IdeaPluginDescriptor descriptor = PluginManagerCore.getPlugin(FlutterUtils.getPluginId());
assert descriptor != null;
final ApplicationInfo info = ApplicationInfo.getInstance();
analytics = new Analytics(clientId, descriptor.getVersion(), info.getVersionName(), info.getFullVersion());
// Set up reporting prefs.
analytics.setCanSend(getCanReportAnalytics());
// Send initial loading hit.
analytics.sendScreenView("main");
FlutterSettings.getInstance().sendSettingsToAnalytics(analytics);
}
return analytics;
}
public static boolean getCanReportAnalytics() {
final PropertiesComponent properties = PropertiesComponent.getInstance();
return !properties.getBoolean(analyticsOptOutKey, false);
}
public static void setCanReportAnalytics(boolean canReportAnalytics) {
if (getCanReportAnalytics() != canReportAnalytics) {
final boolean wasReporting = getCanReportAnalytics();
final PropertiesComponent properties = PropertiesComponent.getInstance();
properties.setValue(analyticsOptOutKey, !canReportAnalytics);
if (analytics != null) {
analytics.setCanSend(getCanReportAnalytics());
}
if (!wasReporting && canReportAnalytics) {
getAnalytics().sendScreenView("main");
}
}
}
public static void sendAnalyticsAction(@NotNull AnAction action) {
sendAnalyticsAction(action.getClass().getSimpleName());
}
public static void sendAnalyticsAction(@NotNull String name) {
getAnalytics().sendEvent("intellij", name);
}
private static void performAndroidStudioCanaryCheck(Project project) {
if (!FlutterUtils.isAndroidStudio()) {
return;
}
final ApplicationInfo info = ApplicationInfo.getInstance();
if (info.getFullVersion().contains("Canary") && !info.getBuild().isSnapshot()) {
FlutterMessages.showWarning(
"Unsupported Android Studio version",
"Canary versions of Android Studio are not supported by the Flutter plugin.",
project);
}
}
}