Wednesday, 6 March 2013

Blackberry Text To Speech(TTS).

Like wise in Android we can implement TTS(Text To Speech ) in our Blackberry Application.
Now a days TTS is very useful.

After a long time finally i got help full link having one sample demo on TTS and that's working perfectly for me.

For Download the Source code and Document Click TTS.

Download the demo code. After register,You received two iSpeech API keys to start speech-enabling your apps. Web Evaluation key and Mobile Production key. Use Mobile Production key to test the demo app.

For Guideline download TTS Implementation Guideline


Hope this will be very helpful to you.





Monday, 4 March 2013

Blackberry Drag N Drop ListField Example.

In this blog i ll show you how to rearrange a ListField in Blackberry.
Like Android we can achieve the DragNDrop custom view in Blackberry.
It will work on  
Torch (9800), Storm (9550), Bold (9700):


For more reference click this Link

Screen-1)


Screen 2) After rearranging the ListField item.

 

Create a Blackberry project.

Write the below code in your MainScreen.java


package com.amit.rearrange.listfield;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class RearrangeMainScreen extends MainScreen {
    private VerticalFieldManager mainManager = new VerticalFieldManager();

    public RearrangeMainScreen() {
        mainManager.add(new LabelField("Welcome To Amit Gupta Blackberry Blog"));
        mainManager.add(new LabelField("Rearranging ListField Example!!"));
        mainManager.add(new MyList());
        add(mainManager);
    }
}

class MyList extends VerticalFieldManager implements ListFieldCallback {
    private int mouseDownY, mouseUpY;
    private static int rowHeight = 45;
    private int mouseDownRowIndex, mouseUpRowIndex;
    int firstSelectedRow = -1, secondSelectedRow = -1;
    private int touchX;
    private int touchY;
    private boolean showShadow = false;

    private ObjectListField list;
    private Object[] listData = new Object[] { "Android", "Blackberry", "Windows Phone",
            "Iphone", "Sembian", "Ubuntu Phone", "Motorola", "Samsung", "HTC", "Sony Experia",
            "LG", "Karbon Mobile", "MicroMax" };

    public MyList() {
        init();
        setBackground(BackgroundFactory.createLinearGradientBackground(
                0x0FCFC0, 0x0CCCC0, 0x09C9C0, 0x01C1C0));
    }

    private void init() {
        list = new ObjectListField(FOCUSABLE);
        list.setRowHeight(rowHeight);
        list.setCallback(this);
        list.set(listData);
        add(list);
    }

    private void interchangeRows(int r1, int r2) {
        Object temp = listData[r1];
        listData[r1] = listData[r2];
        listData[r2] = temp;
        invalidate();
    }

    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int width) {
        graphics.drawText(listData[index].toString(), 25, y + 10);

    }

    public Object get(ListField listField, int index) {
        return listData[index];
    }

    public int getPreferredWidth(ListField listField) {
        return getPreferredWidth();
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        // TODO Auto-generated method stub
        return 0;
    }

    public int indexOfRowAt(int posY) {
        int index = (int) Math.floor(posY / rowHeight * 1.0);
        return index;
    }

    protected boolean keyChar(char ch, int status, int time) {
        if (ch == Characters.SPACE) {
            if (firstSelectedRow == -1) {
                firstSelectedRow = list.getSelectedIndex();
                return true;
            } else {
                secondSelectedRow = list.getSelectedIndex();
                if (firstSelectedRow == secondSelectedRow) {
                    firstSelectedRow = secondSelectedRow = -1;
                    invalidate();
                    return true;
                } else {
                    interchangeRows(firstSelectedRow, secondSelectedRow);
                    firstSelectedRow = secondSelectedRow = -1;
                    return true;
                }
            }
        }
        return super.keyChar(ch, status, time);
    }

    protected void paint(Graphics graphics) {
        if (firstSelectedRow != -1) {
            int x = 0, y = firstSelectedRow * rowHeight;
            int savedColor = graphics.getColor();
            int preAlpha = graphics.getGlobalAlpha();
            graphics.setGlobalAlpha(90);
            graphics.setColor(0xFF22FF);

            graphics.fillRect(0, y, getWidth(), rowHeight);

            graphics.setColor(savedColor);
            graphics.setGlobalAlpha(preAlpha);
        }

        super.paint(graphics);

        if (showShadow && mouseDownRowIndex != -1) {
            int preAlpha = graphics.getGlobalAlpha();
            graphics.setGlobalAlpha(100);
            graphics.drawText(listData[mouseDownRowIndex].toString(), 25,
                    touchY);
            graphics.setGlobalAlpha(preAlpha);
        }
    }

    protected boolean touchEvent(TouchEvent message) {
        int eventCode = message.getEvent();

        // Get the screen coordinates of the touch event
        touchX = message.getX(1);
        touchY = message.getY(1);

        if (eventCode == TouchEvent.DOWN) {
            mouseDownY = touchY;
            mouseDownRowIndex = indexOfRowAt(mouseDownY);
            showShadow = true;
            invalidate();
            return true;
        } else if (eventCode == TouchEvent.UP) {
            showShadow = false;
            mouseUpY = touchY;
            mouseUpRowIndex = indexOfRowAt(mouseUpY);
            if (mouseDownRowIndex != mouseUpRowIndex) {
                interchangeRows(mouseDownRowIndex, mouseUpRowIndex);
                mouseDownRowIndex = mouseUpRowIndex = -1;
                return true;
            }
            mouseDownRowIndex = mouseUpRowIndex = -1;
            invalidate();
        } else if (eventCode == TouchEvent.MOVE) {
            invalidate();
            return true;
        }
        return super.touchEvent(message);
    }
}

 

Note:-

For touch-supported devices: a shadow of text of the first selected item moves along with the cursor.
For non-touch devices: I've used SPACEBAR to select the items to interchange. Select an item, press SPACEBAR, select the second item, press SPACEBAR again, items interchanged !! PressingSPACEBAR on an already selected item will deselect that item.

Download Full Source code Click  DragNDropListField


Sunday, 3 March 2013

Blackberry Custom Scrollable Bottom menu

In this blog i will show you how to implement Custom Scrollable Bottom Menu.

Screen Shots1:-


Screen Shots2) After Clicking menu item button open PopUp dialog

 
Download full Source Code Click CustomBottomMenu


Steps are given below:-

1) Create a Blackberry Project.

public final class MenuScreen extends MainScreen implements FieldChangeListener {

    private String simpleLableText = "Welcome to Amit Blackberry Blog\n Here i use Popup menu and \n CustomHoriziontalField that provide rotation effect\n i added custom popup\n and add CustomHoriziontalField in it \n i hope you like this example and \n it is useful for you,\n thanks \n Amit Gupta";
    private CustomHorizontalField menuField = null;
    private boolean isPopUpInDisplay = false;
    // MENU BUTTONS ----START
    Bitmap bitmapHome = null;
    Bitmap bitmapNext = null;
    Bitmap bitmapBack = null;
    Bitmap bitmapToc = null;
    Bitmap bitmapSettings = null;

    CustomBitmapButtomField btnHome = null;
    CustomBitmapButtomField btnNext = null;
    CustomBitmapButtomField btnBack = null;
    CustomBitmapButtomField btnToc = null;
    CustomBitmapButtomField btnSettings = null;

    PopupScreen popupScreen = null;

    public MenuScreen() {

        add(new LabelField(simpleLableText));
        addItems();
    }

    private void addItems() {
        menuField = new CustomHorizontalField();
        menuField.setBorder(BorderFactory.createBevelBorder(new XYEdges(4, 0,
                0, 0), new XYEdges(Color.DARKBLUE, 0, 0, 0), new XYEdges(
                Color.WHITE, 0, 0, 0)));
        menuField.setBackground(BackgroundFactory
                .createLinearGradientBackground(Color.DARKBLUE, Color.DARKBLUE,
                        Color.LIGHTBLUE, Color.LIGHTBLUE));
        createMenu();
    }

    private void createMenu() {

        bitmapHome = Bitmap.getBitmapResource("img/home.png");
        bitmapNext = Bitmap.getBitmapResource("img/nn.png");
        bitmapBack = Bitmap.getBitmapResource("img/ff.png");
        bitmapToc = Bitmap.getBitmapResource("img/toc.png");
        bitmapSettings = Bitmap.getBitmapResource("img/si.png");

        btnHome = new CustomBitmapButtomField(bitmapHome, bitmapHome,
                bitmapHome);
        btnNext = new CustomBitmapButtomField(bitmapNext, bitmapNext,
                bitmapNext);
        btnBack = new CustomBitmapButtomField(bitmapBack, bitmapBack,
                bitmapBack);
        btnToc = new CustomBitmapButtomField(bitmapToc, bitmapToc, bitmapToc);
        btnSettings = new CustomBitmapButtomField(bitmapSettings,
                bitmapSettings, bitmapSettings);

        btnHome.setChangeListener(this);
        btnNext.setChangeListener(this);
        btnBack.setChangeListener(this);
        btnToc.setChangeListener(this);
        btnSettings.setChangeListener(this);

        addButtonsToMenu();

        popupScreen = new PopupScreen(menuField) {
            protected void sublayout(int width, int height) {
                super.sublayout(width, height);
                super.setExtent(width, height);
                setPosition(0, Display.getHeight() - menuField.getHeight());
            };

            protected boolean keyDown(int keycode, int time) {

                if ((Keypad.key(keycode) == (Keypad.KEY_ESCAPE))
                        || Keypad.key(keycode) == (Keypad.KEY_MENU)) {
                    UiApplication.getUiApplication().popScreen(this);
                    MenuScreen.this.isPopUpInDisplay = false;
                    return true;
                } else
                    return super.keyDown(keycode, time);
            };
        };

        popupScreen.setBackground(BackgroundFactory
                .createSolidTransparentBackground(Color.BLACK, 50));
        popupScreen.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,
                0, 0, 0)));
        isPopUpInDisplay = false;

    }

    public void fieldChanged(Field field, int context) {

        UiApplication.getUiApplication().popScreen(popupScreen);
        isPopUpInDisplay = false;
        menuField.enableMoving = false;

        if (field == btnHome) {
            Dialog.inform("Home Button clicked");
        } else if (field == btnNext) {
            Dialog.inform("Next Button clicked");
        } else if (field == btnBack) {
            Dialog.inform("Back Button clicked");
        } else if (field == btnSettings) {
            Dialog.inform("Settings Button clicked");
        } else if (field == btnToc) {
            Dialog.inform("Toc Button clicked");
        }

    }

    private void addButtonsToMenu() {
        menuField.add(btnHome);
        menuField.add(btnToc);
        menuField.add(btnBack);
        menuField.add(btnNext);
        menuField.add(btnSettings);

        int padding = menuField.getFieldPadding(bitmapHome);

        btnHome.setPadding(new XYEdges(0, padding, 0, padding));
        btnNext.setPadding(new XYEdges(0, padding, 0, padding));
        btnBack.setPadding(new XYEdges(0, padding, 0, padding));
        btnToc.setPadding(new XYEdges(0, padding, 0, padding));
        btnSettings.setPadding(new XYEdges(0, padding, 0, padding));

    }

    protected boolean keyDown(int keycode, int time) {
        if (Keypad.KEY_MENU == Keypad.key(keycode)) {
            System.out.println("isPopupDisplay :" + isPopUpInDisplay);
            if (isPopUpInDisplay) {
                // delete(menuField);
                isPopUpInDisplay = false;
                UiApplication.getUiApplication().popScreen(popupScreen);
                menuField.enableMoving = false;

            } else {
                isPopUpInDisplay = true;
                UiApplication.getUiApplication().pushScreen(popupScreen);
                menuField.getField(2).setFocus();
                menuField.enableMoving = true;

            }
            return true;
        } else
            return super.keyDown(keycode, time);

    }

}

Step2)
Create a package called as "com.amit.custommenu.component"
inside src/com.amit.custommenu.component

Create a class CustomBitmapButtomField.java

package com.amit.custommenu.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.decor.BorderFactory;

public class CustomBitmapButtomField extends ButtonField{
   
   private     Bitmap activeBtn = null;
   private  Bitmap normalBtn = null;
   private  Bitmap focusBtn = null;
  
   private int btnHeight = 0;
   private int btnWidth = 0;
  
  
   public CustomBitmapButtomField(Bitmap normal,Bitmap focus,Bitmap active) {
       activeBtn = active;
       normalBtn = normal ;
       focusBtn = focus;
       btnWidth = normalBtn.getWidth();
       btnHeight = normalBtn.getWidth();
       setMargin(0, 0, 0, 0);
       setPadding(0,0,0,0);
       setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
       setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
   }
  
   protected void paint(Graphics graphics) {
         Bitmap bitmap = null;
         switch (getVisualState()) {
        case VISUAL_STATE_NORMAL:
            bitmap = normalBtn;
            break;
        case VISUAL_STATE_FOCUS:
            bitmap = focusBtn;
            break;
        case VISUAL_STATE_ACTIVE:
            bitmap = activeBtn;
            break;
        default:
            bitmap = normalBtn;
            break;
        }
        graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
    }
  
   public int getPreferredWidth() {
        return btnWidth;
    }
   public int getPreferredHeight() {
        return btnHeight;
    }
   protected void layout(int width, int height) {
        setExtent(btnHeight,btnHeight);
    }
  
  
   
}

2.1 Create class called as CustomHorizontalField.java

package com.amit.custommenu.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class CustomHorizontalField extends HorizontalFieldManager {

   
    public boolean enableMoving = false;
    private int focusOnIndex = 0;   
   
    public void focusChangeNotify(int arg0) {
   
        super.focusChangeNotify(arg0);
       
        if(enableMoving)
        {
            int newFocusIndex = getFieldWithFocusIndex();
            if(newFocusIndex != focusOnIndex)
            {
                if( (newFocusIndex - focusOnIndex )> 0 )
                {
                    startRotation(0, getFieldCount()-1);       
                }else
                {
                    startRotation(getFieldCount()-1, 0);
                }
            }
           
        }else
        {
            focusOnIndex = getFieldWithFocusIndex();
        }
       
    }
    public int getPreferredWidth() {
        return Display.getWidth();
    }
    public int getPreferredHeight() {
        return super.getPreferredHeight();
    }
    protected void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(Display.getWidth(), getPreferredHeight());
        setExtent(Display.getWidth(), getPreferredHeight());
    }
   
    public int getFieldPadding(Bitmap bitmap)
    {
        System.out.println("Bitmap Width :"+bitmap.getWidth());
        System.out.println("getFieldCount:"+getFieldCount());
        System.out.println("Display.getWidth:"+Display.getWidth());
       
        int padding = ( Display.getWidth() - ( bitmap.getWidth() * getFieldCount() ) )/(getFieldCount()*2);
       
        if(padding < 0)
        padding = 0;
       
        return padding;
    }
    private void startRotation(int from,int to)
    {
        Field field = getField(from);
        delete(field);
        insert(field, to);
    }
   
   
}






If you have any doubt fell free to mail in
amithigh.gupta@gmail.com

 

Blackberry AES256 Encryption and Decryption.

Today i am going to share very useful and interesting topic i.e AES 256 Encryption and decryption.
For achieving this i created a Library for doing Encryption operation.

You can get Library file from this Link

Step1)
Create a Blackberry Project named as "AESTest"

Step2) In your Main Screen Write down the following piece of code as given below.

package co.aes.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import net.rim.device.api.crypto.CryptoException;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import com.aes.encryption.AES256Encryption;
import com.aes.encryption.AESUtility;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class AESEncryption extends MainScreen implements
        FieldChangeListener {
    /** This is the Secret Key required for Encryption and Decryption */
    private String secretKey="ABCtfiHT1S1biFnoiFFWZcPwWBnhxqhkQ1Ipyh2yG7U=";
    private String cipher, plainText;
    private ButtonField but_encrypt, but_decrypt;
    private LabelField encrypted, decrypted;
    private VerticalFieldManager vfm;
    private EditField enterText;

    /**
     * Creates a new AESMainScreen object
     */
    public AESEncryption() {
        setTitle("AES");
        but_encrypt = new ButtonField("Encrypt");
        but_decrypt = new ButtonField("Decrypt");
        encrypted = new LabelField();
        decrypted = new LabelField();
        enterText = new EditField();
        enterText.setPadding(15, 15, 15, 15);
        encrypted.setPadding(5, 5, 5, 5);
        decrypted.setPadding(5, 5, 5, 5);
        but_encrypt.setChangeListener(this);
        but_decrypt.setChangeListener(this);

        vfm = new VerticalFieldManager(Field.FIELD_HCENTER
                | Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT) {
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(maxWidth, maxHeight);
                int topSpace = (Display.getHeight() / 8);
                setPadding(topSpace, 0, 0, 0);
            }
        };
        HorizontalFieldManager hfm1 = new HorizontalFieldManager(
                Field.FIELD_HCENTER);
        hfm1.add(but_encrypt);
        hfm1.add(but_decrypt);
        vfm.add(hfm1);
        vfm.add(encrypted);
        vfm.add(decrypted);
        add(enterText);
        add(vfm);
    }

    public void fieldChanged(Field field, int context) {
        /** Encryption Button OnClick Event */
        if (field == but_encrypt) {
            try {
                cipher = AES256Encryption.encryptbyCBC(
                        AESUtility.Base64ConvertedArray(secretKey),
                        AESUtility.getByteInLittleIndian(enterText.getText()));
                System.out.println("Cipher Text:-" + " " + cipher);
                /** Setting the Cipher Text to the Label Field*/
                encrypted.setText("Cipher Text:----" +"\n"+cipher);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (CryptoException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (field == but_decrypt) {
            try {
                plainText = AES256Encryption.decryptCBC(
                        AESUtility.Base64ConvertedArray(secretKey),
                        AESUtility.Base64ConvertedArray(cipher));
            } catch (CryptoException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Plain text:-" + "  " + plainText);
            /** Setting the Plain Text to the Label Field*/
            decrypted.setText("Plain Text:-" + plainText);
        }
    }
}

ScreenShots:-1) Enter your text to Encrypt with the key. You can give your own key.




































ScreenShots:2) After Clicking Encrypt Button, Cipher text will be shown in LabelField.



































ScreenShots:3) After Decrypted.



































Download Full source Code Click AESEncryptionDecryption