Let's say you upload a file to your server, that contains a single line:
1.2.3.4
The process is similar for Android and BB:
1- Retrieve the file from the server. You'll probably have a byte array as result.
2- Convert it to a String with the proper encoding. In case the txt only contains numbers and dots, the encoding is not really important since these are ASCII chars, and ASCII chars are compatible with most usual default encodings like UTF-8 and ISO-8859. So we could probably instantiate the string without dealing with the encoding, like this: String fileContent = new String(byte[] downloadedData)
. Otherwise, make sure you know in advance the txt file encoding and instantiate the string with that encoding.
3- Split the string using the dots as separators. In Android you can do it like this: String[] splitted = String.split(fileContent, '.')
, or use a StringTokenizer
. In BB, as it is based in CLDC, this method in String is not available so you should code it yourself, or use/port one from a well tested library (like Apache Commons' org.apache.commons.lang3.StringUtils.split). After this step you'll have an array of strings, each string being a number ({"1","2","3","4"} in the example).
4- Now create a int array of the same length, and convert each string in the array to its equivalent int, using Integer.parseInt(splitted[i])
on each element i
.
5- Get the version for your app and perform the same steps to get an array of int. In BB, you can call ApplicationDescriptor.currentApplicationDescriptor().getVersion()
. In Android, PackageInfo.versionCode
or PackageInfo.versionName
, depending on what you have specified in the manifest.
6- Notice both arrays don't need to be of the same length. You could have written "1.2.3.4" in your txt, but have "1.2.3" in your AndroidManifest.xml or BlackBerry_App_Descriptor.xml. Normalize both resulting int arrays to have the same length (the lenght of the longer one), and fill the added elements with zeroes. Now you'll have two int arrays (in the example, txtVersion = {1,2,3,4} and appVersion = {1,2,3,0}). Iterate comparing versions one by one. The rule is: if txtVersion[i] > appVersion[i], then you are out of date and an upgrade is needed.