Android Open Source - Alfred4Android Index






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  w w.j a v  a  2s .com
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.Spanned;

import com.toraleap.collimator.data.IndexData.DifferentVersionException;
import com.toraleap.collimator.data.IndexLoader.DeserializingException;
import com.toraleap.collimator.data.IndexLoader.NoSDCardException;
import com.toraleap.collimator.data.IndexLoader.SerializingException;
import com.toraleap.collimator.util.DigestUtil;
import com.toraleap.collimator.util.ThumbnailUtil;

/**
 * ?????????????????????
 * @author    uestc.Mobius <mobius@toraleap.com>
 * @version  2010.1104
 */
public final class Index {

  private static final int MESSAGE_FIRST = 100;
  public static final int MESSAGE_NOSDCARD = MESSAGE_FIRST;
  public static final int MESSAGE_RELOAD_SUCCESS = MESSAGE_FIRST + 1;
  public static final int MESSAGE_RELOAD_FAILED = MESSAGE_FIRST + 2;
  public static final int MESSAGE_SERIALIZING_FAILED = MESSAGE_FIRST + 3;
  public static final int MESSAGE_DESERIALIZING_SUCCESS = MESSAGE_FIRST + 4;
  public static final int MESSAGE_DESERIALIZING_FAILED = MESSAGE_FIRST + 5;
  public static final int MESSAGE_DESERIALIZING_DIFFERENT_VERSION = MESSAGE_FIRST + 6;
  public static final int MESSAGE_UPDATE_THUMBNAIL = MESSAGE_FIRST + 10;
  public static final int MESSAGE_UPDATE_DIGEST = MESSAGE_FIRST + 11;
  public static final int STATUS_FAILED = 0;
  public static final int STATUS_OBSOLETE = 1;
  public static final int STATUS_READY = 2;
  public static final int STATUS_RELOADING = 3;
  public static final int STATUS_DESERIALIZING = 4;
  private static Handler sEventHandler;
  private static SharedPreferences sPrefs;
  
  private static IndexData data;
  private static int status;

  private static ThumbnailUtil thumbUtil;
  private static DigestUtil digestUtil;
  
  private Index() { }
  
  /**
   * ???????????????????
   * @return ??STATUS???????????
   */
  public static int getStatus() {
    return status;
  }
  
  /**
   * ???????????????????????????
   */
  public static void checkObsolete() {
    if (status == STATUS_READY && (IndexLoader.neededReload(data) || sPrefs.getBoolean("index_is_obsolete", true))) {
        sPrefs.edit().putBoolean("index_is_obsolete", true).commit();
        status = STATUS_OBSOLETE;
    }
  }
  
  /**
   * ??????????????????????
   * @return ???????
   */
  public static int length() {
    if (data != null)
      return data.length();
    else
      return 0;
  }
  
  /**
   * ??????????????????
   * @return  ???????????????
   */
  public static long reloadTime() {
    return data.indexTime;
  }
  
  /**
   * ???SD???????????????????
   * @return  ??????????
   */
  public static long availableSpace() {
    return data.availableSpace;
  }
  
  /**
   * ??????????????????????
   */
  public static void interrupt() {
    thumbUtil.interrupt();
    digestUtil.interrupt();
  }
  
  /**
   * ?????????????????????????????????????????????????
   * @param prefs    ????????????????????????????
   * @param handler  ????????????????????????????????????????????????????
   */
  public static void init(Context context, Handler handler) {
    sEventHandler = handler;
    sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    thumbUtil = new ThumbnailUtil(context, handler);
    digestUtil = new DigestUtil(context, handler);
  }
  
  /**
   * ??????????????????????????????????????????????????????????????????????? RELOAD_FINISHED ?????
   */
  public static synchronized void reloadEntriesAsync() {
    status = STATUS_RELOADING;
    new Thread(new Runnable() {
          public void run() {
            interrupt();
          data = null;
            IndexLoader loader = new IndexLoader(sPrefs);
            try {
              IndexData newData = loader.reload();
          IndexLoader.serialize(newData);
            data = newData;
            sPrefs.edit().putBoolean("index_is_obsolete", false).commit();
            status = STATUS_READY;
            checkObsolete();
              sendHandlerMessage(MESSAGE_RELOAD_SUCCESS, 0, 0, null);
            } catch (NoSDCardException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_NOSDCARD, 0, 0, null);
            } catch (SerializingException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_SERIALIZING_FAILED, 0, 0, null);
            }
            }
        }).start();
  }
  
  /**
   * ???????????????????????????????????????????????????????? DESERIALIZED_SUCCESS ?? DESERIALIZED_FAILED ?????
   */
  public static synchronized void deserialization() {
    status = STATUS_DESERIALIZING;
    new Thread(new Runnable() {
          public void run() {
            interrupt();
            try {
              data = IndexLoader.deserialize();
              status = STATUS_READY;
              checkObsolete();
              sendHandlerMessage(MESSAGE_DESERIALIZING_SUCCESS, 0, 0, null);          
            } catch (NoSDCardException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_NOSDCARD, 0, 0, null);
            } catch (FileNotFoundException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_DESERIALIZING_FAILED, 0, 0, null);
        } catch (DeserializingException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_DESERIALIZING_FAILED, 0, 0, null);
        } catch (IOException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_DESERIALIZING_FAILED, 0, 0, null);
        } catch (ClassNotFoundException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_DESERIALIZING_FAILED, 0, 0, null);
        } catch (DifferentVersionException e) {
              status = STATUS_FAILED;
              sendHandlerMessage(MESSAGE_DESERIALIZING_DIFFERENT_VERSION, 0, 0, null);
        } 
            }
        }).start();
  }

  /**
   * ???????????????????????
   * @param what  ???????
   * @param arg1  ???????1 (????????????)
   * @param arg2  ???????2 (????????????)
   * @param obj  ?????????? (????????????)
   */
  private static void sendHandlerMessage(int what, int arg1, int arg2, Object obj) {
    if (null != sEventHandler) {
      Message msg = Message.obtain();
      msg.what = what;
      msg.arg1 = arg1;
      msg.arg2 = arg2;
      msg.obj = obj;
      sEventHandler.sendMessage(msg);
    }
  }
    
  /**
   * ??????????????????????
   * @return  ?????
   */
  public static String getName(int i) { return data.name[i]; }
  /**
   * ??????????????????????????????????????????????????????????????????
   * @return  ???????????????
   */
  public static String getNameAlpha(int i) { return data.nameAlpha[i]; }//Unicode2Alpha.toAlpha(data.name[i]); }
  /**
   * ?????????????????????????
   * @return  ???????
   */
  public static String getPath(int i) { return data.path[i]; }
  /**
   * ?????????????????????????????????????????????????????????????? null??
   * @return  ?????????????????
   */
  public static String getPathAlpha(int i) { return data.pathAlpha[i]; }//Unicode2Alpha.toAlpha(data.path[i]); }
  /**
   * ???????????????????????????????????????? 1970??1??1?? ???????????
   * @return  ??????????????
   */
  public static long getTime(int i) { return data.time[i]; }
  /**
   * ??????????????????????????????????
   * @return  ???????
   */
  public static long getSize(int i) { return data.size[i]; }
  
  /***
   * ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? UPDATE_THUMBNAIL ?????????????????
   * @return ??????????
   */
  public static Bitmap getThumbnail(int i) {
    return thumbUtil.get(data.path[i] + "/" + data.name[i]);
  }
  
  /***
   * ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? RELOAD_UPDATE_DIGEST ?????????????????
   * @return ??????????
   */
  public static Spanned getDigest(int i) {
    return digestUtil.get(data.path[i] + "/" + data.name[i]);
  }

  /**
   * ????????????????????????
   * @param i   ???????
   * @return ???????????
   */
  public static boolean delete(int i) {
    File file = new File(data.path[i], data.name[i]);
    boolean result = file.delete();
    if (result == true) {
      data.name[i] = null;
    }
    return result;
  }
}




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