Question

I need to control the idle time of my app when the user is logged in.

This is my use-case:

When the idle time reaches the 4 minutes, an options dialog should be displayed with the option to close it.

IF the user closes the dialog the dialog just closes
ELSE
the timer for the idle time should just continue and when it reaches the 5 minutes (1 minute later) the dialog must be closed.

a new message dialog must be displayed for 5 seconds.
after the 5 seconds the new dialog must be closed.

I must execute some code I already have to logout the user.

Actually, I have a working code but it's kind of nebulous.

To know the idle time, I do this: DeviceInfo.getIdleTime().

The method is controlled by a RealtimeClockListener, so when the user logs in, I do UiApplication.getUiApplication().addRealtimeClockListener and when it logs out I just do UiApplication.getUiApplication().removeRealtimeClockListener.

I can post my code if needed but I really would like to see a new approach someone could suggest.

Update

This is the solution I'm using.

public static RealtimeClockListener realtimeClockListener = new RealtimeClockListener() {
        int _4Minutes = 60 * 4;
        int _5Minutes = 60 * 5;
        int _5Seconds = 5 * 1000;
        Dialog dialog4Minutes = null;
        Dialog dialog5Minutes = null;
        Timer timer5Seconds = null;
        TimerTask timerTask5Seconds = null;
        public void clockUpdated() {
            if ( Application.getApplication().isForeground() ) { 
                appInactiveTime = (int) DeviceInfo.getIdleTime(); 
                inForegroundFlag = true; 
            } else { 
                if ( inForegroundFlag ) { 
                appInactiveTime = 0; 
                } else { 
                    appInactiveTime = appInactiveTime + 60; 
                } 
                inForegroundFlag = false; 
            }
            synchronized (UiApplication.getEventLock()) {
                UiEngine ui = Ui.getUiEngine();
                if ( appInactiveTime < _4Minutes ){
                    if ( dialog4Minutes != null ) {
                        dialog4Minutes.close();
                        dialog4Minutes = null;
                    }
                }
                if ( appInactiveTime < _5Minutes ){
                    if ( dialog5Minutes != null ) {
                        dialog5Minutes.close();
                        dialog5Minutes = null;
                    }
                }
                if ( appInactiveTime >= _4Minutes && appInactiveTime < _5Minutes ) {
                    if ( dialog4Minutes == null ) {
                        dialog4Minutes = new Dialog("Stay Logged In?", new String[] {"SI", "NO"}, new int[]{1,2}, 2, null);
                        ui.pushGlobalScreen(dialog4Minutes, 1,UiEngine.GLOBAL_QUEUE);
                    }
                } else if ( appInactiveTime >=_5Minutes ) {
                    if ( dialog5Minutes == null ) {
                        dialog5Minutes = new Dialog("You will be disconnected", new String[] {"OK"}, new int[]{1}, 1, null);
                        ui.pushGlobalScreen(dialog5Minutes, 1,UiEngine.GLOBAL_QUEUE);
                        timerTask5Seconds = new TimerTask() {

                            public void run() {
                                UiApplication.getUiApplication().invokeLater(new Runnable() {

                                    public void run() {
                                        dialog5Minutes.close();
                                        try {
                                            //logout in communication manager 
                                            //pop to initial screen
                                        } catch (Exception e) {
                                            //force logout (2nd way)
                                        }finally{
                                            UiApplication.getUiApplication().removeRealtimeClockListener(realtimeClockListener);
                                        }
                                        timerTask5Seconds.cancel();
                                        timerTask5Seconds = null;

                                        timer5Seconds.cancel();
                                        timer5Seconds = null;                                       
                                    }
                                });
                            }
                        };
                        timer5Seconds = new Timer();
                        timer5Seconds.schedule(timerTask5Seconds, _5Seconds);
                        if ( dialog4Minutes != null ) {
                            dialog4Minutes.close();
                            dialog4Minutes = null;
                        }
                    }
                }
            }
        }
    };

Thanks Peter Strange!

Was it helpful?

Solution

I presume that you will use the RealtimeClockListener as a 'tick' that wakes up your application every minute. This works and is efficient, but does mean that your time out time will not be exactly 4 minutes - its actual duration will depend on whereabouts in the 'minute' the user becomes idle.

The trick is to determine what state your application is in when the tick happens or more accurately. I think the following sample code describes this, assuming that initially appInactiveTime is set to 0 and inForegrounFlag is true.

if ( Application.getApplication().isForeground() ) { appInactiveTime = DeviceInfo.getIdleTime(); inForegrounFlag = true; } else { if ( inForegrounFlag ) { // Was in foreground, start idle timing from now appInactiveTime = 0; } else { appInactiveTime = appInactiveTime + 60; } inForegrounFlag = false; }

I believe that using this code, appInactiveTime can now be used to find out if the app has been inactive for more than 4 minutes, by comparing it with 240 (60*4). .

At this point, your App should create and display a Global Screen, if you want it to show regardless of whether the app is in the Foreground or not. Then on the next tick, you can decide if you want to dismiss this screen or not, or if the user has dismissed it. I have the impression you need help with the timing rather than this part of the processing, so I will leave this with you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top