首页>代码>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日
最近浏览更多
YafengLiang  LV15 6月9日
酷少小新  LV2 6月7日
dongxiaoyu  LV1 5月15日
597117933  LV9 5月13日
栈道小生  LV10 4月24日
ninuxf  LV12 4月24日
15515998549  LV1 4月22日
慵懒的小橘猫  LV11 4月11日
jackychen1012  LV2 3月29日
niuwa666 3月27日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友