Android Open Source - Alfred4Android Digest Util






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.util;
//w  ww .j  av a  2s . c om
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.mozilla.universalchardet.UniversalDetector;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.media.ExifInterface;
import android.media.MediaMetadataRetriever;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;

import com.toraleap.collimator.R;

/**
 * ????????????????????????????
 * @author    uestc.Mobius <mobius@toraleap.com>
 * @version  2010.1029
 */
public class DigestUtil extends SoftCache<String, Spanned> {

  private Spanned mDefaultDigest;
  private Spanned mLoadingDigest;
  private String mNullData;
  private String mCharset;
  private Context mContext;
  private SharedPreferences mPrefs;
  private boolean isDisplayDigest;
  
  /**
   * ???????????????????
   * @param context  ??????????(?????? Activity ?????????????? getApplicationContext() ???)
   */
  public DigestUtil(Context context, Handler handler) {
    super(handler);
    mContext = context;
    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    mDefaultDigest = Html.fromHtml(context.getString(R.string.util_digest_none));
    mLoadingDigest = Html.fromHtml(context.getString(R.string.util_digest_loading));
    mNullData = context.getString(R.string.util_digest_nulldata);
    mCharset = context.getString(R.string.util_digest_charset);
    isDisplayDigest = mPrefs.getBoolean("display_digest", true);
  }

  @Override
  Spanned request(String key) {
    return fromFile(key);
  }
  
  @Override
  int getMaxQueueLength() {
    return 10;
  }

  @Override
  Spanned getDefault() {
    return mLoadingDigest;
  }
  
  /**
   * ??????????????????????????????????????????????????120????????????????????????????????????????????????????????????????????????null??
   * @param filename  ?????????????
   * @return ?????????
   */
  private Spanned fromFile(String filename) {
    Spanned digest = null;
    if (isDisplayDigest) {
      String mimeType = FileInfo.mimeType(filename);
      if (mimeType.startsWith("image/")) {
        digest = loadFromImage(filename);
      } else if (mimeType.startsWith("video/")) {
        digest = loadFromVideo(filename);
      } else if (mimeType.startsWith("audio/")) {
        digest = loadFromAudio(filename);
      } else if (mimeType.startsWith("text/")) {
        digest = loadFromText(filename);
      } else if (mimeType.startsWith("application/zip")) {
        digest = loadFromArchive(filename);
      } else if (mimeType.equals("application/vnd.android.package-archive")) {
        digest = loadFromApk(filename);
      }
    }
    return (null == digest) ? mDefaultDigest : digest;
  }
  
  /**
   * ?????????????????120?????????
   * @param path  ?????????????
   * @return ?????????
   */
  private Spanned loadFromText(String path) {
    Spanned digest = null;
    byte[] buffer = new byte[256];
    try {
      FileInputStream input = new FileInputStream(path);
      int length = input.read(buffer);
      input.close();
      if (length >= 0) {
        String str = format(buffer, length).replaceAll("[\\s\\t\\r\\n]+", " ");
        digest = Html.fromHtml(str.substring(0, str.length() - 1));
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return digest;
  }
  
  /**
   * ???????????????????????
   * @param path  ?????????????
   * @return ?????????
   */
  private Spanned loadFromAudio(String path) {
    Spanned digest = null;
    String where = MediaStore.Audio.AudioColumns.DATA + " IN ('" + path.replace("'", "''") + "','/mnt" + path.replace("'", "''") + "')";
    Cursor cursor = mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
        null, where, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    if (cursor != null && cursor.getCount() > 0) {
      cursor.moveToFirst();
      String title = format(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
      String album = format(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));  
      String artist = format(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));  
      int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
      cursor.close();
      digest = Html.fromHtml(mContext.getString(R.string.util_digest_audio_format, title, artist, album, durationToString(duration)));
    }
    return digest;
  }
  
  /**
   * ???????????????????????
   * @param path  ?????????????
   * @return ?????????
   */
  private Spanned loadFromVideo(String path) {
    Spanned digest = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
            retriever.setDataSource(path);
            String width = format(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
            String height = format(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
            String duration = format(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
      digest = Html.fromHtml(mContext.getString(R.string.util_digest_video_format, width, height, durationToString(duration)));
        } catch (Exception ex) {
            ex.printStackTrace();
        } catch (NoSuchMethodError err) {
          err.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                ex.printStackTrace();
            }
        }
    return digest;
  }
  
  /**
   * ???????????????????????
   * @param path  ?????????????
   * @return   ?????????
   */
  private Spanned loadFromImage(String path) {
    Spanned digest = null;
    try {
      ExifInterface exif = new ExifInterface(path);
      String width = format(exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));
      String height = format(exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH));
      String model = format(exif.getAttribute(ExifInterface.TAG_MODEL));
      String date = format(exif.getAttribute(ExifInterface.TAG_DATETIME));
      if (width == null || height == null || width.equals("0") || height.equals("0")) {
        Options options = new Options();
        options.inSampleSize = 1;
            options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        width = String.valueOf(options.outWidth);
        height = String.valueOf(options.outHeight);
      }
      digest = Html.fromHtml(mContext.getString(R.string.util_digest_image_format, width, height, model, date));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return digest;
  }
  
  /**
   * ???????????????????????
   * @param path  ?????????????
   * @return   ?????????
   */
  private Spanned loadFromArchive(String path) {
    Spanned digest = null;
    StringBuilder sb = new StringBuilder();
    int count = 0;
    long size = 0;
    try {
      ZipFile zip = new ZipFile(path);
      Enumeration<? extends ZipEntry> entries = zip.entries();
      while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (count < 3) sb.append("<br>").append(entry.getName());
        count++;
        size += entry.getSize();
      }
      digest = Html.fromHtml(mContext.getString(R.string.util_digest_archive_format, count, FileInfo.sizeString(size), sb.toString()));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return digest;
  }
  
  /**
   * ??????????????????????????
   * @param path  ?????????????
   * @return   ?????????
   */
  private Spanned loadFromApk(String path) {
    Spanned digest = null;
    PackageManager pm = mContext.getPackageManager();      
        PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);      
        if(info != null){
            ApplicationInfo appInfo = info.applicationInfo;
            appInfo.sourceDir = path;
            appInfo.publicSourceDir = path;
            String applabel = format(pm.getApplicationLabel(appInfo).toString());
            String packagename = format(info.packageName);
            String version = format(info.versionName);
            digest = Html.fromHtml(mContext.getString(R.string.util_digest_apk_format, applabel, version, packagename));
        }
    return digest;
  }
  
  private String format(String data) {
    if (data == null || data.trim().length() == 0) return mNullData;
    else return data;
  }
  
  private String format(byte[] data, int length) {
      try {
      UniversalDetector detector = new UniversalDetector(null);
      detector.handleData(data, 0, data.length);
          String encoding = detector.getDetectedCharset();
          if (encoding == null) encoding = mCharset;
      return new String(data, 0, length, encoding);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      return new String(data, 0, length);
    }
  }
  
  private String durationToString(int duration) {
    return String.format("%d:%02d", duration / (1000 * 60), (duration / 1000) % 60);
  }
  
  private String durationToString(String durationString) {
    return durationToString(Integer.parseInt(durationString));
  }
}




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