|
5 | 5 | */
|
6 | 6 | package io.flutter;
|
7 | 7 |
|
| 8 | +import com.intellij.ide.BrowserUtil; |
| 9 | +import com.intellij.ide.plugins.IdeaPluginDescriptor; |
| 10 | +import com.intellij.ide.plugins.PluginManager; |
| 11 | +import com.intellij.ide.util.PropertiesComponent; |
| 12 | +import com.intellij.notification.Notification; |
| 13 | +import com.intellij.notification.NotificationType; |
| 14 | +import com.intellij.notification.Notifications; |
| 15 | +import com.intellij.openapi.actionSystem.AnAction; |
| 16 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 17 | +import com.intellij.openapi.extensions.PluginId; |
8 | 18 | import com.intellij.openapi.project.Project;
|
9 | 19 | import com.intellij.openapi.startup.StartupActivity;
|
| 20 | +import io.flutter.analytics.Analytics; |
10 | 21 | import io.flutter.run.daemon.FlutterDaemonService;
|
11 | 22 | import org.jetbrains.annotations.NotNull;
|
12 | 23 |
|
| 24 | +import javax.swing.event.HyperlinkEvent; |
| 25 | +import java.util.UUID; |
| 26 | + |
13 | 27 | public class FlutterInitializer implements StartupActivity {
|
| 28 | + private static final String analyticsClientIdKey = "io.flutter.analytics.clientId"; |
| 29 | + private static final String analyticsOptOutKey = "io.flutter.analytics.optOut"; |
| 30 | + private static final String analyticsToastShown = "io.flutter.analytics.toastShown"; |
| 31 | + |
| 32 | + private static Analytics analytics; |
| 33 | + |
| 34 | + @NotNull |
| 35 | + public static Analytics getAnalytics() { |
| 36 | + if (analytics == null) { |
| 37 | + final PropertiesComponent properties = PropertiesComponent.getInstance(); |
| 38 | + |
| 39 | + String clientId = properties.getValue(analyticsClientIdKey); |
| 40 | + if (clientId == null) { |
| 41 | + clientId = UUID.randomUUID().toString(); |
| 42 | + properties.setValue(analyticsClientIdKey, clientId); |
| 43 | + } |
| 44 | + |
| 45 | + final IdeaPluginDescriptor descriptor = PluginManager.getPlugin(PluginId.getId("io.flutter")); |
| 46 | + assert descriptor != null; |
| 47 | + analytics = new Analytics(clientId, descriptor.getVersion()); |
| 48 | + |
| 49 | + // Set up reporting prefs. |
| 50 | + analytics.setCanSend(getCanReportAnalytics()); |
| 51 | + |
| 52 | + // Send initial loading hit. |
| 53 | + analytics.sendScreenView("main"); |
| 54 | + } |
| 55 | + |
| 56 | + return analytics; |
| 57 | + } |
| 58 | + |
| 59 | + public static void setCanReportAnalaytics(boolean canReportAnalaytics) { |
| 60 | + if (getCanReportAnalytics() != canReportAnalaytics) { |
| 61 | + final boolean wasReporting = getCanReportAnalytics(); |
| 62 | + |
| 63 | + final PropertiesComponent properties = PropertiesComponent.getInstance(); |
| 64 | + properties.setValue(analyticsOptOutKey, !canReportAnalaytics); |
| 65 | + if (analytics != null) { |
| 66 | + analytics.setCanSend(getCanReportAnalytics()); |
| 67 | + } |
| 68 | + |
| 69 | + if (!wasReporting && canReportAnalaytics) { |
| 70 | + getAnalytics().sendScreenView("main"); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static boolean getCanReportAnalytics() { |
| 76 | + final PropertiesComponent properties = PropertiesComponent.getInstance(); |
| 77 | + return !properties.getBoolean(analyticsOptOutKey, false); |
| 78 | + } |
| 79 | + |
| 80 | + public static void sendActionEvent(@NotNull AnAction action) { |
| 81 | + getAnalytics().sendEvent("intellij", action.getClass().getSimpleName()); |
| 82 | + } |
14 | 83 |
|
15 | 84 | @Override
|
16 | 85 | public void runActivity(@NotNull Project project) {
|
| 86 | + // Initialize the daemon service (this starts a device watcher). |
17 | 87 | FlutterDaemonService.getInstance(project);
|
| 88 | + |
| 89 | + // Initialize analytics. |
| 90 | + final PropertiesComponent properties = PropertiesComponent.getInstance(); |
| 91 | + if (!properties.getBoolean(analyticsToastShown)) { |
| 92 | + properties.setValue(analyticsToastShown, true); |
| 93 | + |
| 94 | + final Notification notification = new Notification( |
| 95 | + FlutterErrors.FLUTTER_NOTIFICATION_GOUP_ID, |
| 96 | + FlutterBundle.message("flutter.analytics.notification.title"), |
| 97 | + FlutterBundle.message("flutter.analytics.notification.content"), |
| 98 | + NotificationType.INFORMATION, |
| 99 | + (notification1, event) -> { |
| 100 | + if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { |
| 101 | + if ("url".equals(event.getDescription())) { |
| 102 | + BrowserUtil.browse("https://www.google.com/policies/privacy/"); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + notification.addAction(new AnAction(FlutterBundle.message("flutter.analytics.notification.accept")) { |
| 107 | + @Override |
| 108 | + public void actionPerformed(AnActionEvent event) { |
| 109 | + notification.expire(); |
| 110 | + getAnalytics(); |
| 111 | + } |
| 112 | + }); |
| 113 | + notification.addAction(new AnAction(FlutterBundle.message("flutter.analytics.notification.decline")) { |
| 114 | + @Override |
| 115 | + public void actionPerformed(AnActionEvent event) { |
| 116 | + notification.expire(); |
| 117 | + setCanReportAnalaytics(false); |
| 118 | + } |
| 119 | + }); |
| 120 | + Notifications.Bus.notify(notification); |
| 121 | + } |
| 122 | + else { |
| 123 | + getAnalytics(); |
| 124 | + } |
18 | 125 | }
|
19 | 126 | }
|
0 commit comments