Tuesday, 15 March 2011

Flex 4 / AIR: copyToAsync does not fire the ProgressEvent

Problem:

While working on an desktop AIR application, I came across the problem that the copyToAsync method of the flash.filesystem.File class does not fire a ProgressEvent.

I would love to use the Async method, because this does not hold up the rest of the application, but I wanted to display some progress indication in e.g. a ProgressBar.

Solution:

Open 2 streams, one to read from and one to write to. Read and write chunks of bytes and calculate the progress according to the bytesAvailable attribute of the read stream.

Sample code:

// This is an object which has a public attribute which contains the source of the file to copy and 
// a public function with a File parameter to copy it to


public var fileObject:File;


public function copyTo(destination:File):void {
     inStream = new FileStream();
     outStream = new FileStream();


     inStream.addEventListener(ProgressEvent.PROGRESS, onProgress );
     inStream.addEventListener(Event.COMPLETE, onReady );


     inStream.openAsync(fileObject, FileMode.READ);
     outStream.openAsync(destination, FileMode.WRITE);
}


private function onProgress(e:ProgressEvent):void {
     // calculate the percentage
     pct = Math.round(e.bytesLoaded/e.bytesTotal*100);


     // if you want to update the progress bar:
     bar.setProgress(pct, 100);


     // if the ProgressEvent is fired, we have data available in the inStream, so we can start writing data
     var bytes:ByteArray = new ByteArray();
     inStream.readBytes(bytes, 0, inStream.bytesAvailable);
     outStream.writeBytes(bytes, 0, bytes.length);
}


private function onReady(e:Event):void {
     // the whole stream is read, so close the files
     inStream.close();
     outStream.close();


     // dispatch a COMPLETE event to let listeners to this object know the copy is done
     fileObject.dispatchEvent(new Event(Event.COMPLETE));
}

And somewhere in the application, associated with the copying process, there's a progress bar like this:

<mx:progressbar id="bar" mode="manual" width="100%"/>

I'm sorry this code's not entirely complete, but I had to use fragments of my existing code to illustrate this principle. Feel free to experiment with it.

Friday, 8 October 2010

Twitvertising?

This week, I realised that Twitter is in danger of SPAM. Or will it be an enrichment? How Twitter can be used for targeted advertisement.


This week, my car needed a checkup. Since I do things digital as much as possible, I made the appointment with the garage by internet. That went quite well. Because I needed to transport quite a lot of stuff, I asked for a bigger car. I have a Volvo V50 myself, and they gave me a Volvo XC70 AWD T5. That's quite a car! I was very happy, 'cause the stuff I needed to transport was so much, that it hadn't fit in my own car. It just fitted in the XC70. I was happy with the car, because it still was very fast and had no problem with the weight.


After I returned the car, I tweeted this message:

"Today I was happy: car was serviced and the temp car was a brand new Volvo XC70 - happy me!"
...Image my surprise - yes, I really was surprised! -  When I got this reply:

That means Dealer24x7 is running an engine to search tweets about car brands, and then replying to it! How brilliant is that!

Of course it can mean, that if more -and also sleezy- companies and advertisers start to do this, Twitter might get flooded with advertisements. I wonder what happens. What I know for sure: Twitter will live on for some time...

Thursday, 17 June 2010

Monday, 4 January 2010

Site specific browsers

As defined in Wikipedia: "A site-specific browser (SSB) is a software application that is dedicated to accessing pages from a single source (site) on a computer network such as the Internet or a private intranet. SSBs typically simplify the more complex functions of a web browser by excluding the menus, toolbars and browser chrome associated with functions that are external to the workings of a single site."

So, basically, this is a browser, disguised as an application? We already knew that browsers now act as new layer between the Operating System of a computer and an application, and is not just for browsing through pages of content. A simple step like this might be a step forward to better integration between your desktop PC and the internet, and thus the differences between applications and web (applications) will fade even more.


In his recent article "Exciting web browser trends in 2010" Devindra Hardawar describes that currently only Google Chrome supports this (just use the "create shortcut" option on the upper right). And in fact, it is quite handy! Devindra says in 2010 more browser will support this, and I agree on that. It isn't a too big thing to implement, I guess.


The most importent thing is if the users will start using it. I did. I use Google's Wave for some weeks now, start Chrome every day to check my mail and found out it is very convenient.


This, in combination with products like Adobe's AIR will bring the internet closer to your desktop and perhaps will bring even Microsoft Windows, Apple OS and Linux closer together.


Thursday, 19 November 2009

Don't take your random() for granted...

In my current project, we use a functionality which chooses a random colour for a machine. To do this, we need to select a colour from a set of 8 colours. In our Flex application we use a function called randomInt, which generates a random integer between a and b:

public static function randomInt(a:int, b:int):int {
return Math.round(Math.random()*(b-a))+a;
}


Usually we look for functions like these on the internet, but I’d like to emphasize that you also should unit-test functions which are made by others.

So I’ve generated 5000 integers between 0 and 20 in a test application, and put that in a graph:



Note that the numbers on the boundaries, 0 and 20, are significantly chosen less times than the others (1-19). This would mean, that if this function is used for picking a random machine colour, the colours 0 and 7 have only a 7% chance to be selected, while others have a 14% chance!

I’ve talked about this to someone who knows a lot of these mathematical things. The problem is the rounding. Since Math.round() returns a pseudo-random number n, where 0 <= n <>
We need to include the boundaries to produce a evenly distributed random integer. This is the new function:
private function randomInt(a:int, c:int):int {
var b:int = c + 1;
return Math.floor(Math.random()*(b-a))+a;
}


Doing the same test, produces this graph:



As you can see, each integer is chosen about the same number of times. Now we know for sure that colours 0 and 7 won’t stay in storage, but are also sold!

Wednesday, 3 June 2009

Nomads in IT...

Yesterday I was present at a job interview at IT.

I represented the technical guys, though I'm more a User Experience Consultant with technical background, but my job is mostly technical right now.

The guy who had the interview was somewhat the same, only a bit more with a designer's background.

It was suprising how difficult it is to find the right position for a User Experience Consultants, especially in the technical department!

They belong nowhere...