]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DownloadActivity.java
b580c4d6b5827eb12a9605ecdcdc91aa51f34085
[Dictionary.git] / src / com / hughes / android / dictionary / DownloadActivity.java
1 package com.hughes.android.dictionary;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileOutputStream;\r
5 import java.io.IOException;\r
6 import java.io.InputStream;\r
7 import java.net.URL;\r
8 import java.util.concurrent.Executor;\r
9 import java.util.concurrent.Executors;\r
10 \r
11 import android.app.Activity;\r
12 import android.content.Intent;\r
13 import android.os.Bundle;\r
14 import android.os.Handler;\r
15 import android.util.Log;\r
16 import android.widget.ProgressBar;\r
17 import android.widget.TextView;\r
18 \r
19 public class DownloadActivity extends Activity {\r
20 \r
21   public static final String SOURCE = "source";\r
22   public static final String DEST = "dest";\r
23 \r
24   String source;\r
25   String dest;\r
26 \r
27   private final Executor downloadExecutor = Executors.newSingleThreadExecutor();\r
28   private final Handler uiHandler = new Handler();\r
29 \r
30   /** Called when the activity is first created. */\r
31   @Override\r
32   public void onCreate(final Bundle savedInstanceState) {\r
33     super.onCreate(savedInstanceState);\r
34     final Intent intent = getIntent();\r
35     source = intent.getStringExtra(SOURCE);\r
36     dest = intent.getStringExtra(DEST);\r
37     if (source == null || dest == null) {\r
38       throw new RuntimeException("null source or dest.");\r
39     }\r
40     setContentView(R.layout.download);\r
41 \r
42     final TextView sourceTextView = (TextView) findViewById(R.id.source);\r
43     sourceTextView.setText(source);\r
44 \r
45     final TextView destTextView = (TextView) findViewById(R.id.dest);\r
46     destTextView.setText(dest);\r
47 \r
48     final ProgressBar progressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);\r
49     progressBar.setIndeterminate(false);\r
50     progressBar.setMax(100);\r
51 \r
52     final InputStream in;\r
53     final FileOutputStream out;\r
54     \r
55     final File destFile = new File(dest);\r
56     final File destTmpFile;\r
57     try {\r
58       destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile.getParentFile());\r
59       final URL uri = new URL(source);\r
60       in = uri.openStream();\r
61       out = new FileOutputStream(destTmpFile);\r
62     } catch (Exception e) {\r
63       Log.e("THAD", "Error downloading file", e);\r
64       setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage());\r
65       return;\r
66     }\r
67 \r
68     final Runnable runnable = new Runnable() {\r
69       public void run() {\r
70         try {\r
71           long byteCount = 0;\r
72           int bytesRead;\r
73           final byte[] bytes = new byte[4096];\r
74           while ((bytesRead = in.read(bytes)) != -1) {\r
75             out.write(bytes, 0, bytesRead);\r
76             byteCount += bytesRead;\r
77             setDownloadStatus(String.format("Downloading: %d bytes so far", byteCount));\r
78           }\r
79           in.close();\r
80           out.close();\r
81           destFile.delete();\r
82           destTmpFile.renameTo(destFile);\r
83           setDownloadStatus(String.format("Downloaded finished: %d bytes", byteCount));\r
84         } catch (IOException e) {\r
85           Log.e("THAD", "Error downloading file", e);\r
86           setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage());\r
87         }\r
88       }\r
89     };\r
90 \r
91     downloadExecutor.execute(runnable);\r
92   }\r
93 \r
94   private void setDownloadStatus(final String status) {\r
95     final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus);\r
96     uiHandler.post(new Runnable() {\r
97       public void run() {\r
98         downloadStatus.setText(status);\r
99       }\r
100     });\r
101   }\r
102 \r
103 }\r