forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlutterSurveyNotifications.java
123 lines (101 loc) · 4.32 KB
/
FlutterSurveyNotifications.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
/*
* Copyright 2019 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.survey;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.lang.dart.DartFileType;
import icons.FlutterIcons;
import io.flutter.FlutterMessages;
import io.flutter.pub.PubRoot;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class FlutterSurveyNotifications {
private static final int NOTIFICATION_DELAY_IN_SECS = 3;
private static final String FLUTTER_LAST_SURVEY_PROMPT_KEY = "FLUTTER_LAST_SURVEY_PROMPT_KEY";
private static final long PROMPT_INTERVAL_IN_MS = TimeUnit.HOURS.toMillis(40);
private static final String SURVEY_ACTION_TEXT = "Take survey";
private static final String SURVEY_DISMISSAL_TEXT = "No thanks";
interface FlutterSurveyNotifier {
void prompt();
}
@NotNull final Project myProject;
FlutterSurveyNotifications(@NotNull Project project) {
this.myProject = project;
}
public static void init(@NotNull Project project) {
project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
@Override
public void fileOpened(@NotNull final FileEditorManager source, @NotNull final VirtualFile file) {
check(file);
}
@Override
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
final VirtualFile file = event.getNewFile();
if (file != null) {
check(file);
}
}
private void check(@NotNull VirtualFile file) {
if (PubRoot.isPubspec(file) || file.getFileType() == DartFileType.INSTANCE) {
new FlutterSurveyNotifications(project).checkForDisplaySurvey();
}
}
});
}
private void checkForDisplaySurvey() {
final FlutterSurvey survey = FlutterSurveyService.getLatestSurveyContent();
if (survey == null) return;
// Limit display to the survey window.
if (!survey.isSurveyOpen()) return;
final PropertiesComponent properties = PropertiesComponent.getInstance();
// Don't prompt more often than every 40 hours.
final long lastPromptedMillis = properties.getLong(FLUTTER_LAST_SURVEY_PROMPT_KEY, 0);
if (System.currentTimeMillis() - lastPromptedMillis < PROMPT_INTERVAL_IN_MS) return;
properties.setValue(FLUTTER_LAST_SURVEY_PROMPT_KEY, String.valueOf(System.currentTimeMillis()));
// Or, if the survey has already been taken.
if (properties.getBoolean(survey.uniqueId)) return;
final Notification notification = new Notification(
FlutterMessages.FLUTTER_NOTIFICATION_GROUP_ID, survey.title, "", NotificationType.INFORMATION
).setIcon(FlutterIcons.Flutter);
notification.addAction(new AnAction(SURVEY_ACTION_TEXT) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
properties.setValue(survey.uniqueId, true);
notification.expire();
String url = survey.urlPrefix + "?Source=IntelliJ";
BrowserUtil.browse(url);
}
});
notification.addAction(new AnAction(SURVEY_DISMISSAL_TEXT) {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
properties.setValue(survey.uniqueId, true);
notification.expire();
}
});
// Display the prompt after a short delay.
try (var scheduler = Executors.newSingleThreadScheduledExecutor()) {
scheduler.schedule(() -> {
if (!myProject.isDisposed()) {
Notifications.Bus.notify(notification, myProject);
}
}, NOTIFICATION_DELAY_IN_SECS, TimeUnit.SECONDS);
scheduler.shutdown();
}
}
}