Saturday, December 17, 2011

PopupScreen example in Blackberry


import java.util.Vector;

import mypackage.LoginScreen;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;



public class PasswordBox extends PopupScreen implements KeyListener, TrackwheelListener
{
   private               String                 m_response;
   private               PasswordEditField      m_answer;
   private               String                 m_password;
   private               String                 m_userName;
   
   public PasswordBox()
   {
       super(new VerticalFieldManager(),Field.FOCUSABLE);

       m_password="moorthy";
                        
       LabelField question = new LabelField("Enter password to unlock.");
       m_answer = new PasswordEditField(" ","");
       add(question);
       add(new SeparatorField());
       add(m_answer);
     
   }
 
 
// This function gets called if the password match
 public void accept()
   {
 close();
 UiEngine ui = Ui.getUiEngine();
 ui.pushScreen(new LoginScreen(0));

        //System.exit(0);
     
   }
 
 

 
 
   ////////////////////////////////////////////
   /// implementation of TrackwheelListener
   ////////////////////////////////////////////
   public boolean trackwheelClick(int status, int time)
   {
       m_response = m_answer.getText();
       if (m_response.equals(m_password))
       {
           accept();
       }
       else
       {
         //  Dialog.alert("Invalid Password !");
       }
       return true;
   }
 
 
   /** Invoked when the trackwheel is released */
   public boolean trackwheelUnclick(int status, int time)
   {
       return false;
   }
 
 
   /** Invoked when the trackwheel is rolled. */
   public boolean trackwheelRoll(int amount, int status, int time)
   {
       return true;
   }
 
 
   /////////////////////////////////////
   /// implementation of Keylistener
   /////////////////////////////////////
   public boolean keyChar(char key, int status, int time)
   {
       //intercept the ESC key - exit the app on its receipt
       boolean retval = false;
       switch (key)
       {
           case Characters.ENTER:
               m_response = m_answer.getText();
               if (m_response.equals(m_password))
               {
                   accept();
               }
               // an alert is displayed if the password is incorrect
               else
               {
                //   Dialog.alert("Invalid Password !");
               }
               retval = true;
               break;
         
             
             
           default:
               retval = super.keyChar(key,status,time);
       }
       return retval;
   }
 
   /** Implementation of KeyListener.keyDown */
   public boolean keyDown(int keycode, int time)
   {
      if(Keypad.key(keycode)==Keypad.KEY_END)
        {
          Application.getApplication().requestForeground();
                  return false;
                 
       }
      return false;
   }
   /** Implementation of KeyListener.keyRepeat */
   public boolean keyRepeat(int keycode, int time)
   {
     
       return false;
   }
   /** Implementation of KeyListener.keyStatus */
   public boolean keyStatus(int keycode, int time)
   {
       return false;
   }
   /** Implementation of KeyListener.keyUp */
   public boolean keyUp(int keycode, int time)
 
   {
    if(Keypad.key(keycode)==Keypad.KEY_END)
        {
             Application.getApplication().requestForeground();
                  return false;
                 
       }
       return false;
   }
 
 /* Invoked when this screen is obscured. A Screen is obscured when it is was the topmost and is no longer by means of:
     (1).  a new screen pushed on the display stack
        (2).  a global screen is displayed above this screen
  (3).  this screen's application goes into the background
      (4).  this screen is pushed and it is deemed obscured by the above rules
 */

   protected void onObscured()
   {
   
     // Application.getApplication().requestForeground();
   }
}

Send SMS example in Blackberry

try{

String[] addressees = {mobile number};
                    if (addressees != null)
                    {
                        mc = (MessageConnection)Connector.open("sms://");
                        TextMessage message = (TextMessage)mc.newMessage(MessageConnection.TEXT_MESSAGE, "sms://");
                        message.setPayloadText(msg);
                        
                        for (int i = 0; i < addressees.length; i++)
                        {
                            if (addressees[i] != null)
                            {
                                message.setAddress("sms://" + addressees[i]);
                                mc.send(message);
                            }
                        }
                        mc.close();
                    }
}
catch (ConnectionNotFoundException ex)
        {
            System.out.println(ex.getMessage());
        }
        catch (IllegalArgumentException ex)
        {
            System.out.println("Ilegal Argument: " + ex.getMessage());
        }
        catch (SIMCardException ex)
        {
            System.out.println("SIMCard Exception: " + ex.getMessage());
        }
        catch (InterruptedIOException e) 
        {
            System.out.println(e.getMessage());
        }
        catch (InterruptedException e) 
        {
            System.out.println(e.getMessage());
        }
        catch (IOException e) 
        {
            System.out.println(e.getMessage());
        }
        catch (NullPointerException e)
        {
            System.out.println(e.getMessage());
        }
        catch (SecurityException e) 
        {
            System.out.println(e.getMessage());
        }
         

Friday, December 9, 2011

Resize image and Field manager Design manual

package mypackage;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class Layout_Design {
   
    /// Dynamically change image//////////////
   
    public Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
    {
        // Handle null image
        if (image == null)
        {
            return null;
        }
     
        //return bestFit(image.getBitmap(), width, height);
     
        int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
     
        int requiredWidthFixed32 = Fixed32.toFP(width);
        int requiredHeightFixed32 = Fixed32.toFP(height);
     
        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
     
        image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
     
        return image.getBitmap();
    }
   
    /////VerticalFieldManager dynamically////////////////////////
   
    public VerticalFieldManager getCustomVerticalManager( final int width, final int height)
    {
        VerticalFieldManager verticalManager = new VerticalFieldManager(net.rim.device.api.ui.Manager.VERTICAL_SCROLL | net.rim.device.api.ui.Manager.VERTICAL_SCROLLBAR)
        {
          
            protected void sublayout( int maxWidth, int maxHeight )
            {
                int displayWidth = width;
                int displayHeight = height;
                super.sublayout( displayWidth, displayHeight);
                setExtent( displayWidth, displayHeight);
            }
        };
       
        return verticalManager;
    }
    public VerticalFieldManager getCustomVerticalManager2( final int width, final int height)
    {
        VerticalFieldManager verticalManager = new VerticalFieldManager(net.rim.device.api.ui.Manager.NO_VERTICAL_SCROLL | net.rim.device.api.ui.Manager.NO_VERTICAL_SCROLLBAR)
        {
          
            protected void sublayout( int maxWidth, int maxHeight )
            {
                int displayWidth = width;
                int displayHeight = height;
                super.sublayout( displayWidth, displayHeight);
                setExtent( displayWidth, displayHeight);
            }
        };
       
        return verticalManager;
    }

    public HorizontalFieldManager getCustomHorizontalManager( final int width, final int height)
    {
        HorizontalFieldManager HorizontalManager = new HorizontalFieldManager(net.rim.device.api.ui.Manager.VERTICAL_SCROLL | net.rim.device.api.ui.Manager.VERTICAL_SCROLLBAR)
        {
          
            protected void sublayout( int maxWidth, int maxHeight )
            {
                int displayWidth = width;
                int displayHeight = height;
                super.sublayout( displayWidth, displayHeight);
                setExtent( displayWidth, displayHeight);
            }
        };
       
        return HorizontalManager;
    }   
 
}




////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1. How to use Bitmap Method?
       
Layout_Design method=new Layout_Design();

                 EncodedImage ei=EncodedImage.getEncodedImageResource("background.png");
                 VericalFieldManager backgroundlay=method.getCustomVerticalManager(deviceWidth, (deviceHeight*80)/100);
 backgroundlay.setBackground(BackgroundFactory.createBitmapBackground(method.getScaledBitmapImage(ei,Display.getWidth(),Display.getHeight())));