Thursday, November 24, 2011

BlackBerry Application


This is my first mobile application which application mainly help for the doctors.I enjoyed so much for developing this application and i met some delicious point so i like to expose those.

Calender pop up screen


Here is CalendarPopupScreen.java


/**
*
* @author aruna
*/
import cutermize.field.BorderedLabelField;
import cutermize.field.ButtonBorderedLabelField;
import cutermize.field.HeaderBorderedLabelField;
import cutermize.field.TableLayoutManager;
import cutermize.field.UnfocusableBorderedLabelField;
import cutermize.field.VerticalSpacerField;

import net.rim.device.api.ui.container.HorizontalFieldManager;

import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.component.LabelField;

import net.rim.device.api.ui.DrawStyle;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import java.util.Calendar;
import java.util.Date;
import net.rim.device.api.util.DateTimeUtilities;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;



public class CalendarPopupScreen extends PopupScreen {

//statics ------------------------------------------------------------------

private static SimpleDateFormat sdfWeekDay = new SimpleDateFormat("E");
private static SimpleDateFormat sdfMonth = new SimpleDateFormat("MMMM yyyy");

private static String SINGLE_BLANK = " ";
private static String FIELD_SIZE_STRING = " 30 ";

//variables ------------------------------------------------------------------

private int _selectedDay = -1; // None selected

private int _currentFocusDay;
private int _currentMonth; // This is the month as usually defined.
// In Calendar, January is month 0.
private String _currentMonthString;
private int _currentYear;
private Calendar _cl = Calendar.getInstance();

private LabelField _prevMonthButton;
private LabelField _currentMonthField;
private LabelField _nextMonthButton;

private static int [] tableStyles = new int [] {
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH,
TableLayoutManager.FIXED_WIDTH
};

private int [] tableSizes = new int [7] ;

private TableLayoutManager _monthManager; // This is where we display the Dates

private Field _initialFocusField = null;

private Font _normalFont;
private Font _boldFont;

public CalendarPopupScreen() {
this(new Date());
}

public CalendarPopupScreen(long dateMilliSecs) {
this(new Date(dateMilliSecs));
}

public CalendarPopupScreen(Date selectedDate) {
super(new VerticalFieldManager());
_cl.setTime(selectedDate);
createScreen(_cl.get(Calendar.DAY_OF_MONTH), _cl.get(Calendar.MONTH) + 1, _cl.get(Calendar.YEAR));
}

public CalendarPopupScreen(int focusDay, int startMonth, int startYear) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
createScreen(focusDay, startMonth, startYear);
}

protected void onDisplay() {
if ( _initialFocusField != null ) {
_initialFocusField.setFocus();
_initialFocusField = null;
}
super.onDisplay();
}

private void createScreen(int focusDay, int startMonth, int startYear) {
// Initial values for Screen
_currentFocusDay = focusDay;
_currentMonth = startMonth;
_currentYear = startYear;

// Try to set Font based on Display Size so thjat Popup fits on Screen
_normalFont = this.getFont().derive(Font.PLAIN);
if ( _normalFont.getAdvance(FIELD_SIZE_STRING) > Display.getWidth()/10 ) {
// Font too big, make it a bit smaller
do {
_normalFont = _normalFont.derive(Font.PLAIN, _normalFont.getHeight()-1);
} while ( _normalFont.getAdvance(FIELD_SIZE_STRING) > Display.getWidth()/10 );
this.setFont(_normalFont);
}
_boldFont = _normalFont.derive(Font.BOLD);

// Create Month and button Fields once
HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
_prevMonthButton = new ButtonBorderedLabelField(SINGLE_BLANK + Characters.BLACK_LEFT_POINTING_TRIANGLE + SINGLE_BLANK,LabelField.FOCUSABLE);
hfm.add(_prevMonthButton);
_currentMonthField = new LabelField(SINGLE_BLANK + _currentMonthString + SINGLE_BLANK);
hfm.add(_currentMonthField);
_nextMonthButton = new ButtonBorderedLabelField(SINGLE_BLANK + Characters.BLACK_RIGHT_POINTING_TRIANGLE + SINGLE_BLANK,LabelField.FOCUSABLE);
hfm.add(_nextMonthButton);
this.add(hfm);
this.add(new VerticalSpacerField(3));

int columnSize = this.getFont().getAdvance(FIELD_SIZE_STRING);
for ( int i = 0; i < tableSizes.length; i++ ) {
tableSizes[i] = columnSize;
}
_monthManager = new TableLayoutManager(tableStyles, tableSizes, 0, TableLayoutManager.FIELD_HCENTER);
this.add(_monthManager);

displayMonth();

}


private void displayMonth() {

_monthManager.deleteAll(); // Delete the stuff currently there

// Determine start of the Month
_cl.set(Calendar.DAY_OF_MONTH, 1);
_cl.set(Calendar.MONTH, _currentMonth - 1);
_cl.set(Calendar.YEAR, _currentYear);
_cl.set(Calendar.HOUR_OF_DAY, 12);
_cl.set(Calendar.MINUTE, 0);
_cl.set(Calendar.SECOND, 0);
_cl.set(Calendar.MILLISECOND, 1);
long startOfMonth = _cl.getTime().getTime();

// set Month in 'header'
_currentMonthString = sdfMonth.formatLocal(_cl.getTime().getTime());
_currentMonthField.setText(SINGLE_BLANK + _currentMonthString + SINGLE_BLANK);

// Figure out where the month display needs to start
int workDay = _cl.get(Calendar.DAY_OF_WEEK);
int startAt = 0;
switch (workDay) {
case(Calendar.MONDAY): {
startAt = -6;
break;
}
case(Calendar.TUESDAY): {
startAt = -1;
break;
}
case(Calendar.WEDNESDAY): {
startAt = -2;
break;
}
case(Calendar.THURSDAY): {
startAt = -3;
break;
}
case(Calendar.FRIDAY): {
startAt = -4;
break;
}
case(Calendar.SATURDAY): {
startAt = -5;
break;
}
case(Calendar.SUNDAY): {
startAt = -6;
break;
}
}
Date workDate = _cl.getTime();
long workDateTime = workDate.getTime() + ((long)startAt) * ((long)DateTimeUtilities.ONEDAY);

long dayTime = workDateTime;
for ( int i = 0; i < 7; i++ ) {
String weekDay = sdfWeekDay.formatLocal(dayTime);
HeaderBorderedLabelField hblf = new HeaderBorderedLabelField(weekDay.substring(0,1), LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
hblf.setFont(_boldFont);
_monthManager.add(hblf);
dayTime = dayTime + DateTimeUtilities.ONEDAY;
}
for ( int i = 0; i < 42; i++ ) { // Need at most 6 rows
workDate.setTime(workDateTime);
_cl.setTime(workDate);
workDateTime = workDateTime + DateTimeUtilities.ONEDAY;
BorderedLabelField blf = null;
int actualDate = _cl.get(Calendar.DAY_OF_MONTH);
String tempDateString = Integer.toString(actualDate);
if ( _cl.get(Calendar.MONTH) == _currentMonth - 1 ) {
blf = new BorderedLabelField(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER | LabelField.FOCUSABLE);
if ( _currentFocusDay == actualDate ) {
_initialFocusField = blf;
}
} else
if ( (i % 7 == 0) && (startOfMonth < _cl.getTime().getTime()) ) {
// We have finished the month
break;
} else {
blf = new UnfocusableBorderedLabelField(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
}
_monthManager.add(blf);
}
if ( this.isDisplayed() && _initialFocusField != null ) {
_initialFocusField.setFocus();
_initialFocusField = null;
}
}

public boolean keyChar(char key, int status, int time) {
boolean retval = false;
switch (key) {
case Characters.ENTER: {
// We have selected something
processFocus();
retval = true;
break;
}
case Characters.ESCAPE: {
close();
retval = true;
break;
}
default:
retval = super.keyChar(key, status, time);
break;
}
return retval;
}



private boolean processFocus() {
// Should deal with whatever the focsu is currently on
Field focusField = this.getDelegate().getLeafFieldWithFocus();
if ( focusField instanceof ButtonBorderedLabelField ) {
_currentFocusDay = -1; // Leave focus on 'button'
_initialFocusField = focusField;
int monthIncrement = 33;
if ( focusField == _prevMonthButton ) {
monthIncrement = -3;
}
_cl.set(Calendar.DAY_OF_MONTH, 1);
_cl.set(Calendar.MONTH, _currentMonth-1);
_cl.set(Calendar.YEAR, _currentYear);
Date workDate = _cl.getTime();
workDate.setTime(workDate.getTime() + (((long)monthIncrement) * ((long)DateTimeUtilities.ONEDAY)));
_cl.setTime(workDate);
_currentMonth = _cl.get(Calendar.MONTH) + 1;
_currentYear = _cl.get(Calendar.YEAR);
displayMonth();
return true;
} else
if ( focusField instanceof BorderedLabelField ) {
LabelField lab = (LabelField) focusField;
_selectedDay = Integer.parseInt(lab.getText());
close();
return true;
}
return false;
}

protected boolean trackwheelClick( int status, int time ) {
// Use navigationClick if only supporting devices post 4.2
if ( processFocus() ) {
return true;
}
return super.trackwheelClick(status, time);
}

public void close() {
UiApplication.getUiApplication().popScreen(this);
}

public Date getSelectedDate() {
if ( _selectedDay == -1 ) {
return null;
}
Calendar cl = Calendar.getInstance();
cl.set(Calendar.YEAR, _currentYear);
cl.set(Calendar.MONTH, _currentMonth - 1);
cl.set(Calendar.DAY_OF_MONTH, _selectedDay);
cl.set(Calendar.HOUR_OF_DAY, 0);
cl.set(Calendar.MINUTE, 0);
cl.set(Calendar.SECOND, 0);
cl.set(Calendar.MILLISECOND, 1);
return cl.getTime();
}

}

Here is CallenderDisplayScreen.java

import java.util.Date;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Bitmap;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;

import net.rim.device.api.ui.component.DateField;

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 net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.BorderFactory;

/**
*
* @author se-3
*/
public class CallenderDisplayScreen extends MainScreen{
private static SimpleDateFormat sdFormatTimeTableDate = new SimpleDateFormat("yyyy-MM-dd");


public CallenderDisplayScreen(){

this.getMainManager().setBackground(
BackgroundFactory.createLinearGradientBackground(0x0099CCFF,
0x0099CCFF,0x00336699,0x00336699)
);
Bitmap borderBitmap = Bitmap.getBitmapResource("border.png");
VerticalFieldManager m = new VerticalFieldManager();
m.setBorder(BorderFactory.createBitmapBorder(new XYEdges(20,20,20,20), borderBitmap));


HorizontalFieldManager hfm = new HorizontalFieldManager();
m.add(hfm);
hfm.add(_fromTableTime);
add(m);
}
public DateField _fromTableTime = new DateField("Date:", System.currentTimeMillis(), DateField.READONLY, sdFormatTimeTableDate) {
public boolean trackwheelClick(int status, int time) {
CalendarPopupScreen cs = new CalendarPopupScreen(this.getDate());
UiApplication.getUiApplication().pushModalScreen(cs);
Date selectedDate = cs.getSelectedDate();
if ( selectedDate != null ) {
this.setDate(selectedDate.getTime());
}
return true;
}
};

}


Here is StartApp.java



import net.rim.device.api.ui.UiApplication;

public class StartApp extends UiApplication{


public static void main(String[] args){
StartApp app=new StartApp();
app.enterEventDispatcher();
}

public StartApp(){
pushScreen(new CallenderDisplayScreen());
}
}

Webservice with Bean class

Feel free to pass object to client through web-service.If you are using on axis frame wrok you have to keep in mind below manifest function.

Here is EmployeeManager service class

package mypackage;

/**

* User: aruna
* Date: Feb 3, 2011
* Time: 10:46:23 AM

*/
public class EmployeeManager {
/**
* Get all employees
* @return object of Employee
*/
public Employee getAllEmployees(Employee employees) {


return employees;
}

/**
* Save employees
* @param employees Employee objects
*/


}

Here is Employee bean class
package mypackage;


public class Employee {
private String name;
private double salary;
private String designation;

/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the salary.
*/
public double getSalary() {
return salary;
}
/**
* @param salary The salary to set.
*/
public void setSalary(double salary) {
this.salary = salary;
}
/**
* @return Returns the designation.
*/
public String getDesignation() {
return designation;
}
/**
* @param designation The designation to set.
*/
public void setDesignation(String designation) {
this.designation = designation;
}
}

Here is server-config.wsdd configuration.






http://mypackage
languageSpecificType="java:mypackage.Employee"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>

MML with Java

A man-machine language or MML is a specification language. MML typically are defined to standardize the interfaces for managing a telecommunications or network device from a console.
If you are using on java below code sample helpful to MML utilisation with java.

This class used to get subscriber's account info


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.io.*;
import java.net.UnknownHostException;

public class MMLCommandGen{

/**
* @param args
*/
Socket inSocket = null;
//PrintWriter out = null;
BufferedOutputStream out1 = null;
BufferedReader in = null;
public static String strStartFlag="`SC`"; //4 bytes

String strMsgHeader=""; //28 bytes

public static String hdVrsnNo="1.02"; //4 bytes
static String hdTrmnlId="internal"; //8 bytes
String hdSrvcName="SRVM"; //8 bytes
static String hdLangType="english"; //8 bytes

String strSsnHeader=""; //18 bytes

String ssnId="";//8 bytes
String ssnCtrlChar="DLGLGN";//6 bytes
static String ssnRsrvdFld="0000";//4 bytes

String strTxnHeader=""; //18 bytes

String txnId="1";//8 bytes
String txnCtrlChar="TXBEG";//6 bytes
//static String txnRsrvdFld="00";//4 bytes
static String txnRsrvdFld="0000";//4 bytes

int msgLenth=0;
String strMsgLength=""; //4 bytes


String strOprInfo="DISP PPS ACNTINFO:MSISDN=0000000000"; //upto 64kb

String strChkSum=""; //8 bytes

String inCommand="";

public String getHdLangType() {
return hdLangType;
}

public static void setHdLangType(String LangType) {
int hdLangTypeLnth=LangType.length();
for (int i=0;i<8-hdLangTypeLnth; i++){
LangType=" "+LangType;
}
hdLangType = LangType;
}

public String getHdSrvcName() {
return hdSrvcName;
}

public void setHdSrvcName(String hdSrvcName) {
int hdSrvcNameLnth=hdSrvcName.length();
for (int i=0;i<8-hdSrvcNameLnth; i++){
hdSrvcName=" "+hdSrvcName;
}
this.hdSrvcName = hdSrvcName;
}

public String getHdTrmnlId() {
return hdTrmnlId;
}

public static void setHdTrmnlId(String hdTmnlId) {
int hdTrmnlIdLnth=hdTmnlId.length();
for (int i=0;i<8-hdTrmnlIdLnth; i++){
hdTmnlId=" "+hdTmnlId;
}
hdTrmnlId = hdTmnlId;
}

public String getHdVrsnNo() {
return hdVrsnNo;
}

public void setHdVrsnNo(String hdVrsnNo) {
this.hdVrsnNo = hdVrsnNo;
}

public String getStrMsgHeader() {
return strMsgHeader;
}

public void setStrMsgHeader(String hdSrvcName) {
setHdSrvcName(hdSrvcName);
this.strMsgHeader =hdVrsnNo+hdTrmnlId+this.hdSrvcName+hdLangType;
}

public String getSsnCtrlChar() {
return ssnCtrlChar;
}

public void setSsnCtrlChar(String ssnCtrlChar) {
int ssnCtrlCharLnth=ssnCtrlChar.length();
for (int i=0;i<6-ssnCtrlCharLnth; i++){
ssnCtrlChar=" "+ssnCtrlChar;
}
this.ssnCtrlChar = ssnCtrlChar;
}

public String getSsnId() {
return ssnId;
}

public void setSsnId(String ssnId) {
int ssnIdLnth=ssnId.length();
for (int i=0;i<8-ssnIdLnth; i++){//=(8-)
ssnId="0"+ssnId;
}
this.ssnId = ssnId;
}

public String getSsnRsrvdFld() {
return ssnRsrvdFld;
}

public static void setSsnRsrvdFld(String rsrvdFld) {
int ssnRsrvdFldLnth=rsrvdFld.length();
for (int i=0;i<4-ssnRsrvdFldLnth; i++){
rsrvdFld="0"+rsrvdFld;
}
ssnRsrvdFld = rsrvdFld;
}

public String getStrSsnHeader() {
return strSsnHeader;
}

public void setStrSsnHeader(String ssnId,String ssnCtrlChar) {
setSsnId(ssnId);
setSsnCtrlChar(ssnCtrlChar);
this.strSsnHeader=this.ssnId+this.ssnCtrlChar+ssnRsrvdFld;
}

public String getStrTxnHeader() {
return strTxnHeader;
}

public void setStrTxnHeader(String txnId,String txnCtrlChar,String txnRsrvdFld) {
setTxnId(txnId);
setTxnCtrlChar(txnCtrlChar);
setTxnRsrvdFld(txnRsrvdFld);
this.strTxnHeader =this.txnId+this.txnCtrlChar+this.txnRsrvdFld;
}

public String getTxnCtrlChar() {
return txnCtrlChar;
}

public void setTxnCtrlChar(String txnCtrlChar) {
int txnCtrlCharLnth=txnCtrlChar.length();
for (int i=0;i<6-txnCtrlCharLnth; i++){
txnCtrlChar=" "+txnCtrlChar;
}
this.txnCtrlChar = txnCtrlChar;
}

public String getTxnId() {
return txnId;
}

public void setTxnId(String txnId) {
int txnIdLnth=txnId.length();
for (int i=0;i<8-txnIdLnth; i++){
txnId="0"+txnId;
}
this.txnId = txnId;
}

public String getTxnRsrvdFld() {
return txnRsrvdFld;
}

public static void setTxnRsrvdFld(String tRsrvdFld) {
int txnRsrvdFldLnth=tRsrvdFld.length();
for (int i=0;i<4-txnRsrvdFldLnth; i++){
tRsrvdFld="0"+tRsrvdFld;
}
txnRsrvdFld = tRsrvdFld;
}

public int getMsgLenth() {
return msgLenth;
}

public void setMsgLenth(int msgLenth) {
this.msgLenth = strMsgHeader.length()+strSsnHeader.length()+strTxnHeader.length()+strOprInfo.length();
}

public String getStrOprInfo() {
return strOprInfo;
}

public void setStrOprInfo(String strOprInfo) {
int strOprInfoLnth=strOprInfo.length()%4;
for (int i=0;i<4-strOprInfoLnth; i++){
strOprInfo=strOprInfo+" ";
}
this.strOprInfo = strOprInfo;
}

public String getStrMsgLength() {
return strMsgLength;
}

public void setStrMsgLength() {
msgLenth = strMsgHeader.length()+strSsnHeader.length()+strTxnHeader.length()+strOprInfo.length();
this.strMsgLength = Integer.toHexString(0x10000|msgLenth).substring(1).toUpperCase();
}

public String getStrChkSum() {
return strChkSum;
}

public void setStrChkSum() {

StringBuffer chkSumSb = new StringBuffer(strMsgHeader+strSsnHeader+strTxnHeader+strOprInfo);

char []res=new char[4];

for (int j = 0; j < res.length; j++) {
for (int i = j; i < chkSumSb.length(); i = i + 4) {
res[j] = (char) (res[j] ^ chkSumSb.charAt(i));
}
res[j] = (char) ((~res[j]) & 0x00ff);
}

strChkSum="";

for(int i =0;i<4;i++){
strChkSum=strChkSum+Integer.toHexString((int)res[i]);
}
this.strChkSum=strChkSum.toUpperCase();
}

public String getInCommand() {
return inCommand;
}

public void setInCommand() {
System.out.println("strStartFlag+strMsgLength+strMsgHeader+strSsnHeader+strTxnHeader+strOprInfo+strChkSum"+strSsnHeader+strTxnHeader);
this.inCommand = strStartFlag+strMsgLength+strMsgHeader+strSsnHeader+strTxnHeader+strOprInfo+strChkSum;
}

public void setInCommand(String cmd) {
this.inCommand = cmd;
}

public void setSocketConnection()throws UnknownHostException,IOException{

inSocket = new Socket("127.0.0.1",8080);
}

public void socketWriter(String command)throws IOException{

out1 = new BufferedOutputStream(inSocket.getOutputStream());
OutputStreamWriter out = new OutputStreamWriter(out1, "US-ASCII");
out.write(getInCommand());

out.close();
}

public String socketReader()throws IOException{
String resp="";

in = new BufferedReader(new InputStreamReader(inSocket.getInputStream()));

resp=in.readLine();

return resp;
}



}