+
+ final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ final String action = intent.getAction();
+
+ if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
+ final long downloadId = intent.getLongExtra(
+ DownloadManager.EXTRA_DOWNLOAD_ID, 0);
+ final DownloadManager.Query query = new DownloadManager.Query();
+ query.setFilterById(downloadId);
+ final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+ final Cursor cursor = downloadManager.query(query);
+
+ if (!cursor.moveToFirst()) {
+ Log.e(LOG, "Couldn't find download.");
+ return;
+ }
+
+ final String dest = cursor
+ .getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
+ final int status = cursor
+ .getInt(cursor
+ .getColumnIndex(DownloadManager.COLUMN_STATUS));
+ if (DownloadManager.STATUS_SUCCESSFUL != status) {
+ Log.w(LOG,
+ "Download failed: status=" + status +
+ ", reason=" + cursor.getString(cursor
+ .getColumnIndex(DownloadManager.COLUMN_REASON)));
+ Toast.makeText(context, getString(R.string.downloadFailed, dest),
+ Toast.LENGTH_LONG).show();
+ return;
+ }
+
+ Log.w(LOG, "Download finished: " + dest);
+ final File localZipFile = new File(Uri.parse(dest).getPath());
+
+ try {
+ ZipFile zipFile = new ZipFile(localZipFile);
+ final ZipEntry zipEntry = zipFile.entries().nextElement();
+ Log.d(LOG, "Unzipping entry: " + zipEntry.getName());
+ final InputStream zipIn = zipFile.getInputStream(zipEntry);
+ final OutputStream zipOut = new FileOutputStream(
+ new File(application.getDictDir(), zipEntry.getName()));
+ copyStream(zipIn, zipOut);
+ zipFile.close();
+ application.backgroundUpdateDictionaries(dictionaryUpdater);
+ Toast.makeText(context, getString(R.string.downloadFinished, dest),
+ Toast.LENGTH_LONG).show();
+ } catch (Exception e) {
+ Toast.makeText(context, getString(R.string.downloadFailed, dest),
+ Toast.LENGTH_LONG).show();
+ Log.e(LOG, "Failed to unzip.", e);
+ } finally {
+ localZipFile.delete();
+ }
+ }
+ }
+ };