Skip to content

Commit

Permalink
Idle detector (qzind#797)
Browse files Browse the repository at this point in the history
* Add idle detector to perform some 'slow the first time' actions

Co-authored-by: Tres Finocchiaro <[email protected]>
  • Loading branch information
Brett Berenz and tresf authored Jun 15, 2021
1 parent dbd6ca2 commit 04b2d6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
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(false, true);
});
// 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();
}
}

}
7 changes: 6 additions & 1 deletion src/qz/printer/PrintServiceMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@
public class PrintServiceMatcher {
private static final Logger log = LoggerFactory.getLogger(PrintServiceMatcher.class);

public static NativePrinterMap getNativePrinterList(boolean silent) {
public static NativePrinterMap getNativePrinterList(boolean silent, boolean withAttributes) {
NativePrinterMap printers = NativePrinterMap.getInstance();
printers.putAll(PrintServiceLookup.lookupPrintServices(null, null));
if(!silent) log.debug("Found {} printers", printers.size());
if(withAttributes) printers.values().forEach(NativePrinter::getDriverAttributes);
return printers;
}

public static NativePrinterMap getNativePrinterList(boolean silent) {
return getNativePrinterList(silent, false);
}

public static NativePrinterMap getNativePrinterList() {
return getNativePrinterList(false);
}
Expand Down
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

0 comments on commit 04b2d6e

Please sign in to comment.