首页>代码>Idea插件,延长试用时间>/ide-eval-resetter-master/src/main/java/io/zhile/research/intellij/MainComponent.java
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 org.jetbrains.annotations.NotNull;

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.PRODUCT_NAME + Constants.PRODUCT_HASH, 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
        }
    }

    @Override
    public void disposeComponent() {
        // for compatibility
    }

    @Override
    public @NotNull
    String getComponentName() {
        return getClass().getName(); // for compatibility
    }
}
最近下载更多
最代码官方  LV168 2024年11月3日
最近浏览更多
yigu123  LV6 10月22日
sos0531  LV8 10月13日
perfect  LV17 10月11日
梁小贤  LV1 9月12日
vincemokea  LV9 9月8日
夜起星河  LV8 8月15日
锦州队长  LV2 7月22日
阿凡达  LV9 7月21日
windstorm 6月30日
暂无贡献等级
YafengLiang  LV15 6月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友