1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary;
17 import android.Manifest;
18 import android.app.AlertDialog;
19 import android.app.DownloadManager;
20 import android.app.DownloadManager.Request;
21 import android.content.BroadcastReceiver;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.SharedPreferences;
27 import android.content.SharedPreferences.Editor;
28 import android.content.pm.PackageManager;
29 import android.database.Cursor;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.os.Environment;
33 import android.os.Handler;
34 import android.preference.PreferenceManager;
35 import android.provider.Settings;
36 import android.support.v4.app.ActivityCompat;
37 import android.support.v4.content.ContextCompat;
38 import android.support.v4.view.MenuItemCompat;
39 import android.support.v7.app.ActionBar;
40 import android.support.v7.app.ActionBarActivity;
41 import android.support.v7.widget.SearchView;
42 import android.support.v7.widget.SearchView.OnQueryTextListener;
43 import android.support.v7.widget.Toolbar;
44 import android.text.InputType;
45 import android.util.Log;
46 import android.util.TypedValue;
47 import android.view.ContextMenu;
48 import android.view.ContextMenu.ContextMenuInfo;
49 import android.view.LayoutInflater;
50 import android.view.Menu;
51 import android.view.MenuItem;
52 import android.view.View;
53 import android.view.View.OnClickListener;
54 import android.view.ViewGroup;
55 import android.view.inputmethod.EditorInfo;
56 import android.view.inputmethod.InputMethodManager;
57 import android.widget.AdapterView.AdapterContextMenuInfo;
58 import android.widget.BaseAdapter;
59 import android.widget.Button;
60 import android.widget.CompoundButton;
61 import android.widget.CompoundButton.OnCheckedChangeListener;
62 import android.widget.FrameLayout;
63 import android.widget.LinearLayout;
64 import android.widget.ListAdapter;
65 import android.widget.ListView;
66 import android.widget.TextView;
67 import android.widget.Toast;
68 import android.widget.ToggleButton;
70 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
71 import com.hughes.android.util.IntentLauncher;
73 import java.io.BufferedInputStream;
75 import java.io.FileInputStream;
76 import java.io.FileOutputStream;
77 import java.io.IOException;
78 import java.io.InputStream;
79 import java.io.OutputStream;
80 import java.net.MalformedURLException;
82 import java.nio.ByteBuffer;
83 import java.nio.channels.FileChannel;
84 import java.util.Collections;
85 import java.util.HashSet;
86 import java.util.List;
88 import java.util.zip.ZipEntry;
89 import java.util.zip.ZipFile;
90 import java.util.zip.ZipInputStream;
93 // Delete, move to top.
95 public class DictionaryManagerActivity extends ActionBarActivity {
97 static final String LOG = "QuickDic";
98 static boolean blockAutoLaunch = false;
100 private ListView listView;
101 private ListView getListView() {
102 if (listView == null) {
103 listView = (ListView)findViewById(android.R.id.list);
107 private void setListAdapter(ListAdapter adapter) {
108 getListView().setAdapter(adapter);
110 private ListAdapter getListAdapter() {
111 return getListView().getAdapter();
114 // For DownloadManager bug workaround
115 private Set<Long> finishedDownloadIds = new HashSet<Long>();
117 DictionaryApplication application;
119 SearchView filterSearchView;
120 ToggleButton showDownloadable;
122 LinearLayout dictionariesOnDeviceHeaderRow;
123 LinearLayout downloadableDictionariesHeaderRow;
127 Runnable dictionaryUpdater = new Runnable() {
130 if (uiHandler == null) {
133 uiHandler.post(new Runnable() {
142 final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
144 public synchronized void onReceive(Context context, Intent intent) {
145 final String action = intent.getAction();
147 if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
148 final long downloadId = intent.getLongExtra(
149 DownloadManager.EXTRA_DOWNLOAD_ID, 0);
150 if (finishedDownloadIds.contains(downloadId)) return; // ignore double notifications
151 final DownloadManager.Query query = new DownloadManager.Query();
152 query.setFilterById(downloadId);
153 final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
154 final Cursor cursor = downloadManager.query(query);
156 if (cursor == null || !cursor.moveToFirst()) {
157 Log.e(LOG, "Couldn't find download.");
161 final String dest = cursor
162 .getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
163 final int status = cursor
165 .getColumnIndex(DownloadManager.COLUMN_STATUS));
166 if (DownloadManager.STATUS_SUCCESSFUL != status) {
167 final int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
169 "Download failed: status=" + status +
170 ", reason=" + reason);
171 String msg = Integer.toString(reason);
173 case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
176 case DownloadManager.ERROR_FILE_ERROR:
179 case DownloadManager.ERROR_INSUFFICIENT_SPACE:
180 msg = "Not enough space";
183 new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(getString(R.string.downloadFailed, msg)).setNeutralButton("Close", null).show();
187 Log.w(LOG, "Download finished: " + dest + " Id: " + downloadId);
188 Toast.makeText(context, getString(R.string.unzippingDictionary, dest),
189 Toast.LENGTH_LONG).show();
192 final Uri zipUri = Uri.parse(dest);
193 File localZipFile = null;
194 InputStream zipFileStream = null;
195 ZipInputStream zipFile = null;
196 FileOutputStream zipOut = null;
198 if (zipUri.getScheme().equals("content")) {
199 zipFileStream = context.getContentResolver().openInputStream(zipUri);
202 localZipFile = new File(zipUri.getPath());
203 zipFileStream = new FileInputStream(localZipFile);
205 zipFile = new ZipInputStream(new BufferedInputStream(zipFileStream));
206 final ZipEntry zipEntry = zipFile.getNextEntry();
207 Log.d(LOG, "Unzipping entry: " + zipEntry.getName());
208 File targetFile = new File(application.getDictDir(), zipEntry.getName());
209 if (targetFile.exists()) {
210 targetFile.renameTo(new File(targetFile.getAbsolutePath().replace(".quickdic", ".bak.quickdic")));
211 targetFile = new File(application.getDictDir(), zipEntry.getName());
213 zipOut = new FileOutputStream(targetFile);
214 copyStream(zipFile, zipOut);
215 application.backgroundUpdateDictionaries(dictionaryUpdater);
216 Toast.makeText(context, getString(R.string.installationFinished, dest),
217 Toast.LENGTH_LONG).show();
218 finishedDownloadIds.add(downloadId);
219 Log.w(LOG, "Unzipping finished: " + dest + " Id: " + downloadId);
220 } catch (Exception e) {
221 String msg = getString(R.string.unzippingFailed, dest);
222 File dir = application.getDictDir();
223 if (!dir.canWrite() || !application.checkFileCreate(dir)) {
224 msg = getString(R.string.notWritable, dir.getAbsolutePath());
226 new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(msg).setNeutralButton("Close", null).show();
227 Log.e(LOG, "Failed to unzip.", e);
230 if (zipOut != null) zipOut.close();
231 } catch (IOException e) {}
233 if (zipFile != null) zipFile.close();
234 } catch (IOException e) {}
236 if (zipFileStream != null) zipFileStream.close();
237 } catch (IOException e) {}
238 if (localZipFile != null) localZipFile.delete();
244 public static Intent getLaunchIntent(Context c) {
245 final Intent intent = new Intent(c, DictionaryManagerActivity.class);
246 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
250 public void readableCheckAndError(boolean requestPermission) {
251 final File dictDir = application.getDictDir();
252 if (dictDir.canRead() && dictDir.canExecute()) return;
253 blockAutoLaunch = true;
254 if (requestPermission &&
255 ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
256 ActivityCompat.requestPermissions(this,
257 new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,
258 Manifest.permission.WRITE_EXTERNAL_STORAGE
262 blockAutoLaunch = true;
264 AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
265 builder.setTitle(getString(R.string.error));
266 builder.setMessage(getString(
267 R.string.unableToReadDictionaryDir,
268 dictDir.getAbsolutePath(),
269 Environment.getExternalStorageDirectory()));
270 builder.setNeutralButton("Close", null);
271 builder.create().show();
275 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
276 readableCheckAndError(false);
278 application.backgroundUpdateDictionaries(dictionaryUpdater);
284 public void onCreate(Bundle savedInstanceState) {
285 DictionaryApplication.INSTANCE.init(getApplicationContext());
286 application = DictionaryApplication.INSTANCE;
287 // This must be first, otherwise the action bar doesn't get
289 setTheme(application.getSelectedTheme().themeId);
291 super.onCreate(savedInstanceState);
292 Log.d(LOG, "onCreate:" + this);
294 setTheme(application.getSelectedTheme().themeId);
296 blockAutoLaunch = false;
299 setContentView(R.layout.dictionary_manager_activity);
301 dictionariesOnDeviceHeaderRow = (LinearLayout) LayoutInflater.from(
302 getListView().getContext()).inflate(
303 R.layout.dictionary_manager_header_row_on_device, getListView(), false);
305 downloadableDictionariesHeaderRow = (LinearLayout) LayoutInflater.from(
306 getListView().getContext()).inflate(
307 R.layout.dictionary_manager_header_row_downloadable, getListView(), false);
309 showDownloadable = (ToggleButton) downloadableDictionariesHeaderRow
310 .findViewById(R.id.hideDownloadable);
311 showDownloadable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
313 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
314 onShowDownloadableChanged();
318 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
319 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
320 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(
321 thanksForUpdatingLatestVersion)) {
322 blockAutoLaunch = true;
323 startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent(getApplicationContext()));
324 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion)
328 registerReceiver(broadcastReceiver, new IntentFilter(
329 DownloadManager.ACTION_DOWNLOAD_COMPLETE));
332 registerForContextMenu(getListView());
333 getListView().setItemsCanFocus(true);
335 readableCheckAndError(true);
337 onCreateSetupActionBar();
340 private void onCreateSetupActionBar() {
341 ActionBar actionBar = getSupportActionBar();
342 actionBar.setDisplayShowTitleEnabled(false);
343 actionBar.setDisplayShowHomeEnabled(false);
344 actionBar.setDisplayHomeAsUpEnabled(false);
346 filterSearchView = new SearchView(getSupportActionBar().getThemedContext());
347 filterSearchView.setIconifiedByDefault(false);
348 // filterSearchView.setIconified(false); // puts the magnifying glass in
351 filterSearchView.setQueryHint(getString(R.string.searchText));
352 filterSearchView.setSubmitButtonEnabled(false);
353 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
354 FrameLayout.LayoutParams.WRAP_CONTENT);
355 filterSearchView.setLayoutParams(lp);
356 filterSearchView.setInputType(InputType.TYPE_CLASS_TEXT);
357 filterSearchView.setImeOptions(
358 EditorInfo.IME_ACTION_DONE |
359 EditorInfo.IME_FLAG_NO_EXTRACT_UI |
360 // EditorInfo.IME_FLAG_NO_FULLSCREEN | // Requires API
362 EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
364 filterSearchView.setOnQueryTextListener(new OnQueryTextListener() {
366 public boolean onQueryTextSubmit(String query) {
367 filterSearchView.clearFocus();
372 public boolean onQueryTextChange(String filterText) {
377 filterSearchView.setFocusable(true);
379 actionBar.setCustomView(filterSearchView);
380 actionBar.setDisplayShowCustomEnabled(true);
382 // Avoid wasting space on large left inset
383 Toolbar tb = (Toolbar)filterSearchView.getParent();
384 tb.setContentInsetsRelative(0, 0);
388 public void onDestroy() {
390 unregisterReceiver(broadcastReceiver);
393 private static void copyStream(final InputStream ins, final FileOutputStream outs)
395 ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 64);
396 FileChannel out = outs.getChannel();
399 final byte[] bytes = new byte[1024 * 64];
401 bytesRead = ins.read(bytes, pos, bytes.length - pos);
402 if (bytesRead != -1) pos += bytesRead;
403 if (bytesRead == -1 ? pos != 0 : 2*pos >= bytes.length) {
404 buf.put(bytes, 0, pos);
407 while (buf.hasRemaining()) out.write(buf);
410 } while (bytesRead != -1);
414 protected void onStart() {
416 uiHandler = new Handler();
420 protected void onStop() {
426 protected void onResume() {
429 if (PreferenceActivity.prefsMightHaveChanged) {
430 PreferenceActivity.prefsMightHaveChanged = false;
432 startActivity(getIntent());
435 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
436 showDownloadable.setChecked(prefs.getBoolean(C.SHOW_DOWNLOADABLE, true));
438 if (!blockAutoLaunch &&
439 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
440 prefs.contains(C.DICT_FILE) &&
441 prefs.contains(C.INDEX_SHORT_NAME)) {
442 Log.d(LOG, "Skipping DictionaryManager, going straight to dictionary.");
443 startActivity(DictionaryActivity.getLaunchIntent(getApplicationContext(),
444 new File(prefs.getString(C.DICT_FILE, "")),
445 prefs.getString(C.INDEX_SHORT_NAME, ""),
446 prefs.getString(C.SEARCH_TOKEN, "")));
451 // Remove the active dictionary from the prefs so we won't autolaunch
453 final Editor editor = prefs.edit();
454 editor.remove(C.DICT_FILE);
455 editor.remove(C.INDEX_SHORT_NAME);
456 editor.remove(C.SEARCH_TOKEN);
459 application.backgroundUpdateDictionaries(dictionaryUpdater);
465 public boolean onCreateOptionsMenu(final Menu menu) {
466 if ("true".equals(Settings.System.getString(getContentResolver(), "firebase.test.lab")))
468 return false; // testing the menu is not very interesting
470 final MenuItem sort = menu.add(getString(R.string.sortDicts));
471 MenuItemCompat.setShowAsAction(sort, MenuItem.SHOW_AS_ACTION_NEVER);
472 sort.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
473 public boolean onMenuItemClick(final MenuItem menuItem) {
474 application.sortDictionaries();
480 application.onCreateGlobalOptionsMenu(this, menu);
485 public void onCreateContextMenu(final ContextMenu menu, final View view,
486 final ContextMenuInfo menuInfo) {
487 super.onCreateContextMenu(menu, view, menuInfo);
488 Log.d(LOG, "onCreateContextMenu, " + menuInfo);
490 final AdapterContextMenuInfo adapterContextMenuInfo =
491 (AdapterContextMenuInfo) menuInfo;
492 final int position = adapterContextMenuInfo.position;
493 final MyListAdapter.Row row = (MyListAdapter.Row) getListAdapter().getItem(position);
495 if (row.dictionaryInfo == null) {
499 if (position > 0 && row.onDevice) {
500 final android.view.MenuItem moveToTopMenuItem =
501 menu.add(R.string.moveToTop);
502 moveToTopMenuItem.setOnMenuItemClickListener(new
503 android.view.MenuItem.OnMenuItemClickListener() {
505 public boolean onMenuItemClick(android.view.MenuItem item) {
506 application.moveDictionaryToTop(row.dictionaryInfo);
514 final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
516 .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
518 public boolean onMenuItemClick(android.view.MenuItem item) {
519 application.deleteDictionary(row.dictionaryInfo);
527 private void onShowDownloadableChanged() {
529 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
530 prefs.putBoolean(C.SHOW_DOWNLOADABLE, showDownloadable.isChecked());
534 class MyListAdapter extends BaseAdapter {
536 List<DictionaryInfo> dictionariesOnDevice;
537 List<DictionaryInfo> downloadableDictionaries;
540 DictionaryInfo dictionaryInfo;
543 private Row(DictionaryInfo dictionaryInfo, boolean onDevice) {
544 this.dictionaryInfo = dictionaryInfo;
545 this.onDevice = onDevice;
549 private MyListAdapter(final String[] filters) {
550 dictionariesOnDevice = application.getDictionariesOnDevice(filters);
551 if (showDownloadable.isChecked()) {
552 downloadableDictionaries = application.getDownloadableDictionaries(filters);
554 downloadableDictionaries = Collections.emptyList();
559 public int getCount() {
560 return 2 + dictionariesOnDevice.size() + downloadableDictionaries.size();
564 public Row getItem(int position) {
566 return new Row(null, true);
570 if (position < dictionariesOnDevice.size()) {
571 return new Row(dictionariesOnDevice.get(position), true);
573 position -= dictionariesOnDevice.size();
576 return new Row(null, false);
580 assert position < downloadableDictionaries.size();
581 return new Row(downloadableDictionaries.get(position), false);
585 public long getItemId(int position) {
590 public View getView(int position, View convertView, ViewGroup parent) {
591 if (convertView instanceof LinearLayout &&
592 convertView != dictionariesOnDeviceHeaderRow &&
593 convertView != downloadableDictionariesHeaderRow) {
595 * This is done to try to avoid leaking memory that used to
596 * happen on Android 4.0.3
598 ((LinearLayout) convertView).removeAllViews();
601 final Row row = getItem(position);
604 if (row.dictionaryInfo == null) {
605 return dictionariesOnDeviceHeaderRow;
607 return createDictionaryRow(row.dictionaryInfo, parent, true);
610 if (row.dictionaryInfo == null) {
611 return downloadableDictionariesHeaderRow;
613 return createDictionaryRow(row.dictionaryInfo, parent, false);
618 private void setMyListAdapater() {
619 final String filter = filterSearchView == null ? "" : filterSearchView.getQuery()
621 final String[] filters = filter.trim().toLowerCase().split("(\\s|-)+");
622 setListAdapter(new MyListAdapter(filters));
625 private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
626 final ViewGroup parent, boolean canLaunch) {
628 View row = LayoutInflater.from(parent.getContext()).inflate(
629 R.layout.dictionary_manager_row, parent, false);
630 final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
631 final TextView details = (TextView) row.findViewById(R.id.dictionaryDetails);
632 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
634 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
635 final Button downloadButton = (Button) row.findViewById(R.id.downloadButton);
636 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
637 boolean broken = false;
638 if (!dictionaryInfo.isValid()) {
642 if (downloadable != null && (!canLaunch || updateAvailable)) {
645 R.string.downloadButton,
646 downloadable.zipBytes / 1024.0 / 1024.0));
647 downloadButton.setMinWidth(application.languageButtonPixels * 3 / 2);
648 downloadButton.setOnClickListener(new OnClickListener() {
650 public void onClick(View arg0) {
651 downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
655 downloadButton.setVisibility(View.INVISIBLE);
658 LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
659 final List<IndexInfo> sortedIndexInfos = application
660 .sortedIndexInfos(dictionaryInfo.indexInfos);
661 final StringBuilder builder = new StringBuilder();
662 if (updateAvailable) {
663 builder.append(getString(R.string.updateAvailable));
665 for (IndexInfo indexInfo : sortedIndexInfos) {
666 final View button = IsoUtils.INSTANCE.createButton(buttons.getContext(), dictionaryInfo,
667 indexInfo, application.languageButtonPixels);
668 buttons.addView(button);
671 button.setOnClickListener(
672 new IntentLauncher(buttons.getContext(),
673 DictionaryActivity.getLaunchIntent(getApplicationContext(),
674 application.getPath(dictionaryInfo.uncompressedFilename),
675 indexInfo.shortName, "")));
678 button.setEnabled(false);
679 button.setFocusable(false);
681 if (builder.length() != 0) {
682 builder.append("; ");
684 builder.append(getString(R.string.indexInfo, indexInfo.shortName,
685 indexInfo.mainTokenCount));
687 builder.append("; ");
688 builder.append(getString(R.string.downloadButton, dictionaryInfo.uncompressedBytes / 1024.0 / 1024.0));
690 name.setText("Broken: " + application.getDictionaryName(dictionaryInfo.uncompressedFilename));
691 builder.append("; Cannot be used, redownload, check hardware/file system");
692 // Allow deleting, but cannot open
693 row.setLongClickable(true);
695 details.setText(builder.toString());
698 row.setClickable(true);
699 row.setOnClickListener(new IntentLauncher(parent.getContext(),
700 DictionaryActivity.getLaunchIntent(getApplicationContext(),
701 application.getPath(dictionaryInfo.uncompressedFilename),
702 dictionaryInfo.indexInfos.get(0).shortName, "")));
703 // do not setFocusable, for keyboard navigation
704 // offering only the index buttons is better.
705 row.setLongClickable(true);
707 row.setBackgroundResource(android.R.drawable.menuitem_background);
712 private synchronized void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
715 destFile = new File(new URL(downloadUrl).getPath()).getName();
716 } catch (MalformedURLException e) {
717 throw new RuntimeException("Invalid download URL!", e);
719 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
720 final DownloadManager.Query query = new DownloadManager.Query();
721 query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
722 final Cursor cursor = downloadManager.query(query);
724 // Due to a bug, cursor is null instead of empty when
725 // the download manager is disabled.
726 if (cursor == null) {
727 String msg = getString(R.string.downloadManagerQueryFailed);
728 new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error))
729 .setMessage(getString(R.string.downloadFailed, msg))
730 .setNeutralButton("Close", null).show();
734 while (cursor.moveToNext()) {
735 if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
737 if (destFile.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))))
740 if (!cursor.isAfterLast()) {
741 downloadManager.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)));
744 R.string.downloadButton,
745 bytes / 1024.0 / 1024.0));
750 Request request = new Request(
751 Uri.parse(downloadUrl));
753 Log.d(LOG, "Downloading to: " + destFile);
754 request.setTitle(destFile);
756 File destFilePath = new File(application.getDictDir(), destFile);
757 destFilePath.delete();
759 request.setDestinationUri(Uri.fromFile(destFilePath));
760 } catch (Exception e) {
764 downloadManager.enqueue(request);
765 } catch (SecurityException e) {
766 request = new Request(Uri.parse(downloadUrl));
767 request.setTitle(destFile);
768 downloadManager.enqueue(request);
770 Log.w(LOG, "Download started: " + destFile);
771 downloadButton.setText("X");