Thursday, March 10, 2011

Converting Android java code to BlackBerry

So far it looks like mission impossible

First we need to downgrade language level from 1.6 to 1.3. In this how-to instruction you need to cry after this :)

I need to convert:
  • UI layer
  • Business Logic
  • SOAP calls
Let's look closer and see what we can do.
We can forget about UI unless I'll be able to find a way to parse Android's layout XMLs and generate something in runtime.

For now I prefer to plan generate sources for BlackBerry, Android and iPhone. So it will be old good Metaprogramming. I like this idea but rough estimation and debugging process is very frightening.




Wednesday, March 9, 2011

Get IMEI

protected String getIMEI() {

return GPRSInfo.imeiToString(GPRSInfo.getIMEI());

}


Add menu to MainScreen

//Add following to your MainScreen class

protected void makeMenu(Menu menu, int instance) {

super.makeMenu(menu, instance);

menu.add(new MenuItem("About", 10, 20) {

public void run() {

doAbout();

}

});


menu.add(new MenuItem("Logout", 20, 10) {

public void run() {

doLogout();

}

});


}


private void doLogout() {

//TODO: Implement doLogout

}


private void doAbout() {

//TODO: Implement doAbout

}


Monday, March 7, 2011

Filesystem Corrupt in Simulator

I just got:


To solve this:
  1. Go to C:\Eclipse\plugins\net.rim.ejde.componentpack6.0.0_6.0.0.30\components\simulator
  2. Delete all *.dmp files
  3. Restart simulator

Sunday, March 6, 2011

Resize header dynamically based on Display width

We need to prepare 3 images:




public class HeaderManager extends HorizontalFieldManager {

public HeaderManager() {

super(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL | Manager.USE_ALL_WIDTH | Manager.FIELD_RIGHT);



this.setBackground(BackgroundFactory.createSolidBackground(Color.RED));



Bitmap image;

Bitmap leftImage = Bitmap.getBitmapResource("header_left.png");

this.add(new BitmapField(leftImage));


image = Bitmap.getBitmapResource("header_middle.png");

Bitmap rightImage = Bitmap.getBitmapResource("header_right.png");


this.add(new BitmapField(resizeBitmap(image, Display.getWidth() - leftImage.getWidth() - rightImage.getWidth(), image.getHeight())));


VerticalFieldManager vert = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL | Manager.USE_ALL_WIDTH | Manager.FIELD_RIGHT);


vert.add(new BitmapField(rightImage, Field.FIELD_RIGHT));


this.add(vert);


}



public static Bitmap resizeBitmap(Bitmap image, int width, int height) {

int imageWidth = image.getWidth();

int imageHeight = image.getHeight();


// Need an array (for RGB, with the size of original image)

int rgb[] = new int[imageWidth * imageHeight];


// Get the RGB array of image into "rgb"

image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);


// Call to our function and obtain rgb2

int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);


// Create an image with that RGB array

Bitmap temp2 = new Bitmap(width, height);


temp2.setARGB(rgb2, 0, width, 0, 0, width, height);


return temp2;

}


private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {

int out[] = new int[x2 * y2];

for (int yy = 0; yy <>

int dy = yy * y / y2;

for (int xx = 0; xx <>

int dx = xx * x / x2;

out[(x2 * yy) + xx] = ini[(x * dy) + dx];

}

}

return out;

}

}

Resize bitmap

public static Bitmap resizeBitmap(Bitmap image, int width, int height) {

int imageWidth = image.getWidth();

int imageHeight = image.getHeight();


// Need an array (for RGB, with the size of original image)

int rgb[] = new int[imageWidth * imageHeight];


// Get the RGB array of image into "rgb"

image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);


// Call to our function and obtain rgb2

int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);


// Create an image with that RGB array

Bitmap temp2 = new Bitmap(width, height);


temp2.setARGB(rgb2, 0, width, 0, 0, width, height);


return temp2;

}


private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {

int out[] = new int[x2 * y2];

for (int yy = 0; yy <>

int dy = yy * y / y2;

for (int xx = 0; xx <>

int dx = xx * x / x2;

out[(x2 * yy) + xx] = ini[(x * dy) + dx];

}

}

return out;

}

Development environment. The final.

It was bad. Really bad!

I spend too much time trying to make everything work for me!

First I was going to use IDE from RIM. But it works only with devices. And debugging on devices is too painful.

I found great post form developers with similar issue. I also like to use IntelliJ.

I did all steps from post.

Killing moment for me was to restart simulator each time I have new binaries.

So my current solution is:
  • I do all coding in IntelliJ on mac;
  • folder with sources is shared with virtual windows machine;
  • I run and debug code in Eclipse on Windows inside Virtual machine;
  • It sounds horrible but using Unity mode of VM Ware is better then nothing (in my case using only Eclipse)
  • I even got windows keyboard to not switch my mind in Mac's
So far it looks good until I make a mistake and my application crashes inside simulator....





Thursday, March 3, 2011

Show modal window while doing something

DialogFieldManager manager = new DialogFieldManager();

manager.addCustomField(new LabelField("Please Wait..."));

final Screen popup = new PopupScreen(manager);

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

UiApplication.getUiApplication().pushScreen(popup);

}

});


//Do something long and important


UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());

}

});


Blackberry development environment

OK. I have task. I have Mac Book Pro with 8 Gb of RAM. Nothing will stop me!

Started collecting devices for testing. Was surprised that everybody is happy to give me these devices.

Just found out that blackberry has IDE for Mac OS. Geart! I can use friendly for me environment.

Installed. It is useless. It work only with devices.

On device you can only run in debug mode.

It takes forever to attach debugger.

If your application crashes -- device will restart!

Looking for alternatives.

Remove "Changes Made!" message when hitting back button

To get rid following dialog:


Put following code inside Screen class:

public boolean onClose(){

setDirty(false);

return super.onClose();

}



Tuesday, March 1, 2011

Go to previous screen(s)

//We have screens A -> B -> C

//Current screen is C

//Go to previous screen (B)

UiApplication.getUiApplication().popScreen(this);


//Go to screen A

UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());

Show alert in blackberry

protected static void showError(final String message) {

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

Dialog.alert(message);

}

});

}

Day 1. Excitement

I’m excited. Just finished my Android application (followed application). And immediately was told port it to BlackBerry.

Soon I’ll have skills in one more technology!