K
K
KOS_MOS2012-02-06 20:04:38
Java
KOS_MOS, 2012-02-06 20:04:38

Java/Android - 3rd party class interaction - newbie question?

Practicing Java programming for Android.
There was a question that I can not correctly solve and I want to hear your advice.
The example is simplified, but the essence is the following - I am writing an Android program that receives a list of links from the server and downloads them to the phone, here are two classes that are used for downloading:

<font color="black"><font color="#0000ff">public</font> <font color="#0000ff">class</font> Download {<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">public</font> <font color="#0000ff">long</font> getTotal() {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">return</font> total;<br/>
&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">protected</font> <font color="#0000ff">void</font> setTotal(<font color="#0000ff">long</font> total) {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">this</font>.total = total;<br/>
&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">private</font> <font color="#0000ff">long</font> total = 0;<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">public</font> boolean download(String link, String path) throws IOException {<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008000">// download the file</font><br/>
&nbsp;&nbsp;&nbsp;&nbsp;InputStream input = <font color="#0000ff">new</font> BufferedInputStream(url.openStream());<br/>
&nbsp;&nbsp;&nbsp;&nbsp;OutputStream output = <font color="#0000ff">new</font> FileOutputStream(outputFile);<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">int</font> count = 0;<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">byte</font> data[] = <font color="#0000ff">new</font> <font color="#0000ff">byte</font>[1024];<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">while</font> ((count = input.read(data)) != -1) {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">this</font>.setTotal(<font color="#0000ff">this</font>.getTotal() + count);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.write(data, 0, count);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">return</font> <font color="#0000ff">true</font>;<br/>
&nbsp;&nbsp;}<br/>
}<br/>
<br/>
<font color="#0000ff">public</font> <font color="#0000ff">class</font> DownloadBatch {<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">public</font> boolean download(ArrayList&lt;String[]&gt; tasks) throws IOException {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<br/>
&nbsp;&nbsp;&nbsp;&nbsp;Download loader = <font color="#0000ff">new</font> Download();<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">for</font> (String[] task : tasks) {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loader.download(task[0], task[1]);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">return</font> <font color="#0000ff">true</font>;<br/>
&nbsp;&nbsp;}<br/>
}</font><br/>
<br/>
<font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>

The Download class downloads a file from the specified address to the specified location, the DownloadBatch class allows you to batch download using the Download class.
The application uses only the DownloadBatch class, let the download process be displayed in the ProgressDialog in the format - the name of the file being downloaded / how many have been downloaded.
How to display this information in real time in ProgressDialog?
The main nuance is that the Download and DownloadBatch classes cannot be edited.
Only two ideas came to mind:
  1. Use AOP, for example with AspectJ
  2. Write a class whose object will periodically poll the DownloadBatch class

I'm not sure if these are the best options and would like to know the opinion of more experienced people - what is the best thing to do in such situations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anmipo, 2012-02-06
@anmipo

If the Download and DownloadBatch classes cannot be changed, then there is no way, because the progress state knows only loaderdeclared in DownloadBatch.download()and does not exist outside this method.
If, however, the code can be changed, you can adapt the example from this article: " Processes and threads in Android: writing AsyncTask correctly " (just read the comments).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question