]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DownloadActivity.java
0ed845417a8c4f180785327d356568f2b159d03a
[Dictionary.git] / src / com / hughes / android / dictionary / DownloadActivity.java
1 // Copyright 2011 Google Inc. All Rights Reserved.\r
2 //\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
6 //\r
7 //     http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 package com.hughes.android.dictionary;\r
16 \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
29 \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
37 \r
38 public class DownloadActivity extends Activity {\r
39 \r
40   public static final String SOURCE = "source";\r
41   public static final String DEST = "dest";\r
42 \r
43   String source;\r
44   String dest;\r
45   long bytesProcessed = 0;\r
46   long contentLength = -1;\r
47 \r
48   private final Executor downloadExecutor = Executors.newSingleThreadExecutor();\r
49   private final Handler uiHandler = new Handler();\r
50 \r
51   final AtomicBoolean stop = new AtomicBoolean(false);\r
52 \r
53   /** Called when the activity is first created. */\r
54   @Override\r
55   public void onCreate(final Bundle savedInstanceState) {\r
56     //((DictionaryApplication)getApplication()).applyTheme(this);\r
57 \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
64     }\r
65     setContentView(R.layout.download_activity);\r
66 \r
67     final TextView sourceTextView = (TextView) findViewById(R.id.source);\r
68     sourceTextView.setText(source);\r
69 \r
70     final TextView destTextView = (TextView) findViewById(R.id.dest);\r
71     destTextView.setText(dest);\r
72 \r
73     final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
74     progressBar.setIndeterminate(false);\r
75     progressBar.setMax(100);\r
76     \r
77     bytesProcessed = 0;\r
78     contentLength = 100;\r
79     setDownloadStatus(getString(R.string.openingConnection));\r
80 \r
81     final Runnable runnable = new Runnable() {\r
82       public void run() {\r
83 \r
84         try {\r
85           final File destFile = new File(dest);\r
86           destFile.getParentFile().mkdirs();\r
87 \r
88           final File destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
89               .getParentFile());\r
90           destTmpFile.deleteOnExit();\r
91 \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
96           if (in == null) {\r
97             throw new IOException("Unable to open InputStream from source: " + source);\r
98           }\r
99           final FileOutputStream out = new FileOutputStream(destTmpFile); \r
100           int bytesRead = copyStream(in, out, R.string.downloading);\r
101           \r
102           if (bytesRead == -1 && !stop.get()) {\r
103             destFile.delete();\r
104             destTmpFile.renameTo(destFile);\r
105           } else {\r
106            Log.d("THAD", "Stopped downloading file.");\r
107           }\r
108           \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
120             }\r
121           }\r
122           \r
123           setDownloadStatus(String.format(getString(R.string.downloadFinished),\r
124               bytesProcessed));\r
125           \r
126           // If all went well, we can exit this activity.\r
127           uiHandler.post(new Runnable() {\r
128             @Override\r
129             public void run() {\r
130               finish();\r
131             }\r
132           });\r
133           \r
134         } catch (IOException e) {\r
135           Log.e("THAD", "Error downloading file", e);\r
136           setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
137         }\r
138       }\r
139 \r
140       private int copyStream(final InputStream in, final OutputStream out, final int messageId)\r
141           throws IOException {\r
142         bytesProcessed = 0;\r
143         int bytesRead;\r
144         final byte[] bytes = new byte[1024 * 16];\r
145         int count = 0;\r
146         while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
147           out.write(bytes, 0, bytesRead);\r
148           bytesProcessed += bytesRead;\r
149           if (count++ % 20 == 0) {\r
150             setDownloadStatus(getString(messageId, bytesProcessed, contentLength));\r
151           }\r
152         }\r
153         in.close();\r
154         out.close();\r
155         return bytesRead;\r
156       }\r
157     };\r
158 \r
159     downloadExecutor.execute(runnable);\r
160   }\r
161 \r
162   @Override\r
163   protected void onStop() {\r
164     stop.set(true);\r
165     super.onStop();\r
166   }\r
167 \r
168   private void setDownloadStatus(final String status) {\r
169     uiHandler.post(new Runnable() {\r
170       public void run() {\r
171         final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
172         if (contentLength > 0) {\r
173           progressBar.setProgress((int) (bytesProcessed * 100 / contentLength));\r
174         }\r
175         \r
176         final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r
177         downloadStatus.setText(status);\r
178       }\r
179     });\r
180   }\r
181 \r
182 }\r