javascriptextjssencha-touch

Ext.TaskManager.newTask() is not working when the ipad is locked


I am trying to run a task that prompts the user if he/she wants to extend the session in the browser.

The prompt works correctly on the desktop but fails when the iPad is locked.

The way the code is written is I have task A which should run another task B based on certain conditions.

            var uiCheckTime = null;
            var uiPromptMsg = false;
            var initialPopUpTime= null;
            var  sessionAboutToTimeoutMessageBox = Ext.create('view.osMessageBox', {
            title: ''
            });

            var promptMsg = function () {            
            initialPopUpTime = Ext.Date.now();
            task.start();
            var countdownMinutes =  Math.floor((sessionTimeOutCountDown)/(60));
            var countdownSeconds = sessionTimeOutCountDown - (countdownMinutes * 60) + ' second(s)';
            if(countdownMinutes == 0){
                //if number of minutes equals to zero, do not display minutes part.
                countdownMinutes = '';
            }
            else
            {
                countdownMinutes = countdownMinutes + ' minute(s) and ';
            }
            return Ext.String.format(timeoutConfirmationtMessage, countdownMinutes, countdownSeconds);
        };

            Ext.Ajax.on('requestcomplete', function (conn, response, options) {
            
            APPUTILS.ajaxResponseFilter(conn, response, options);
            
            resetTimeout();
            
            var date = new Date();
            
            var currentTime = date.getTime();
            uiCheckTime = Ext.Date.now();
            uiPromptMsg = false;
            sessionAboutToTimeoutPromptTask.start();
             
        });

        //Prompt to inform that Http Session is about to Timeout
        var sessionAboutToTimeoutPromptTask =Ext.TaskManager.newTask( {
            run: function(){                
                if(Ext.Date.now()>=uiCheckTime+(uiSessionExpiryPromptIntervalInMin*60*1000) && !uiPromptMsg)
                {
                    
                    uiPromptMsg = true;
                    sessionAboutToTimeoutMessageBox.show({
                    message : promptMsg(),
                    buttons : Ext.Msg.YES,
                    buttonText : {
                        yes : "<span osviewid = 'OS_USER_SESSION_EXTEND_YES'>Yes</span>"
                    },
                    fn : function(btn, text) {
                        sessionTimeOutCountDown = uiSessionExpiryTimeInMin - uiSessionExpiryPromptIntervalInMin;
                        if (btn == 'yes') {
                            uiPromptMsg= false;
                            uiCheckTime = Ext.Date.now()
                            var date = new Date();
                            var currentTime = date.getTime();       
                            resetTimeout();
                            extendSessionAjaxRequest(false);
                        }
                    }
                });
                }
            },
            interval:1000,
            scope:me

        });


        //Task to show the timeout count-down
        var task = Ext.TaskManager.newTask({
            run:  function () {
                var currentTime = Ext.Date.now();
                var actualTimeout = uiSessionExpiryTimeInMin*60 - uiSessionExpiryPromptIntervalInMin*60;
                var timeDifference =Math.floor((currentTime-initialPopUpTime)/1000);
                if(timeDifference>=(actualTimeout))
                {
                    sessionTimeOutTask.delay(0);
                }
                else
                {
                    sessionTimeOutCountDown = actualTimeout-timeDifference;
                }

                var countdownMinutes =  Math.floor((sessionTimeOutCountDown)/(60));
                var countdownSeconds = sessionTimeOutCountDown - (countdownMinutes * 60) + ' second(s)';
                
                if(countdownMinutes == 0){
                    //if number of minutes equals to zero, do not display minutes part.
                    countdownMinutes = '';
                }
                else
                {
                    countdownMinutes = countdownMinutes + ' minute(s) and ';
                }
                sessionAboutToTimeoutMessageBox.updateText( 
                        Ext.String.format(timeoutConfirmationtMessage, countdownMinutes, countdownSeconds)
                );
            },
            interval: 1000,
            scope:me
        });

        var resetTimeout = function(){
            sessionTimeOutTask.cancel();
            sessionAboutToTimeoutPromptTask.stop();
            task.stop();
            resetTimer();
        }




Here task A is sessionAboutToTimeoutPromptTask and task B is task the function reset timeout() will reset stop/cancel all the task to default state. When the user loads the page task A is started using the start method, and task B is the counter which is updated on a message box, on desktop/PC it works as intended the session is expired after the timer runs out.

The same test case does not work on Ipad when the URL is hit and the device is locked. what happens is inside the task A, the if condition is satisfied and the message box is shown, but the countdown only starts when the device is unlocked, i think the issue is that the message box does not start the timer when it is locked. What i want is the timer should still continue even if the device is locked and the session should be expired.

Any Solutions to how i can acive this?


Solution

  • You have to take control over pause(toBackground) and resume (toForeground).

    Do something like this:

    document.addEventListener('pause', this.onPause, false); 
    document.addEventListener('resume', this.onResume, false);
    
    hasBeenSleeping: false,
    
    onPause: function() {
        this.hasBeenSleeping = true;
    }
    
    onResume: function() {
        if (this.hasBeenSleeping) {
            this.hasBeenSleeping = false;
            if (currentlyAskingForSession) {
                closeDialogAboutSession();
            }
        }
    }
    

    OR

    onPause: function() {
        Ext.fireEvent('sleeping');
    }
    
    Dialog: {
       Ext.on('sleeping', this.closeAndLogout);
    }