1 package com.hughes.android.dictionary;
\r
4 import java.io.FileOutputStream;
\r
5 import java.io.IOException;
\r
6 import java.io.InputStream;
\r
8 import java.net.URLConnection;
\r
9 import java.util.concurrent.Executor;
\r
10 import java.util.concurrent.Executors;
\r
11 import java.util.concurrent.atomic.AtomicBoolean;
\r
13 import android.app.Activity;
\r
14 import android.content.Intent;
\r
15 import android.os.Bundle;
\r
16 import android.os.Handler;
\r
17 import android.util.Log;
\r
18 import android.widget.ProgressBar;
\r
19 import android.widget.TextView;
\r
21 public class DownloadActivity extends Activity {
\r
23 public static final String SOURCE = "source";
\r
24 public static final String DEST = "dest";
\r
28 long bytesDownloaded = 0;
\r
29 long contentLength = -1;
\r
31 private final Executor downloadExecutor = Executors.newSingleThreadExecutor();
\r
32 private final Handler uiHandler = new Handler();
\r
34 final AtomicBoolean stop = new AtomicBoolean(false);
\r
36 /** Called when the activity is first created. */
\r
38 public void onCreate(final Bundle savedInstanceState) {
\r
39 super.onCreate(savedInstanceState);
\r
40 final Intent intent = getIntent();
\r
41 source = intent.getStringExtra(SOURCE);
\r
42 dest = intent.getStringExtra(DEST);
\r
43 if (source == null || dest == null) {
\r
44 throw new RuntimeException("null source or dest.");
\r
46 setContentView(R.layout.download_activity);
\r
48 final TextView sourceTextView = (TextView) findViewById(R.id.source);
\r
49 sourceTextView.setText(source);
\r
51 final TextView destTextView = (TextView) findViewById(R.id.dest);
\r
52 destTextView.setText(dest);
\r
54 final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
\r
55 progressBar.setIndeterminate(false);
\r
56 progressBar.setMax(100);
\r
58 final InputStream in;
\r
59 final FileOutputStream out;
\r
61 final File destFile = new File(dest);
\r
62 final File destTmpFile;
\r
64 destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile
\r
66 destTmpFile.deleteOnExit();
\r
67 final URL uri = new URL(source);
\r
68 final URLConnection connection = uri.openConnection();
\r
69 contentLength = connection.getContentLength();
\r
70 in = connection.getInputStream();
\r
71 out = new FileOutputStream(destTmpFile);
\r
72 } catch (Exception e) {
\r
73 Log.e("THAD", "Error downloading file", e);
\r
74 setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));
\r
78 final Runnable runnable = new Runnable() {
\r
81 bytesDownloaded = 0;
\r
83 final byte[] bytes = new byte[1024 * 8];
\r
85 while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {
\r
86 out.write(bytes, 0, bytesRead);
\r
87 bytesDownloaded += bytesRead;
\r
88 if (count++ % 20 == 0) {
\r
89 setDownloadStatus(getString(R.string.downloading,
\r
90 bytesDownloaded, contentLength));
\r
95 if (bytesRead == -1 && !stop.get()) {
\r
97 destTmpFile.renameTo(destFile);
\r
99 Log.d("THAD", "Stopped downloading file.");
\r
101 setDownloadStatus(String.format(getString(R.string.downloadFinished),
\r
103 } catch (IOException e) {
\r
104 Log.e("THAD", "Error downloading file", e);
\r
105 setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));
\r
110 downloadExecutor.execute(runnable);
\r
114 protected void onStop() {
\r
119 private void setDownloadStatus(final String status) {
\r
120 uiHandler.post(new Runnable() {
\r
121 public void run() {
\r
122 final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
\r
123 if (contentLength > 0) {
\r
124 progressBar.setProgress((int) (bytesDownloaded * 100 / contentLength));
\r
127 final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);
\r
128 downloadStatus.setText(status);
\r