package com.android.providers.downloads.test.util;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import aurora.app.AuroraActivity;
import com.android.providers.downloads.test.R;
public class FileIconLoader {
private static final Map<String, Integer> sIconMap = new HashMap<String, Integer>();
static public class AppSnippet {
CharSequence label;
Drawable icon;
public AppSnippet(CharSequence label, Drawable icon) {
this.label = label;
this.icon = icon;
}
}
static {
addItem(new String[] { "apk" }, R.drawable.file_icon_apk_light);
addItem(new String[] { "flac" }, R.drawable.file_icon_flac_light);
addItem(new String[] { "aac" }, R.drawable.file_icon_aac_light);
addItem(new String[] { "mp3" }, R.drawable.file_icon_mp3_light);
addItem(new String[] { "wma" }, R.drawable.file_icon_wma_light);
addItem(new String[] { "wav" }, R.drawable.file_icon_wav_light);
addItem(new String[] { "mid" }, R.drawable.file_icon_mid_light);
addItem(new String[] { "amr" }, R.drawable.file_icon_amr_light);
addItem(new String[] { "m4a" }, R.drawable.file_icon_m4a_light);
addItem(new String[] { "ogg" }, R.drawable.file_icon_ogg_light);
addItem(new String[] { "3gpp" }, R.drawable.file_icon_3gpp_light);
addItem(new String[] { "mp4", "wmv", "mpeg", "m4v", "3gp", "3g2",
"3gpp2", "asf" }, R.drawable.file_icon_video_light);
addItem(new String[] { "jpg", "jpeg", "gif", "png", "bmp", "wbmp" },
R.drawable.file_icon_picture_light);
addItem(new String[] { "txt", "log", "xml", "ini", "lrc" },
R.drawable.file_icon_txt_light);
addItem(new String[] { "doc", "ppt", "docx", "pptx", "xsl", "xslx", },
R.drawable.file_icon_office_light);
addItem(new String[] { "pdf" }, R.drawable.file_icon_pdf_light);
addItem(new String[] { "zip" }, R.drawable.file_icon_zip_light);
addItem(new String[] { "gnz" }, R.drawable.file_icon_theme_light);
addItem(new String[] { "rar" }, R.drawable.file_icon_rar_light);
}
/**
* Utility method to load application label
*
* @param pContext context of package that can load the resources
* @param appInfo ApplicationInfo object of package whose resources are to be loaded
* @param snippetId view id of app snippet view
*/
public static AppSnippet getAppSnippet(
AuroraActivity pContext, ApplicationInfo appInfo, File sourceFile) {
final String archiveFilePath = sourceFile.getAbsolutePath();
Resources pRes = pContext.getResources();
AssetManager assmgr = new AssetManager();
assmgr.addAssetPath(archiveFilePath);
Resources res = new Resources(assmgr, pRes.getDisplayMetrics(), pRes.getConfiguration());
CharSequence label = null;
// Try to load the label from the package's resources. If an app has not explicitly
// specified any label, just use the package name.
if (appInfo.labelRes != 0) {
try {
label = res.getText(appInfo.labelRes);
} catch (Resources.NotFoundException e) {
}
}
if (label == null) {
label = (appInfo.nonLocalizedLabel != null) ?
appInfo.nonLocalizedLabel : appInfo.packageName;
}
Drawable icon = null;
// Try to load the icon from the package's resources. If an app has not explicitly
// specified any resource, just use the default icon for now.
if (appInfo.icon != 0) {
try {
icon = res.getDrawable(appInfo.icon);
} catch (Resources.NotFoundException e) {
}
}
// if (icon == null) {
// icon = pContext.getPackageManager().getDefaultActivityIcon();
// }
return new AppSnippet(label, icon);
}
private static final void addItem(String[] files, int id) {
for (String s : files) {
sIconMap.put(s, id);
}
}
public static final int getFileIconIdByExtName(String fileName) {
Integer id = sIconMap.get(fileName);
if (id == null) {
return R.drawable.file_icon_default_light;
} else {
return id;
}
}
public static final int getFileIconId(String fileName) {
if (fileName == null) {
return R.drawable.file_icon_null_light;
}
if (!Utils.isSDCardReady()) {
return R.drawable.gn_app_icon_default;
}
if (!new File(fileName).exists()) {
return R.drawable.file_icon_deleted;
}
int idx = fileName.lastIndexOf('.');
int drawable = getFileIconIdByExtName(fileName.substring(idx + 1));
return drawable;
}
/**
*
* 根据文件后缀名获得对应的MIME类型。
*
* @param file
*/
private String getMIMEType(File file)
{
String type = "*/*";
String fName = file.getName();
// 获取后缀名前的分隔符"."在fName中的位置。
int dotIndex = fName.lastIndexOf(".");
if (dotIndex < 0) {
return type;
}
/* 获取文件的后缀名 */
String end = fName.substring(dotIndex, fName.length()).toLowerCase();
if (end == "")
return type;
// 在MIME和文件类型的匹配表中找到对应的MIME类型。
for (int i = 0; i < MIME_MapTable.length; i++) {
if (end.equals(MIME_MapTable[i][0])) {
type = MIME_MapTable[i][1];
break;
}
}
return type;
}
// 建立一个MIME类型与文件后缀名的匹配表
private final String[][] MIME_MapTable = {
// {后缀名, MIME类型}
{ ".3gp", "video/3gpp" },
{ ".apk", "application/vnd.android.package-archive" },
{ ".asf", "video/x-ms-asf" }, { ".avi", "video/x-msvideo" },
{ ".bin", "application/octet-stream" }, { ".bmp", "image/bmp" },
{ ".c", "text/plain" }, { ".class", "application/octet-stream" },
{ ".conf", "text/plain" }, { ".cpp", "text/plain" },
{ ".doc", "application/msword" },
{ ".exe", "application/octet-stream" }, { ".gif", "image/gif" },
{ ".gtar", "application/x-gtar" }, { ".gz", "application/x-gzip" },
{ ".h", "text/plain" }, { ".htm", "text/html" },
{ ".html", "text/html" }, { ".jar", "application/java-archive" },
{ ".java", "text/plain" }, { ".jpeg", "image/jpeg" },
{ ".jpg", "image/jpeg" }, { ".js", "application/x-javascript" },
{ ".log", "text/plain" }, { ".m3u", "audio/x-mpegurl" },
{ ".m4a", "audio/mp4a-latm" }, { ".m4b", "audio/mp4a-latm" },
{ ".m4p", "audio/mp4a-latm" }, { ".m4u", "video/vnd.mpegurl" },
{ ".m4v", "video/x-m4v" }, { ".mov", "video/quicktime" },
{ ".mp2", "audio/x-mpeg" }, { ".mp3", "audio/x-mpeg" },
{ ".mp4", "video/mp4" },
{ ".mpc", "application/vnd.mpohun.certificate" },
{ ".mpe", "video/mpeg" }, { ".mpeg", "video/mpeg" },
{ ".mpg", "video/mpeg" }, { ".mpg4", "video/mp4" },
{ ".mpga", "audio/mpeg" },
{ ".msg", "application/vnd.ms-outlook" }, { ".ogg", "audio/ogg" },
{ ".pdf", "application/pdf" }, { ".png", "image/png" },
{ ".pps", "application/vnd.ms-powerpoint" },
{ ".ppt", "application/vnd.ms-powerpoint" },
{ ".prop", "text/plain" },
{ ".rar", "application/x-rar-compressed" },
{ ".rc", "text/plain" }, { ".rmvb", "audio/x-pn-realaudio" },
{ ".rtf", "application/rtf" }, { ".sh", "text/plain" },
{ ".tar", "application/x-tar" },
{ ".tgz", "application/x-compressed" }, { ".txt", "text/plain" },
{ ".wav", "audio/x-wav" }, { ".wma", "audio/x-ms-wma" },
{ ".wmv", "audio/x-ms-wmv" },
{ ".wps", "application/vnd.ms-works" },
// {".xml", "text/xml"},
{ ".xml", "text/plain" }, { ".z", "application/x-compress" },
{ ".zip", "application/zip" }, { "", "*/*" } };
}