Android解析パッケージ名情報


public ApplicationRet.Application getApkFileInfo(Context ctx, String apkPath) {
		File apkFile = new File(apkPath);
		if (!apkFile.exists() || !apkPath.toLowerCase().endsWith(".apk")) {
			return null;
		}
		ApplicationRet.Application appInfoData;
		String PATH_PackageParser = "android.content.pm.PackageParser";
		String PATH_AssetManager = "android.content.res.AssetManager";
		try {
			//     pkgParserCls      ,   
			Class<?> pkgParserCls = Class.forName(PATH_PackageParser);
			Class<?>[] typeArgs = { String.class };
			Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
			Object[] valueArgs = { apkPath };
			Object pkgParser = pkgParserCt.newInstance(valueArgs);

			//  pkgParserCls   parsePackage  
			DisplayMetrics metrics = new DisplayMetrics();
			metrics.setToDefaults();//          ,       
			typeArgs = new Class<?>[] { File.class, String.class,
					DisplayMetrics.class, int.class };
			Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod(
					"parsePackage", typeArgs);

			valueArgs = new Object[] { new File(apkPath), apkPath, metrics, 0 };

			//   pkgParser_parsePackageMtd     
			Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser,
					valueArgs);

			//           "applicationInfo"     
			if (pkgParserPkg == null) {
				return null;
			}
			Field appInfoFld = pkgParserPkg.getClass().getDeclaredField(
					"applicationInfo");

			//    "pkgParserPkg"    "appInfoFld"  
			if (appInfoFld.get(pkgParserPkg) == null) {
				return null;
			}
			ApplicationInfo info = (ApplicationInfo) appInfoFld
					.get(pkgParserPkg);

			//     assetMagCls      ,  
			Class<?> assetMagCls = Class.forName(PATH_AssetManager);
			Object assetMag = assetMagCls.newInstance();
			//  assetMagCls   addAssetPath  
			typeArgs = new Class[1];
			typeArgs[0] = String.class;
			Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod(
					"addAssetPath", typeArgs);
			valueArgs = new Object[1];
			valueArgs[0] = apkPath;
			//   assetMag_addAssetPathMtd  
			assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);

			//   Resources      ,   
			Resources res = ctx.getResources();
			typeArgs = new Class[3];
			typeArgs[0] = assetMag.getClass();
			typeArgs[1] = res.getDisplayMetrics().getClass();
			typeArgs[2] = res.getConfiguration().getClass();
			Constructor<Resources> resCt = Resources.class
					.getConstructor(typeArgs);
			valueArgs = new Object[3];
			valueArgs[0] = assetMag;
			valueArgs[1] = res.getDisplayMetrics();
			valueArgs[2] = res.getConfiguration();
			res = (Resources) resCt.newInstance(valueArgs);
			
			PackageManager pm = ctx.getPackageManager();
			//   apk     
			appInfoData = new ApplicationRet.Application();
			if (info != null) {
				if (info.icon != 0) {//     ,       
					Drawable icon = res.getDrawable(info.icon);//   
					appInfoData.setLocalAppIcon(icon);
				}
				if (info.labelRes != 0) {
					String name = (String) res.getText(info.labelRes);//   
					appInfoData.setTitle(name);
				} else {
					String loadLabelName = info.loadLabel(pm).toString();
					if(loadLabelName == null || "".equals(loadLabelName)) {
						String apkName = apkFile.getName();
						appInfoData.setTitle(apkName.substring(0,
								apkName.lastIndexOf(".")));
					} else {
						appInfoData.setTitle(loadLabelName);
					}
				}
				String pkgName = info.packageName;//   
				appInfoData.setPkg(pkgName);
			} else {
				return null;
			}
			
			PackageInfo packageInfo = pm.getPackageArchiveInfo(apkPath,
					PackageManager.GET_ACTIVITIES);
			if (packageInfo != null) {
				appInfoData.setVersionName(packageInfo.versionName);
				appInfoData.setVersion(packageInfo.versionCode);//    
			}
			appInfoData
					.setIsAppFlag(ActionController.manager_download_app_flag_downloaded);
			return appInfoData;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
if (info.labelRes != 0) {
					String name = (String) res.getText(info.labelRes);//   
					appInfoData.setTitle(name);
				} else {
					String loadLabelName = info.loadLabel(pm).toString();
					if(loadLabelName == null || "".equals(loadLabelName)) {
						String apkName = apkFile.getName();
						appInfoData.setTitle(apkName.substring(0,
								apkName.lastIndexOf(".")));
					} else {
						appInfoData.setTitle(loadLabelName);
					}
				}
        ,  apk        ,   info.labelRes         ,                   String.xml ,
       AndroidManifest.xml  label   , :android:label="  ",    info.labelRes     ,   info.loadLabel()  。