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
Highlighted
Regular Contributor
Posts: 59
Registered: ‎03-01-2011
My Device: Not Specified

thread issue for Downloading media.

I hav a Main Productscreen containing list of media files. and clicking on any media file will push anotherscreen(ActvityIndicatorScreen) which will start downloading in thread and also displaying  progress bar in percentage. The issue is that when i come back from this activityscreen (using back button/escape key) to back productScreen  then Downloading process will remain continue in backend thread. But clicking on that media file again push Activity screen which will start downloading again and display progress bar activity from start. This way multiple times downloading of same file is starting.

I am downloading & saving file in chunks. (coz of large size) 

code is below . Pls helpout me how can i get reference of old screen to keep track of old downloading

this is code of ActivityIndicator screen which i am always pushing on clicking any media file using

following line of code

ProgressIndicatorScreen pIndicatorScreen =newProgressIndicatorScreen(url);

UiApplication.getUiApplication().pushScreen(pIndicatorScreen);

class ProgressIndicatorScreen extends MainScreen {
	ProgressIndicatorView view = new ProgressIndicatorView(0);
	ProgressIndicatorModel model = new ProgressIndicatorModel(0, 100, 0);
	ProgressIndicatorController controller = new ProgressIndicatorController();
	 OutputStream outStream=null;
	ProgressThread _progressThread;
	FileConnection fc=null;
	boolean cancel=false;
	public ProgressIndicatorScreen(String url) 
	{

		setTitle("Downloading..");

		model.setController(controller);
		view.setModel(model);
		view.setController(controller);
		controller.setModel(model);
		controller.setView(view);

		view.setLabel("Percent completion");
		view.createProgressBar(Field.FIELD_HCENTER);
		add(view);
		_progressThread = new ProgressThread(url);
		_progressThread.setPriority(Thread.MAX_PRIORITY); 
		System.gc();
		_progressThread.start();
	}

	// A thread that simulates the processing of data
	class ProgressThread extends Thread {
		DataInputStream dis = null;
		DataInputStream dis1 = null;
		
		DataOutputStream dos = null;
		
		HttpConnection hc;
		String filename;
		String url;
		 StreamConnection stream = null;
         InputStream input = null;
         MDSPushInputStream pushInputStream=null;
         private StreamConnectionNotifier _notify;
		ProgressThread(String url) {
			this.url = url;
		}

		public void run() {
			
			int percentage;
			// ProgressIndicatorDemoScreen.this.model.setValue(i);
			try {
				ConnectionFactory connfac = new ConnectionFactory();
				ConnectionDescriptor conndesc;
				FileConnection FileConn = null;
				conndesc = connfac.getConnection(url);
				if (conndesc != null) {
					hc = (HttpConnection) conndesc.getConnection();
					final int responsecode = hc.getResponseCode();
					dis = hc.openDataInputStream();
					// to download the media file into sd card
					int len = (int) hc.getLength();
					byte[] data = new byte[8192];
					if (len > 0) {
						int actual = 0;
						int bytesread = 0;
						// data = new byte[len];
						int tempLen=8192;
						int filenameStart = url.lastIndexOf('/');
						int paramStart = url.indexOf(';');
						// If there is no semicolon, the file name ends at the
						// end
						// of the line
						if (paramStart < 0) {
							paramStart = url.length();
						}
						filename = url.substring(filenameStart, paramStart);
						 fc = (FileConnection) Connector
								.open(Config.CACHE_URL + filename);
							try 
	                        {
							 	data = new byte[8192];
	                            int chunk = 0;
	                            if (!fc.exists()) {
        							fc.create();
	                            }
	                            long total = 0;
	                            outStream = fc.openOutputStream();
	                            while ( -1 != (chunk = dis.read(data)) && cancel==false)
	                            {
	                            
	                                  outStream.write(data, 0, chunk);
	                                  total += chunk;
	                                 // publishProgress((int) (total * 100 / lenghtOfFile));
	                                  ProgressIndicatorScreen.this.model.setValue((int)(total * 100 / len));
	                                  if(total==len)
	                                  {
	                                	  cancel=true;
	                                  }
	        					}
    							outStream.close();
    							ProgressIndicatorScreen.this.model.setValue(100);
	                        	System.out.println("file downloading complete");
    							// playing/Browsing file
	                        	if (!fc.exists()) {
									BrowserSession browserSession = Browser
											.getDefaultSession();
									int semiIndex = url.lastIndexOf(';');
									url = url.substring(0, semiIndex);
									browserSession.displayPage(url);
									browserSession.showBrowser();
								} else {
									if(fc.fileSize()==len)
									{
										BrowserSession browserSession = Browser
											.getDefaultSession();
										browserSession.displayPage(Config.CACHE_URL
											+ filename);
										browserSession.showBrowser();
									}
								}
	                        	fc.close();
	                        	//
	                        } 
	                        catch (IOException e1) 
	                        {
	                            System.out.println(e1.getMessage());                
	                        }
					
					}

				}
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}

		}
	}
	
	public boolean onClose()
	{
		System.out.println("ON CLOSEING SCREEN");
		try {
			outStream.close();
			this.save();
			cancel=true;
			UiApplication.getUiApplication().popScreen();
			
			//fc.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}

 

.

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: thread issue for Downloading media.

i would suggest that you implement a data storage class that every screen uses and where the results of the download are processed.
you could also put the download itself into a service, to have further MVC appliance (i see that you have model, view and controller already, but you don't really use them as it seems).

if you put all functionality in a single class, like you have with your screen, it is likely that you become confused.
----------------------------------------------------------
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
Regular Contributor
Posts: 59
Registered: ‎03-01-2011
My Device: Not Specified

Re: thread issue for Downloading media.

thankz for quick reply.

Can u pls guide me little bit more how to implement datastorage class functionality. giving some example of code