/* CASA Framework for ActionScript 3.0 Copyright (c) 2008, Contributors of CASA Framework All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the CASA Framework nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.casaframework.time { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import org.casaframework.control.IRunnable; import org.casaframework.events.InactivityEvent; import org.casaframework.time.Interval; import org.casaframework.util.StageReference; import org.casaframework.time.Stopwatch; [Event(name="activated", type="org.casaframework.events.InactivityEvent")] [Event(name="inactive", type="org.casaframework.events.InactivityEvent")] /** Detects user inactivity by checking for a void in mouse movement and key presses. @author Aaron Clinger @version 04/27/08 @usageNote You must first initialize {@link StageReference} before using this class. @example package { import flash.display.MovieClip; import org.casaframework.time.Inactivity; import org.casaframework.util.StageReference; import org.casaframework.events.InactivityEvent; public class MyExample extends MovieClip { protected var _inactivity:Inactivity; public function MyExample() { super(); StageReference.getInstance().stage = this.stage; this._inactivity = new Inactivity(3000); this._inactivity.addEventListener(InactivityEvent.INACTIVE, this.onUserInactive); this._inactivity.addEventListener(InactivityEvent.ACTIVATED, this.onUserActivated); this._inactivity.start(); } public function onUserInactive(e:InactivityEvent):void { trace("User inactive for " + e.milliseconds + " milliseconds."); } public function onUserActivated(e:InactivityEvent):void { trace("User active after being inactive for " + e.milliseconds + " milliseconds."); } } } */ public class Inactivity extends EventDispatcher implements IRunnable { protected var _interval:Interval; protected var _stopwatch:Stopwatch; /** Creates an Inactivity. @param milliseconds: The time until a user is considered inactive. @usageNote You must first initialize {@link StageReference} before using this class. */ public function Inactivity(milliseconds:uint) { super(); this._interval = Interval.setTimeout(this._userInactive, milliseconds); this._stopwatch = new Stopwatch(); } /** Starts detecting inactivity. */ public function start():void { if (this._interval.running) return; StageReference.getInstance().stage.addEventListener(Event.RESIZE, this._userInput); StageReference.getInstance().stage.addEventListener(KeyboardEvent.KEY_DOWN, this._userInput); StageReference.getInstance().stage.addEventListener(KeyboardEvent.KEY_UP, this._userInput); StageReference.getInstance().stage.addEventListener(MouseEvent.MOUSE_DOWN, this._userInput); StageReference.getInstance().stage.addEventListener(MouseEvent.MOUSE_MOVE, this._userInput); this._stopwatch.start(); this._interval.start(); } /** Stops detecting inactivity. */ public function stop():void { this._interval.reset(); StageReference.getInstance().stage.removeEventListener(Event.RESIZE, this._userInput); StageReference.getInstance().stage.removeEventListener(KeyboardEvent.KEY_DOWN, this._userInput); StageReference.getInstance().stage.removeEventListener(KeyboardEvent.KEY_UP, this._userInput); StageReference.getInstance().stage.removeEventListener(MouseEvent.MOUSE_DOWN, this._userInput); StageReference.getInstance().stage.removeEventListener(MouseEvent.MOUSE_MOVE, this._userInput); } /** @sends InactivityEvent#INACTIVE - Dispatched when the user is inactive. */ protected function _userInactive():void { this.stop(); var event:InactivityEvent = new InactivityEvent(InactivityEvent.INACTIVE); event.milliseconds = this._interval.delay; this.dispatchEvent(event); } /** @sends InactivityEvent#ACTIVATED - Dispatched when the user becomes active after a period of inactivity. */ protected function _userInput(e:*):void { if (!this._interval.running) { var event:InactivityEvent = new InactivityEvent(InactivityEvent.ACTIVATED); event.milliseconds = this._stopwatch.time; this.dispatchEvent(event); this.start(); } this._interval.reset(); this._interval.start(); } } }