파일 관리자 - apk 설치 패키지에 대한 정보 얻기

//    
//  apk      
public static ApplicationInfo getApplicationInfo(Context context,String archiveFilePath) {
		String PACKAGE_PARSER_CLASS_NAME = "android.content.pm.PackageParser";

		ApplicationInfo applicationInfo = null;
		
		try {
			//    PackageParser  class
			Class<?> packageParserClass = Class.forName(PACKAGE_PARSER_CLASS_NAME);

			Class<?>[] parameterTypes = new Class[1];
			parameterTypes[0] = String.class;
			//        
			Constructor<?> packageParserConstructor = packageParserClass.getConstructor(parameterTypes);
			
			Object[] args = new Object[1];
			//PackageParser          
			args[0] = archiveFilePath;
			//    packageparser    
			Object packageParser = packageParserConstructor.newInstance(args);

			Resources pRes = context.getResources();

			parameterTypes = new Class[4];
			parameterTypes[0] = File.class;
			parameterTypes[1] = String.class;
			parameterTypes[2] = pRes.getDisplayMetrics().getClass();
			parameterTypes[3] = Integer.TYPE;
			//  parsePackage  ,    public Package parsePackage(File sourceFile, String destCodePath,
            //DisplayMetrics metrics, int flags) 
			Method parsePackage = packageParserClass.getDeclaredMethod("parsePackage", parameterTypes);

			args = new Object[4];
			args[0] = new File(archiveFilePath);
			args[1] = archiveFilePath;
			args[2] = pRes.getDisplayMetrics();
			args[3] = 0;
			Object packageInfo = parsePackage.invoke(packageParser, args);
			//      
			Field applicationInfoField = packageInfo.getClass().getDeclaredField("applicationInfo");
			applicationInfo = (ApplicationInfo) applicationInfoField.get(packageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return applicationInfo;
	}
//  apk     
public static Resources getResource(Context context, String archiveFilePath) {
		String ASSET_MANAGER_CLASS_NAME = "android.content.res.AssetManager";
		Resources resources = null;

		try {
			Class<?> assetManagerClass = Class.forName(ASSET_MANAGER_CLASS_NAME);

			Constructor<?> assetManagerConstructor = assetManagerClass.getConstructor((Class[]) null);
			Object assetManager = assetManagerConstructor.newInstance((Object[]) null);

			Class<?>[] parameterTypes = new Class[1];
			parameterTypes[0] = String.class;
			Method addAssetPath = assetManagerClass.getDeclaredMethod("addAssetPath", parameterTypes);

			Object[] args = new Object[1];
			args[0] = archiveFilePath;
			addAssetPath.invoke(assetManager, args);

			Resources pRes = context.getResources();

			parameterTypes = new Class[3];
			parameterTypes[0] = assetManager.getClass();
			parameterTypes[1] = pRes.getDisplayMetrics().getClass();
			parameterTypes[2] = pRes.getConfiguration().getClass();
			Constructor<?> resourcesConstructor = Resources.class.getConstructor(parameterTypes);

			args = new Object[3];
			args[0] = assetManager;
			args[1] = pRes.getDisplayMetrics();
			args[2] = pRes.getConfiguration();
			resources = (Resources) resourcesConstructor.newInstance(args);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resources;
	}
/**
	 *     apk     
	 * @param context
	 * @param file
	 * @return
	 */
	public static Drawable getApkIcon(Context context, File file) {
		Drawable icon = null;
		PackageManager pm = context.getPackageManager();
		ApplicationInfo applicationInfo = getApplicationInfo(context, file.getPath());
		Resources resources = getResource(context, file.getPath());
		
		if (applicationInfo == null || resources == null) {
			icon = pm.getDefaultActivityIcon();
		} else{
			if (applicationInfo.icon != 0) {
				try {
					icon = resources.getDrawable(applicationInfo.icon);
				} catch (Resources.NotFoundException e) {
				}
			}
			if (icon == null) {
				icon = pm.getDefaultActivityIcon();
			}
		}

		int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
		icon.setBounds(0, 0, iconSize, iconSize);
		return icon;
	}

좋은 웹페이지 즐겨찾기