SHOW:
|
|
- or go back to the newest paste.
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 | // power mode when we're silent | |
43 | private var silentPowerMode:String; | |
44 | ||
45 | //----------------------------------- | |
46 | // main constructor for app | |
47 | // | |
48 | public function AlarmFreezing() { | |
49 | stage.align = StageAlign.TOP_LEFT; | |
50 | stage.scaleMode = StageScaleMode.NO_SCALE; | |
51 | ||
52 | silentPowerMode = QNXSystemPowerMode.STANDBY; | |
53 | QNXSystem.system.inactivePowerMode = silentPowerMode; | |
54 | ||
55 | - | QNXSystem.system.transitionTime = 1000; |
55 | + | |
56 | addEventListener(Event.ACTIVATE, onActivate); | |
57 | addEventListener(Event.DEACTIVATE, onDeactivate); | |
58 | ||
59 | QNXSystem.system.addEventListener(QNXSystemEvent.ALARM, onAlarm); | |
60 | QNXSystem.system.setAlarm(ALARM_TIMEOUT, true); // periodic | |
61 | //~ QNXSystem.system.transitionTime = 1000; | |
62 | ||
63 | addEventListener(Event.ENTER_FRAME, onFrame); | |
64 | } | |
65 | ||
66 | ||
67 | //----------------------------------- | |
68 | // | |
69 | private function onAddedToStage(e:Event):void { | |
70 | note_text = new TextInput(); | |
71 | note_text.text = ''; | |
72 | note_text.width = 800; | |
73 | note_text.prompt = '(Enter text)'; | |
74 | note_text.height = 50; | |
75 | addChild(note_text); | |
76 | ||
77 | start_button = new LabelButton(); | |
78 | start_button.label = 'Start'; | |
79 | start_button.enabled = true; | |
80 | addChild(start_button); | |
81 | ||
82 | stop_button = new LabelButton(); | |
83 | stop_button.label = 'Stop'; | |
84 | stop_button.enabled = false; | |
85 | addChild(stop_button); | |
86 | ||
87 | var fmt3:TextFormat = new TextFormat(); | |
88 | //~ fmt3.font = 'Myriad Pro'; | |
89 | fmt3.size = 48; | |
90 | fmt3.color = 0x009922; | |
91 | ||
92 | time_display = new Label(); | |
93 | time_display.format = fmt3; | |
94 | time_display.text = 'stopped'; | |
95 | time_display.autoSize = TextFieldAutoSize.LEFT; | |
96 | addChild(time_display); | |
97 | ||
98 | // ---------------- | |
99 | // define layout | |
100 | start_button.x = 10; | |
101 | start_button.y = 10; | |
102 | stop_button.x = 200; | |
103 | stop_button.y = 10; | |
104 | ||
105 | note_text.x = 10; | |
106 | note_text.y = 120; | |
107 | time_display.x = 10; | |
108 | time_display.y = 200; | |
109 | ||
110 | // ---------------- | |
111 | // define handlers | |
112 | start_button.addEventListener(MouseEvent.CLICK, onStart); | |
113 | stop_button.addEventListener(MouseEvent.CLICK, onStop); | |
114 | } | |
115 | ||
116 | ||
117 | //----------------------------------- | |
118 | // | |
119 | private function onFrame(e:Event):void { | |
120 | trace('frame', QNXSystem.system.powerMode, QNXSystem.system.inactivePowerMode); | |
121 | } | |
122 | ||
123 | ||
124 | //----------------------------------- | |
125 | // | |
126 | private function onActivate(e:Event):void { | |
127 | trace('activate', QNXSystem.system.powerMode, QNXSystem.system.inactivePowerMode); | |
128 | silentPowerMode = QNXSystemPowerMode.NORMAL; | |
129 | QNXSystem.system.powerMode = silentPowerMode; | |
130 | } | |
131 | ||
132 | ||
133 | //----------------------------------- | |
134 | // | |
135 | private function onDeactivate(e:Event):void { | |
136 | trace('deactivate', QNXSystem.system.powerMode, QNXSystem.system.inactivePowerMode, playing); | |
137 | silentPowerMode = QNXSystemPowerMode.STANDBY; | |
138 | if (!playing) | |
139 | QNXSystem.system.powerMode = silentPowerMode; | |
140 | } | |
141 | ||
142 | ||
143 | //----------------------------------- | |
144 | // | |
145 | private function onStart(e:Event):void { | |
146 | start_button.enabled = false; | |
147 | stop_button.enabled = true; | |
148 | time_display.text = '00:00'; | |
149 | show_timer(new Date()); | |
150 | } | |
151 | ||
152 | ||
153 | //----------------------------------- | |
154 | // | |
155 | private function onStop(e:Event):void { | |
156 | stop_button.enabled = false; | |
157 | start_button.enabled = true; | |
158 | pause_timer(0); | |
159 | } | |
160 | ||
161 | ||
162 | //----------------------------------- | |
163 | // | |
164 | public function show_timer(start_time:Date):void { | |
165 | timer_id = setInterval(onTimer, 1000); // testing to see effect of poorly chosen interval | |
166 | this.start_time = start_time; | |
167 | onTimer(); | |
168 | } | |
169 | ||
170 | ||
171 | //----------------------------------- | |
172 | // | |
173 | public function pause_timer(elapsed:Number):void { | |
174 | clearInterval(timer_id); | |
175 | time_display.text = 'stopped'; | |
176 | note_text.text = ''; | |
177 | } | |
178 | ||
179 | ||
180 | //----------------------------------- | |
181 | // | |
182 | public function onTimer():void { | |
183 | var elapsed:int = (new Date()).valueOf() - start_time.valueOf(); | |
184 | update_time(elapsed / 1000); | |
185 | } | |
186 | ||
187 | - | //~ trace('alarm'); |
187 | + | |
188 | - | QNXSystem.system.powerMode = QNXSystemPowerMode.THROTTLED; |
188 | + | |
189 | // | |
190 | public function update_time(elapsed:Number):void { | |
191 | var seconds:int = elapsed % 60; | |
192 | elapsed /= 60; | |
193 | var minutes:int = elapsed % 60; | |
194 | var hours:int = elapsed / 60; | |
195 | ||
196 | var text:String = ''; | |
197 | if (hours) | |
198 | - | QNXSystem.system.powerMode = QNXSystemPowerMode.STANDBY; |
198 | + | |
199 | ||
200 | if (minutes < 10) | |
201 | text += '0' + String(minutes) + ':'; | |
202 | else | |
203 | text += String(minutes) + ':'; | |
204 | ||
205 | if (seconds < 10) | |
206 | text += '0' + String(seconds); | |
207 | else | |
208 | text += String(seconds); | |
209 | ||
210 | time_display.text = text; | |
211 | } | |
212 | ||
213 | [Embed(source='warbly.mp3')] | |
214 | private var Asset_warbly:Class; | |
215 | private var sound_alarm:Sound = new Asset_warbly(); | |
216 | ||
217 | ||
218 | //----------------------------------- | |
219 | // | |
220 | private var playing:Boolean; | |
221 | ||
222 | public function onAlarm(e:Event):void { | |
223 | trace('alarm', QNXSystem.system.powerMode, QNXSystem.system.inactivePowerMode); | |
224 | var mode:String = (silentPowerMode == QNXSystemPowerMode.STANDBY) ? QNXSystemPowerMode.THROTTLED : QNXSystemPowerMode.NORMAL; | |
225 | QNXSystem.system.inactivePowerMode = QNXSystemPowerMode.THROTTLED; | |
226 | QNXSystem.system.powerMode = mode; | |
227 | ||
228 | var chan:SoundChannel = sound_alarm.play(); | |
229 | playing = true; | |
230 | ||
231 | chan.addEventListener(Event.SOUND_COMPLETE, onReminderSoundComplete); | |
232 | } | |
233 | ||
234 | ||
235 | //----------------------------------- | |
236 | // | |
237 | public function onReminderSoundComplete(e:Event):void { | |
238 | trace('alarm done'); | |
239 | playing = false; | |
240 | ||
241 | QNXSystem.system.powerMode = silentPowerMode; | |
242 | QNXSystem.system.inactivePowerMode = QNXSystemPowerMode.STANDBY; | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 |