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
7 import java.io.OutputStream;
\r
9 import java.net.URLConnection;
\r
10 import java.util.concurrent.Executor;
\r
11 import java.util.concurrent.Executors;
\r
12 import java.util.concurrent.atomic.AtomicBoolean;
\r
13 import java.util.zip.ZipEntry;
\r
14 import java.util.zip.ZipFile;
\r
16 import android.app.Activity;
\r
17 import android.content.Intent;
\r
18 import android.os.Bundle;
\r
19 import android.os.Handler;
\r
20 import android.util.Log;
\r
21 import android.widget.ProgressBar;
\r
22 import android.widget.TextView;
\r
24 public class DownloadActivity extends Activity {
\r
26 public static final String SOURCE = "source";
\r
27 public static final String DEST = "dest";
\r
31 long bytesProcessed = 0;
\r
32 long contentLength = -1;
\r
34 private final Executor downloadExecutor = Executors.newSingleThreadExecutor();
\r
35 private final Handler uiHandler = new Handler();
\r
37 final AtomicBoolean stop = new AtomicBoolean(false);
\r
39 /** Called when the activity is first created. */
\r
41 public void onCreate(final Bundle savedInstanceState) {
\r
42 ((DictionaryApplication)getApplication()).applyTheme(this);
\r
44 super.onCreate(savedInstanceState);
\r
45 final Intent intent = getIntent();
\r
46 source = intent.getStringExtra(SOURCE);
\r
47 dest = intent.getStringExtra(DEST);
\r
48 if (source == null || dest == null) {
\r
49 throw new RuntimeException("null source or dest.");
\r
51 setContentView(R.layout.download_activity);
\r
53 final TextView sourceTextView = (TextView) findViewById(R.id.source);
\r
54 sourceTextView.setText(source);
\r
56 final TextView destTextView = (TextView) findViewById(R.id.dest);
\r
57 destTextView.setText(dest);
\r
59 final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
\r
60 progressBar.setIndeterminate(false);
\r
61 progressBar.setMax(100);
\r
64 contentLength = 100;
\r
65 setDownloadStatus(getString(R.string.openingConnection));
\r
67 final Runnable runnable = new Runnable() {
\r
71 final File destFile = new File(dest);
\r
72 destFile.getParentFile().mkdirs();
\r
74 final File destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile
\r
76 destTmpFile.deleteOnExit();
\r
78 final URL uri = new URL(source);
\r
79 final URLConnection connection = uri.openConnection();
\r
80 contentLength = connection.getContentLength();
\r
81 final InputStream in = connection.getInputStream();
\r
82 final FileOutputStream out = new FileOutputStream(destTmpFile);
\r
83 int bytesRead = copyStream(in, out, R.string.downloading);
\r
85 if (bytesRead == -1 && !stop.get()) {
\r
87 destTmpFile.renameTo(destFile);
\r
89 Log.d("THAD", "Stopped downloading file.");
\r
92 if (dest.toLowerCase().endsWith(".zip")) {
\r
93 final ZipFile zipFile = new ZipFile(destFile);
\r
94 final File destUnzipped = new File(dest.substring(0, dest.length() - 4));
\r
95 final ZipEntry zipEntry = zipFile.getEntry(destUnzipped.getName());
\r
96 if (zipEntry != null) {
\r
97 destUnzipped.delete();
\r
98 Log.d("THAD", "Unzipping entry: " + zipEntry.getName() + " to " + destUnzipped);
\r
99 final InputStream zipIn = zipFile.getInputStream(zipEntry);
\r
100 final OutputStream zipOut = new FileOutputStream(destUnzipped);
\r
101 contentLength = zipEntry.getSize();
\r
102 bytesRead = copyStream(zipIn, zipOut, R.string.unzipping);
\r
106 setDownloadStatus(String.format(getString(R.string.downloadFinished),
\r
108 } catch (IOException e) {
\r
109 Log.e("THAD", "Error downloading file", e);
\r
110 setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));
\r
114 private int copyStream(final InputStream in, final OutputStream out, final int messageId)
\r
115 throws IOException {
\r
116 bytesProcessed = 0;
\r
118 final byte[] bytes = new byte[1024 * 16];
\r
120 while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {
\r
121 out.write(bytes, 0, bytesRead);
\r
122 bytesProcessed += bytesRead;
\r
123 if (count++ % 20 == 0) {
\r
124 setDownloadStatus(getString(messageId, bytesProcessed, contentLength));
\r
133 downloadExecutor.execute(runnable);
\r
137 protected void onStop() {
\r
142 private void setDownloadStatus(final String status) {
\r
143 uiHandler.post(new Runnable() {
\r
144 public void run() {
\r
145 final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
\r
146 if (contentLength > 0) {
\r
147 progressBar.setProgress((int) (bytesProcessed * 100 / contentLength));
\r
150 final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);
\r
151 downloadStatus.setText(status);
\r