Skip to content

Commit

Permalink
gapic: Check for updates every 8 hours
Browse files Browse the repository at this point in the history
Unless the user has opted out of checking.
  • Loading branch information
ben-clayton committed Aug 3, 2017
1 parent 07bd109 commit f74360c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
31 changes: 31 additions & 0 deletions gapic/src/main/com/google/gapid/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.gapid.util.MacApplication;
import com.google.gapid.util.Messages;
import com.google.gapid.util.OS;
import com.google.gapid.util.UpdateWatcher;
import com.google.gapid.views.AtomTree;
import com.google.gapid.views.ContextSelector;
import com.google.gapid.views.FramebufferView;
Expand All @@ -59,6 +60,12 @@
import com.google.gapid.widgets.TabArea.TabInfo;
import com.google.gapid.widgets.Widgets;

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 org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
Expand All @@ -85,6 +92,7 @@
* The main {@link ApplicationWindow} containing all of the UI components.
*/
public class MainWindow extends ApplicationWindow {
private static final Logger LOG = Logger.getLogger(MainWindow.class.getName());
protected final Client client;
protected final ModelsAndWidgets maw;
protected Action gotoAtom, gotoMemory;
Expand Down Expand Up @@ -266,9 +274,32 @@ public void onStateFollowed(Path.Any path) {
tabs.showTab(MainTab.Type.ApiState);
}
});

watchForUpdates();

return shell;
}

private void watchForUpdates() {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
new UpdateWatcher(maw.models().settings, client, (release) -> {
try {
URI uri = new URI(release.getBrowserUrl());
statusBar.setNotification("New update available", () -> {
try {
desktop.browse(uri);
} catch (IOException e) {
LOG.log(Level.WARNING, "Couldn't launch browser", e);
}
});
} catch (URISyntaxException e) {
LOG.log(Level.WARNING, "Invalid release URI", e);
};
});
}
}

@Override
public boolean close() {
if (super.close()) {
Expand Down
26 changes: 26 additions & 0 deletions gapic/src/main/com/google/gapid/models/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public class Settings {
public boolean skipWelcomeScreen = false;
public String[] recentFiles = new String[0];
public String adb = "";
public boolean autoCheckForUpdates = true;
public boolean updateAvailable = false;
public long lastCheckForUpdates = 0; // milliseconds since January 1, 1970, 00:00:00 GMT.

public static Settings load() {
Settings result = new Settings();
Expand Down Expand Up @@ -171,6 +174,9 @@ private void updateFrom(Properties properties) {
skipWelcomeScreen = getBoolean(properties, "skip.welcome");
recentFiles = getStringList(properties, "open.recent", recentFiles);
adb = tryFindAdb(properties.getProperty("adb.path", ""));
autoCheckForUpdates = getBoolean(properties, "updates.autoCheck", autoCheckForUpdates);
lastCheckForUpdates = getLong(properties, "updates.lastCheck", 0);
updateAvailable = getBoolean(properties, "updates.available", updateAvailable);
}

private void updateTo(Properties properties) {
Expand Down Expand Up @@ -204,6 +210,9 @@ private void updateTo(Properties properties) {
properties.setProperty("skip.welcome", Boolean.toString(skipWelcomeScreen));
setStringList(properties, "open.recent", recentFiles);
properties.setProperty("adb.path", adb);
properties.setProperty("updates.autoCheck", Boolean.toString(autoCheckForUpdates));
properties.setProperty("updates.lastCheck", Long.toString(lastCheckForUpdates));
properties.setProperty("updates.available", Boolean.toString(updateAvailable));
}

private static Point getPoint(Properties properties, String name) {
Expand All @@ -224,10 +233,27 @@ private static int getInt(Properties properties, String name, int dflt) {
}
}

private static long getLong(Properties properties, String name, long dflt) {
String value = properties.getProperty(name);
if (value == null) {
return dflt;
}

try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
return dflt;
}
}

private static boolean getBoolean(Properties properties, String name) {
return "true".equalsIgnoreCase(properties.getProperty(name, ""));
}

private static boolean getBoolean(Properties properties, String name, boolean dflt) {
return "true".equalsIgnoreCase(properties.getProperty(name, dflt ? "true" : "false"));
}

private static int[] getIntList(Properties properties, String name, int[] dflt) {
String value = properties.getProperty(name);
if (value == null) {
Expand Down
80 changes: 80 additions & 0 deletions gapic/src/main/com/google/gapid/util/UpdateWatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gapid.util;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.gapid.models.Settings;
import com.google.gapid.proto.service.Service.Release;
import com.google.gapid.server.Client;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
* Utility class for checking for new releases of GAPID.
*/
public class UpdateWatcher {
private static final int CHECK_INTERVAL_HOURS = 8;
private static final long CHECK_INTERVAL_MS = CHECK_INTERVAL_HOURS * 60 * 60 * 1000;
private static final boolean INCLUDE_PRE_RELEASES = true;

private final Settings settings;
private final Client client;
private final Listener listener;

/** Callback interface */
public interface Listener {
/** Called whenever a new release is found. */
void onNewReleaseAvailable(Release release);
}

public UpdateWatcher(Settings settings, Client client, Listener listener) {
this.settings = settings;
this.client = client;
this.listener = listener;
if (settings.updateAvailable) {
Scheduler.EXECUTOR.schedule(this::doCheck, 0, TimeUnit.MILLISECONDS);
} else {
scheduleCheck();
}
}

private void scheduleCheck() {
long now = new Date().getTime();
long timeSinceLastUpdateMS = now - settings.lastCheckForUpdates;
long delay = Math.max(CHECK_INTERVAL_MS - timeSinceLastUpdateMS, 0);
Scheduler.EXECUTOR.schedule(this::doCheck, delay, TimeUnit.MILLISECONDS);
}

private void doCheck() {
if (settings.autoCheckForUpdates) {
ListenableFuture<Release> future = client.checkForUpdates(INCLUDE_PRE_RELEASES);
settings.updateAvailable = false;
try {
Release release = future.get();
if (release != null) {
settings.updateAvailable = true;
listener.onNewReleaseAvailable(release);
}
} catch (InterruptedException | ExecutionException e) {
/* never mind */
}
}
settings.lastCheckForUpdates = new Date().getTime();
settings.save();
scheduleCheck();
}
}
8 changes: 8 additions & 0 deletions gapic/src/main/com/google/gapid/views/SettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import com.google.gapid.util.Messages;
import com.google.gapid.widgets.FileTextbox;

import com.google.gapid.widgets.Widgets;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
Expand All @@ -38,6 +40,7 @@
*/
public class SettingsDialog extends TitleAreaDialog {
private final Settings settings;
private Button autoCheckForUpdates;
private FileTextbox adbPath;

public SettingsDialog(Shell parent, Settings settings) {
Expand All @@ -50,6 +53,7 @@ public static void showSettingsDialog(Shell shell, Settings settings) {
}

private void update() {
settings.autoCheckForUpdates = autoCheckForUpdates.getSelection();
settings.adb = adbPath.getText().trim();
}

Expand All @@ -76,6 +80,10 @@ protected Control createDialogArea(Composite parent) {

Composite container = createComposite(area, new GridLayout(2, false));
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

createLabel(container, "Automatically check for updates:");
autoCheckForUpdates = Widgets.createCheckbox(container, "", settings.autoCheckForUpdates);

createLabel(container, "Path to adb:*");
adbPath = withLayoutData(new FileTextbox.File(container, settings.adb) {
@Override
Expand Down

0 comments on commit f74360c

Please sign in to comment.