import java.net.URL;\r
import java.util.concurrent.Executor;\r
import java.util.concurrent.Executors;\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
\r
import android.app.Activity;\r
import android.content.Intent;\r
private final Executor downloadExecutor = Executors.newSingleThreadExecutor();\r
private final Handler uiHandler = new Handler();\r
\r
+ final AtomicBoolean stop = new AtomicBoolean(false);\r
+\r
/** Called when the activity is first created. */\r
@Override\r
public void onCreate(final Bundle savedInstanceState) {\r
\r
final InputStream in;\r
final FileOutputStream out;\r
- \r
+\r
final File destFile = new File(dest);\r
final File destTmpFile;\r
try {\r
- destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile.getParentFile());\r
+ destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
+ .getParentFile());\r
final URL uri = new URL(source);\r
in = uri.openStream();\r
out = new FileOutputStream(destTmpFile);\r
try {\r
long byteCount = 0;\r
int bytesRead;\r
- final byte[] bytes = new byte[4096];\r
- while ((bytesRead = in.read(bytes)) != -1) {\r
+ final byte[] bytes = new byte[1024 * 8];\r
+ int count = 0;\r
+ while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
out.write(bytes, 0, bytesRead);\r
byteCount += bytesRead;\r
- setDownloadStatus(String.format("Downloading: %d bytes so far", byteCount));\r
+ if (count++ % 20 == 0) {\r
+ setDownloadStatus(String.format("Downloading: %d bytes so far",\r
+ byteCount));\r
+ }\r
}\r
in.close();\r
out.close();\r
- destFile.delete();\r
- destTmpFile.renameTo(destFile);\r
- setDownloadStatus(String.format("Downloaded finished: %d bytes", byteCount));\r
+ if (bytesRead == -1 && !stop.get()) {\r
+ destFile.delete();\r
+ destTmpFile.renameTo(destFile);\r
+ } else {\r
+ Log.d("THAD", "Stopped downloading file.");\r
+ }\r
+ setDownloadStatus(String.format("Downloaded finished: %d bytes",\r
+ byteCount));\r
} catch (IOException e) {\r
Log.e("THAD", "Error downloading file", e);\r
- setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage());\r
+ setDownloadStatus("Error downloading file: \n"\r
+ + e.getLocalizedMessage());\r
}\r
}\r
};\r
downloadExecutor.execute(runnable);\r
}\r
\r
+ @Override\r
+ protected void onStop() {\r
+ stop.set(true);\r
+ super.onStop();\r
+ }\r
+\r
private void setDownloadStatus(final String status) {\r
final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r
+// final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
uiHandler.post(new Runnable() {\r
public void run() {\r
downloadStatus.setText(status);\r