@Override
  public boolean onChildClick(
      ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    final IndexItem e =
        (IndexItem)
            ((DownloadIndexAdapter) getListAdapter()).getChild(groupPosition, childPosition);
    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 true;
    }

    final DownloadEntry entry = e.createDownloadEntry(DownloadIndexActivity.this);
    if (entry != null) {
      // if(!fileToUnzip.exists()){
      // builder.setMessage(MessageFormat.format(getString(R.string.download_question), baseName,
      // extractDateAndSize(e.getValue())));
      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());
    }
    return true;
  }
 @Override
 protected FilterResults performFiltering(CharSequence constraint) {
   FilterResults results = new FilterResults();
   if (constraint == null || constraint.length() == 0) {
     results.values = indexFiles.values();
     results.count = indexFiles.size();
   } else {
     String[] vars = constraint.toString().split("\\s");
     for (int i = 0; i < vars.length; i++) {
       vars[i] = vars[i].trim().toLowerCase();
     }
     List<IndexItem> filter = new ArrayList<IndexItem>();
     for (IndexItem item : indexFiles.values()) {
       boolean add = true;
       for (String var : vars) {
         if (var.length() > 0) {
           if (!item.getVisibleName().toLowerCase().contains(var)
               && !item.getDescription().toLowerCase().contains(var)) {
             add = false;
           }
         }
       }
       if (add) {
         filter.add(item);
       }
     }
     results.values = filter;
     results.count = filter.size();
   }
   return results;
 }
 public void setIndexFiles(List<IndexItem> indexFiles) {
   this.indexFiles.clear();
   for (IndexItem i : indexFiles) {
     this.indexFiles.put(i.getFileName(), i);
   }
   List<IndexItemCategory> cats = categorizeIndexItems(indexFiles);
   synchronized (this) {
     list.clear();
     list.addAll(cats);
   }
   notifyDataSetChanged();
 }
示例#4
0
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   if (item.getItemId() == RELOAD_ID) {
     // re-create the thread
     downloadListIndexThread = new DownloadIndexListThread();
     downloadIndexList();
   } else {
     final DownloadIndexAdapter listAdapter = (DownloadIndexAdapter) getListAdapter();
     if (item.getItemId() == SELECT_ALL_ID) {
       int selected = 0;
       for (int i = 0; i < listAdapter.getCount(); i++) {
         IndexItem es = listAdapter.getItem(i);
         if (!entriesToDownload.containsKey(es.getFileName())) {
           selected++;
           entriesToDownload.put(es.getFileName(), createDownloadEntry(es));
         }
       }
       Toast.makeText(
               this,
               MessageFormat.format(getString(R.string.items_were_selected), selected),
               Toast.LENGTH_SHORT)
           .show();
       listAdapter.notifyDataSetInvalidated();
       if (selected > 0) {
         findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE);
       }
     } else if (item.getItemId() == FILTER_EXISTING_REGIONS) {
       final Collection<String> listAlreadyDownloaded = listAlreadyDownloadedWithAlternatives();
       final List<IndexItem> filtered = new ArrayList<IndexItem>();
       for (String file : listAlreadyDownloaded) {
         IndexItem fileItem = listAdapter.getIndexFiles().get(file);
         if (fileItem != null) {
           filtered.add(fileItem);
         }
       }
       listAdapter.clear();
       for (IndexItem fileItem : filtered) {
         listAdapter.add(fileItem);
       }
     } else if (item.getItemId() == DESELECT_ALL_ID) {
       entriesToDownload.clear();
       listAdapter.notifyDataSetInvalidated();
       findViewById(R.id.DownloadButton).setVisibility(View.GONE);
     } else {
       return false;
     }
   }
   return true;
 }
示例#5
0
    @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;
    }
  public List<IndexItemCategory> categorizeIndexItems(Collection<IndexItem> indexItems) {
    final Map<String, IndexItemCategory> cats =
        new TreeMap<String, DownloadIndexActivity.IndexItemCategory>();
    for (IndexItem i : indexItems) {
      int nameId = R.string.index_name_other;
      int order = 0;
      String lc = i.getFileName().toLowerCase();
      if (lc.endsWith(".voice.zip")) {
        nameId = R.string.index_name_voice;
        order = 1;
      } else if (lc.contains(".ttsvoice.zip")) {
        nameId = R.string.index_name_tts_voice;
        order = 2;
      } else if (lc.startsWith("us")) {
        nameId = R.string.index_name_us;
        order = 31;
      } else if (lc.contains("_northamerica_")) {
        nameId = R.string.index_name_north_america;
        order = 30;
      } else if (lc.contains("_centralamerica_") || lc.contains("central-america")) {
        nameId = R.string.index_name_central_america;
        order = 40;
      } else if (lc.contains("_southamerica_") || lc.contains("south-america")) {
        nameId = R.string.index_name_south_america;
        order = 45;
      } else if (lc.startsWith("france_")) {
        nameId = R.string.index_name_france;
        order = 17;
      } else if (lc.startsWith("germany_")) {
        nameId = R.string.index_name_germany;
        order = 16;
      } else if (lc.contains("_europe_")) {
        nameId = R.string.index_name_europe;
        order = 15;
      } else if (lc.startsWith("russia_")) {
        nameId = R.string.index_name_russia;
        order = 18;
      } else if (lc.contains("africa")) {
        nameId = R.string.index_name_africa;
        order = 80;
      } else if (lc.contains("_asia_")) {
        nameId = R.string.index_name_asia;
        order = 50;
      } else if (lc.contains("_oceania_") || lc.contains("australia")) {
        nameId = R.string.index_name_oceania;
        order = 70;
      } else if (lc.contains("_wiki_")) {
        nameId = R.string.index_name_wiki;
        order = 10;
      }

      String name = getString(nameId);
      if (!cats.containsKey(name)) {
        cats.put(name, new IndexItemCategory(name, order));
      }
      cats.get(name).items.add(i);
    }
    ArrayList<IndexItemCategory> r =
        new ArrayList<DownloadIndexActivity.IndexItemCategory>(cats.values());
    Collections.sort(r);
    return r;
  }
    @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;
    }
示例#8
0
  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;
  }
示例#9
0
  @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());
      }
    }
  }