peter9477

AlarmFreezing

Nov 7th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.display.StageAlign;
  4.     import flash.display.StageScaleMode;
  5.  
  6.     import flash.events.Event;
  7.     import flash.events.MouseEvent;
  8.  
  9.     import flash.media.Sound;
  10.     import flash.media.SoundChannel;
  11.  
  12.     import flash.text.TextFieldAutoSize;
  13.     import flash.text.TextFormat;
  14.  
  15.     import flash.utils.*;
  16.     import mx.utils.ObjectUtil;
  17.  
  18.     import qnx.events.QNXSystemEvent;
  19.  
  20.     import qnx.system.QNXSystem;
  21.     import qnx.system.QNXSystemPowerMode;
  22.  
  23.     import qnx.ui.buttons.LabelButton;
  24.     import qnx.ui.text.Label;
  25.     import qnx.ui.text.TextInput;
  26.  
  27.  
  28.     //-----------------------------------
  29.     //
  30.     [SWF(backgroundColor='#bbbbbb')]
  31.     public class AlarmFreezing extends Sprite {
  32.         public static const ALARM_TIMEOUT:int = 10 * 1000;
  33.  
  34.         private var note_text:TextInput;
  35.         private var start_button:LabelButton;
  36.         private var stop_button:LabelButton;
  37.         private var time_display:Label;
  38.  
  39.         private var start_time:Date;
  40.         private var timer_id:uint;
  41.  
  42.         //-----------------------------------
  43.         // main constructor for app
  44.         //
  45.         public function AlarmFreezing() {
  46.             stage.align = StageAlign.TOP_LEFT;
  47.             stage.scaleMode = StageScaleMode.NO_SCALE;
  48.  
  49.             QNXSystem.system.inactivePowerMode = QNXSystemPowerMode.THROTTLED;
  50.  
  51.             addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  52.  
  53.             QNXSystem.system.addEventListener(QNXSystemEvent.ALARM, onAlarm);
  54.             QNXSystem.system.setAlarm(ALARM_TIMEOUT, true); // periodic
  55.             QNXSystem.system.transitionTime = 1000;
  56.         }
  57.  
  58.  
  59.         //-----------------------------------
  60.         //
  61.         private function onAddedToStage(e:Event):void {
  62.             note_text = new TextInput();
  63.             note_text.text = '';
  64.             note_text.width = 800;
  65.             note_text.prompt = '(Enter text)';
  66.             note_text.height = 50;
  67.             addChild(note_text);
  68.  
  69.             start_button = new LabelButton();
  70.             start_button.label = 'Start';
  71.             start_button.enabled = true;
  72.             addChild(start_button);
  73.  
  74.             stop_button = new LabelButton();
  75.             stop_button.label = 'Stop';
  76.             stop_button.enabled = false;
  77.             addChild(stop_button);
  78.  
  79.             var fmt3:TextFormat = new TextFormat();
  80.             //~ fmt3.font = 'Myriad Pro';
  81.             fmt3.size = 48;
  82.             fmt3.color = 0x009922;
  83.  
  84.             time_display = new Label();
  85.             time_display.format = fmt3;
  86.             time_display.text = 'stopped';
  87.             time_display.autoSize = TextFieldAutoSize.LEFT;
  88.             addChild(time_display);
  89.  
  90.             // ----------------
  91.             // define layout
  92.             start_button.x = 10;
  93.             start_button.y = 10;
  94.             stop_button.x = 200;
  95.             stop_button.y = 10;
  96.  
  97.             note_text.x = 10;
  98.             note_text.y = 120;
  99.             time_display.x = 10;
  100.             time_display.y = 200;
  101.  
  102.             // ----------------
  103.             // define handlers
  104.             start_button.addEventListener(MouseEvent.CLICK, onStart);
  105.             stop_button.addEventListener(MouseEvent.CLICK, onStop);
  106.         }
  107.  
  108.  
  109.         //-----------------------------------
  110.         //
  111.         private function onStart(e:Event):void {
  112.             start_button.enabled = false;
  113.             stop_button.enabled = true;
  114.             time_display.text = '00:00';
  115.             show_timer(new Date());
  116.         }
  117.  
  118.  
  119.         //-----------------------------------
  120.         //
  121.         private function onStop(e:Event):void {
  122.             stop_button.enabled = false;
  123.             start_button.enabled = true;
  124.             pause_timer(0);
  125.         }
  126.  
  127.  
  128.         //-----------------------------------
  129.         //
  130.         public function show_timer(start_time:Date):void {
  131.             timer_id = setInterval(onTimer, 1000);  // testing to see effect of poorly chosen interval
  132.             this.start_time = start_time;
  133.             onTimer();
  134.         }
  135.  
  136.  
  137.         //-----------------------------------
  138.         //
  139.         public function pause_timer(elapsed:Number):void {
  140.             clearInterval(timer_id);
  141.             time_display.text = 'stopped';
  142.             note_text.text = '';
  143.         }
  144.  
  145.  
  146.         //-----------------------------------
  147.         //
  148.         public function onTimer():void {
  149.             var elapsed:int = (new Date()).valueOf() - start_time.valueOf();
  150.             update_time(elapsed / 1000);
  151.         }
  152.  
  153.  
  154.         //-----------------------------------
  155.         //
  156.         public function update_time(elapsed:Number):void {
  157.             var seconds:int = elapsed % 60;
  158.             elapsed /= 60;
  159.             var minutes:int = elapsed % 60;
  160.             var hours:int = elapsed / 60;
  161.  
  162.             var text:String = '';
  163.             if (hours)
  164.                 text = String(hours) + ':';
  165.  
  166.             if (minutes < 10)
  167.                 text += '0' + String(minutes) + ':';
  168.             else
  169.                 text += String(minutes) + ':';
  170.  
  171.             if (seconds < 10)
  172.                 text += '0' + String(seconds);
  173.             else
  174.                 text += String(seconds);
  175.  
  176.             time_display.text = text;
  177.         }
  178.  
  179.         [Embed(source='warbly.mp3')]
  180.         private var Asset_warbly:Class;
  181.         private var sound_alarm:Sound = new Asset_warbly();
  182.  
  183.  
  184.         //-----------------------------------
  185.         //
  186.         public function onAlarm(e:Event):void {
  187.             //~ trace('alarm');
  188.             QNXSystem.system.powerMode = QNXSystemPowerMode.THROTTLED;
  189.             var chan:SoundChannel = sound_alarm.play();
  190.  
  191.             chan.addEventListener(Event.SOUND_COMPLETE, onReminderSoundComplete);
  192.         }
  193.  
  194.  
  195.         //-----------------------------------
  196.         //
  197.         public function onReminderSoundComplete(e:Event):void {
  198.             QNXSystem.system.powerMode = QNXSystemPowerMode.STANDBY;
  199.         }
  200.     }
  201. }
  202.  
  203.  
Add Comment
Please, Sign In to add comment