]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DownloadActivity.java
go
[Dictionary.git] / src / com / hughes / android / dictionary / DownloadActivity.java
index 9ad663ae3b84f5be31eae1117306369ba91668dc..a7834021c1bd6b098731568dfe11f56008bdbf93 100755 (executable)
@@ -4,11 +4,14 @@ import java.io.File;
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
+import java.io.OutputStream;\r
 import java.net.URL;\r
 import java.net.URLConnection;\r
 import java.util.concurrent.Executor;\r
 import java.util.concurrent.Executors;\r
 import java.util.concurrent.atomic.AtomicBoolean;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipFile;\r
 \r
 import android.app.Activity;\r
 import android.content.Intent;\r
@@ -25,7 +28,7 @@ public class DownloadActivity extends Activity {
 \r
   String source;\r
   String dest;\r
-  long bytesDownloaded = 0;\r
+  long bytesProcessed = 0;\r
   long contentLength = -1;\r
 \r
   private final Executor downloadExecutor = Executors.newSingleThreadExecutor();\r
@@ -36,6 +39,8 @@ public class DownloadActivity extends Activity {
   /** Called when the activity is first created. */\r
   @Override\r
   public void onCreate(final Bundle savedInstanceState) {\r
+    ((DictionaryApplication)getApplication()).applyTheme(this);\r
+\r
     super.onCreate(savedInstanceState);\r
     final Intent intent = getIntent();\r
     source = intent.getStringExtra(SOURCE);\r
@@ -54,57 +59,75 @@ public class DownloadActivity extends Activity {
     final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
     progressBar.setIndeterminate(false);\r
     progressBar.setMax(100);\r
-\r
-    final InputStream in;\r
-    final FileOutputStream out;\r
-\r
-    final File destFile = new File(dest);\r
-    final File destTmpFile;\r
-    try {\r
-      destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
-          .getParentFile());\r
-      destTmpFile.deleteOnExit();\r
-      final URL uri = new URL(source);\r
-      final URLConnection connection = uri.openConnection();\r
-      contentLength = connection.getContentLength();\r
-      in = connection.getInputStream();\r
-      out = new FileOutputStream(destTmpFile);\r
-    } catch (Exception e) {\r
-      Log.e("THAD", "Error downloading file", e);\r
-      setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
-      return;\r
-    }\r
+    \r
+    bytesProcessed = 0;\r
+    contentLength = 100;\r
+    setDownloadStatus(getString(R.string.openingConnection));\r
 \r
     final Runnable runnable = new Runnable() {\r
       public void run() {\r
+\r
         try {\r
-          bytesDownloaded = 0;\r
-          int bytesRead;\r
-          final byte[] bytes = new byte[1024 * 8];\r
-          int count = 0;\r
-          while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
-            out.write(bytes, 0, bytesRead);\r
-            bytesDownloaded += bytesRead;\r
-            if (count++ % 20 == 0) {\r
-              setDownloadStatus(getString(R.string.downloading,\r
-                  bytesDownloaded, contentLength));\r
-            }\r
-          }\r
-          in.close();\r
-          out.close();\r
+          final File destFile = new File(dest);\r
+          destFile.getParentFile().mkdirs();\r
+\r
+          final File destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile\r
+              .getParentFile());\r
+          destTmpFile.deleteOnExit();\r
+\r
+          final URL uri = new URL(source);\r
+          final URLConnection connection = uri.openConnection();\r
+          contentLength = connection.getContentLength();\r
+          final InputStream in = connection.getInputStream();\r
+          final FileOutputStream out = new FileOutputStream(destTmpFile); \r
+          int bytesRead = copyStream(in, out, R.string.downloading);\r
+          \r
           if (bytesRead == -1 && !stop.get()) {\r
             destFile.delete();\r
             destTmpFile.renameTo(destFile);\r
           } else {\r
            Log.d("THAD", "Stopped downloading file.");\r
           }\r
+          \r
+          if (dest.toLowerCase().endsWith(".zip")) {\r
+            final ZipFile zipFile = new ZipFile(destFile);\r
+            final File destUnzipped = new File(dest.substring(0, dest.length() - 4));\r
+            final ZipEntry zipEntry = zipFile.getEntry(destUnzipped.getName());\r
+            if (zipEntry != null) {\r
+              destUnzipped.delete();\r
+              Log.d("THAD", "Unzipping entry: " + zipEntry.getName() + " to " + destUnzipped);\r
+              final InputStream zipIn = zipFile.getInputStream(zipEntry);\r
+              final OutputStream zipOut = new FileOutputStream(destUnzipped);\r
+              contentLength = zipEntry.getSize();\r
+              bytesRead = copyStream(zipIn, zipOut, R.string.unzipping);\r
+            }\r
+          }\r
+          \r
           setDownloadStatus(String.format(getString(R.string.downloadFinished),\r
-              bytesDownloaded));\r
+              bytesProcessed));\r
         } catch (IOException e) {\r
           Log.e("THAD", "Error downloading file", e);\r
           setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
         }\r
       }\r
+\r
+      private int copyStream(final InputStream in, final OutputStream out, final int messageId)\r
+          throws IOException {\r
+        bytesProcessed = 0;\r
+        int bytesRead;\r
+        final byte[] bytes = new byte[1024 * 16];\r
+        int count = 0;\r
+        while ((bytesRead = in.read(bytes)) != -1 && !stop.get()) {\r
+          out.write(bytes, 0, bytesRead);\r
+          bytesProcessed += bytesRead;\r
+          if (count++ % 20 == 0) {\r
+            setDownloadStatus(getString(messageId, bytesProcessed, contentLength));\r
+          }\r
+        }\r
+        in.close();\r
+        out.close();\r
+        return bytesRead;\r
+      }\r
     };\r
 \r
     downloadExecutor.execute(runnable);\r
@@ -121,7 +144,7 @@ public class DownloadActivity extends Activity {
       public void run() {\r
         final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
         if (contentLength > 0) {\r
-          progressBar.setProgress((int) (bytesDownloaded * 100 / contentLength));\r
+          progressBar.setProgress((int) (bytesProcessed * 100 / contentLength));\r
         }\r
         \r
         final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r