]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DownloadActivity.java
Major refactor of down dictionary list is stored by app.
[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 dictFile, 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 ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
86     progressBar.setIndeterminate(false);\r
87     progressBar.setMax(100);\r
88     \r
89     bytesProcessed = 0;\r
90     contentLength = 100;\r
91     setDownloadStatus(getString(R.string.openingConnection));\r
92 \r
93     final Runnable runnable = new Runnable() {\r
94       public void run() {\r
95 \r
96         try {\r
97           final File destFile = new File(dest);\r
98           destFile.getParentFile().mkdirs();\r
99 \r
100           final File destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
101               .getParentFile());\r
102           destTmpFile.deleteOnExit();\r
103 \r
104           final URL uri = new URL(source);\r
105           final URLConnection connection = uri.openConnection();\r
106           contentLength = connection.getContentLength();\r
107           final InputStream in = connection.getInputStream();\r
108           if (in == null) {\r
109             throw new IOException("Unable to open InputStream from source: " + source);\r
110           }\r
111           final FileOutputStream out = new FileOutputStream(destTmpFile); \r
112           int bytesRead = copyStream(in, out, R.string.downloading);\r
113           \r
114           if (bytesRead == -1 && !stop.get()) {\r
115             destFile.delete();\r
116             destTmpFile.renameTo(destFile);\r
117           } else {\r
118            Log.d("THAD", "Stopped downloading file.");\r
119           }\r
120           \r
121           if (dest.toLowerCase().endsWith(".zip")) {\r
122             final ZipFile zipFile = new ZipFile(destFile);\r
123             final File destUnzipped = new File(dest.substring(0, dest.length() - 4));\r
124             final ZipEntry zipEntry = zipFile.getEntry(destUnzipped.getName());\r
125             if (zipEntry != null) {\r
126               destUnzipped.delete();\r
127               Log.d("THAD", "Unzipping entry: " + zipEntry.getName() + " to " + destUnzipped);\r
128               final InputStream zipIn = zipFile.getInputStream(zipEntry);\r
129               final OutputStream zipOut = new FileOutputStream(destUnzipped);\r
130               contentLength = zipEntry.getSize();\r
131               bytesRead = copyStream(zipIn, zipOut, R.string.unzipping);\r
132             }\r
133           }\r
134           \r
135           setDownloadStatus(String.format(getString(R.string.downloadFinished),\r
136               bytesProcessed));\r
137           \r
138           // If all went well, we can exit this activity.\r
139           uiHandler.post(new Runnable() {\r
140             @Override\r
141             public void run() {\r
142               finish();\r
143             }\r
144           });\r
145           \r
146         } catch (IOException e) {\r
147           Log.e("THAD", "Error downloading file", e);\r
148           setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
149         }\r
150       }\r
151 \r
152       private int copyStream(final InputStream in, final OutputStream out, final int messageId)\r
153           throws IOException {\r
154         bytesProcessed = 0;\r
155         int bytesRead;\r
156         final byte[] bytes = new byte[1024 * 16];\r
157         int count = 0;\r
158         while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
159           out.write(bytes, 0, bytesRead);\r
160           bytesProcessed += bytesRead;\r
161           if (count++ % 20 == 0) {\r
162             setDownloadStatus(getString(messageId, bytesProcessed, contentLength));\r
163           }\r
164         }\r
165         in.close();\r
166         out.close();\r
167         return bytesRead;\r
168       }\r
169     };\r
170 \r
171     downloadExecutor.execute(runnable);\r
172   }\r
173 \r
174   @Override\r
175   protected void onStop() {\r
176     stop.set(true);\r
177     super.onStop();\r
178   }\r
179 \r
180   private void setDownloadStatus(final String status) {\r
181     uiHandler.post(new Runnable() {\r
182       public void run() {\r
183         final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
184         if (contentLength > 0) {\r
185           progressBar.setProgress((int) (bytesProcessed * 100 / contentLength));\r
186         }\r
187         \r
188         final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r
189         downloadStatus.setText(status);\r
190       }\r
191     });\r
192   }\r
193 \r
194 }\r