+ public Row getItem(int position) {
+ if (position == 0) {
+ return new Row(null, true);
+ }
+ position -= 1;
+
+ if (position < dictionariesOnDevice.size()) {
+ return new Row(dictionariesOnDevice.get(position), true);
+ }
+ position -= dictionariesOnDevice.size();
+
+ if (position == 0) {
+ return new Row(null, false);
+ }
+ position -= 1;
+
+ assert position < downloadableDictionaries.size();
+ return new Row(downloadableDictionaries.get(position), false);
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return position;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ if (convertView instanceof LinearLayout &&
+ convertView != dictionariesOnDeviceHeaderRow &&
+ convertView != downloadableDictionariesHeaderRow) {
+ /*
+ * This is done to try to avoid leaking memory that used to
+ * happen on Android 4.0.3
+ */
+ ((LinearLayout) convertView).removeAllViews();
+ }
+
+ final Row row = getItem(position);
+
+ if (row.onDevice) {
+ if (row.dictionaryInfo == null) {
+ return dictionariesOnDeviceHeaderRow;
+ }
+ return createDictionaryRow(row.dictionaryInfo, parent, true);
+ }
+
+ if (row.dictionaryInfo == null) {
+ return downloadableDictionariesHeaderRow;
+ }
+ return createDictionaryRow(row.dictionaryInfo, parent, false);
+ }
+
+ }
+
+ private void setMyListAdapater() {
+ final String filter = filterSearchView == null ? "" : filterSearchView.getQuery()
+ .toString();
+ final String[] filters = filter.trim().toLowerCase().split("(\\s|-)+");
+ setListAdapter(new MyListAdapter(filters));
+ }
+
+ private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
+ final ViewGroup parent, boolean canLaunch) {
+
+ View row = LayoutInflater.from(parent.getContext()).inflate(
+ R.layout.dictionary_manager_row, parent, false);
+ final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
+ final TextView details = (TextView) row.findViewById(R.id.dictionaryDetails);
+ name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
+
+ final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
+ final Button downloadButton = (Button) row.findViewById(R.id.downloadButton);
+ final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
+ boolean broken = false;
+ if (!dictionaryInfo.isValid()) {
+ broken = true;
+ canLaunch = false;
+ }
+ if (downloadable != null && (!canLaunch || updateAvailable)) {
+ downloadButton
+ .setText(getString(
+ R.string.downloadButton,
+ downloadable.zipBytes / 1024.0 / 1024.0));
+ downloadButton.setMinWidth(application.languageButtonPixels * 3 / 2);
+ downloadButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View arg0) {
+ downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
+ }
+ });
+ } else {
+ downloadButton.setVisibility(View.INVISIBLE);
+ }
+
+ LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
+ final List<IndexInfo> sortedIndexInfos = application
+ .sortedIndexInfos(dictionaryInfo.indexInfos);
+ final StringBuilder builder = new StringBuilder();
+ if (updateAvailable) {
+ builder.append(getString(R.string.updateAvailable));
+ }
+ for (IndexInfo indexInfo : sortedIndexInfos) {
+ final View button = application.createButton(buttons.getContext(), dictionaryInfo,
+ indexInfo);
+ buttons.addView(button);
+
+ if (canLaunch) {
+ button.setOnClickListener(
+ new IntentLauncher(buttons.getContext(),
+ DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ indexInfo.shortName, "")));
+
+ } else {
+ button.setEnabled(false);
+ }
+ if (builder.length() != 0) {
+ builder.append("; ");
+ }
+ builder.append(getString(R.string.indexInfo, indexInfo.shortName,
+ indexInfo.mainTokenCount));
+ }
+ if (downloadable != null || dictionaryInfo != null) {
+ builder.append("; ");
+ builder.append(getString(R.string.downloadButton, (dictionaryInfo != null ? dictionaryInfo.uncompressedBytes : downloadable.uncompressedBytes) / 1024.0 / 1024.0));
+ }
+ if (broken) {
+ name.setText("Broken: " + application.getDictionaryName(dictionaryInfo.uncompressedFilename));
+ builder.append("; Cannot be used, redownload, check hardware/file system");
+ // Allow deleting, but cannot open
+ row.setLongClickable(true);
+ }
+ details.setText(builder.toString());
+
+ if (canLaunch) {
+ row.setClickable(true);
+ row.setOnClickListener(new IntentLauncher(parent.getContext(),
+ DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ dictionaryInfo.indexInfos.get(0).shortName, "")));
+ row.setFocusable(true);
+ row.setLongClickable(true);
+ }
+ row.setBackgroundResource(android.R.drawable.menuitem_background);
+
+ return row;
+ }
+
+ private void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
+ DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+ final DownloadManager.Query query = new DownloadManager.Query();
+ query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
+ final Cursor cursor = downloadManager.query(query);
+
+ // Due to a bug, cursor is null instead of empty when
+ // the download manager is disabled.
+ if (cursor == null) {
+ new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error))
+ .setMessage(getString(R.string.downloadFailed, R.string.downloadManagerQueryFailed))
+ .setNeutralButton("Close", null).show();
+ return;
+ }
+
+ while (cursor.moveToNext()) {
+ if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
+ break;
+ }
+ if (!cursor.isAfterLast()) {
+ downloadManager.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)));
+ downloadButton
+ .setText(getString(
+ R.string.downloadButton,
+ bytes / 1024.0 / 1024.0));
+ cursor.close();
+ return;
+ }
+ cursor.close();
+ Request request = new Request(
+ Uri.parse(downloadUrl));
+ try {
+ final String destFile = new File(new URL(downloadUrl).getFile())
+ .getName();
+ Log.d(LOG, "Downloading to: " + destFile);
+
+ try {
+ request.setDestinationInExternalFilesDir(getApplicationContext(), null, destFile);
+ } catch (IllegalStateException e) {
+ request.setDestinationUri(Uri.fromFile(new File(Environment
+ .getExternalStorageDirectory(), destFile)));
+ }
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);