Category Archives: code

Every bug is your fault

When you write software you need to take responsibility of any bug that users might blame you for. No matter if it is your fault or not. If the end users think it is your fault, then you should take responsibility to fix the problem no matter where it actually lies.

As posted by Brandon Bloom:

When you run a business, if your software has a bug, your customers don’t care if it is your fault or Linus’ or some random Rails developer’s. They care that your software is bugged. Everyone’s software becomes my software because all of their bugs are my bugs. When something goes wrong, you need to seek out what is broken, and you need to fix it. You fix it at the right spot in the stack to minimize risks, maintenance costs, and turnaround time. Sometimes, a quick workaround is best. Other times, you’ll need to recompile your compiler. Often, you can ask someone else to fix it upstream, but just as often, you’ll need to fix it yourself.

True hackers have come to terms with a simple fact: If it runs on my machine, it’s my software. I’m responsible for it. I must understand it. Building from source is the rule and not an exception. I must control my environment and I must control my dependencies.

Although this attitude needs to extend beyond dependencies, and also cover things like your distribution.

Posting to Facebook and Twitter

The following code will help you post to Facebook or Twitter from a BlackBerry Java application. This code requires the use of some API elements that were not introduced until OS 7 so you will want to use a different build in order to continue to support older devices. This code was used in version 3.1 of the game Pixelated.

Code for posting to Facebook


public void postToFacebook(String text)
{
JSONObject context = new JSONObject();
MenuItem fb = null;
try
{
context.put(SendCommandContextKeys.TEXT, text);
SendCommand[] sendCommands = SendCommandRepository.getInstance().get(SendCommand.TYPE_TEXT, context, false);

if (sendCommands != null && sendCommands.length > 0)
{
SendCommandMenu scm = new SendCommandMenu(sendCommands, 0, 0);
SendCommandMenuItem[] scmi = scm.getSendCommandMenuItems();
for(int i=0;i<scmi.length;i++)
{
if(scmi[i].toString().equals("Facebook"))
{
fb = scmi[i];
}
}
fb.run();
}
}
catch(Exception e){}
}

Code for posting to Twitter


public void postToTwitter(String text)
{
JSONObject context = new JSONObject();
MenuItem tw = null;
try
{
context.put(SendCommandContextKeys.TEXT, text);
SendCommand[] sendCommands = SendCommandRepository.getInstance().get(SendCommand.TYPE_TEXT, context, false);

if (sendCommands != null && sendCommands.length > 0)
{
SendCommandMenu scm = new SendCommandMenu(sendCommands, 0, 0);
SendCommandMenuItem[] scmi = scm.getSendCommandMenuItems();
for(int i=0;i<scmi.length;i++)
{
if(scmi[i].toString().equals("Twitter"))
{
tw = scmi[i];
}
}
tw.run();
}
}
catch(Exception e){}
}

OS 4.x is dead

I have decided to end support for all BlackBerry phones running OS 4.x and require OS 5.0 or higher for all apps going forwards. Version 3.1 of Pixelated which was updated two weeks ago will be the last update for these devices. Apps that support OS 4.6 and 4.7 will still be available for download in BlackBerry AppWorld, but they will not receive any more updates.

Many Ebscer apps have required a minimum of OS 5.0 for a while now. Twinkle, Liar’s Dice, Xploding Boxes, and 9 Men’s Morris have all required OS 5.0 from the start. The Jack-O-Lantern app has required OS 5.0 since the update to version 2.0 last September, and Binary Clock has done so since the update to version 2.0 in January. Furthermore, many of the new features added to the other apps didn’t apply to the builds for OS 4.x anyhow as they were dependent on API calls such as the BBM API, the purchase API, or the SendMenu API, that are only available in the more recent operating system versions.

Last month showed only 4.6% of BlackBerry users running an OS older then 5.0. This percentage continues to go down every month. At this point there isn’t much need for additional updates on these outdated devices.

If you are still using a device that runs on OS 4.6 you should view this as proof that it is time to get a new phone. Every phone released since mid-2008 (almost four years ago), can be updated to at least OS 5.0 including (now ancient) devices such as the Bold 9000, Curve 8900, and the Nextel branded Curve 8350i. If your phone is even older then that you should have replaced your phone twice by now. If you have a 4.x device that you can’t replace because it was issued to you by your work, you should take it as a sign that your boss hates you, and that you should just quit your job.

Thoughts on AdvancedUI for Java

While it has been available for awhile now, RIM has recently open sourced their AdvancedUI for Java classes and added them to github. If you have not downloaded these classes before, you should do so.

I first found out about these classes in early 2010, and have pretty much used at least one of them in every release since then. For an example the menu on top of the statistics pages in Pixelated (as seen to the right) is done with the Pillbox Class from this collection.

The JustifiedVerticalFieldManager is another favorite, which makes many good looking designs much easier to create.

The bigger question is why these were have not been included in the OS for awhile now. Publicly posting these classes again is nice for giving them some added visibility, but nothing compares to simply adding them to the OS. While doing so now would be kind of pointless (is anyone ever going to make an app that requires a minimum of OS 7.1?) all indications are that they could have done so a few years ago. The copy in github has had its metadata updated, but when these classes were first released they were dated as having been created in 2008. Even if you allow for the fact that they may not have been ready for the launch of OS 4.7.0 I have no idea why they were not included in OS 5.0 when it was released in late 2009. Today OS 5.0 is the minimum threshold for most developers, and it would be nice to simply have these features baked in.

But far more importantly then the convenience for me, is the fact that these classes should be far more readily available for all developers. My guess is that there are many devs making BlackBerry Java apps without any clue that these classes exist. And that doesn’t help anyone.

While it may be too late for these classes to get the visibility that they deserve, lets hope that RIM doesn’t repeat this mistake with their BBX tools. Although on a related note, the WebWorks getting started page does not include any links to bbUI.js, perhaps that could be a start.

How to enable Profile Boxes using BBM SDK 1.2

In the original version of the BBM SDK it was a real pain for users to have to tweak the settings in order to enable apps to create profile boxes. Version 1.2 of the SDK still requires some work from the user but makes things a little better by allowing you to directly launch to screen where this needs to be edited. The code for doing so is as follows:

UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
try
{
if(platformContext.getSettingsManager().getSetting( SettingsManager.SETTING_PROFILE_BOX ) == SettingsManager.VALUE_DISABLED)
{
Dialog d = new Dialog(Dialog.D_OK,"In order to enable BBM Profile Boxes please check \"Post recent activities to my profile\" on the next screen", 0, null, 0 );
d.doModal();
platformContext.requestAppSettings();
}
}
catch(Exception e){}
}
});

This code assumes that platformContext is the name of your already created and registered BBMPlatformContext object. This code uses a check so, the user does not see anything if this feature is already enabled. The UiApplication invokeAndWait wrapper is required, as an exception will be thrown if this code is not run on a UI thread. Also note that this code requires v1.2 of the BBM SDK, and therefore version 6.0.1 of BBM.

Is an object contained in an array?

Most of the traditional ways to see if an object is contained in an array in Java require the use of sets and generics. However, generics were not included in Java until version 1.5, while the Java that runs on the BlackBerry OS is based off of J2ME, which is based off of version 1.4 of Java. As such a different approach needs to be used to determine if an object is in an array when coding for BlackBerry. While you could (of course) write your own method using a for loop, there is an easier way.

import net.rim.device.api.util.Arrays;

String[] nameArray = new String[]{"John","Paul","George","Ringo"};
String testValue = "Paul";
if(Arrays.contains(nameArray,testValue))
{

}

While this examples uses an array of strings, this approach will in fact work for any Object type.

BBM SDK is bigger news then OS 7

For BlackBerry developers (and therefore users) the release of the BBM SDK is a much bigger deal then OS7. While the new operating system cleans up the homescreen, and includes new features based on additional hardware (such as NFC and AR) there is not much added in the new operating system for developers to add to existing apps (with the exception of location based apps).

So for developers looking to expand upon their existing app catalog, adding BBM integration is a far more attractive option. Additionally, using the BBM SDK doesn’t require users to all buy new BlackBerrys, and instead can be integrated with apps on existing devices all the way back to OS5, which covers the vast majority of in market devices.

RIM is really pushing the BBM SDK with a series of webcasts, and a Hackathon, but even before these announcements were getting many developers to spend time adding these features to their apps. None of the new options that RIM has introduced in the past few years has resulted in as much developer excitement as this SDK already has.

Twinkle was one of the first apps to add Universal Search, and Hockey Scores was one of the first to add in-app payments, however there hasn’t been as much interest in these options from other developers as there already is for the BBM SDK.

Swipe down menus should also swipe back up

On the BlackBerry PlayBook, the top bezel is reserved for developers in order to create an app specific “swipe-down menu”. While not enough apps make use of this, for those that do there does not yet appear to be any standard behavior on how to close these menus. I propose a simple solution, that the menu should swipe back up off the screen.

Most developers do tween the menu back off the top of the screen after a selection, or a touch outside of the menu area, but like you can swipe down to display the menu, you should also be able to swipe back up in order to hide it again. This is the approach that has been added to version 2.7 of Pixelated and version 1.1 of Black Out.

In order to implement this, when the menu is opened gesture swipe are listened for using the code: stage.addEventListener( TransformGestureEvent.GESTURE_SWIPE, onSwipe) and then if the TransformGestureEvent’s offsetY == -1 (in the up direction), and the localY is less then the menu’s height, a command is then sent to remove the menu from the screen. (Otherwise the swipe is ignored).

This gives a more consistent user interface, as swiping from the top bezel to the screen opens the menu, and swiping from the on screen menu to the top bezel hides it. The more developers that use this, the more customers will know to expect it, and the better their PlayBook experience will be.

Trick to providing better email support

By default when user needs support for their application their only way of contact is to use the “Contact Support” button in BlackBerry AppWorld. While this does work it has many short comings. There is only one email address listed for all of a vendor’s apps, and unless the customer writes it out in detail there is no way of knowing even which app the user needs help with. Simple questions like “Does my device support this app?” can’t be answered, because there is no way of knowing the user’s device, or the app that they are interested in.

In order to get around this limitation, and provide better customer support Ebscer application user a built in email contact button that pre-populates the email’s subject line with the name of the application, the version of the application, the device model, and the version of the operating system on the device. The button for this is placed onto the about page in all apps, and sometimes also as a menu option.

The code for handling the button press in the Twinkle app is as follows:

String s = "Support for Twinkle V2.2 on BB"+DeviceInfo.getDeviceName()+" running OS"+CodeModuleManager.getModuleVersion(CodeModuleManager.getModuleHandleForObject(""));
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES,new MessageArguments(MessageArguments.ARG_NEW,"twinkle@ebscer.com",s,""));

If your customers contact you with this method instead (and in my experience most will), then you can skip the preliminaries and get straight to answering their questions. This make life easier for everyone, as you don’t need to send extra emails back and forth just to get the basics.

BlackBerry 7 icons should only be 76×76 pixels

Recently RIM has suggested that icons for BlackBerry 7 devices such as the 9900 should be 92×92 pixels. A quick look at this in the simulator, however reveals that this is way to big, and a 92×92 pixel icons looks absurdly large. While using the whole 92 pixels may be appropriate if you are filling the edges with transparencys and drop shadows, a more realistic size for a normal icon is 76×76 pixels. At 76 pixels you can still get a high quality icon, but also one that doesn’t dwarf all of those around it.

More PlayBook Applications need to make use of the swipe down menu

The bezel on the top of the PlayBook is reserved for developers to integrate a menu that can be swiped down from the top. However, an unfortunately large number of applications do not currently make use of this option. This breaks the consistency of the platform, and makes it more difficult for users to quickly get used to the tablet’s interface.

If not enough apps make use of the swipe down menu, users will not expect it to exist, and for those applications that do use it, users are liable to be unaware of the menu’s existence, and any additional items that are located there. Furthermore the alternative is to place a menu button on the screen that takes up space, and prevents the app from feeling like a native application.

Currently of the top 10 free apps, only 3 (including Pixelated) support a swipe down menu. Furthermore RIM is not even including one in all of its own applications such as the Scrapbook App, or even BlackBerry AppWorld. Given all of the support that RIM has offered to EA for Need For Speed, you would think that it would implement this basic UI function, but instead this app ignores the top bezel, and uses a convoluted two finger swipe instead.

RIM does not appear particularly interested in promoting this swipe down menu, but should they change their mind, they could do so by requiring that an app make use of such a menu in order to be featured in AppWorld. If RIM were to do this, the number of apps implementing a menu would rather quickly hit critical mass, and in turn give users a more consistent, and predictable interface to navigate.

How to invoke AppWorld in ActionScript

The ActionScript SDK for BlackBerry AppWorld does not include any published api’s to directly invoke native applications such as BlackBerry AppWorld. There is however a round about way to do so. The navigateToURL() method can be used to launch the web browser. Furthermore the browser intercepts specific app URLs to launching BlackBerry AppWorld instead. So if you wanted to launch AppWorld directly from inside an ActionScript app, all you need to do is to use the navigateToURL() method by pointing it at an AppWorld URL.

The following is an event that is fired following a click to a “Launch AppWorld” button.

public function lauchAppWorld(event:MouseEvent):void
{
navigateToURL(new URLRequest(“http://appworld.blackberry.com/webstore/content/1839”))
}

You can test this approach by simply entering the URL into the browser on your PlayBook. The one downside this course of action has however, is that along the way you will end up launching the browser with a blank window. But until more API’s are available this is at least an approach that works.

Update:

A better approach has been found. In order to go straight to AppWorld you can use navigateToURL() with a link in the format “appworld://content/1839” instead. The below approach is much cleaner and preferred to the work around listed above.

public function lauchAppWorld(event:MouseEvent):void
{
navigateToURL(new URLRequest(“appworld://content/1839”))
}

RIM’s PlayBook development guide is wrong

I recently found some issues with my apps on the BlackBerry PlayBook. These issues were a direct result of following RIM’s BlackBerry Tablet development guide. They need to fix this and stop making suggestions that simply do not work.

The article wisely recommends that you make sure that the application’s state is saved when it is deactivated and pushed to the background, but recommends a technique that is inconsistent at best. They are recommending that you save data by listening for Event.DEACTIVATE which detects when another application receives focus. However this event is never fired when the application exits (unless you run in Paused mode like the simulators used to). Assuming that you could use this approach to save data upon exiting is at the root of a lot of the problems in the early PlayBook applications.

Instead you can actually detect the application closing by using NativeApplication.nativeApplication.addEventListener(Event.EXITING,saveData) to listen for an exiting event, and saving data then. Until RIM’s development guide on the life cycle of PlayBook applications mentions this, it will just be wrong.