How To Change the field on focus in blackberry - blackberry

Hello Sir I m making an app in which i got stuck in a problem please help me out ...
in one page i m getting the data from server and displaying that data on the page now i m use Focusable for highlighting them ...now my requirement is that when focus come on any data the it should change the field taking that name on which focus is present ..
following is the code which is showing the data on the page now it is working on field change where i need to click the label field ....
static LabelField[] CrDrLabels;
public LowerCreditors() throws Exception {
super(VerticalFieldManager.VERTICAL_SCROLL);
CrDrLabels = new LabelField[CrDrList.VendorNameArr.length];
for (int i = 0; i < CrDrLabels.length; i++) {
final int t = i;
VerticalFieldManager hfm = new VerticalFieldManager(USE_ALL_WIDTH) {
protected void sublayout(int Width, int Height) {
// TODO Auto-generated method stub
super.sublayout(Width, Height);
setPositionChild(getField(0), 15, 0);
setPositionChild(getField(1), Display.getWidth()
- getFont().getAdvance(CrDrList.VendorValArr[t])
- 10, 0);
}
};
CrDrLabels[i] = new LabelField(CrDrList.VendorNameArr[i],
LabelField.FOCUSABLE | Field.USE_ALL_WIDTH) {
protected boolean navigationClick(int status, int time) {
fieldChanged(this, 0);
return true;
};
};
LabelField Value = new LabelField(CrDrList.VendorValArr[i]);
CrDrLabels[i].setPadding(0, 170, 0, 0);
hfm.add(CrDrLabels[i]);
hfm.add(Value);
add(hfm);
}
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
for (int i = 0; i < CrDrList.VendorNameArr.length; i++) {
getFieldWithFocus();
if (field == CrDrLabels[i]) {
String LN = CrDrLabels[i].getText();
CrDrList.cname1 = LN;
LabelField cname1 = CrDrLabels[i];
CrDrList.selLad = CrDrLabels[i];
}
}
}
and the the other code is
menu.add(new MenuItem("View last 5 days transaction", 20, 10) {
public void run() {
if(property.Status.equals("online"))
{
Viewlast5daystransaction();
}
else
{
Dialog.alert("Please login Online to access the data..");
}
}
String LedgerName = CrDrList.cname1;
private void Viewlast5daystransaction() {
//Dialog.alert("Last 5 Days Transaction");
int resCode = 0;
HttpConnection connection = null;
try {
connection = getConn(property.LedgerDetailsURL);
} catch (Exception e1) {
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
e1.printStackTrace();
}
try {
resCode = connection.getResponseCode();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(resCode==0)
{
Dialog.alert("Internet Connectivity not available.. Please try again later..");
TallyA TA;
try {
TA = new TallyA();
UiApplication.getUiApplication().popScreen();
UiApplication.getUiApplication().pushScreen(TA);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sPostdata = "{\"tok\":\""
+ CompanyList.tok;
System.out.println("Postedddddd Dataaaaaaaaa" + sPostdata);
try {
String response = LedgerSearchReport();
System.out.println("response" + response);
ParseBankdetails.parsebankdetails(response);
System.out.println("response : " + response);
if(response != null)
{
LastFiveDaysCr LF;
try {
LF = new LastFiveDaysCr();
UiApplication.getUiApplication().pushScreen(LF);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Dialog.alert("No Data..");
Creditiors LF;
try {
LF = new Creditiors();
UiApplication.getUiApplication().pushScreen(LF);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
});
in this field only i want that instead of field change there should be focus change ...
please help me on that ...

I think you should implement the logic in "onFoucus()" method ,instead of using "onfieldChanged()" for CrDrLabels[i];

Related

Vaadin upload with PipedInputStream & PipedOutputStream example

I just started learning Vaadin 8 and my first example is Upload button. I was stuck with an issue where I could not solve the problem for many hours and hours.
Here it is,
I am returning PipedOutputStream in the receiveUpload method,
Here is the code for receiveUpload method,
public OutputStream receiveUpload(String filename, String mimeType) {
this.fileName = filename;
this.mimeType = mimeType;
try {
pipedOutputStream = new PipedOutputStream();
pipedInputStream = new PipedInputStream(pipedOutputStream);
if (filename == null || filename.trim().length() == 0) {
upload.interruptUpload();
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return pipedOutputStream;
}
In the uploadSucceeded method, I need to take the pipedinputstream and send it another method to load the stream into the database
public void uploadSucceeded(SucceededEvent event) {
try {
fileUploadOperation.upload(pipedInputStream); --> I need to push all the stream data in one go into a method to generate a file at the business layer
} catch (Exception e) {
e.printStackTrace();
}
}
When I was running the application, it hangs out for a long time and I could not figure out where it is. Later I could notice that both piped input and piped output streams should be created in separate threads or at least one of them in a separate thread but don't know how to handle it.
Any help
I am pasting the complete class for more information,
public class WebCommunityView implements Receiver, FailedListener, SucceededListener, StartedListener, FinishedListener {
private PipedOutputStream pipedOutputStream = null;
private PipedInputStream pipedInputStream = null;
private Upload upload = null;
private String fileName = null, mimeType = null;
private Grid<FileListProperties> fileListGrid = null;
public final static WebCommunityView newInstance(WebContentScreen screen) {
vw.initBody();
return vw;
}
protected void initBody() {
VerticalLayout verticalLayout = new VerticalLayout();
fileListGrid = new Grid<FileListProperties>();
fileListGrid.addColumn(FileListProperties::getCreatedDate).setCaption("Date");
fileListGrid.addColumn(FileListProperties::getFileName).setCaption("File Name");
fileListGrid.addColumn(FileListProperties::getUserName).setCaption("User Name");
fileListGrid.addComponentColumn(this::buildDownloadButton).setCaption("Download");
fileListGrid.setItems(loadGridWithFileInfo());
upload = new Upload("", this);
upload.setImmediateMode(false);
upload.addFailedListener((Upload.FailedListener) this);
upload.addSucceededListener((Upload.SucceededListener) this);
upload.addStartedListener((Upload.StartedListener) this);
upload.addFinishedListener((Upload.FinishedListener) this);
Label fileUploadLabel = new Label("Label"));
verticalLayout.addComponent(currentListLabel);
verticalLayout.addComponent(fileListGrid);
verticalLayout.addComponent(fileUploadLabel);
verticalLayout.addComponent(upload);
mainbody.addComponent(verticalLayout);
}
#Override
public void uploadSucceeded(SucceededEvent event) {
try {
//Model Layer
fileUploadOperation.upload(pipedInputStream);
fileUploadOperation.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void uploadFailed(FailedEvent event) {
if (event.getFilename() == null) {
Notification.show("Upload failed", Notification.Type.HUMANIZED_MESSAGE);
}
try {
//Model Layer
fileUploadOperation.abort();
} catch (Exception e) {
e.printStackTrace();
}
}
public OutputStream receiveUpload(String filename, String mimeType) {
this.fileName = filename;
this.mimeType = mimeType;
try {
pipedOutputStream = new PipedOutputStream();
new Thread() {
public void run() {
try {
System.out.println("pipedInputStream Thread started");
pipedInputStream = new PipedInputStream(pipedOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
if (filename == null || filename.trim().length() == 0) {
screen.displayMessage("Please select a file to upload !", WebContentScreen.MESSAGE_TYPE_WARNING);
upload.interruptUpload();
} else {
Properties properties = new Properties();
properties.setProperty("NAME", fileName);
properties.setProperty("MIME_TYPE", mimeType);
//Model Layer
fileUploadOperation.initialize(properties);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("pipedOutputStream:"+pipedOutputStream);
return pipedOutputStream;
}
private List<FileListProperties> loadGridWithFileInfo() {
List<FileListProperties> list = null;
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
list = new ArrayList<FileListProperties>(1);
Collection<FileInfo> fileInfoList = fileCommandQuery.lstFilesForDownload();
for (Iterator iterator = fileInfoList.iterator(); iterator.hasNext();) {
FileInfo fileInfo = (FileInfo) iterator.next();
Properties properties = fileInfo.getProperties();
Collection<String> mandatoryParameters = fileInfo.getMandatoryProperties();
FileListProperties fileListProperties = new FileListProperties();
for (Iterator iterator2 = mandatoryParameters.iterator(); iterator2.hasNext();) {
String key = (String) iterator2.next();
String value = properties.getProperty(key);
if (key != null && key.equalsIgnoreCase("NAME")) {
fileListProperties.setFileName(value);
} else if (key != null && key.equalsIgnoreCase("USER_NAME")) {
fileListProperties.setUserName(value);
} else if (key != null && key.equalsIgnoreCase("CREATED_DATE")) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1550566760000L);
fileListProperties.setCreatedDate(dateFormat.format(calendar.getTime()));
}
}
if (fileListProperties != null) {
list.add(fileListProperties);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
dateFormat = null;
}
return list;
}
private Button buildDownloadButton(FileListProperties fileListProperties) {
Button button = new Button("...");
button.addClickListener(e -> downloadFile(fileListProperties));
return button;
}
private void downloadFile(FileListProperties fileListProperties) {
}
}
The problem in your example is that you need to do the actual file handling in a separate thread. The Viritin add-on contains a component called UploadFileHandler that simplifies this kind of usage a lot. It will provide you InputStream to consume. The integration test for the component contains this kind of usage example.
Also, my recent blog entry about the subject might help.

how to use multi choice spinner in mono for android?

I want to use multi choice spinner in mono for android.
I want to bind the countries to the spinner
Now in the normal spinner there is label with radio button.
But I want the label with the Checkbox.
can any one please help me.
AlertDialog.Builder alt_bld = new AlertDialog.Builder(
CareCardActivity.this);
alt_bld.setTitle("Select Recepients");
alt_bld.setMultiChoiceItems(tempname, new boolean[tempname.length] , new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
alt_bld.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
ListView list = ((AlertDialog) dialog).getListView();
Log.v("LIST COUNT:: ", ""+list.getCount());
for (int i = 0; i < list.getCount(); i++) {
boolean checked = list.isItemChecked(i);
if (checked) {
sb.append(contactNumber[i]).append(";");
}
}
sb = sb.replace(
sb.length() - 1,
sb.length(), "");
txtPhoneNo.setText(sb.toString());
}
});
alt_bld.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog alert = alt_bld.create();
alert.show();
I have tried this code in the eclipse it is working fine in it, but I want to do it for Mono develop in C#.
call this function on click of button
ShowDialog(DIALOG_MULTIPLE_CHOICE);
Add Following Code
protected override Dialog OnCreateDialog(int id)
{
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, OnDateSet, date.Year, date.Month - 1, date.Day);
case DIALOG_MULTIPLE_CHOICE: {
var builder = new AlertDialog.Builder (this);
// builder.SetIcon (Resource.Drawable.ic_popup_reminder);
builder.SetTitle ("Select Country");
builder.SetMultiChoiceItems (countryName, new bool[countryName.Length], MultiListClicked);
builder.SetPositiveButton ("ok", OkClicked);
builder.SetNegativeButton ("Cancel", CancelClicked);
return builder.Create ();
}
}
return null;
}
private void MultiListClicked (object sender, DialogMultiChoiceClickEventArgs e)
{
Console.WriteLine ("countryMultiListClicked");
if (e.IsChecked) {
mSelectedItems.Add (countryName [(int)e.Which]);
mSelectedItemsID.Add (countryID [(int)e.Which]);
}
else if (mSelectedItems.Contains(countryName [(int)e.Which]))
{
mSelectedItems.Remove(countryName [(int)e.Which]);
mSelectedItemsID.Remove(countryID [(int)e.Which]);
}
}
private void OkClicked (object sender, DialogClickEventArgs e)
{
Console.WriteLine ("countryOkClicked");
String listString = "";
for (int i =0; i<mSelectedItems.Count; i++) {
listString += mSelectedItems [i] + ",";
}
if (listString.Length > 0) {
listString = listString.Remove (listString.Length - 1);
}
et_country.Text = listString;
listStringId = "";
for (int i =0; i<mSelectedItemsID.Count; i++) {
listStringId += mSelectedItemsID [i] + ",";
}
if (listStringId.Length > 0) {
listStringId = listStringId.Remove (listStringId.Length - 1);
}
Console.WriteLine (listStringId);
}
private void CancelClicked (object sender, DialogClickEventArgs e)
{
Console.WriteLine("countryCancelClicked");
}
This works fine ........!

show Photo of my contact from PIM Contact list in blackberry?

Please anybody let me know how to work with this code, I want to display the Contact Photo on screen. getting null from getPhoto() method. I searched a lot but got nothing apart from this code from Contact Interface. but its not working for me.....!!!
public class PhotoExample {
private Contact _contact;
public PhotoExample(Contact contact) throws PIMException {
ContactList contactList = (ContactList)
PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
_contact = contactList.createContact();
/*_contact = contact;*/
}
public void setPhoto() throws IOException {
byte[] photo = getSamplePhoto();
byte[] photoEncoded = Base64OutputStream.encode(photo, 0, photo.length, false, false);
if (_contact.countValues(Contact.PHOTO) > 0) {
_contact.setBinary(Contact.PHOTO, 0, PIMItem.ATTR_NONE, photoEncoded, 0, photo.length);
} else {
_contact.addBinary(Contact.PHOTO, PIMItem.ATTR_NONE, photoEncoded, 0, photo.length);
}
}
public byte[] getPhoto() throws IOException {
if (_contact.countValues(Contact.PHOTO) > 0) {
byte[] photoEncoded = _contact.getBinary(Contact.PHOTO, 0);
return Base64InputStream.decode(photoEncoded, 0, photoEncoded.length);
} else {
return null;
}
}
private static byte[] getSamplePhoto() {
return null;
// return the raw bytes of the photo to use
}
/*public static void main(String[] args) throws Throwable {
PhotoExample example = new PhotoExample();
example.setPhoto();
example.getPhoto();
}*/
}
I am using the above code like this: -
try {
_photo = new PhotoExample(_contact);
b = _photo.getPhoto();
} catch (PIMException e) {
System.out.println(e+"===>");
e.printStackTrace();
} catch (IOException e) {
System.out.println(e+"===>");
e.printStackTrace();
}
After a long time , I tried this task again after a few searching, now i got success and want to share as this code may be useful for many.
BlackBerryContactList contactList = (BlackBerryContactList)BlackBerryPIM.getInstance().openPIMList(BlackBerryPIM.CONTACT_LIST, BlackBerryPIM.READ_WRITE);
Enumeration contactListItems = contactList.items();
while (contactListItems.hasMoreElements()) {
BlackBerryContact contact = (BlackBerryContact)contactListItems.nextElement();
byte[] imageBytesBase64 = contact.getBinary(BlackBerryContact.PHOTO, 0);
byte[] imageBytes = null;
try {
imageBytes = Base64InputStream.decode(imageBytesBase64, 0, imageBytesBase64.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EncodedImage encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
Bitmap bitmap = encodedImage.getBitmap();
bitmaps.addElement(bitmap);
BitmapField fd= new BitmapField(bitmap, Field.FIELD_HCENTER);
add(fd);

Blackberry Push Integration Client Sample Code

I need a sample application code for push integration in my blackberry application. I have registered my application for the push credentials and have received them.
please help,
Kind Regards,
Rupesh
This is fully working push application code it may be help you for implement push notification.
public class push_Main {
/**
* Entry point for this application
* #param args Command line arguments (not used)
*/
private static final String REGISTER_SUCCESSFUL = "rc=200";
private static final String DEREGISTER_SUCCESSFUL = REGISTER_SUCCESSFUL;
private static final String USER_ALREADY_SUBSCRIBED = "rc=10003";
private static final String ALREADY_UNSUSCRIBED_BY_USER = "rc=10004";
private static final String ALREADY_UNSUSCRIBED_BY_PROVIDER = "rc=10005";
private static final String PUSH_PORT = ""; //push port
private static final String BPAS_URL = "http://pushapi.eval.blackberry.com";
private static final String APP_ID = ""; // add application id
// private static final String CONNECTION_SUFFIX = ";deviceside=false;ConnectionType=seekrit string";
private static String URL = "http://:100"; // PORT 100 add your posh port.
private static final int CHUNK_SIZE = 256;
public static ListeningThread _listeningThread;
public static StreamConnectionNotifier _notify;
private static final long ID = 0x954a603c0dee81e0L;
public push_Main() {
// TODO Auto-generated constructor stub
NotificationsManager.registerSource(ID, theSource, NotificationsConstants.IMPORTANT);
if(_listeningThread==null)
{
System.out.println("msg on listening thread 1");
_listeningThread = new ListeningThread();
System.out.println("msg on listening thread 2");
_listeningThread.start();
System.out.println("msg on listhning thread 3 ");
}
}
public static class ListeningThread extends Thread
{
private boolean _stop = false;
/**
* Stops the thread from listening.
*/
private synchronized void stop()
{
_stop = true;
try
{
// Close the connection so the thread will return.
_notify.close();
}
catch (Exception e)
{
}
}
/**
* Listen for data from the HTTP url. After the data has been read,
* render the data onto the screen.
* #see java.lang.Runnable#run()
*/
public void run()
{
StreamConnection stream = null;
InputStream input = null;
MDSPushInputStream pushInputStream=null;
while (!_stop)
{
try
{
// Synchronize here so that we don't end up creating a connection that is never closed.
synchronized(this)
{
// Open the connection once (or re-open after an IOException), so we don't end up
// in a race condition, where a push is lost if it comes in before the connection
// is open again. We open the url with a parameter that indicates that we should
// always use MDS when attempting to connect.
System.out.println("\n\n msg connection 1");
_notify = (StreamConnectionNotifier)Connector.open(URL);
System.out.println("\n\n msg connection 2");
}
while (!_stop)
{
// NOTE: the following will block until data is received.
System.out.println("\n\n msg notify 1");
stream = _notify.acceptAndOpen();
System.out.println("\n\n msg 1 ");
try
{
System.out.println("\n\n msg 2");
input = stream.openInputStream();
System.out.println("\n\n msg 3 ");
pushInputStream= new MDSPushInputStream((HttpServerConnection)stream, input);
System.out.println("\n\n msg 4");
// Extract the data from the input stream.
DataBuffer db = new DataBuffer();
byte[] data = new byte[CHUNK_SIZE];
int chunk = 0;
while ( -1 != (chunk = input.read(data)) )
{
db.write(data, 0, chunk);
}
updateMessage(data);
// This method is called to accept the push.
pushInputStream.accept();
data = db.getArray();
}
catch (IOException e1)
{
// A problem occurred with the input stream , however, the original
// StreamConnectionNotifier is still valid.
// errorDialog(e1.toString());
}
finally
{
if ( input != null )
{
try
{
input.close();
}
catch (IOException e2)
{
}
}
if ( stream != null )
{
try
{
stream.close();
}
catch (IOException e2)
{
}
}
}
}
}
catch (IOException ioe)
{
// Likely the stream was closed. Catches the exception thrown by
// _notify.acceptAndOpen() when this program exits.
errorDialog(ioe.toString());
}
finally
{
/*
if ( _notify != null )
{
try
{
_notify.close();
_notify = null;
}
catch ( IOException e )
{
}
}
*/
}
}
}
}
private static void updateMessage(final byte[] data)
{
System.out.println("\n\n msg 6");
Application.getApplication().invokeLater(new Runnable()
{
public void run()
{
// Query the user to load the received message.
// Dialog.alert( new String(data));
UiApplication.getUiApplication().invokeLater( new Runnable() {
public void run()
{
NotificationsManager.triggerImmediateEvent(ID, 0, null, null);
Dialog d = new Dialog( Dialog.D_OK, new String(data) ,0, null, Screen.DEFAULT_CLOSE);
// _dialogShowing = true;
UiApplication.getUiApplication().pushGlobalScreen( d, 10, UiApplication.GLOBAL_MODAL );
// Dialog is closed at this point, so we cancel the event.
}
} );
}
});
}
public static void registerBpas() {
/**
* As the connection suffix is fixed I just use a Thread to call the connection code
*
**/
new Thread() {
public void run() {
try {
final String registerUrl = formRegisterRequest(BPAS_URL, APP_ID, null) + Conn.getConnectionParameters();
System.out.println("\n\n\n msg registerBPAS URL is: "+ registerUrl);
HttpConnection httpConnection = (HttpConnection) Connector.open(registerUrl);
InputStream is = httpConnection.openInputStream();
String response = new String(IOUtilities.streamToBytes(is));
System.out.println("\n\n\n\n\n\n msg RESPOSE CODE : " + response);
close(httpConnection, is, null);
String nextUrl = formRegisterRequest(BPAS_URL, APP_ID, response) + Conn.getConnectionParameters();
System.out.println("\n\n\n\n\n\n msg nextUrl : " + nextUrl);
HttpConnection nextHttpConnection = (HttpConnection) Connector.open(nextUrl);
InputStream nextInputStream = nextHttpConnection.openInputStream();
response = new String(IOUtilities.streamToBytes(nextInputStream));
System.out.println("\n\n\n\n\n\n msg RESPOSE CODE 1: " + response);
close(nextHttpConnection, is, null);
if (REGISTER_SUCCESSFUL.equals(response) || USER_ALREADY_SUBSCRIBED.equals(response)) {
System.out.println("msg Registered successfully for BIS push");
} else {
System.out.println("msg BPAS rejected registration");
}
} catch (final IOException e) {
System.out.println("msg IOException on register() " + e + " " + e.getMessage());
}
}
}.start();
}
public static void close(Connection conn, InputStream is, OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
public static void errorDialog(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(message);
}
});
}
private static String formRegisterRequest(String bpasUrl, String appId, String token) {
StringBuffer sb = new StringBuffer(bpasUrl);
sb.append("/mss/PD_subReg?");
sb.append("serviceid=").append(appId);
sb.append("&osversion=").append(DeviceInfo.getSoftwareVersion());
sb.append("&model=").append(DeviceInfo.getDeviceName());
if (token != null && token.length() > 0) {
sb.append("&").append(token);
}
return sb.toString();
}
}

how to increase performance to load images from SDCard to my application?

Hi i made one small application to show all sdcard images should display in my application as Thumbnail view .my program is like following
Here problem is that in simulator it loads fast and accurate where as phone it take lots of time. it is loaded some of 30MB around not getting problem. whare it is 80MB around it is not showing images on my screen.
Note: This is perfectly working in simulator but Not device
and my images path is "file:///SDCard/images/"
My code is:
this is my application starter class
public class StartUp extends UiApplication{
public static void main(String[] args) {
StartUp start=new StartUp();
start.enterEventDispatcher();
}
public StartUp() {
pushScreen(new ViewerMainScreen());
}
}
this is my ViewerMainScreen.java
public class ViewerMainScreen extends MainScreen implements FieldChangeListener{
private ObjectListField fileList;
private String currentPath = "file:///";
public VerticalFieldManager vert_manager=null;
private BitmapField before[]=null,next[]=null;
private int size=0;Border b1,b2;
TableLayoutManager colFMgr=null;
private Vector img_data=null;
private int count=0;
public ViewerMainScreen() {
super();
setTitle("Browse to image...");
initGUI();
}
public void initGUI() {
try {
// add(getFileList());
img_data=getAllList("file:///SDCard/images");
b1=BorderFactory.createSimpleBorder(new XYEdges(3,3,3,3),new XYEdges(Color.RED, Color.RED, Color.RED, Color.RED),Border.STYLE_SOLID);
b2=BorderFactory.createSimpleBorder(new XYEdges(0,0,0,0));
size=img_data.size();
before=new BitmapField[size];
next=new BitmapField[size];
vert_manager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
colFMgr = new TableLayoutManager(new int[] {
TableLayoutManager.USE_PREFERRED_SIZE,
TableLayoutManager.USE_PREFERRED_SIZE,
TableLayoutManager.USE_PREFERRED_SIZE,
TableLayoutManager.USE_PREFERRED_SIZE,
}, Manager.HORIZONTAL_SCROLL|VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
for(int i= 0;i < size;i++)
{
EncodedImage load_img=EncodedImage.getEncodedImageResource("loading.jpg");
before[i] = new BitmapField(load_img.getBitmap(),Field.FOCUSABLE){
protected boolean navigationClick(int status,int time) {
fieldChangeNotify(0);
return super.navigationClick(status, time);
}
protected void onFocus(int direction)
{
this.setBorder(b1);
invalidate();
}
protected void onUnfocus()
{
this.setBorder(b2);
invalidate();
}
};
colFMgr.add(before[i]);
}
add(colFMgr);
Thread t=new Thread(){
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
callImageThread();
}
};
t.start();
} catch (Exception e) {
Dialog.alert(e.getMessage());
}
}
private void callImageThread() {
for(int i=0;i<size;i++)
{
if(count==6){
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ImageThread th=new ImageThread(this, "file:///SDCard/images/"+img_data.elementAt(i), i);
th.start();
count++;
}
}
public void ReplaceImage(EncodedImage img,int index)
{
before[index].setBitmap(img.getBitmap());
before[index].setChangeListener(this);
count--;
System.out.println("Thread count: ========================"+Thread.activeCount()+"============================================="+count);
}
public void fieldChanged(Field field, int context) {
for(int i=0;i<size;i++)
{
if(field==before[i])
{
synchronized (UiApplication.getEventLock()) {
EncodedImage img=null;
img=loadFile("file:///SDCard/images/"+img_data.elementAt(i));
int displayWidth = Fixed32.toFP(Display.getWidth()-100);
int imageWidth = Fixed32.toFP(img.getWidth());
int scalingFactorX = Fixed32.div(imageWidth, displayWidth);
int displayHeight = Fixed32.toFP(Display.getHeight()-100);
int imageHeight = Fixed32.toFP(img.getHeight());
int scalingFactorY = Fixed32.div(imageHeight, displayHeight);
EncodedImage scaledImage = img.scaleImage32(scalingFactorX, scalingFactorY);
UiApplication.getUiApplication().pushScreen(new ZoomScreen(scaledImage));
}
}
}
}
public void displayMessage(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform(message);
}
});
}
private Vector getAllList(String path){
Vector data=new Vector();
FileConnection fileConnection=null;
try{
fileConnection = (FileConnection) Connector.open(path);
if (fileConnection.isDirectory()) {
Enumeration directoryEnumerator = fileConnection.list();
while (directoryEnumerator.hasMoreElements()) {
data.addElement(directoryEnumerator.nextElement());
}
}
}catch (Exception e) {
System.out.println(e.getMessage());
}
finally
{
if(fileConnection!=null){
try {
fileConnection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return data;
}
private EncodedImage loadFile(String path) {
FileConnection fileConnection =null;
InputStream inputStream =null;
EncodedImage encodedImage=null;
try {
fileConnection = (FileConnection) Connector.open(path);
if(fileConnection.exists()){
inputStream = fileConnection.openInputStream();
byte[] imageBytes = new byte[(int)fileConnection.fileSize()];
inputStream.read(imageBytes);
encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
if(fileConnection!=null){
try {
fileConnection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (IOException e) {
Dialog.alert("Insert an SD Card.");
}
return encodedImage;
}
}
This is my ImageThread.java
public class ImageThread extends Thread{
private ViewerMainScreen screen;
private String path;
private int index;
private EncodedImage scaledImage=null;
public ImageThread(ViewerMainScreen screen,String path,int index) {
this.screen=screen;
this.path=path;
this.index=index;
}
public void callMainScreen(EncodedImage eimg,int Rindex) {
screen.ReplaceImage(eimg, Rindex);
}
public void run() {
FileConnection fileConnection =null;
InputStream inputStream =null;
EncodedImage encodedImage=null;
try {
fileConnection = (FileConnection) Connector.open(path);
if(fileConnection.exists()){
inputStream = fileConnection.openInputStream();
byte[] imageBytes = new byte[(int)fileConnection.fileSize()];
inputStream.read(imageBytes);
encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
int displayWidth = Fixed32.toFP(100);
int imageWidth = Fixed32.toFP(encodedImage.getWidth());
int scalingFactorX = Fixed32.div(imageWidth, displayWidth);
int displayHeight = Fixed32.toFP(100);
int imageHeight = Fixed32.toFP(encodedImage.getHeight());
int scalingFactorY = Fixed32.div(imageHeight, displayHeight);
scaledImage = encodedImage.scaleImage32(scalingFactorX, scalingFactorY);
callMainScreen(scaledImage, index);
}
} catch (IOException e) {
Dialog.alert("Insert an SD Card.");
}
finally
{
if(fileConnection!=null){
try {
fileConnection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
please help me how to increase performance of my code
I would load images only when user scrolls screen. For example load first two screens on screen start (in different thread) and load next when user is moving down. So set ScrollListener to your manager and use field postion and height to determine which is on the screen and which is going to be on screen soon.
Also use limited size thread pool and queue for images loading. Unfortunately java concurrent is not par of j2me. But there are several similar pattern implementations, like ThreadPool.
And It would be super if you also remember case when user scrolls fast - cancel scheduled loading if it's not required. For every loading task remember BitmapField position and size. Before task starts file loading check if BitmapField is visible (going to be visible) with current scroll position.
Also be aware about threads count. I see that you try to sleep main loading thread when count is 6. But there is still small possibility that 400 ms is not enough to finish work for current threads and you can easily start to create more threads.
Also minor things - use Bitmap.scaleInto (If you target is 5.0+), use synchronization around size field, next is never used, use a little better names like nextImages, threadsCount, etc.

Resources