+ 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, final 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);
+ if (!canLaunch || updateAvailable) {
+ final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
+ 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);
+ }
+ });
+ } 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(
+ 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));
+ }
+ details.setText(builder.toString());
+
+ if (canLaunch) {
+ row.setClickable(true);
+ row.setOnClickListener(new IntentLauncher(parent.getContext(),
+ DictionaryActivity.getLaunchIntent(
+ 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) {
+ DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+ Request request = new Request(
+ Uri.parse(downloadUrl));
+ try {
+ final String destFile = new File(new URL(downloadUrl).getFile())
+ .getName();
+ Log.d(LOG, "Downloading to: " + destFile);
+
+ request.setDestinationUri(Uri.fromFile(new File(Environment
+ .getExternalStorageDirectory(), destFile)));
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);