]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DownloadActivity.java
Long-press on lang button shows list.
[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   public static final String MESSAGE = "message";\r
43 \r
44   String source;\r
45   String dest;\r
46   String message;\r
47   long bytesProcessed = 0;\r
48   long contentLength = -1;\r
49 \r
50   private final Executor downloadExecutor = Executors.newSingleThreadExecutor();\r
51   private final Handler uiHandler = new Handler();\r
52 \r
53   final AtomicBoolean stop = new AtomicBoolean(false);\r
54   \r
55   public static Intent getLaunchIntent(final String source, final String dest, final String message) {\r
56     final Intent intent = new Intent();\r
57     intent.setClassName(DownloadActivity.class.getPackage().getName(), DownloadActivity.class.getName());\r
58     intent.putExtra(SOURCE, source);\r
59     intent.putExtra(DEST, dest);\r
60     intent.putExtra(MESSAGE, message);\r
61     return intent;\r
62   }\r
63 \r
64   /** Called when the activity is first created. */\r
65   @Override\r
66   public void onCreate(final Bundle savedInstanceState) {\r
67     //((DictionaryApplication)getApplication()).applyTheme(this);\r
68 \r
69     super.onCreate(savedInstanceState);\r
70     final Intent intent = getIntent();\r
71     source = intent.getStringExtra(SOURCE);\r
72     dest = intent.getStringExtra(DEST);\r
73     message = intent.getStringExtra(MESSAGE);\r
74     if (source == null || dest == null) {\r
75       throw new RuntimeException("null source or dest.");\r
76     }\r
77     setContentView(R.layout.download_activity);\r
78 \r
79     final TextView sourceTextView = (TextView) findViewById(R.id.source);\r
80     sourceTextView.setText(source);\r
81 \r
82     final TextView destTextView = (TextView) findViewById(R.id.dest);\r
83     destTextView.setText(dest);\r
84 \r
85     final TextView messageTextView = (TextView) findViewById(R.id.downloadMessage);\r
86     messageTextView.setText(message);\r
87 \r
88     final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
89     progressBar.setIndeterminate(false);\r
90     progressBar.setMax(100);\r
91     \r
92     bytesProcessed = 0;\r
93     contentLength = 100;\r
94     setDownloadStatus(getString(R.string.openingConnection));\r
95 \r
96     final Runnable runnable = new Runnable() {\r
97       public void run() {\r
98 \r
99         try {\r
100           final File destFile = new File(dest);\r
101           destFile.getParentFile().mkdirs();\r
102 \r
103           final File destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
104               .getParentFile());\r
105           destTmpFile.deleteOnExit();\r
106 \r
107           final URL uri = new URL(source);\r
108           final URLConnection connection = uri.openConnection();\r
109           contentLength = connection.getContentLength();\r
110           final InputStream in = connection.getInputStream();\r
111           if (in == null) {\r
112             throw new IOException("Unable to open InputStream from source: " + source);\r
113           }\r
114           final FileOutputStream out = new FileOutputStream(destTmpFile); \r
115           int bytesRead = copyStream(in, out, R.string.downloading);\r
116           \r
117           if (bytesRead == -1 && !stop.get()) {\r
118             destFile.delete();\r
119             destTmpFile.renameTo(destFile);\r
120           } else {\r
121            Log.d("THAD", "Stopped downloading file.");\r
122           }\r
123           \r
124           if (dest.toLowerCase().endsWith(".zip")) {\r
125             final ZipFile zipFile = new ZipFile(destFile);\r
126             final File destUnzipped = new File(dest.substring(0, dest.length() - 4));\r
127             final ZipEntry zipEntry = zipFile.getEntry(destUnzipped.getName());\r
128             if (zipEntry != null) {\r
129               destUnzipped.delete();\r
130               Log.d("THAD", "Unzipping entry: " + zipEntry.getName() + " to " + destUnzipped);\r
131               final InputStream zipIn = zipFile.getInputStream(zipEntry);\r
132               final OutputStream zipOut = new FileOutputStream(destUnzipped);\r
133               contentLength = zipEntry.getSize();\r
134               bytesRead = copyStream(zipIn, zipOut, R.string.unzipping);\r
135               destFile.delete();\r
136             }\r
137           }\r
138           \r
139           setDownloadStatus(String.format(getString(R.string.downloadFinished),\r
140               bytesProcessed));\r
141           \r
142           // If all went well, we can exit this activity.\r
143           uiHandler.post(new Runnable() {\r
144             @Override\r
145             public void run() {\r
146               finish();\r
147             }\r
148           });\r
149           \r
150         } catch (IOException e) {\r
151           Log.e("THAD", "Error downloading file", e);\r
152           setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
153         }\r
154       }\r
155 \r
156       private int copyStream(final InputStream in, final OutputStream out, final int messageId)\r
157           throws IOException {\r
158         bytesProcessed = 0;\r
159         int bytesRead;\r
160         final byte[] bytes = new byte[1024 * 16];\r
161         int count = 0;\r
162         while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
163           out.write(bytes, 0, bytesRead);\r
164           bytesProcessed += bytesRead;\r
165           if (count++ % 20 == 0) {\r
166             setDownloadStatus(getString(messageId, bytesProcessed, contentLength));\r
167           }\r
168         }\r
169         in.close();\r
170         out.close();\r
171         return bytesRead;\r
172       }\r
173     };\r
174 \r
175     downloadExecutor.execute(runnable);\r
176   }\r
177 \r
178   @Override\r
179   protected void onStop() {\r
180     stop.set(true);\r
181     super.onStop();\r
182   }\r
183 \r
184   private void setDownloadStatus(final String status) {\r
185     uiHandler.post(new Runnable() {\r
186       public void run() {\r
187         final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
188         if (contentLength > 0) {\r
189           progressBar.setProgress((int) (bytesProcessed * 100 / contentLength));\r
190         }\r
191         \r
192         final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r
193         downloadStatus.setText(status);\r
194       }\r
195     });\r
196   }\r
197 \r
198 }\r