Ejemplo n.º 1
0
  /**
   * 删除文件
   *
   * @param path
   * @param type
   */
  public void deleteFile(String path, int type) {
    File file = new File(path);
    boolean ret = false;
    if (file.exists()) {
      ret = file.delete();
    } else {
      ret = true;
    }

    if (ret) {
      Category category = null;
      String key = MediaFileUtil.getPathFromFilepath(path);
      if (type == TYPE_AUDIO) {
        category = mAudioCategory.get(key);
        mDeletedAudioList.add(path);
      }

      if (type == TYPE_IMAGE) {
        category = mImageCategory.get(key);
        mDeletedImageList.add(path);
      }

      if (type == TYPE_VIDEO) {
        category = mVideoCategory.get(key);
        mDeletedVideoList.add(path);
      }

      if (null != category) {
        category.deleteFile(path);
      }

      removeDataFromDB(path, type);
    }

    mFileObserver.deleteFinished(path, ret);
  }
Ejemplo n.º 2
0
  @SuppressWarnings("unchecked")
  private void compareFile(
      Cursor cur,
      ConcurrentHashMap<String, Category> categoryMap,
      ArrayList<String> deletedFileList,
      int type) {
    ArrayList<FileInfo> filesRemoved = new ArrayList<FileInfo>();
    ArrayList<FileInfo> filesAdded = new ArrayList<FileInfo>();

    // 把缓存中的拷贝一份到tmp中,方便比对
    HashMap<String, Category> tmp = new HashMap<String, Category>();

    Iterator<Category> values = categoryMap.values().iterator();
    while (values.hasNext()) {
      Category cat = values.next();
      Category newCat = new Category();

      newCat.filePath = cat.filePath;
      newCat.files = (ArrayList<FileInfo>) cat.files.clone();
      newCat.size = cat.size;

      tmp.put(cat.filePath, newCat);
    }
    HashSet<String> filesInDB = new HashSet<String>();
    do {
      FileInfo file = extractFileInfo(cur, type);
      if (null == file) {
        continue;
      }

      // 查找是否有这个文件夹
      if (tmp.containsKey(getCategoryKey(file, type))) {
        Category category = tmp.get(getCategoryKey(file, type));
        int size = category.files.size();
        boolean bFind = false;
        // 找到这个文件则从tmp中删除,说明原来已经有了
        for (int i = size - 1; i >= 0; --i) {
          FileInfo info = category.files.get(i);
          if (0 == info.fullFilePath.compareTo(file.fullFilePath)) {
            bFind = true;
            category.files.remove(i);
            break;
          }
        }
        if (!bFind) {
          // 没有找到则加入缓存
          if (!deletedFileList.contains(file.fullFilePath)) {
            if (categoryMap.get(getCategoryKey(file, type)).addFile(file)) {
              filesAdded.add(file);
              // Log.d("onChange", "add " + file.fullFilePath);
            }
          }
        }
      } else {
        if (categoryMap.containsKey(getCategoryKey(file, type))) {
          if (!deletedFileList.contains(file.fullFilePath)) {
            if (categoryMap.get(getCategoryKey(file, type)).addFile(file)) {
              filesAdded.add(file);
              // Log.d("onChange", "add " + file.fullFilePath);
            }
          }
        } else {
          if (!deletedFileList.contains(file.fullFilePath)) {
            // 没有这个文件夹则新建一个文件夹,加入缓存
            // Log.d("onChange", "add category " + file.filePath);
            // Log.d("onChange", "add " + file.fullFilePath);
            Category category = makeCategory(file, type);
            filesAdded.add(file);
            categoryMap.put(category.filePath, category);
          }
        }
      }
      filesInDB.add(file.fullFilePath);
    } while (cur.moveToNext());
    if (mFileObserver != null && filesAdded != null && !filesAdded.isEmpty()) {
      mFileObserver.filesAdded(filesAdded, type);
    }
    // tmp中剩下的则是被删除的文件
    Collection<Category> categories = tmp.values();
    Object[] ar = categories.toArray();
    int size = ar.length;
    for (int i = 0; i < size; ++i) {
      Object category = ar[i];
      filesRemoved.addAll(((Category) category).files);
    }

    // 把删除的文件从缓存中删除
    for (FileInfo info : filesRemoved) {
      Category category = categoryMap.get(getCategoryKey(info, type));
      if (null != category) {
        // Log.d("onChange", "delete file " + info.fullFilePath);
        category.deleteFile(info.fullFilePath);
        if (category.files.isEmpty()) {
          categoryMap.remove(category.filePath);
        }
      }
    }

    Iterator<String> it = deletedFileList.iterator();
    while (it.hasNext()) {
      if (!filesInDB.contains(it.next())) {
        it.remove();
      }
    }

    if (mFileObserver != null && filesRemoved != null && !filesRemoved.isEmpty()) {
      mFileObserver.filesRemoved(filesRemoved, type);
    }
  }