BlackBerry Webworks Javascript Extension to allow File Downloads

If you are a BlackBerry Webworks developer I’m sure you’re always noticing that something is missing.
This time, I took some time to develop something that is really trivial in other plataforms, but due to Webworks nature, it’s bit triky.

My goal was simple: Download an image to the device, and show it in the Webworks app.
So as usual I googled it, and found that are at least one solution for the BlackBerry PlayBook, but not for OS5+.

Since I think that this is useful, I’ve decided to create an Javascript Extension to do this, and you can now download it here

The installation process is as usual, just download that file to your “ext” folder, and make it accessible to the application by adding it to the “config.xml”:

<feature id="blackberry.io.filetransfer" required="false" version="1.0.0"/>

To be able to access those downloaded files through the widget you may need to also add this line to the “config.xml” file:

<access subdomains="false" uri="file:///"/>

With this Javascript Ext you’ll have only one method available:

blackberry.io.filetransfer.download(arguments);

And the “arguments” can be:

  • url (required): Is the download url. ex: “https://www.google.com/images/srpr/logo3w.png”
  • dest (required): Is the local file location. ex:”file:///store/home/user/logo/logo.png”
  • type (optional): Can be ‘POST’ or ‘GET’ (default: ‘GET’)
  • success (optional): Is a Javascript function that is called after the download is complete
  • error(optional): Is a Javascript function that is called if the download fails for some reason
  • headers (optional): For this version the headers only allows you to set the “Authorization” and/or “Content-Type”, but if you need more support please let me know.

And now I’m leaving two examples of how it really works:
Example 1: Download the google logo and notify when it’s done

<script language=”javascript” type=”text/javascript”>

// Here whe need to create the local folder, if it doesn’t exist already
if (!blackberry.io.dir.exists(“file:///store/home/user/logos/”)) {
blackberry.io.dir
.createNewDir(“file:///store/home/user/logos/”);
}

// Download the file
blackberry.io.filetransfer.download({
url: “https://www.google.com/images/srpr/logo3w.png“, // the image url
type : ‘GET’, // explicitly setting method type to ‘GET’
dest: “file:///store/home/user/logos/GoogleLogo.png”, // the local file
success : function(data){ // The function called after the file is downloaded
alert(‘success’);
},
error : function(data){ // The function called if something fails
alert(‘error:’+data);
}
})
</script>

Example 2: Download the google logo with HTTP Basic Authentication

<script language=”javascript” type=”text/javascript”>
function make_base_auth(user, password) {
var tok = user + ‘:’ + pass;
var hash = Base64.encode(tok);
return “Basic ” + hash;
}

// Here whe need to create the local folder, if it doesn’t exist already
if (!blackberry.io.dir.exists(“file:///store/home/user/logos/”)) {
blackberry.io.dir
.createNewDir(“file:///store/home/user/logos/”);
}

// Download the file
blackberry.io.filetransfer.download({
url: “https://www.google.com/images/srpr/logo3w.png“, // the image url
type : ‘GET’, // explicitly setting method type to ‘GET’
headers:{
Authorization: make_base_auth(“my_username”,”my_password”),
“Content-Type”: “application/x-www-form-urlencoded”
},
dest: “file:///store/home/user/logos/GoogleLogo.png”, // the local file
success : function(data){ // The function called after the file is downloaded
alert(‘success’);
},
error : function(data){ // The function called if something fails
alert(‘error:’+data);
}
})
</script>

You can find the source code here https://github.com/dbotelho/blackberry.io.filetransfer

Linux Mint 13 on VirtualBox needs some tweaks to use USB mouse

Since I’m kind of disappointed with the lasted Ubuntu version (12.04), that just makes my 4 year-old laptop useless, I’m now considering alternatives.
The first one that popped up was Linux Mint.

I’ve tested the latest build Linux Mint 13 on VirtualBox and I’ve noticed that my USB mouse didn’t work there.
To fix this I had to execute this command:
sudo usermod -aG lp <MY_USERNAME>

and it fixed that issue.

I’m going to try it a bit, just to see if I’m ready to do the switch.