first commit

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile
2020-06-05 17:01:18 +08:00
parent b2e258cc82
commit faa9c83e58
13 changed files with 583 additions and 0 deletions
@@ -0,0 +1,58 @@
package io.zhile.research.intellij;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ApplicationComponent;
import io.zhile.research.intellij.action.ResetAction;
import io.zhile.research.intellij.helper.Constants;
import io.zhile.research.intellij.helper.NotificationHelper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.prefs.Preferences;
public class MainComponent implements ApplicationComponent {
private static final long RESET_PERIOD = 2160000000L; // 25 days
public void initComponent() {
Preferences prefs = Preferences.userRoot().node(Constants.PLUGIN_NAME);
long lastResetTime = prefs.getLong(Constants.RESET_TIME_KEY, 0L);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (lastResetTime > 0) {
Date date = new Date(lastResetTime);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
NotificationHelper.showInfo(null, "The last reset time: " + format.format(date));
}
new ResetTimerTask(lastResetTime).run();
}
}, 3000);
}
protected static class ResetTimerTask extends TimerTask {
private final long lastResetTime;
public ResetTimerTask(long lastResetTime) {
this.lastResetTime = lastResetTime;
}
@Override
public void run() {
if (System.currentTimeMillis() - lastResetTime > RESET_PERIOD) {
String message = "It has been a long time since the last reset!\nWould you like to reset it again?";
Notification notification = NotificationHelper.NOTIFICATION_GROUP.createNotification(Constants.PLUGIN_NAME, null, message, NotificationType.INFORMATION);
notification.addAction(new ResetAction());
notification.notify(null);
}
new Timer().schedule(new ResetTimerTask(lastResetTime), 3600000); // 60 min
}
}
}
@@ -0,0 +1,97 @@
package io.zhile.research.intellij.action;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import io.zhile.research.intellij.helper.Constants;
import io.zhile.research.intellij.helper.NotificationHelper;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.nio.file.Paths;
import java.util.prefs.Preferences;
public class ResetAction extends AnAction {
private static final String DEVICE_ID_KEY = "device_id";
private static final String NEW_MACHINE_ID_KEY = "user_id_on_machine";
private static final String OLD_MACHINE_ID_KEY = "JetBrains.UserIdOnMachine";
private static final String DEFAULT_COMPANY_NAME = "jetbrains";
private static final String PRODUCT_NAME = ApplicationNamesInfo.getInstance().getProductName();
public ResetAction() {
super("Reset " + PRODUCT_NAME + "'s Eval", "Reset my IDE eval information", AllIcons.General.Reset);
}
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
Project project = anActionEvent.getProject();
File evalFile = getEvalFile();
if (evalFile.exists()) {
if (!FileUtil.delete(evalFile)) {
NotificationHelper.showError(project, "Remove eval folder failed!");
return;
}
}
ApplicationInfoEx appInfo = ApplicationInfoImpl.getShadowInstance();
String companyName = appInfo.getShortCompanyName();
String node = StringUtil.isEmptyOrSpaces(companyName) ? DEFAULT_COMPANY_NAME : companyName.toLowerCase();
Preferences prefs = Preferences.userRoot().node(node);
Preferences.userRoot().remove(OLD_MACHINE_ID_KEY);
prefs.remove(NEW_MACHINE_ID_KEY);
prefs.remove(DEVICE_ID_KEY);
Preferences.userRoot().node(Constants.PLUGIN_NAME).put(Constants.RESET_TIME_KEY, Long.toString(System.currentTimeMillis()));
if (appInfo.isVendorJetBrains() && SystemInfo.isWindows) {
String[] names = new String[]{"PermanentUserId", "PermanentDeviceId"};
for (String name : names) {
if (deleteSharedFile(name)) {
continue;
}
NotificationHelper.showError(project, "Remove " + name + " file failed!");
}
}
MessageDialogBuilder.YesNo dialog = MessageDialogBuilder.yesNo(Constants.PLUGIN_NAME, "Reset successfully!\nWould you like to restart your IDE?");
if (dialog.isYes()) {
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().exit(true, false, true));
}
}
@Override
public boolean isDumbAware() {
return false;
}
protected boolean deleteSharedFile(String fileName) {
String appData = System.getenv("APPDATA");
if (appData == null) {
return false;
}
File dir = Paths.get(appData, "JetBrains", fileName).toFile();
return dir.delete();
}
protected File getEvalFile() {
String configPath = PathManager.getConfigPath();
return new File(configPath, "eval");
}
}
@@ -0,0 +1,14 @@
package io.zhile.research.intellij.helper;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.cl.PluginClassLoader;
import com.intellij.openapi.extensions.PluginId;
public class Constants {
public static final PluginClassLoader CLASS_LOADER = (PluginClassLoader) Constants.class.getClassLoader();
public static final IdeaPluginDescriptor PLUGIN_DESCRIPTOR = CLASS_LOADER.getPluginDescriptor();
public static final PluginId PLUGIN_ID = CLASS_LOADER.getPluginId();
public static final String PLUGIN_NAME = PLUGIN_DESCRIPTOR == null ? "ide-eval-resetter" : PLUGIN_DESCRIPTOR.getName();
public static final String RESET_TIME_KEY = "trail_reset_time";
}
@@ -0,0 +1,59 @@
package io.zhile.research.intellij.helper;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nullable;
public class NotificationHelper {
public static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup(Constants.PLUGIN_ID.getIdString(), NotificationDisplayType.BALLOON, true);
public static Notification show(@Nullable Project project, String title, String subtitle, String content, NotificationType type) {
if (title == null) {
title = Constants.PLUGIN_NAME;
}
Notification notification = NOTIFICATION_GROUP.createNotification(title, subtitle, content, type);
notification.notify(project);
return notification;
}
public static Notification showError(@Nullable Project project, String title, String subtitle, String content) {
return show(project, title, subtitle, content, NotificationType.ERROR);
}
public static Notification showError(@Nullable Project project, String title, String content) {
return showError(project, title, null, content);
}
public static Notification showError(@Nullable Project project, String content) {
return showError(project, null, null, content);
}
public static Notification showWarn(@Nullable Project project, String title, String subtitle, String content) {
return show(project, title, subtitle, content, NotificationType.WARNING);
}
public static Notification showWarn(@Nullable Project project, String title, String content) {
return showWarn(project, title, null, content);
}
public static Notification showWarn(@Nullable Project project, String content) {
return showWarn(project, null, null, content);
}
public static Notification showInfo(@Nullable Project project, String title, String subtitle, String content) {
return show(project, title, subtitle, content, NotificationType.INFORMATION);
}
public static Notification showInfo(@Nullable Project project, String title, String content) {
return showInfo(project, title, null, content);
}
public static Notification showInfo(@Nullable Project project, String content) {
return showInfo(project, null, null, content);
}
}
+24
View File
@@ -0,0 +1,24 @@
<idea-plugin>
<id>io.zhile.research.ide-eval-resetter</id>
<name>ide-eval-resetter</name>
<vendor url="https://zhile.io">zhile.io</vendor>
<description><![CDATA[
I can reset your IDE eval information.<br>
<em>Click "Help" menu and select "Reset IDE Eval"</em>
]]></description>
<depends>com.intellij.modules.lang</depends>
<actions>
<action class="io.zhile.research.intellij.action.ResetAction" id="io.zhile.research.intellij.action.ResetAction">
<add-to-group group-id="HelpMenu" anchor="last"/>
<add-to-group group-id="WelcomeScreen.Documentation" anchor="last"/>
</action>
</actions>
<application-components>
<component>
<implementation-class>io.zhile.research.intellij.MainComponent</implementation-class>
</component>
</application-components>
</idea-plugin>