If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
arunbiet
Posts: 376
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Progress bar issue

I am implementing I-App downlaod of media.I am using the below two classes

 

package com.imi.dynamicodp.common;

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

public final class InProgressWaitingScreen {
	public PopupScreen popup;
	public GaugeField gaugeField;
	private boolean useful;
	LabelField label;
	private int timeout = 1;
	ProgressBarThread progressBarThread;

	public InProgressWaitingScreen(String title) {
		DialogFieldManager manager = new DialogFieldManager();
		popup = new PopupScreen(manager);
		label = new LabelField(title);
		gaugeField = new GaugeField("", 0, 100,5,
				GaugeField.PERCENT);
		manager.addCustomField(label);
		manager.addCustomField(gaugeField);

		progressBarThread = new ProgressBarThread(this);
		progressBarThread.start();
	}

	public void open() {
		UiApplication.getUiApplication().pushScreen(popup);
		popup.doPaint();
	}

	public void close() {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				if (popup.isDisplayed()) {
					UiApplication.getUiApplication().popScreen(popup);
					progressBarThread.stop();
				}
			}
		});
	}

	private class ProgressBarThread extends Thread {
		private InProgressWaitingScreen progressScreen;

		public ProgressBarThread(InProgressWaitingScreen screen) {
			progressScreen = screen;
		}

		public synchronized void stop() {
			useful = false;
		}

		public void run() {
			useful = true;
			int iterations = 0;

			while (useful) {
				try {
					Thread.sleep(timeout);
				} catch (Exception e) {

				}

				if (++iterations > 100)
					iterations = 1;

				progressScreen.gaugeField.setValue(iterations);
				
				progressScreen.popup.invalidate();
				
				System.out.println("invalidate(" + iterations + ")");
			}
		}
	}

}

 And this downloadhelper class

 

package com.imi.dynamicodp.common;


import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.content.Invocation;
import javax.microedition.content.Registry;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;

import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.ui.UiApplication;

import com.imi.dynamicodp.main.DynamicODP;
import com.imimobile.dynamicodp.http.ConnectorODP;

public class DownloadHelper implements Runnable {

   private String _url;
   private String type;
   String filename;
   FileConnection outputFile;
   InProgressWaitingScreen progressbar;
   public DownloadHelper(String url) {
      _url = url;
  progressbar = new InProgressWaitingScreen( "" );
      progressbar.open();
     
   }
   
   public void run() {
	  
      HttpConnection connection = null;
      OutputStream output = null;
      InputStream input = null;
      try {
    	
         connection = (HttpConnection) ConnectorODP.open(_url);
         System.out.print("download url------------"+_url);
         if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
            input = new DataInputStream(connection.openInputStream());
            int len = (int) connection.getLength();   // Get the content length
            if (len > 0) {
            	String mimeType = connection.getType();
            	String contentType[]=StringUtil.split(mimeType, "/");
            	 if(mimeType.startsWith("image/")){
            		 filename = _url.substring(_url.lastIndexOf('=') + 1);
            		  outputFile = (FileConnection) Connector.open("file:///SDCard/BlackBerry/pictures/" + filename+"."+contentType[contentType.length-1],
                             Connector.READ_WRITE);
            		 }
        		else if(mimeType.startsWith("video/")){
        			filename = _url.substring(_url.lastIndexOf('=') + 1);
            	        			 outputFile = (FileConnection) Connector.open("file:///SDCard/BlackBerry/videos/" + filename+"."+contentType[contentType.length-1], 
                            Connector.READ_WRITE);
        		}
        		else if(mimeType.startsWith("audio/")){
        			filename = _url.substring(_url.lastIndexOf('=') + 1);
            	        			 outputFile = (FileConnection) Connector.open("file:///SDCard/BlackBerry/music/" + filename+"."+contentType[contentType.length-1], 
                            Connector.READ_WRITE);
        		}
        		else {
        			  UiApplication.getUiApplication().invokeLater(new Runnable() {
        					public void run() {
        			if (DynamicODP.downloadDialog != null) {
          				DynamicODP.downloadDialog.cancel();
          				DynamicODP.downloadDialog = null;
          			}
        					}});
        			BrowserSession site = Browser.getDefaultSession();
	        		site.displayPage(_url);
	        		site.showBrowser();
        		}
            	 
               if (!outputFile.exists()) {
                  outputFile.create();
               }
               if (len <= outputFile.availableSize()) {
                  output = outputFile.openDataOutputStream();
                  int maxRead = 1024;  
                  byte[] buffer = new byte[maxRead];
                  int bytesRead;

                  for (;;) {
                     bytesRead = input.read(buffer);
                     if (bytesRead <= 0) {
                        break;
                     }
                     System.out.print("writing downlaod thread");
                     output.write(buffer, 0, bytesRead);
                  }
                  output.close();
                  progressbar.close();
                  synchronized(UiApplication.getEventLock()) {
          			if (DynamicODP.downloadDialog != null) {
          				DynamicODP.downloadDialog.cancel();
          				DynamicODP.downloadDialog = null;
          			}
          		}
                  final Registry registry = Registry.getRegistry(DownloadHelper.class.getName());
                  if (mimeType.startsWith("image/")){
                	  Invocation invocation1 = new Invocation("file:///SDCard/BlackBerry/pictures/"+filename+"."+contentType[contentType.length-1]); 
                          try { 
                              registry.invoke(invocation1); 
                          } catch (Exception ex) { }
                  }else if(mimeType.startsWith("video/")){
                	  Invocation invocation1 = new Invocation("file:///SDCard/BlackBerry/videos/"+filename+"."+contentType[contentType.length-1]); 
                          try { 
                              registry.invoke(invocation1); 
                          } catch (Exception ex) { }
                  }else if(mimeType.startsWith("audio/")){
                	  Invocation invocation1 = new Invocation("file:///SDCard/BlackBerry/music/"+filename+"."+contentType[contentType.length-1]); 
                          try { 
                              registry.invoke(invocation1); 
                          } catch (Exception ex) { }
                  }
               }
              
            }
         }
      } catch (java.io.IOException ioe) {
         ioe.printStackTrace();
      } finally {
         try {
            if (output != null) {
               output.close();
            }
            if (connection != null) {
               connection.close();
            }
            if (input != null) {
               input.close();
            }
         } catch (IOException e) {
         }
      }
   }
   
}

 Everything is working fine.The problem is that my progressbar is not consistent.It completes 2-3 times 100% before downloading the content.I want that progress should show 100% only when my download is complete

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Progress bar issue

maybe you can work with the content length and download in chunks, but in general you won't get reliable information and should maybe just display a busy animation or something without a progress.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.