之前写过2篇关于PackageManager的学习笔记
今天继续学习PackageManager中PackageParser这个类。
该类存在于android.content.pm包中,官方文档并没有放出该类,所以只能查看源码
packageparser源码网站
PackageParser为PackageManager专门解析android配置档文件AndroidManifest.xml所建立的。里面的一系列方法都是通过XMLPullParser工具解析该xml文件。由于方法太多,主要对里面的内部类做一些简单的介绍,起一个抛砖的作用。首先进官网查看一下我们的主配置文件含有哪些节点?然后讲解就会很容易了。
Activity节点信息
activity中intent节点信息
保存所有组件,activity,provider,receiver都属于组件
保存instrumentation节点信息
保存Intent的信息
新权限?信息。sdk版本号,文件版本号,权限名称
保存了该包中所有文件节点信息
public final static class Package {
public String packageName;
// For now we only support one application per package.
public final ApplicationInfo applicationInfo = new ApplicationInfo();
public final ArrayList permissions = new ArrayList(0);
public final ArrayList permissionGroups = new ArrayList(0);
public final ArrayList activities = new ArrayList(0);
public final ArrayList receivers = new ArrayList(0);
public final ArrayList providers = new ArrayList(0);
public final ArrayList services = new ArrayList(0);
public final ArrayList instrumentation = new ArrayList(0);
public final ArrayList requestedPermissions = new ArrayList();
public final ArrayList requestedPermissionsRequired = new ArrayList();
public ArrayList protectedBroadcasts;
public ArrayList libraryNames = null;
public ArrayList usesLibraries = null;
public ArrayList usesOptionalLibraries = null;
public String[] usesLibraryFiles = null;
public ArrayList preferredActivityFilters = null;
public ArrayList mOriginalPackages = null;
public String mRealPackage = null;
public ArrayList mAdoptPermissions = null;
// We store the application meta-data independently to avoid multiple unwanted references
public Bundle mAppMetaData = null;
// If this is a 3rd party app, this is the path of the zip file.
public String mPath;
// The version code declared for this package.
public int mVersionCode;
// The version name declared for this package.
public String mVersionName;
// The shared user id that this package wants to use.
public String mSharedUserId;
// The shared user label that this package wants to use.
public int mSharedUserLabel;
// Signatures that were read from the package.
public Signature mSignatures[];
// For use by package manager service for quick lookup of
// preferred up order.
public int mPreferredOrder = 0;
// For use by the package manager to keep track of the path to the
// file an app came from.
public String mScanPath;
// For use by package manager to keep track of where it has done dexopt.
public boolean mDidDexOpt;
// // User set enabled state.
// public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
//
// // Whether the package has been stopped.
// public boolean mSetStopped = false;
// Additional data supplied by callers.
public Object mExtras;
// Whether an operation is currently pending on this package
public boolean mOperationPending;
/*
* Applications hardware preferences
*/
public final ArrayList configPreferences =
new ArrayList();
/*
* Applications requested features
*/
public ArrayList reqFeatures = null;
public int installLocation;
/* An app that's required for all users and cannot be uninstalled for a user */
public boolean mRequiredForAllUsers;
/* The restricted account authenticator type that is used by this application */
public String mRestrictedAccountType;
/* The required account type without which this application will not function */
public String mRequiredAccountType;
/**
* Digest suitable for comparing whether this package's manifest is the
* same as another.
*/
public ManifestDigest manifestDigest;
/**
* Data used to feed the KeySetManager
*/
public Set mSigningKeys;
public Map> mKeySetMapping;
public Package(String _name) {
packageName = _name;
applicationInfo.packageName = _name;
applicationInfo.uid = -1;
}
public void setPackageName(String newName) {
packageName = newName;
applicationInfo.packageName = newName;
for (int i=permissions.size()-1; i>=0; i--) {
permissions.get(i).setPackageName(newName);
}
for (int i=permissionGroups.size()-1; i>=0; i--) {
permissionGroups.get(i).setPackageName(newName);
}
for (int i=activities.size()-1; i>=0; i--) {
activities.get(i).setPackageName(newName);
}
for (int i=receivers.size()-1; i>=0; i--) {
receivers.get(i).setPackageName(newName);
}
for (int i=providers.size()-1; i>=0; i--) {
providers.get(i).setPackageName(newName);
}
for (int i=services.size()-1; i>=0; i--) {
services.get(i).setPackageName(newName);
}
for (int i=instrumentation.size()-1; i>=0; i--) {
instrumentation.get(i).setPackageName(newName);
}
}
public boolean hasComponentClassName(String name) {
for (int i=activities.size()-1; i>=0; i--) {
if (name.equals(activities.get(i).className)) {
return true;
}
}
for (int i=receivers.size()-1; i>=0; i--) {
if (name.equals(receivers.get(i).className)) {
return true;
}
}
for (int i=providers.size()-1; i>=0; i--) {
if (name.equals(providers.get(i).className)) {
return true;
}
}
for (int i=services.size()-1; i>=0; i--) {
if (name.equals(services.get(i).className)) {
return true;
}
}
for (int i=instrumentation.size()-1; i>=0; i--) {
if (name.equals(instrumentation.get(i).className)) {
return true;
}
}
return false;
}
public String toString() {
return "Package{"
+ Integer.toHexString(System.identityHashCode(this))
+ " " + packageName + "}";
}
}
. . .
自己创造,保存一些ParsePackage自身用于唯一标识的信息
保存permission节点信息
保存
保存provider节点信息
保存provider中的intent节点信息
保存service标签的信息
保存Service标签中的intent属性节点信息
将权限信息保存在String数组中
public static class SplitPermissionInfo {
public final String rootPerm;
public final String[] newPerms;
public final int targetSdk;
public SplitPermissionInfo(String rootPerm, String[] newPerms, int targetSdk) {
this.rootPerm = rootPerm;
this.newPerms = newPerms;
this.targetSdk = targetSdk;
}
}