From fd5e2d27b104f5fc96c5e367074c00e9e132d291 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Tue, 9 Jun 2020 12:05:04 +0800 Subject: [PATCH] compatibility update Signed-off-by: pengzhile --- build.gradle | 2 +- .../research/intellij/MainComponent.java | 15 ++++++++++-- .../research/intellij/action/ResetAction.java | 24 +++++++++---------- .../research/intellij/helper/Constants.java | 11 ++++----- .../intellij/helper/NotificationHelper.java | 3 ++- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index b9da8f8..d04c0d4 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'io.zhile.research.intellij' -version '1.0.1' +version '1.0.2' repositories { mavenCentral() diff --git a/src/main/java/io/zhile/research/intellij/MainComponent.java b/src/main/java/io/zhile/research/intellij/MainComponent.java index 5b0332b..ab22119 100644 --- a/src/main/java/io/zhile/research/intellij/MainComponent.java +++ b/src/main/java/io/zhile/research/intellij/MainComponent.java @@ -6,6 +6,7 @@ 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 org.jetbrains.annotations.NotNull; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -19,8 +20,7 @@ public class MainComponent implements ApplicationComponent { public void initComponent() { Preferences prefs = Preferences.userRoot().node(Constants.PLUGIN_NAME); - long lastResetTime = prefs.getLong(Constants.PRODUCT_NAME + Constants.RESET_TIME_KEY, 0L); - + long lastResetTime = prefs.getLong(Constants.PRODUCT_NAME + Constants.PRODUCT_HASH, 0L); new Timer().schedule(new TimerTask() { @Override @@ -55,4 +55,15 @@ public class MainComponent implements ApplicationComponent { new Timer().schedule(new ResetTimerTask(lastResetTime), 3600000); // 60 min } } + + @Override + public void disposeComponent() { + // for compatibility + } + + @Override + public @NotNull + String getComponentName() { + return getClass().getName(); // for compatibility + } } diff --git a/src/main/java/io/zhile/research/intellij/action/ResetAction.java b/src/main/java/io/zhile/research/intellij/action/ResetAction.java index da38728..980862d 100644 --- a/src/main/java/io/zhile/research/intellij/action/ResetAction.java +++ b/src/main/java/io/zhile/research/intellij/action/ResetAction.java @@ -8,7 +8,6 @@ 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; @@ -18,11 +17,10 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.nio.file.Paths; +import java.util.prefs.BackingStoreException; 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"; @@ -45,13 +43,16 @@ public class ResetAction extends AnAction { 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); + try { + Preferences.userRoot().remove(OLD_MACHINE_ID_KEY); + Preferences.userRoot().node(node).removeNode(); + } catch (BackingStoreException e) { + NotificationHelper.showError(project, e.getMessage()); + return; + } - Preferences.userRoot().node(Constants.PLUGIN_NAME).put(Constants.PRODUCT_NAME + Constants.RESET_TIME_KEY, Long.toString(System.currentTimeMillis())); + Preferences.userRoot().node(Constants.PLUGIN_NAME).put(Constants.PRODUCT_NAME + Constants.PRODUCT_HASH, Long.toString(System.currentTimeMillis())); if (appInfo.isVendorJetBrains() && SystemInfo.isWindows) { String[] names = new String[]{"PermanentUserId", "PermanentDeviceId"}; @@ -61,13 +62,12 @@ public class ResetAction extends AnAction { } NotificationHelper.showError(project, "Remove " + name + " file failed!"); + return; } } - 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)); - } + NotificationHelper.showInfo(project, "Reset successfully!\nPlease restart your IDE and enjoy it!"); + ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().restart()); } @Override diff --git a/src/main/java/io/zhile/research/intellij/helper/Constants.java b/src/main/java/io/zhile/research/intellij/helper/Constants.java index 4853cef..022775b 100644 --- a/src/main/java/io/zhile/research/intellij/helper/Constants.java +++ b/src/main/java/io/zhile/research/intellij/helper/Constants.java @@ -1,16 +1,15 @@ package io.zhile.research.intellij.helper; -import com.intellij.ide.plugins.IdeaPluginDescriptor; +import com.intellij.ide.plugins.PluginManager; import com.intellij.ide.plugins.cl.PluginClassLoader; import com.intellij.openapi.application.ApplicationNamesInfo; +import com.intellij.openapi.application.PathManager; 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 PRODUCT_NAME = ApplicationNamesInfo.getInstance().getProductName(); - - public static final String RESET_TIME_KEY = "trail_reset_time"; + public static final String PLUGIN_NAME = PluginManager.getPlugin(PLUGIN_ID).getName(); + public static final String PRODUCT_NAME = ApplicationNamesInfo.getInstance().getFullProductName(); + public static final String PRODUCT_HASH = Integer.toString(PathManager.getConfigPath().hashCode()); } diff --git a/src/main/java/io/zhile/research/intellij/helper/NotificationHelper.java b/src/main/java/io/zhile/research/intellij/helper/NotificationHelper.java index 4150fbb..92e19c1 100644 --- a/src/main/java/io/zhile/research/intellij/helper/NotificationHelper.java +++ b/src/main/java/io/zhile/research/intellij/helper/NotificationHelper.java @@ -1,5 +1,6 @@ package io.zhile.research.intellij.helper; +import com.intellij.icons.AllIcons; import com.intellij.notification.Notification; import com.intellij.notification.NotificationDisplayType; import com.intellij.notification.NotificationGroup; @@ -8,7 +9,7 @@ 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 final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup(Constants.PLUGIN_ID.getIdString(), NotificationDisplayType.BALLOON, true, null, AllIcons.General.Reset); public static Notification show(@Nullable Project project, String title, String subtitle, String content, NotificationType type) { if (title == null) {