之前写过2篇关于PackageManager的学习笔记
Android中PackageManager学习笔记(1)-ApplicationInfo
Android中PackageManager学习笔记(2)-PackageInfo
今天继续学习PackageManager中PackageParser这个类。
PackageParser
该类存在于android.content.pm包中,官方文档并没有放出该类,所以只能查看源码
PackageParser为PackageManager专门解析android配置档文件AndroidManifest.xml所建立的。里面的一系列方法都是通过XMLPullParser工具解析该xml文件。由于方法太多,主要对里面的内部类做一些简单的介绍,起一个抛砖的作用。首先进官网查看一下我们的主配置文件含有哪些节点?然后讲解就会很容易了。
public final static class Activity extends Component<ActivityIntentInfo>
Activity节点信息
public final static class ActivityIntentInfo extends IntentInfo
activity中intent节点信息
public static class Component<II extends IntentInfo>
保存所有组件,activity,provider,receiver都属于组件
public final static class Instrumentation extends Component
保存instrumentation节点信息
public static class IntentInfo extends IntentFilter
保存Intent的信息
public static class NewPermissionInfo
新权限?信息。sdk版本号,文件版本号,权限名称
public final static class Package
保存了该包中所有文件节点信息
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<Permission> permissions = new ArrayList<Permission>(0);
public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
public final ArrayList<Service> services = new ArrayList<Service>(0);
public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
public final ArrayList<String> requestedPermissions = new ArrayList<String>();
public final ArrayList<Boolean> requestedPermissionsRequired = new ArrayList<Boolean>();
public ArrayList<String> protectedBroadcasts;
public ArrayList<String> libraryNames = null;
public ArrayList<String> usesLibraries = null;
public ArrayList<String> usesOptionalLibraries = null;
public String[] usesLibraryFiles = null;
public ArrayList<ActivityIntentInfo> preferredActivityFilters = null;
public ArrayList<String> mOriginalPackages = null;
public String mRealPackage = null;
public ArrayList<String> 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<ConfigurationInfo> configPreferences =
new ArrayList<ConfigurationInfo>();
/*
* Applications requested features
*/
public ArrayList<FeatureInfo> 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<PublicKey> mSigningKeys;
public Map<String, Set<PublicKey>> 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 + "}";
}
}
public static class PackageLite
<manifest>节点信息
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="string"
android:sharedUserId="string"
android:sharedUserLabel="string resource"
android:versionCode="integer"
android:versionName="string"
android:installLocation=["auto" | "internalOnly" | "preferExternal"] >
. . .
</manifest>
static class ParseComponentArgs extends ParsePackageItemArgs
ParsePackageItemArgs
自己创造,保存一些ParsePackage自身用于唯一标识的信息
public final static class Permission extends Component<IntentInfo>
保存permission节点信息
public final static class PermissionGroup extends Component<IntentInfo>
保存<permission-group>节点信息
public final static class Provider extends Component<ProviderIntentInfo>
保存provider节点信息
public static final class ProviderIntentInfo extends IntentInfo
保存provider中的intent节点信息
public final static class Service extends Component<ServiceIntentInfo>
保存service标签的信息
public final static class ServiceIntentInfo extends IntentInfo
保存Service标签中的intent属性节点信息
public static class SplitPermissionInfo
将权限信息保存在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;
}
}