VolumeMonitor.as 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2009, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.air.filesystem
  28. {
  29. import flash.events.EventDispatcher;
  30. import flash.utils.Timer;
  31. import flash.events.TimerEvent;
  32. import flash.filesystem.File;
  33. import flash.utils.Dictionary;
  34. import com.adobe.air.filesystem.events.FileMonitorEvent;
  35. import com.adobe.utils.ArrayUtil;
  36. /**
  37. * Dispatched when a volume is added to the system.
  38. *
  39. * @eventType com.adobe.air.filesystem.events.FileMonitor.ADD_VOLUME
  40. */
  41. [Event(name="ADD_VOLUME", type="com.adobe.air.filesystem.events.FileMonitor")]
  42. /**
  43. * Dispatched when a volume is removed from the system.
  44. *
  45. * @eventType com.adobe.air.filesystem.events.FileMonitor.REMOVE_VOLUME
  46. */
  47. [Event(name="REMOVE_VOLUME", type="com.adobe.air.filesystem.events.FileMonitor")]
  48. /**
  49. * Class that monitors changes to the File volumes attached to the operating
  50. * system.
  51. */
  52. public class VolumeMonitor extends EventDispatcher
  53. {
  54. private var timer:Timer;
  55. private var _interval:Number;
  56. private static const DEFAULT_MONITOR_INTERVAL:Number = 2000;
  57. private var volumes:Dictionary;
  58. /**
  59. * Constructor.
  60. *
  61. * @param interval How often in milliseconds the system is polled for
  62. * volume change events. Default value is 2000, minimum value is 1000
  63. */
  64. public function VolumeMonitor(interval:Number = -1)
  65. {
  66. if(interval != -1)
  67. {
  68. if(interval < 1000)
  69. {
  70. _interval = 1000;
  71. }
  72. else
  73. {
  74. _interval = interval;
  75. }
  76. }
  77. else
  78. {
  79. _interval = DEFAULT_MONITOR_INTERVAL;
  80. }
  81. }
  82. /**
  83. * How often the system is polled for Volume change events.
  84. */
  85. public function get interval():Number
  86. {
  87. return _interval;
  88. }
  89. /**
  90. * Begins the monitoring of changes to the attached File volumes.
  91. */
  92. public function watch():void
  93. {
  94. if(!timer)
  95. {
  96. timer = new Timer(_interval);
  97. timer.addEventListener(TimerEvent.TIMER, onTimerEvent,false,0, true);
  98. }
  99. //we reinitialize the hash every time we start watching
  100. volumes = new Dictionary();
  101. var v:Array = FileUtil.getRootDirectories();
  102. for each(var f:File in v)
  103. {
  104. //null or undefined
  105. if(volumes[f.url] == null)
  106. {
  107. volumes[f.url] = f;
  108. }
  109. }
  110. timer.start();
  111. }
  112. /**
  113. * Stops monitoring for changes to the attached File volumes.
  114. */
  115. public function unwatch():void
  116. {
  117. timer.stop();
  118. timer.removeEventListener(TimerEvent.TIMER, onTimerEvent);
  119. }
  120. private function onTimerEvent(e:TimerEvent):void
  121. {
  122. var v:Array = FileUtil.getRootDirectories();
  123. var outEvent:FileMonitorEvent;
  124. var found:Boolean = false;
  125. for(var key:String in volumes)
  126. {
  127. for each(var f:File in v)
  128. {
  129. //trace("--\n" + key);
  130. //trace(f.url);
  131. if(f.url == key)
  132. {
  133. found = true;
  134. break;
  135. }
  136. }
  137. if(!found)
  138. {
  139. outEvent = new FileMonitorEvent(FileMonitorEvent.REMOVE_VOLUME);
  140. outEvent.file = volumes[key];
  141. dispatchEvent(outEvent);
  142. delete volumes[key];
  143. }
  144. found = false;
  145. }
  146. for each(var f2:File in v)
  147. {
  148. //null or undefined
  149. if(volumes[f2.url] == null)
  150. {
  151. volumes[f2.url] = f2;
  152. outEvent = new FileMonitorEvent(FileMonitorEvent.ADD_VOLUME);
  153. outEvent.file = f2;
  154. dispatchEvent(outEvent);
  155. }
  156. }
  157. }
  158. }
  159. }