Manage user session (active, idle) on swing app

View: New views
5 Messages — Rating Filter:   Alert me  

Manage user session (active, idle) on swing app

by cseguin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,



I'm working on a desktop app built on nb 5.5 .



I need to monitor user activity in order to block the app after a given time idle.



Any ideas?

------------------------
Carlos.





Manage user session (active, idle) on swing app

by cseguin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've been reading all docs about lookup api, nodeAction, GlobalContext....



and at this point I'm not sure if there is way to detect if the user is doing somenthing on my netbeans app or if it is idle.



For instance, let say the user has my app opened but is not using it and is using any other app on the compueter.



Will the nodes, dataobjects etc inactive or the last being active will be always active even if nobody is using it?



That is to say, is it possible to check for active nodes hoping to find none of them active? Or there will always be one active?



Thanks all

------------------------
Carlos.





Re: Manage user session (active, idle) on swing app

by Tim Boudreau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cseguin wrote:
I've been reading all docs about lookup api, nodeAction, GlobalContext....
You could listen on the main window's active/inactive state, although this won't tell you if the user walked away from the computer and left your app's main window active.

For something *that* general, you probably don't need anything special from NetBeans - just use an AWTEventListener and a timer - listen to all input events in all windows of the app globally.  If too much time goes by between events, the app is idle.  Something like this:

class IdleChecker implements AWTEventListener, ActionListener {
    private static final int TIMEOUT = 120000; //2 minutes
    private final Timer timer = new Timer(TIMEOUT, this);
    private IdleChecker() {
        timer.setRepeats(false);
        timer.start();
    }
    static void init() {
        IdleChecker main = new IdleChecker();
        //Detect all focus events in the system.  Ignore mouse events
        //because mouseEntered() can happen w/o the window being active
        Toolkit.getDefaultToolkit().addAWTEventListener(main,
                AWTEvent.KEY_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
    }
    public void eventDispatched(AWTEvent event) {
        //Force the timer to start counting from 0 again
        timer.restart();
    }
    public void actionPerformed(ActionEvent e) {
        //System has been idle > TIMEOUT milliseconds - do whatever here
    }
}

-Tim

Re: Manage user session (active, idle) on swing app

by Tim Boudreau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Tim Boudreau wrote:
cseguin wrote:
I've been reading all docs about lookup api, nodeAction, GlobalContext....
You could listen on the main window's active/inactive state, although this won't tell you if the user walked away from the computer and left your app's main window active.

For something *that* general, you probably don't need anything special from NetBeans - just use an AWTEventListener and a timer - listen to all input events in all windows of the app globally.  
Bear in mind that it is not terribly polite for applications to wake up and start doing work when they are not the active window on the user's desktop - if the user's machine is memory constrained, this can force your app to be swapped back into memory while the user is trying to do something else in another application, impacting that application's performance.  

So this sort of thing is to be avoided where possible - in the IDE we try to have NetBeans be completely quiet when not active.  However, if you need to, you need to, and it sounds like you do.

-Tim

Manage user session (active, idle) on swing app

by cseguin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Tim for your help. It is indeed a clean solution, exactly what I/we need here.



I was blinded looking for the answer just on the platform itself and should have looked deeper.




>

> Bear in mind that it is not terribly polite for applications to wake up and

> start doing work when they are not the active window on the user's desktop -

>




Well, I do not have much choice here, apart to inform about the drawback which I will.





Thanks again.



Best regards,

Carlos

------------------------
Carlos.