1 // Copyright 2011 Google Inc. All Rights Reserved.
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
15 package com.hughes.android.dictionary;
\r
17 import java.io.File;
\r
18 import java.io.FileOutputStream;
\r
19 import java.io.IOException;
\r
20 import java.io.InputStream;
\r
21 import java.io.OutputStream;
\r
22 import java.net.URL;
\r
23 import java.net.URLConnection;
\r
24 import java.util.concurrent.Executor;
\r
25 import java.util.concurrent.Executors;
\r
26 import java.util.concurrent.atomic.AtomicBoolean;
\r
27 import java.util.zip.ZipEntry;
\r
28 import java.util.zip.ZipFile;
\r
30 import android.app.Activity;
\r
31 import android.content.Intent;
\r
32 import android.os.Bundle;
\r
33 import android.os.Handler;
\r
34 import android.util.Log;
\r
35 import android.widget.ProgressBar;
\r
36 import android.widget.TextView;
\r
38 public class DownloadActivity extends Activity {
\r
40 public static final String SOURCE = "source";
\r
41 public static final String DEST = "dest";
\r
45 long bytesProcessed = 0;
\r
46 long contentLength = -1;
\r
48 private final Executor downloadExecutor = Executors.newSingleThreadExecutor();
\r
49 private final Handler uiHandler = new Handler();
\r
51 final AtomicBoolean stop = new AtomicBoolean(false);
\r
53 /** Called when the activity is first created. */
\r
55 public void onCreate(final Bundle savedInstanceState) {
\r
56 ((DictionaryApplication)getApplication()).applyTheme(this);
\r
58 super.onCreate(savedInstanceState);
\r
59 final Intent intent = getIntent();
\r
60 source = intent.getStringExtra(SOURCE);
\r
61 dest = intent.getStringExtra(DEST);
\r
62 if (source == null || dest == null) {
\r
63 throw new RuntimeException("null source or dest.");
\r
65 setContentView(R.layout.download_activity);
\r
67 final TextView sourceTextView = (TextView) findViewById(R.id.source);
\r
68 sourceTextView.setText(source);
\r
70 final TextView destTextView = (TextView) findViewById(R.id.dest);
\r
71 destTextView.setText(dest);
\r
73 final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
\r
74 progressBar.setIndeterminate(false);
\r
75 progressBar.setMax(100);
\r
78 contentLength = 100;
\r
79 setDownloadStatus(getString(R.string.openingConnection));
\r
81 final Runnable runnable = new Runnable() {
\r
85 final File destFile = new File(dest);
\r
86 destFile.getParentFile().mkdirs();
\r
88 final File destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile
\r
90 destTmpFile.deleteOnExit();
\r
92 final URL uri = new URL(source);
\r
93 final URLConnection connection = uri.openConnection();
\r
94 contentLength = connection.getContentLength();
\r
95 final InputStream in = connection.getInputStream();
\r
97 throw new IOException("Unable to open InputStream from source: " + source);
\r
99 final FileOutputStream out = new FileOutputStream(destTmpFile);
\r
100 int bytesRead = copyStream(in, out, R.string.downloading);
\r
102 if (bytesRead == -1 && !stop.get()) {
\r
104 destTmpFile.renameTo(destFile);
\r
106 Log.d("THAD", "Stopped downloading file.");
\r
109 if (dest.toLowerCase().endsWith(".zip")) {
\r
110 final ZipFile zipFile = new ZipFile(destFile);
\r
111 final File destUnzipped = new File(dest.substring(0, dest.length() - 4));
\r
112 final ZipEntry zipEntry = zipFile.getEntry(destUnzipped.getName());
\r
113 if (zipEntry != null) {
\r
114 destUnzipped.delete();
\r
115 Log.d("THAD", "Unzipping entry: " + zipEntry.getName() + " to " + destUnzipped);
\r
116 final InputStream zipIn = zipFile.getInputStream(zipEntry);
\r
117 final OutputStream zipOut = new FileOutputStream(destUnzipped);
\r
118 contentLength = zipEntry.getSize();
\r
119 bytesRead = copyStream(zipIn, zipOut, R.string.unzipping);
\r
123 setDownloadStatus(String.format(getString(R.string.downloadFinished),
\r
125 } catch (IOException e) {
\r
126 Log.e("THAD", "Error downloading file", e);
\r
127 setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));
\r
131 private int copyStream(final InputStream in, final OutputStream out, final int messageId)
\r
132 throws IOException {
\r
133 bytesProcessed = 0;
\r
135 final byte[] bytes = new byte[1024 * 16];
\r
137 while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {
\r
138 out.write(bytes, 0, bytesRead);
\r
139 bytesProcessed += bytesRead;
\r
140 if (count++ % 20 == 0) {
\r
141 setDownloadStatus(getString(messageId, bytesProcessed, contentLength));
\r
150 downloadExecutor.execute(runnable);
\r
154 protected void onStop() {
\r
159 private void setDownloadStatus(final String status) {
\r
160 uiHandler.post(new Runnable() {
\r
161 public void run() {
\r
162 final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
\r
163 if (contentLength > 0) {
\r
164 progressBar.setProgress((int) (bytesProcessed * 100 / contentLength));
\r
167 final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);
\r
168 downloadStatus.setText(status);
\r