@Override public View getView(final int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater inflater = getLayoutInflater(); v = inflater.inflate(net.osmand.plus.R.layout.download_index_list_item, parent, false); } final View row = v; TextView item = (TextView) row.findViewById(R.id.download_item); TextView description = (TextView) row.findViewById(R.id.download_descr); IndexItem e = getItem(position); item.setText( e.getVisibleDescription(DownloadIndexActivity.this) + "\n" + e.getVisibleName()); // $NON-NLS-1$ description.setText(e.getDate() + "\n" + e.getSize() + " MB"); CheckBox ch = (CheckBox) row.findViewById(R.id.check_download_item); ch.setChecked(entriesToDownload.containsKey(e.getFileName())); ch.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { final CheckBox ch = (CheckBox) v.findViewById(R.id.check_download_item); ch.setChecked(!ch.isChecked()); DownloadIndexActivity.this.onListItemClick( getListView(), row, position, getItemId(position)); } }); if (indexFileNames != null) { String sfName = convertServerFileNameToLocal(e.getFileName()); if (!indexFileNames.containsKey(sfName)) { item.setTextColor(Color.WHITE); } else { if (e.getDate() != null) { if (e.getDate().equals(indexFileNames.get(sfName))) { item.setTextColor(Color.GREEN); } else { item.setTextColor(Color.BLUE); } } else { item.setTextColor(Color.GREEN); } } } return row; }
@Override public View getChildView( final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater inflater = getLayoutInflater(); v = inflater.inflate(net.osmand.plus.R.layout.download_index_list_item, parent, false); } final View row = v; TextView item = (TextView) row.findViewById(R.id.download_item); TextView description = (TextView) row.findViewById(R.id.download_descr); IndexItem e = (IndexItem) getChild(groupPosition, childPosition); item.setText( (e.getVisibleDescription(DownloadIndexActivity.this) + "\n" + e.getVisibleName()) .trim()); //$NON-NLS-1$ description.setText(e.getDate() + "\n" + e.getSize() + " MB"); CheckBox ch = (CheckBox) row.findViewById(R.id.check_download_item); ch.setChecked(entriesToDownload.containsKey(e.getFileName())); ch.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { final CheckBox ch = (CheckBox) v.findViewById(R.id.check_download_item); ch.setChecked(!ch.isChecked()); DownloadIndexActivity.this.onChildClick( getListView(), row, groupPosition, childPosition, getChildId(groupPosition, childPosition)); } }); if (indexFileNames != null) { String sfName = convertServerFileNameToLocal(e.getFileName()); if (!indexFileNames.containsKey(sfName)) { item.setTextColor(getResources().getColor(R.color.index_unknown)); } else { if (e.getDate() != null) { if (e.getDate().equals(indexActivatedFileNames.get(sfName))) { item.setText( item.getText() + "\n" + getResources().getString(R.string.local_index_installed) + " : " + indexActivatedFileNames.get(sfName)); item.setTextColor(getResources().getColor(R.color.act_index_uptodate)); // GREEN } else if (e.getDate().equals(indexFileNames.get(sfName))) { item.setText( item.getText() + "\n" + getResources().getString(R.string.local_index_installed) + " : " + indexFileNames.get(sfName)); item.setTextColor( getResources().getColor(R.color.deact_index_uptodate)); // DARK_GREEN } else if (indexActivatedFileNames.containsKey(sfName)) { item.setText( item.getText() + "\n" + getResources().getString(R.string.local_index_installed) + " : " + indexActivatedFileNames.get(sfName)); item.setTextColor( getResources().getColor(R.color.act_index_updateable)); // LIGHT_BLUE } else { item.setText( item.getText() + "\n" + getResources().getString(R.string.local_index_installed) + " : " + indexFileNames.get(sfName)); item.setTextColor( getResources().getColor(R.color.deact_index_updateable)); // DARK_BLUE } } else { item.setTextColor(getResources().getColor(R.color.act_index_uptodate)); } } } return row; }
private DownloadEntry createDownloadEntry(IndexItem item) { String fileName = item.getFileName(); File parent = null; String toSavePostfix = null; String toCheckPostfix = null; boolean unzipDir = false; File externalStorageDirectory = OsmandSettings.getOsmandSettings(getApplicationContext()).getExternalStorageDirectory(); if (fileName.endsWith(IndexConstants.POI_INDEX_EXT)) { parent = new File(externalStorageDirectory, ResourceManager.POI_PATH); toSavePostfix = POI_INDEX_EXT; toCheckPostfix = POI_INDEX_EXT; } else if (fileName.endsWith(IndexConstants.POI_INDEX_EXT_ZIP)) { parent = new File(externalStorageDirectory, ResourceManager.POI_PATH); toSavePostfix = POI_INDEX_EXT_ZIP; toCheckPostfix = POI_INDEX_EXT; } else if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { parent = new File(externalStorageDirectory, ResourceManager.APP_DIR); toSavePostfix = BINARY_MAP_INDEX_EXT; toCheckPostfix = BINARY_MAP_INDEX_EXT; } else if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { parent = new File(externalStorageDirectory, ResourceManager.APP_DIR); toSavePostfix = BINARY_MAP_INDEX_EXT_ZIP; toCheckPostfix = BINARY_MAP_INDEX_EXT; } else if (fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP)) { parent = new File(externalStorageDirectory, ResourceManager.VOICE_PATH); toSavePostfix = VOICE_INDEX_EXT_ZIP; toCheckPostfix = ""; // $NON-NLS-1$ unzipDir = true; } else if (fileName.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_ZIP)) { parent = new File(externalStorageDirectory, ResourceManager.VOICE_PATH); toSavePostfix = TTSVOICE_INDEX_EXT_ZIP; toCheckPostfix = ""; // $NON-NLS-1$ unzipDir = true; } if (parent != null) { parent.mkdirs(); } final DownloadEntry entry; if (parent == null || !parent.exists()) { Toast.makeText( DownloadIndexActivity.this, getString(R.string.sd_dir_not_accessible), Toast.LENGTH_LONG) .show(); entry = null; } else { entry = new DownloadEntry(); int ls = fileName.lastIndexOf('_'); entry.baseName = fileName.substring(0, ls); entry.fileToSave = new File(parent, entry.baseName + toSavePostfix); entry.unzip = unzipDir; SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy"); // $NON-NLS-1$ try { Date d = format.parse(item.getDate()); entry.dateModified = d.getTime(); } catch (ParseException e1) { } try { entry.sizeMB = Double.parseDouble(item.getSize()); } catch (NumberFormatException e1) { } entry.parts = 1; if (item.getParts() != null) { entry.parts = Integer.parseInt(item.getParts()); } entry.fileToUnzip = new File(parent, entry.baseName + toCheckPostfix); } return entry; }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); final IndexItem e = ((DownloadIndexAdapter) getListAdapter()).getItem(position); String key = e.getFileName(); final CheckBox ch = (CheckBox) v.findViewById(R.id.check_download_item); if (ch.isChecked()) { ch.setChecked(!ch.isChecked()); entriesToDownload.remove(key); if (entriesToDownload.isEmpty()) { int x = getListView().getScrollX(); int y = getListView().getScrollY(); findViewById(R.id.DownloadButton).setVisibility(View.GONE); getListView().scrollTo(x, y); } return; } final DownloadEntry entry = createDownloadEntry(e); if (entry != null) { // if(!fileToUnzip.exists()){ // builder.setMessage(MessageFormat.format(getString(R.string.download_question), baseName, // extractDateAndSize(e.getValue()))); if (entry.fileToUnzip.exists()) { Builder builder = new AlertDialog.Builder(this); MessageFormat format; if (entry.fileToUnzip.isDirectory()) { format = new MessageFormat("{0,date,dd.MM.yyyy}", Locale.US); // $NON-NLS-1$ } else { format = new MessageFormat( "{0,date,dd.MM.yyyy}, {1, number,##.#} MB", Locale.US); // $NON-NLS-1$ } String description = format.format( new Object[] { new Date(entry.fileToUnzip.lastModified()), ((float) entry.fileToUnzip.length() / MB) }); String descriptionEx = e.getDate() + ", " + e.getSize() + " MB"; builder.setMessage( MessageFormat.format( getString(R.string.download_question_exist), entry.baseName, description, descriptionEx)); builder.setPositiveButton( R.string.default_buttons_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { entriesToDownload.put(e.getFileName(), entry); int x = getListView().getScrollX(); int y = getListView().getScrollY(); findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE); getListView().scrollTo(x, y); ch.setChecked(!ch.isChecked()); } }); builder.setNegativeButton(R.string.default_buttons_no, null); builder.show(); } else { entriesToDownload.put(e.getFileName(), entry); int x = getListView().getScrollX(); int y = getListView().getScrollY(); findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE); getListView().scrollTo(x, y); ch.setChecked(!ch.isChecked()); } } }