06-24-2011 02:31 AM
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;
}
.
06-24-2011 03:07 AM
06-24-2011 03:17 AM
thankz for quick reply.
Can u pls guide me little bit more how to implement datastorage class functionality. giving some example of code