Exemplo n.º 1
0
 private Category makeCategory(FileInfo file, int type) {
   Category category = new Category();
   switch (type) {
     case TYPE_IMAGE:
       category.filePath = file.filePath;
       category.alias = ((ImageFile) file).bucketName;
       break;
     case TYPE_AUDIO:
       category.alias = ((AudioFile) file).album;
       category.filePath = ((AudioFile) file).album;
       break;
     case TYPE_VIDEO:
       category.filePath = file.filePath;
       break;
   }
   category.uri = category.filePath;
   category.init();
   category.addFile(file);
   return category;
 }
Exemplo 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);
    }
  }
Exemplo n.º 3
0
  public synchronized void initImage() {
    if (null == mImageCategory) {
      mImageCategory = new ConcurrentHashMap<String, Category>();
    }
    mImageCategory.clear();

    String volumeName = "external";
    Uri uri = Images.Media.getContentUri(volumeName);
    String[] columns =
        new String[] {
          BaseColumns._ID,
          MediaColumns.DATA,
          MediaColumns.DISPLAY_NAME,
          MediaColumns.SIZE,
          MediaColumns.DATE_ADDED,
          MediaColumns.DATE_MODIFIED,
          ImageColumns.BUCKET_DISPLAY_NAME,
          MediaColumns.MIME_TYPE
        };
    Cursor cur = null;
    try {
      cur =
          mContext
              .getContentResolver()
              .query(uri, columns, buildSelectionByCategory(TYPE_IMAGE), null, null);
      if (cur == null) {
        return;
      }

      if (!cur.moveToFirst()) {
        return;
      }

      do {
        ImageFile file = ImageFile.getInfo(cur);
        if (null == file) {
          continue;
        }
        // 取一张图片的路径,add by yangbing
        //				if (MediaOpenSettingConstants.sImagePath == null) {
        //					if (file.fullFilePath != null && !"".equals(file.fullFilePath)) {
        //						MediaOpenSettingConstants.sImagePath = file.fullFilePath;
        //					}
        //				}
        if (mImageCategory.containsKey(file.filePath)) {
          Category category = mImageCategory.get(file.filePath);
          category.addFile(file);
        } else {
          Category category = new Category();
          category.filePath = file.filePath;
          category.uri = category.filePath;
          category.alias = file.bucketName;
          category.init();
          category.addFile(file);
          mImageCategory.put(file.filePath, category);
        }
      } while (cur.moveToNext());
    } catch (SQLiteException e) {
      Log.e("FileEngine", "init image error");
    } finally {
      if (cur != null) {
        cur.close();
      }
    }
  }
Exemplo n.º 4
0
  public synchronized void initVideo() {
    if (null == mVideoCategory) {
      mVideoCategory = new ConcurrentHashMap<String, Category>();
    }
    mVideoCategory.clear();

    String volumeName = "external";
    Uri uri = Video.Media.getContentUri(volumeName);

    String[] columns =
        new String[] {
          BaseColumns._ID,
          MediaColumns.DATA,
          MediaColumns.DISPLAY_NAME,
          MediaColumns.SIZE,
          MediaColumns.DATE_ADDED,
          MediaColumns.DATE_MODIFIED,
          VideoColumns.DURATION,
          MediaColumns.MIME_TYPE
        };
    Cursor cur = null;
    try {
      cur =
          mContext
              .getContentResolver()
              .query(uri, columns, buildSelectionByCategory(TYPE_AUDIO), null, null);

      if (cur == null) {
        return;
      }

      if (!cur.moveToFirst()) {
        return;
      }

      do {
        VideoFile file = VideoFile.getInfo(cur);
        if (null == file) {
          continue;
        }

        if (mVideoCategory.containsKey(file.filePath)) {
          Category category = mVideoCategory.get(file.filePath);
          category.addFile(file);
        } else {
          Category category = new Category();
          category.filePath = file.filePath;
          category.uri = category.filePath;
          category.init();
          category.addFile(file);
          mVideoCategory.put(file.filePath, category);
        }
      } while (cur.moveToNext());
    } catch (SQLiteException e) {
      Log.e("FileEngine", "init video error");
    } finally {
      if (cur != null) {
        cur.close();
      }
    }
  }
Exemplo n.º 5
0
  public synchronized void initAudio() {
    if (null == mAudioCategory) {
      mAudioCategory = new ConcurrentHashMap<String, Category>();
    }
    mAudioCategory.clear();
    Uri uri = Audio.Media.EXTERNAL_CONTENT_URI;

    String[] columns =
        new String[] {
          BaseColumns._ID,
          MediaColumns.DATA,
          MediaColumns.DISPLAY_NAME,
          MediaColumns.SIZE,
          MediaColumns.DATE_ADDED,
          MediaColumns.DATE_MODIFIED,
          AudioColumns.DURATION,
          AudioColumns.ARTIST,
          AudioColumns.ALBUM,
          AudioColumns.ALBUM_ID,
          MediaColumns.MIME_TYPE,
          MediaColumns.TITLE
        };

    Cursor cur = null;
    try {
      cur =
          mContext
              .getContentResolver()
              .query(uri, columns, buildSelectionByCategory(TYPE_AUDIO), null, null);
      if (cur == null) {
        return;
      }

      if (!cur.moveToFirst()) {
        return;
      }

      do {
        AudioFile file = AudioFile.getInfo(cur);
        if (null == file) {
          continue;
        }
        // 取一首歌曲的路径,add by yangbing
        //				if (MediaOpenSettingConstants.sMusicPath == null) {
        //					if (file.fullFilePath != null && !"".equals(file.fullFilePath)) {
        //						MediaOpenSettingConstants.sMusicPath = file.fullFilePath;
        //					}
        //				}
        if (mAudioCategory.containsKey(file.album)) {
          Category category = mAudioCategory.get(file.album);
          category.addFile(file);
        } else {
          Category category = new Category();
          category.filePath = file.album;
          category.uri = category.filePath;
          category.alias = file.album;
          category.addFile(file);
          mAudioCategory.put(category.filePath, category);
        }
      } while (cur.moveToNext());
    } catch (SQLiteException e) {
      Log.e("FileEngine", "init audio error");
    } finally {
      if (cur != null) {
        cur.close();
      }
    }
  }