Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idle detector #797

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/qz/common/TrayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import qz.auth.Certificate;
import qz.auth.RequestState;
import qz.installer.shortcut.ShortcutCreator;
import qz.printer.PrintServiceMatcher;
import qz.printer.action.WebApp;
import qz.ui.*;
import qz.ui.component.IconCache;
import qz.ui.tray.TrayType;
Expand All @@ -32,8 +34,9 @@
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -76,6 +79,9 @@ public class TrayManager {
// Action to run when reload is triggered
private Thread reloadThread;

// Actions to run if idle after startup
private ArrayList<Timer> idleTimers;

public TrayManager() {
this(false);
}
Expand Down Expand Up @@ -162,7 +168,7 @@ public TrayManager(boolean isHeadless) {
try {
Thread.sleep(1000);
if (darkDesktopMode != SystemUtilities.isDarkDesktop(true) ||
darkTaskbarMode != SystemUtilities.isDarkTaskbar(true)) {
darkTaskbarMode != SystemUtilities.isDarkTaskbar(true)) {
darkDesktopMode = SystemUtilities.isDarkDesktop();
darkTaskbarMode = SystemUtilities.isDarkTaskbar();
iconCache.fixTrayIcons(darkTaskbarMode);
Expand All @@ -182,14 +188,34 @@ public TrayManager(boolean isHeadless) {
}
});
}
} catch(InterruptedException ignore) {}
}
catch(InterruptedException ignore) {}
}
}).start();
}

if (tray != null) {
addMenuItems();
}

// Initialize idle actions
idleTimers = new ArrayList<>();

// Slow to find printers the first time if a lot of printers are installed
performIfIdle((int)TimeUnit.SECONDS.toMillis(10), evt -> {
log.debug("IDLE: Performing first run of find printers");
PrintServiceMatcher.getNativePrinterList(true);
tresf marked this conversation as resolved.
Show resolved Hide resolved
});
// Slow to start JavaFX the first time
performIfIdle((int)TimeUnit.SECONDS.toMillis(60), evt -> {
log.debug("IDLE: Starting up JFX for HTML printing");
try {
WebApp.initialize();
}
catch(IOException e) {
log.error("Idle runner failed to preemptively start JavaFX service");
}
});
}

/**
Expand Down Expand Up @@ -647,4 +673,23 @@ public boolean isHeadless() {
return headless;
}

private void performIfIdle(int idleQualifier, ActionListener performer) {
Timer timer = new Timer(idleQualifier, evt -> {
performer.actionPerformed(evt);
idleTimers.remove(evt.getSource());
});
timer.setRepeats(false);
timer.start();

idleTimers.add(timer);
}

public void voidIdleActions() {
if (idleTimers.size() > 0) {
log.trace("Not idle, stopping any actions that haven't ran yet");
idleTimers.forEach(Timer::stop);
idleTimers.clear();
}
}

}
4 changes: 4 additions & 0 deletions src/qz/ws/PrintSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ private void processMessage(Session session, JSONObject json, SocketConnection c
return;
}

if (call != SocketMethod.GET_VERSION) {
trayManager.voidIdleActions();
}

// used in usb calls
DeviceOptions dOpts = new DeviceOptions(params, DeviceOptions.DeviceMode.parse(call.getCallName()));

Expand Down