Package org.wiztools.restclient.ui.update

Source Code of org.wiztools.restclient.ui.update.AppUpdateRunner

package org.wiztools.restclient.ui.update;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.wiztools.appupdate.Version;
import org.wiztools.appupdate.VersionImpl;
import org.wiztools.appupdate.VersionUrl;
import org.wiztools.appupdate.VersionWSUtil;
import org.wiztools.restclient.IGlobalOptions;
import org.wiztools.restclient.RCConstants;

/**
*
* @author subwiz
*/
public class AppUpdateRunner implements Runnable {
   
    @Inject private IGlobalOptions options;
   
    private static final Logger LOG = Logger.getLogger(AppUpdateRunner.class.getName());
   
    private static final String UPDATE_URL = "http://static.wiztools.org/v/restclient.json";
   
    private static final String PROP_UPDATE_CHECK_LAST = "update.check.last";
    private static final String PROP_UPDATE_CHECK_ENABLED = "update.check.enabled";
    private static final long TIME_GAP = 604800000l; // 1 week in millis
   
    private void openUrl(String url) {
        Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
        if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(new URI(url));
            }
            catch(URISyntaxException | IOException ex) {
                LOG.log(Level.INFO, "Error when opening browser", ex);
            }
        }
    }

    @Override
    public void run() {
       
        // Verify if the run needs to happen:
        final String strEnabled = options.getProperty(PROP_UPDATE_CHECK_ENABLED);
        if(strEnabled != null && strEnabled.equals("false")) {
            return;
        }
        final String strLastRun = options.getProperty(PROP_UPDATE_CHECK_LAST);
        if(strLastRun != null) {
            final long lastRun = Long.parseLong(strLastRun);
            if((lastRun+TIME_GAP) > System.currentTimeMillis()) {
                return;
            }
        }
       
        // Verify version:
        try {
            final VersionUrl data = VersionWSUtil.getLatestVersion(UPDATE_URL);
            Version latestVersion = data.getVersion();
            Version currentVersion = new VersionImpl(RCConstants.VERSION);
           
            // Newer version available:
            if(latestVersion.isGreaterThan(currentVersion)) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        final int opt = JOptionPane.showConfirmDialog(null,
                                "An updated version of RESTClient is available. Do you want to download?",
                                "Download update?",
                                JOptionPane.YES_NO_OPTION);
                        if(opt == JOptionPane.YES_OPTION) {
                            openUrl(data.getDlUrl());
                        }
                    }
                });
            }
           
            // Record last update verification time:
            options.setProperty(PROP_UPDATE_CHECK_LAST,
                    String.valueOf(System.currentTimeMillis()));
        }
        catch(IOException ex) {
            LOG.log(Level.INFO, "Cannot perform update check...", ex);
        }
    }
   
}
TOP

Related Classes of org.wiztools.restclient.ui.update.AppUpdateRunner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.