Download .sha256 file if it exists
app/src/main/java/eu/lepiller/nani/DictionaryDownloadActivity.java
| 149 | 149 | ||
| 150 | 150 | Log.d(TAG, "Start downloading"); | |
| 151 | 151 | ||
| 152 | - | for (Map.Entry<URL, File> e : d.getDownloads().entrySet()) { | |
| 152 | + | for (Map.Entry<String, File> e : d.getDownloads().entrySet()) { | |
| 153 | 153 | try { | |
| 154 | - | URL url = e.getKey(); | |
| 155 | - | Log.d(TAG, "URL: " + url.toString()); | |
| 154 | + | String uri = e.getKey(); | |
| 155 | + | Log.d(TAG, "URL: " + uri); | |
| 156 | + | ||
| 157 | + | // Download a .sha256 file if it exists | |
| 158 | + | URL url = new URL(uri + ".sha256"); | |
| 159 | + | byte data[] = new byte[4096]; | |
| 160 | + | int count; | |
| 161 | + | File file; | |
| 162 | + | ||
| 163 | + | connection = (HttpURLConnection) url.openConnection(); | |
| 164 | + | connection.connect(); | |
| 165 | + | if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) { | |
| 166 | + | input = connection.getInputStream(); | |
| 167 | + | file = new File(e.getValue() + ".sha256"); | |
| 168 | + | file.getParentFile().mkdirs(); | |
| 169 | + | output = new FileOutputStream(file); | |
| 170 | + | while((count = input.read(data)) != -1) { | |
| 171 | + | if (isCancelled()) { | |
| 172 | + | input.close(); | |
| 173 | + | return null; | |
| 174 | + | } | |
| 175 | + | output.write(data, 0, count); | |
| 176 | + | } | |
| 177 | + | } | |
| 178 | + | ||
| 179 | + | // .sha256 file now downloaded | |
| 180 | + | url = new URL(uri); | |
| 156 | 181 | connection = (HttpURLConnection) url.openConnection(); | |
| 157 | 182 | connection.connect(); | |
| 158 | 183 | ||
… | |||
| 171 | 196 | ||
| 172 | 197 | // download the file | |
| 173 | 198 | input = connection.getInputStream(); | |
| 174 | - | File file = e.getValue(); | |
| 175 | - | file.getParentFile().mkdirs(); | |
| 199 | + | file = e.getValue(); | |
| 176 | 200 | output = new FileOutputStream(file); | |
| 177 | 201 | ||
| 178 | - | byte data[] = new byte[4096]; | |
| 179 | 202 | long total = 0; | |
| 180 | - | int count; | |
| 181 | 203 | while ((count = input.read(data)) != -1) { | |
| 182 | 204 | // allow canceling with back button | |
| 183 | 205 | if (isCancelled()) { | |
app/src/main/java/eu/lepiller/nani/dictionary/Dictionary.java
| 42 | 42 | ||
| 43 | 43 | abstract public int size(); | |
| 44 | 44 | ||
| 45 | - | // Used for downloads: tells what siwe we currently have downloaded | |
| 45 | + | // Used for downloads: tells what size we currently have downloaded | |
| 46 | 46 | abstract int currentSize(); | |
| 47 | 47 | ||
| 48 | - | public abstract Map<URL, File> getDownloads(); | |
| 48 | + | public abstract Map<String, File> getDownloads(); | |
| 49 | 49 | ||
| 50 | 50 | public Drawable getDrawable(Context context) { | |
| 51 | 51 | Drawable drawable; |
app/src/main/java/eu/lepiller/nani/dictionary/FileDictionary.java
| 54 | 54 | } | |
| 55 | 55 | ||
| 56 | 56 | @Override | |
| 57 | - | public Map<URL, File> getDownloads() { | |
| 58 | - | HashMap<URL, File> result = new HashMap<>(); | |
| 59 | - | try { | |
| 60 | - | result.put(new URL(mUrl), getFile()); | |
| 61 | - | } catch (MalformedURLException e) { | |
| 62 | - | e.printStackTrace(); | |
| 63 | - | } | |
| 57 | + | public Map<String, File> getDownloads() { | |
| 58 | + | HashMap<String, File> result = new HashMap<>(); | |
| 59 | + | result.put(mUrl, getFile()); | |
| 64 | 60 | return result; | |
| 65 | 61 | } | |
| 66 | 62 |