+ 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 == null || !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) {
+ final int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
+ Log.w(LOG,
+ "Download failed: status=" + status +
+ ", reason=" + reason);
+ String msg = Integer.toString(reason);
+ switch (reason) {
+ case DownloadManager.ERROR_FILE_ALREADY_EXISTS: msg = "File exists"; break;
+ case DownloadManager.ERROR_FILE_ERROR: msg = "File error"; break;
+ case DownloadManager.ERROR_INSUFFICIENT_SPACE: msg = "Not enough space"; break;
+ }
+ new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(getString(R.string.downloadFailed, reason)).setNeutralButton("Close", null).show();
+ return;
+ }
+
+ Log.w(LOG, "Download finished: " + dest);
+ Toast.makeText(context, getString(R.string.unzippingDictionary, dest),
+ Toast.LENGTH_LONG).show();
+
+
+ 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);
+ File targetFile = new File(application.getDictDir(), zipEntry.getName());
+ if (targetFile.exists()) {
+ targetFile.renameTo(new File(targetFile.getAbsolutePath().replace(".quickdic", ".bak.quickdic")));
+ targetFile = new File(application.getDictDir(), zipEntry.getName());
+ }
+ final OutputStream zipOut = new FileOutputStream(targetFile);
+ copyStream(zipIn, zipOut);
+ zipFile.close();
+ application.backgroundUpdateDictionaries(dictionaryUpdater);
+ Toast.makeText(context, getString(R.string.installationFinished, dest),
+ Toast.LENGTH_LONG).show();
+ } catch (Exception e) {
+ String msg = getString(R.string.unzippingFailed, dest);
+ File dir = application.getDictDir();
+ if (!dir.canWrite() || !application.checkFileCreate(dir)) {
+ msg = getString(R.string.notWritable, dir.getAbsolutePath());
+ }
+ new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(msg).setNeutralButton("Close", null).show();
+ Log.e(LOG, "Failed to unzip.", e);
+ } finally {
+ localZipFile.delete();
+ }
+ }
+ }
+ };
+
+ public static Intent getLaunchIntent(Context c) {
+ final Intent intent = new Intent(c, DictionaryManagerActivity.class);