Android Open Source - Alfred4Android Index Loader






From Project

Back to project page Alfred4Android.

License

The source code is released under:

Apache License

If you think the Android project Alfred4Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.toraleap.collimator.data;
/* w ww .j a  v a  2 s. c o  m*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Stack;

import android.content.SharedPreferences;
import android.os.Environment;
import android.os.StatFs;

import com.toraleap.collimator.data.IndexData.DifferentVersionException;
import com.toraleap.collimator.util.FileInfo;
import com.toraleap.collimator.util.Unicode2Alpha;

/**
 * ???????????????????????????????????????
 * @author    uestc.Mobius <mobius@toraleap.com>
 * @version  2010.1025
 */
final class IndexLoader {
  
  // ????????
  private boolean isIndexHidden = false;
  private boolean isIndexSystem = false;
  private boolean isIndexDotPrefix = false;
  private boolean isIndexFirstLetter = true;
  private boolean isIndexAllType = false;

  private final HashSet<String> blackList = new HashSet<String>();
  
  public IndexLoader(SharedPreferences prefs) {
    isIndexHidden = prefs.getBoolean("index_hidden", false);
    isIndexSystem = prefs.getBoolean("index_system", false);
    isIndexDotPrefix = prefs.getBoolean("index_dotprefix", false);
    isIndexFirstLetter = prefs.getBoolean("index_firstletter", true);
    isIndexAllType = prefs.getBoolean("index_alltype", false);
    getBlacklist();
  }
  
  /**
   * ?????????????????????SD????????????????
   * @return ??????????????
   * @throws NoSDCardException ?????????SD??
   */
  public IndexData reload() throws NoSDCardException {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      throw new NoSDCardException();
    }
    // ???????
    Stack<String> stack = new Stack<String>();
    stack.push(Environment.getExternalStorageDirectory().getPath());
    // ??????????
    IndexData data = new IndexData();
    ArrayList<String> lName = new ArrayList<String>();
    ArrayList<String> lPath = new ArrayList<String>();
    ArrayList<String> lNameAlpha = new ArrayList<String>();
    ArrayList<String> lPathAlpha = new ArrayList<String>();
    ArrayList<Long> lSize = new ArrayList<Long>();
    ArrayList<Long> lTime = new ArrayList<Long>();
    // ??????????
    while (!stack.isEmpty()) {
      String parent = stack.pop();
      String parentAlpha = null;
      if (isIndexFirstLetter) {
        parentAlpha = Unicode2Alpha.toAlpha(parent);
      }
      File path = new File(parent);
      File[] files = path.listFiles();
      if (null == files) continue;
      for (File f : files)
      {
        if (f.isDirectory()) {
          if (isQualifiedDirectory(f)) stack.push(f.getPath());
        }
        else {
          if (isQualifiedFile(f)) {
            lName.add(f.getName());
            lPath.add(parent);
            lTime.add(f.lastModified());
            lSize.add(f.length());
            if (isIndexFirstLetter) {
              lNameAlpha.add(Unicode2Alpha.toAlpha(f.getName()));
              lPathAlpha.add(parentAlpha);
            } else {
              lNameAlpha.add("");
              lPathAlpha.add("");
            }
          }
        }
      }
    }
    int length = lName.size();
    data.indexTime = System.currentTimeMillis();
    data.availableSpace = getAvailableSpace();
    data.name = lName.toArray(new String[length]);
    data.path = lPath.toArray(new String[length]);
    data.nameAlpha = lNameAlpha.toArray(new String[length]);
    data.pathAlpha = lPathAlpha.toArray(new String[length]);
    data.size = new long[length];
    for (int i = 0; i < length; i++) data.size[i] = lSize.get(i).longValue();
    data.time = new long[length];
    for (int i = 0; i < length; i++) data.time[i] = lTime.get(i).longValue();
    return data;
    }
  
  /**
   * ?????????????
   * @param data  ???????????
   * @throws SerializingException ????????
   */
  public static void serialize(IndexData data) throws SerializingException {
    File f;
    DataOutputStream out = null;
    try {
      f = new File(Environment.getExternalStorageDirectory().getPath() + "/.collimator");
      if (!f.exists()) f.mkdirs();
      f = new File(Environment.getExternalStorageDirectory().getPath() + "/.collimator/index.dat");
      if (f.exists()) f.delete();
      out = new DataOutputStream(new FileOutputStream(f));
      data.write(out);
    } catch (Exception e) {
      e.printStackTrace();
      throw new SerializingException();
    } finally {
      if (out != null)
        try {
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
  }
  
  /**
   * ???????????????
   * @return ???????????????????????????????null??
   * @throws NoSDCardException ?????????SD??
   * @throws DeserializingException ???????????????
   * @throws DifferentVersionException ???????????
   * @throws ClassNotFoundException ?????????
   * @throws IOException ????????????
   */
  public static IndexData deserialize() throws NoSDCardException, DeserializingException, IOException, ClassNotFoundException, DifferentVersionException {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      throw new NoSDCardException();
    }
    File f = new File(Environment.getExternalStorageDirectory().getPath() + "/.collimator/index.dat");
    if (!f.exists()) {
      throw new FileNotFoundException();
    }
    DataInputStream in = null;
    FileInputStream file = null;
    try {
      file = new FileInputStream(f.getPath());
      in = new DataInputStream(file);
      IndexData data = new IndexData();
      data.read(in);
      return data;
    } finally {
      if (in != null)
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      if (file != null)
        try {
          file.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
  }
  
  /**
   * ??????????????
   */
  private void getBlacklist() {
    String root = Environment.getExternalStorageDirectory().getPath();
    blackList.add(root + "/lost.dir");
    blackList.add(root + "/android");
    blackList.add(root + "/brut.googlemaps");    
    blackList.add(root + "/navione");    
    blackList.add(root + "/picstore");    
  }
  
  /**
   * ????????????????????????????
   * @param file  ????????????????
   * @return ????????
   */
  private boolean isQualifiedDirectory(File file) {
    if (file.getName().equals(".") || file.getName().equals("..")) return false;
    if (!isIndexDotPrefix && file.getName().startsWith(".")) return false;
    if (!isIndexHidden && file.isHidden()) return false;
    if (!isIndexSystem && blackList.contains(file.getPath().toLowerCase())) return false;
    if (new File(file.getPath(), ".nomedia").exists()) return false;
    return true;
  }
  
  /**
   * ???????????????????????
   * @param file  ???????????????
   * @return ??????????
   */
  private boolean isQualifiedFile(File file) {
    if (!isIndexDotPrefix && file.getName().startsWith(".")) return false;
    if (!isIndexHidden && file.isHidden()) return false;
    if (!isIndexAllType && FileInfo.mimeType(file.getName()).equals("*.*")) return false;
    return true;
  }
  
  /**
   * ??????????????????
   * @return ???????????
   */
  private static long getAvailableSpace() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      long indexSize = 0;
      File indexFile = new File(Environment.getExternalStorageDirectory().getPath() + "/.collimator/index.dat");
      if (indexFile.exists()) indexSize = indexFile.length();
      StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
      return stat.getBlockSize() * stat.getAvailableBlocks() + indexSize;
    }
    return 0;
  }
  
  /**
   * ?????????????????????????
   * @param data  ????????????
   * @return ????????
   */
  public static boolean neededReload(IndexData data) {
      if (Math.abs(getAvailableSpace() - data.availableSpace) >= 1024L * 50)  return true;
      return false;
  }
  
  @SuppressWarnings("serial")
  public static class NoSDCardException extends Exception {
    public NoSDCardException() {
      super("No SDCard was found.");
    }
    public NoSDCardException(String detailMessage) {
      super(detailMessage);
    }
  }
  
  @SuppressWarnings("serial")
  public static class DeserializingException extends Exception {
    public DeserializingException() {
      super("Deserializing failed.");
    }
    public DeserializingException(String detailMessage) {
      super(detailMessage);
    }
  }
  
  @SuppressWarnings("serial")
  public static class SerializingException extends Exception {
    public SerializingException() {
      super("Serializing failed.");
    }
    public SerializingException(String detailMessage) {
      super(detailMessage);
    }
  }
}




Java Source Code List

android.media.MediaMetadataRetriever.java
com.toraleap.collimator.AppSearchActivity.java
com.toraleap.collimator.GlobalContext.java
com.toraleap.collimator.HelpActivity.java
com.toraleap.collimator.PrefsActivity.java
com.toraleap.collimator.SearchActivity.java
com.toraleap.collimator.ShortcutActivity.java
com.toraleap.collimator.bll.FileScannerService.java
com.toraleap.collimator.bll.TagGenerator.java
com.toraleap.collimator.dal.DBColumns.java
com.toraleap.collimator.dal.DBHelper.java
com.toraleap.collimator.dal.DBOperation.java
com.toraleap.collimator.data.Expression.java
com.toraleap.collimator.data.IndexData.java
com.toraleap.collimator.data.IndexLoader.java
com.toraleap.collimator.data.Index.java
com.toraleap.collimator.data.Match.java
com.toraleap.collimator.data.Matcher.java
com.toraleap.collimator.data.Sorter.java
com.toraleap.collimator.ext.GlobalProvider.java
com.toraleap.collimator.ext.Playlist.java
com.toraleap.collimator.model.BaseTag.java
com.toraleap.collimator.ui.FloatingDialog.java
com.toraleap.collimator.ui.MatchAdapter.java
com.toraleap.collimator.util.DigestUtil.java
com.toraleap.collimator.util.FileInfo.java
com.toraleap.collimator.util.MimeTypeMap.java
com.toraleap.collimator.util.RecursiveFileObserver.java
com.toraleap.collimator.util.ShortcutHelper.java
com.toraleap.collimator.util.SoftCache.java
com.toraleap.collimator.util.ThumbnailUtil.java
com.toraleap.collimator.util.Unicode2Alpha.java