下面列出了怎么用android.content.pm.PackageParser.ActivityIntentInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/** Add all components defined in the given package to the internal structures. */
void addAllComponents(PackageParser.Package pkg, boolean chatty) {
final ArrayList<PackageParser.ActivityIntentInfo> newIntents = new ArrayList<>();
synchronized (mLock) {
addActivitiesLocked(pkg, newIntents, chatty);
addReceiversLocked(pkg, chatty);
addProvidersLocked(pkg, chatty);
addServicesLocked(pkg, chatty);
}
final String setupWizardPackage = sPackageManagerInternal.getKnownPackageName(
PACKAGE_SETUP_WIZARD, UserHandle.USER_SYSTEM);
for (int i = newIntents.size() - 1; i >= 0; --i) {
final PackageParser.ActivityIntentInfo intentInfo = newIntents.get(i);
final PackageParser.Package disabledPkg = sPackageManagerInternal
.getDisabledSystemPackage(intentInfo.activity.info.packageName);
final List<PackageParser.Activity> systemActivities =
disabledPkg != null ? disabledPkg.activities : null;
adjustPriority(systemActivities, intentInfo, setupWizardPackage);
}
}
@GuardedBy("mLock")
private void addActivitiesLocked(PackageParser.Package pkg,
List<PackageParser.ActivityIntentInfo> newIntents, boolean chatty) {
final int activitiesSize = pkg.activities.size();
StringBuilder r = null;
for (int i = 0; i < activitiesSize; i++) {
PackageParser.Activity a = pkg.activities.get(i);
a.info.processName =
fixProcessName(pkg.applicationInfo.processName, a.info.processName);
mActivities.addActivity(a, "activity", newIntents);
if (DEBUG_PACKAGE_SCANNING && chatty) {
if (r == null) {
r = new StringBuilder(256);
} else {
r.append(' ');
}
r.append(a.info.name);
}
}
if (DEBUG_PACKAGE_SCANNING && chatty) {
Log.d(TAG, " Activities: " + (r == null ? "<NONE>" : r));
}
}
List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType,
int flags, List<PackageParser.Activity> packageActivities, int userId) {
if (!sUserManager.exists(userId)) {
return null;
}
if (packageActivities == null) {
return null;
}
mFlags = flags;
final boolean defaultOnly = (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0;
final int activitiesSize = packageActivities.size();
ArrayList<PackageParser.ActivityIntentInfo[]> listCut = new ArrayList<>(activitiesSize);
ArrayList<PackageParser.ActivityIntentInfo> intentFilters;
for (int i = 0; i < activitiesSize; ++i) {
intentFilters = packageActivities.get(i).intents;
if (intentFilters != null && intentFilters.size() > 0) {
PackageParser.ActivityIntentInfo[] array =
new PackageParser.ActivityIntentInfo[intentFilters.size()];
intentFilters.toArray(array);
listCut.add(array);
}
}
return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId);
}
private void removeActivity(PackageParser.Activity a, String type) {
mActivities.remove(a.getComponentName());
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " " + type + " "
+ (a.info.nonLocalizedLabel != null ? a.info.nonLocalizedLabel
: a.info.name) + ":");
Log.v(TAG, " Class=" + a.info.name);
}
final int intentsSize = a.intents.size();
for (int j = 0; j < intentsSize; j++) {
PackageParser.ActivityIntentInfo intent = a.intents.get(j);
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " IntentFilter:");
intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
}
removeFilter(intent);
}
}
@Override
protected boolean isFilterStopped(PackageParser.ActivityIntentInfo filter, int userId) {
if (!sUserManager.exists(userId)) return true;
PackageParser.Package p = filter.activity.owner;
if (p != null) {
PackageSetting ps = (PackageSetting) p.mExtras;
if (ps != null) {
// System apps are never considered stopped for purposes of
// filtering, because there may be no way for the user to
// actually re-launch them.
return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0
&& ps.getStopped(userId);
}
}
return false;
}
/**
* <em>WARNING</em> for performance reasons, the passed in intentList WILL BE
* MODIFIED. Do not pass in a list that should not be changed.
*/
private static <T> void getIntentListSubset(List<ActivityIntentInfo> intentList,
IterGenerator<T> generator, Iterator<T> searchIterator) {
// loop through the set of actions; every one must be found in the intent filter
while (searchIterator.hasNext()) {
// we must have at least one filter in the list to consider a match
if (intentList.size() == 0) {
break;
}
final T searchAction = searchIterator.next();
// loop through the set of intent filters
final Iterator<ActivityIntentInfo> intentIter = intentList.iterator();
while (intentIter.hasNext()) {
final ActivityIntentInfo intentInfo = intentIter.next();
boolean selectionFound = false;
// loop through the intent filter's selection criteria; at least one
// of them must match the searched criteria
final Iterator<T> intentSelectionIter = generator.generate(intentInfo);
while (intentSelectionIter != null && intentSelectionIter.hasNext()) {
final T intentSelection = intentSelectionIter.next();
if (intentSelection != null && intentSelection.equals(searchAction)) {
selectionFound = true;
break;
}
}
// the selection criteria wasn't found in this filter's set; this filter
// is not a potential match
if (!selectionFound) {
intentIter.remove();
}
}
}
}
private static boolean isProtectedAction(ActivityIntentInfo filter) {
final Iterator<String> actionsIter = filter.actionsIterator();
while (actionsIter != null && actionsIter.hasNext()) {
final String filterAction = actionsIter.next();
if (PROTECTED_ACTIONS.contains(filterAction)) {
return true;
}
}
return false;
}
private void addActivity(PackageParser.Activity a, String type,
List<PackageParser.ActivityIntentInfo> newIntents) {
mActivities.put(a.getComponentName(), a);
if (DEBUG_SHOW_INFO) {
final CharSequence label = a.info.nonLocalizedLabel != null
? a.info.nonLocalizedLabel
: a.info.name;
Log.v(TAG, " " + type + " " + label + ":");
}
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " Class=" + a.info.name);
}
final int intentsSize = a.intents.size();
for (int j = 0; j < intentsSize; j++) {
PackageParser.ActivityIntentInfo intent = a.intents.get(j);
if (newIntents != null && "activity".equals(type)) {
newIntents.add(intent);
}
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " IntentFilter:");
intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
}
if (!intent.debugCheck()) {
Log.w(TAG, "==> For Activity " + a.info.name);
}
addFilter(intent);
}
}
@Override
protected boolean allowFilterResult(
PackageParser.ActivityIntentInfo filter, List<ResolveInfo> dest) {
ActivityInfo filterAi = filter.activity.info;
for (int i = dest.size() - 1; i >= 0; --i) {
ActivityInfo destAi = dest.get(i).activityInfo;
if (destAi.name == filterAi.name && destAi.packageName == filterAi.packageName) {
return false;
}
}
return true;
}
private void log(String reason, ActivityIntentInfo info, int match,
int userId) {
Slog.w(TAG, reason
+ "; match: "
+ DebugUtils.flagsToString(IntentFilter.class, "MATCH_", match)
+ "; userId: " + userId
+ "; intent info: " + info);
}
@Override
protected void dumpFilter(PrintWriter out, String prefix,
PackageParser.ActivityIntentInfo filter) {
out.print(prefix);
out.print(Integer.toHexString(System.identityHashCode(filter.activity)));
out.print(' ');
filter.activity.printComponentShortName(out);
out.print(" filter ");
out.println(Integer.toHexString(System.identityHashCode(filter)));
}
/**
* Reprocess any protected filters that have been deferred. At this point, we've scanned
* all of the filters defined on the /system partition and know the special components.
*/
void fixProtectedFilterPriorities() {
if (!mDeferProtectedFilters) {
return;
}
mDeferProtectedFilters = false;
if (mProtectedFilters == null || mProtectedFilters.size() == 0) {
return;
}
final List<ActivityIntentInfo> protectedFilters = mProtectedFilters;
mProtectedFilters = null;
final String setupWizardPackage = sPackageManagerInternal.getKnownPackageName(
PACKAGE_SETUP_WIZARD, UserHandle.USER_SYSTEM);
if (DEBUG_FILTERS && setupWizardPackage == null) {
Slog.i(TAG, "No setup wizard;"
+ " All protected intents capped to priority 0");
}
for (int i = protectedFilters.size() - 1; i >= 0; --i) {
final ActivityIntentInfo filter = protectedFilters.get(i);
if (filter.activity.info.packageName.equals(setupWizardPackage)) {
if (DEBUG_FILTERS) {
Slog.i(TAG, "Found setup wizard;"
+ " allow priority " + filter.getPriority() + ";"
+ " package: " + filter.activity.info.packageName
+ " activity: " + filter.activity.className
+ " priority: " + filter.getPriority());
}
// skip setup wizard; allow it to keep the high priority filter
continue;
}
if (DEBUG_FILTERS) {
Slog.i(TAG, "Protected action; cap priority to 0;"
+ " package: " + filter.activity.info.packageName
+ " activity: " + filter.activity.className
+ " origPrio: " + filter.getPriority());
}
filter.setPriority(0);
}
}
@Override
protected ActivityIntentInfo[] newArray(int size) {
return new ActivityIntentInfo[size];
}
@Override
protected boolean isPackageForFilter(String packageName,
PackageParser.ActivityIntentInfo info) {
return packageName.equals(info.activity.owner.packageName);
}
@Override
protected Object filterToLabel(PackageParser.ActivityIntentInfo filter) {
return filter.activity;
}
public Iterator<E> generate(ActivityIntentInfo info) {
return null;
}
@Override
public Iterator<String> generate(ActivityIntentInfo info) {
return info.actionsIterator();
}
@Override
public Iterator<String> generate(ActivityIntentInfo info) {
return info.categoriesIterator();
}
@Override
public Iterator<String> generate(ActivityIntentInfo info) {
return info.schemesIterator();
}
@Override
public Iterator<IntentFilter.AuthorityEntry> generate(ActivityIntentInfo info) {
return info.authoritiesIterator();
}
/**
* 获取启动的Activity和Application的MetaData,Receiver intent等信息
*/
private void queryDefaultActivityAndSpecialInfo(PackageParser.Package pkg) {
// 2.2系统的解析没有赋值meta-data,所以自己取一下
if (pkg.mAppMetaData != null) {
packageInfo.applicationInfo.metaData = new Bundle(pkg.mAppMetaData);
}
for (int i = 0; i < pkg.activities.size(); i++) {
android.content.pm.PackageParser.Activity activity = pkg.activities.get(i);
ArrayList<android.content.pm.PackageParser.ActivityIntentInfo> intentFilters = activity.intents;
IntentInfo intentInfo = new IntentInfo();
intentInfo.type = "activity";
intentInfo.className = activity.className;
intentInfo.categories = new ArrayList<String>();
intentInfo.actions = new ArrayList<String>();
for (int j = 0; j < intentFilters.size(); j++) {
boolean hasAction = false;
boolean hasCategory = false;
for (int k = 0; k < intentFilters.get(j).countCategories(); k++) {
intentInfo.categories.add(intentFilters.get(j).getCategory(k));
if (intentFilters.get(j).getCategory(k)
.equalsIgnoreCase("android.intent.category.LAUNCHER")
&& pkg.activities.get(i).info.enabled) {
hasCategory = true;
}
}
for (int k = 0; k < intentFilters.get(j).countActions(); k++) {
intentInfo.actions.add(intentFilters.get(j).getAction(k));
if (intentFilters.get(j).getAction(k).equalsIgnoreCase("android.intent.action.MAIN")
&& pkg.activities.get(i).info.enabled) {
hasAction = true;
}
}
if (hasAction && hasCategory) {
android.content.pm.PackageParser.ActivityIntentInfo activityInfo = intentFilters.get(j);
defaultActivityName = activityInfo.activity.className;
}
}
mIntentInfos.add(intentInfo);
}
// 有receiver,处理一下receiver
if (pkg.receivers != null && pkg.receivers.size() > 0) {
mRecvIntentFilters = new HashMap<String, ArrayList<ActivityIntentInfo>>();
for (Activity receiver : pkg.receivers) {
mRecvIntentFilters.put(receiver.className, (ArrayList<ActivityIntentInfo>) receiver.intents);
}
}
}
@Override
public Map<String, ArrayList<ActivityIntentInfo>> getRecvIntentFilters() {
return mRecvIntentFilters;
}
/**
* getRecvIntentFilters
*
* @return Map<String, ArrayList<ActivityIntentInfo>>
*/
Map<String, ArrayList<ActivityIntentInfo>> getRecvIntentFilters();