tags:

views:

8

answers:

1

In his blog post, Christian Cantrell shows how to use STORAGE_VOLUME_MOUNT in ActionScript.

He has written a Flex app called FileTile.

I would like to see a JavaScript alert box that says “You have inserted “ + e.storageVolume.name, and “You have removed a storage volume”.

A: 
<html>
<head>
<title>New Adobe AIR Project</title>
<script type="text/javascript" src="lib/air/AIRAliases.js"></script>
<script type="text/javascript" src="lib/air/AIRIntrospector.js"></script>
<script type="text/javascript" src="lib/jQuery/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
var msg = '<h1>Please insert your SwatchDog (SD) card.</h1>';

function trace() {
    var message = 'Hello Max';
    air.Introspector.Console.log(message);
}

function readFile(v) {
    var myFile = air.File.desktopDirectory.resolvePath("MyFile.txt");
    var fileStream = new air.FileStream();
    fileStream.open(myFile, air.FileMode.READ);
    $('#app').append('<p>' + fileStream.readUTFBytes(fileStream.bytesAvailable) + '</p>');
    fileStream.close();
}

function onVolumeMount(e) {
    $('#app').html('<h1>Thank you</h1><p>I can see you have multiple devices:</p>');
    var volumes = air.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
    for (var i = 0; i < volumes.length; i++) {
        $('#app').append(volumes[i].rootDirectory.nativePath);
    }

    if (e.storageVolume.isRemovable) {
        if (e.storageVolume.name == 'SWATCHDOG') {
        $('#app').append('<p>And SwatchDog is drive: ' + e.storageVolume.rootDirectory.nativePath + '</p>');
        readFile(e.storageVolume)
      } else {
        $('#app').append('<p>But the one you just plugged in is not SwatchDog.</p>');
      }
    }
}

function onVolumeUnmount(e) {
    $('#app').html('<h1>Goodbye!</h1>' + msg);
}


jQuery(function($){
    var PluggedIn = false;
    var volumes = air.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
    for (var i = 0; i < volumes.length; i++) {
        if (volumes[i].isRemovable) {
            PluggedIn = true;
            if (volumes[i].name == 'SWATCHDOG') {
                $('#app').append('I see you already have SwatchDog plugged in!');
                readFile(volumes[i])
            } else {
                $('#app').append('What you have plugged in is not SwatchDog.');
            }
        }
    }
    if (!PluggedIn){
        $('#app').append(msg);
    }
    air.StorageVolumeInfo.storageVolumeInfo.addEventListener(air.StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount);
    air.StorageVolumeInfo.storageVolumeInfo.addEventListener(air.StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);
})
</script>
</head>
<body>
    <div id="app">
    </div>
    <button onclick="trace();">Say Hello Max!</button>
</body>
</html>
cf_PhillipSenn

related questions