+ public int getItemViewType(int position) {
+ final Row row = getItem(position);
+ if (row.dictionaryInfo == null) {
+ return row.onDevice ? 0 : 1;
+ }
+ assert row.dictionaryInfo.indexInfos.size() <= 2;
+ return 2;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ if (convertView == dictionariesOnDeviceHeaderRow ||
+ convertView == downloadableDictionariesHeaderRow) {
+ return convertView;
+ }
+
+ final Row row = getItem(position);
+
+ if (row.dictionaryInfo == null) {
+ assert convertView == null;
+ return row.onDevice ? dictionariesOnDeviceHeaderRow : downloadableDictionariesHeaderRow;
+ }
+ return createDictionaryRow(row.dictionaryInfo, parent, convertView, row.onDevice);
+ }
+
+ }
+
+ 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, View row, boolean canLaunch) {
+
+ if (row == null) {
+ 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);
+ }
+ });
+ downloadButton.setVisibility(View.VISIBLE);
+ } else {
+ downloadButton.setVisibility(View.GONE);
+ }
+
+ 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));
+ }
+ assert buttons.getChildCount() == 4;
+ for (int i = 0; i < 2; i++) {
+ final Button textButton = (Button)buttons.getChildAt(2*i);
+ final ImageButton imageButton = (ImageButton)buttons.getChildAt(2*i + 1);
+ if (i >= sortedIndexInfos.size()) {
+ textButton.setVisibility(View.GONE);
+ imageButton.setVisibility(View.GONE);
+ continue;
+ }
+ final IndexInfo indexInfo = sortedIndexInfos.get(i);
+ final View button = IsoUtils.INSTANCE.setupButton(textButton, imageButton, dictionaryInfo,
+ indexInfo, application.languageButtonPixels);
+
+ if (canLaunch) {
+ button.setOnClickListener(
+ new IntentLauncher(buttons.getContext(),
+ DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ indexInfo.shortName, "")));
+
+ }
+ button.setEnabled(canLaunch);
+ button.setFocusable(canLaunch);
+ if (builder.length() != 0) {
+ builder.append("; ");
+ }
+ builder.append(getString(R.string.indexInfo, indexInfo.shortName,
+ indexInfo.mainTokenCount));
+ }
+ builder.append("; ");
+ builder.append(getString(R.string.downloadButton, dictionaryInfo.uncompressedBytes / 1024.0 / 1024.0));
+ if (broken) {
+ name.setText("Broken: " + application.getDictionaryName(dictionaryInfo.uncompressedFilename));
+ builder.append("; Cannot be used, redownload, check hardware/file system");
+ }
+ details.setText(builder.toString());
+
+ if (canLaunch) {
+ row.setOnClickListener(new IntentLauncher(parent.getContext(),
+ DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ dictionaryInfo.indexInfos.get(0).shortName, "")));
+ // do not setFocusable, for keyboard navigation
+ // offering only the index buttons is better.
+ }
+ row.setClickable(canLaunch);
+ // Allow deleting, even if we cannot open
+ row.setLongClickable(broken || canLaunch);
+ row.setBackgroundResource(android.R.drawable.menuitem_background);
+
+ return row;
+ }
+
+ private synchronized void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
+ String destFile;
+ try {
+ destFile = new File(new URL(downloadUrl).getPath()).getName();
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Invalid download URL!", e);
+ }
+ 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) {
+ String msg = getString(R.string.downloadManagerQueryFailed);
+ new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error))
+ .setMessage(getString(R.string.downloadFailed, msg))
+ .setNeutralButton("Close", null).show();
+ return;
+ }
+
+ while (cursor.moveToNext()) {
+ if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
+ break;
+ if (destFile.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))))
+ 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));
+
+ Log.d(LOG, "Downloading to: " + destFile);
+ request.setTitle(destFile);
+
+ File destFilePath = new File(application.getDictDir(), destFile);
+ destFilePath.delete();
+ try {
+ request.setDestinationUri(Uri.fromFile(destFilePath));
+ } catch (Exception e) {
+ }
+
+ try {
+ downloadManager.enqueue(request);
+ } catch (SecurityException e) {
+ request = new Request(Uri.parse(downloadUrl));
+ request.setTitle(destFile);
+ downloadManager.enqueue(request);