下面列出了android.content.pm.PackageParser#Service() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@GuardedBy("mLock")
private void addServicesLocked(PackageParser.Package pkg, boolean chatty) {
final int servicesSize = pkg.services.size();
StringBuilder r = null;
for (int i = 0; i < servicesSize; i++) {
PackageParser.Service s = pkg.services.get(i);
s.info.processName = fixProcessName(pkg.applicationInfo.processName,
s.info.processName);
mServices.addService(s);
if (DEBUG_PACKAGE_SCANNING && chatty) {
if (r == null) {
r = new StringBuilder(256);
} else {
r.append(' ');
}
r.append(s.info.name);
}
}
if (DEBUG_PACKAGE_SCANNING && chatty) {
Log.d(TAG, " Services: " + (r == null ? "<NONE>" : r));
}
}
List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType,
int flags, List<PackageParser.Service> packageServices, int userId) {
if (!sUserManager.exists(userId)) return null;
if (packageServices == null) {
return null;
}
mFlags = flags;
final boolean defaultOnly = (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0;
final int servicesSize = packageServices.size();
ArrayList<PackageParser.ServiceIntentInfo[]> listCut = new ArrayList<>(servicesSize);
ArrayList<PackageParser.ServiceIntentInfo> intentFilters;
for (int i = 0; i < servicesSize; ++i) {
intentFilters = packageServices.get(i).intents;
if (intentFilters != null && intentFilters.size() > 0) {
PackageParser.ServiceIntentInfo[] array =
new PackageParser.ServiceIntentInfo[intentFilters.size()];
intentFilters.toArray(array);
listCut.add(array);
}
}
return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut, userId);
}
void addService(PackageParser.Service s) {
mServices.put(s.getComponentName(), s);
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " "
+ (s.info.nonLocalizedLabel != null
? s.info.nonLocalizedLabel : s.info.name) + ":");
Log.v(TAG, " Class=" + s.info.name);
}
final int intentsSize = s.intents.size();
int j;
for (j = 0; j < intentsSize; j++) {
PackageParser.ServiceIntentInfo intent = s.intents.get(j);
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " IntentFilter:");
intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
}
if (!intent.debugCheck()) {
Log.w(TAG, "==> For Service " + s.info.name);
}
addFilter(intent);
}
}
void removeService(PackageParser.Service s) {
mServices.remove(s.getComponentName());
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " " + (s.info.nonLocalizedLabel != null
? s.info.nonLocalizedLabel : s.info.name) + ":");
Log.v(TAG, " Class=" + s.info.name);
}
final int intentsSize = s.intents.size();
int j;
for (j = 0; j < intentsSize; j++) {
PackageParser.ServiceIntentInfo intent = s.intents.get(j);
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " IntentFilter:");
intent.dump(new LogPrinter(Log.VERBOSE, TAG), " ");
}
removeFilter(intent);
}
}
public List<ResolveInfo> queryIntentForPackage(Intent intent, String resolvedType, int flags,
ArrayList<PackageParser.Service> packageServices) {
if (packageServices == null) {
return null;
}
mFlags = flags;
final boolean defaultOnly = (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0;
final int N = packageServices.size();
ArrayList<PackageParser.ServiceIntentInfo[]> listCut = new ArrayList<PackageParser.ServiceIntentInfo[]>(N);
ArrayList<PackageParser.ServiceIntentInfo> intentFilters;
for (int i = 0; i < N; ++i) {
intentFilters = packageServices.get(i).intents;
if (intentFilters != null && intentFilters.size() > 0) {
PackageParser.ServiceIntentInfo[] array = new PackageParser.ServiceIntentInfo[intentFilters.size()];
intentFilters.toArray(array);
listCut.add(array);
}
}
return super.queryIntentFromList(intent, resolvedType, defaultOnly, listCut);
}
@Override
protected ResolveInfo newResult(PackageParser.ServiceIntentInfo filter, int match) {
final PackageParser.Service service = filter.service;
ServiceInfo si = PackageParserCompat.generateServiceInfo(service, mFlags);
if (si == null) {
return null;
}
final ResolveInfo res = new ResolveInfo();
res.serviceInfo = si;
if ((mFlags & PackageManager.GET_RESOLVED_FILTER) != 0) {
res.filter = filter;
}
res.priority = filter.getPriority();
res.preferredOrder = service.owner.mPreferredOrder;
res.match = match;
res.isDefault = filter.hasDefault;
res.labelRes = filter.labelRes;
res.nonLocalizedLabel = filter.nonLocalizedLabel;
res.icon = filter.icon;
return res;
}
public List queryIntentForPackage(Intent intent, String resolvedType,
int flags, ArrayList<PackageParser.Service> packageServices) {
if (packageServices == null) {
return null;
}
mFlags = flags;
final boolean defaultOnly = (flags & PackageManager.MATCH_DEFAULT_ONLY) != 0;
int N = packageServices.size();
ArrayList<ArrayList<PackageParser.ServiceIntentInfo>> listCut = new ArrayList<ArrayList<PackageParser.ServiceIntentInfo>>(
N);
ArrayList<PackageParser.ServiceIntentInfo> intentFilters;
for (int i = 0; i < N; ++i) {
intentFilters = packageServices.get(i).intents;
if (intentFilters != null && intentFilters.size() > 0) {
listCut.add(intentFilters);
}
}
return super.queryIntentFromList(intent, resolvedType, defaultOnly,
listCut);
}
protected void dumpFilterLabel(PrintWriter out, String prefix, Object label, int count) {
final PackageParser.Service service = (PackageParser.Service) label;
out.print(prefix);
out.print(Integer.toHexString(System.identityHashCode(service)));
out.print(' ');
service.printComponentShortName(out);
if (count > 1) {
out.print(" ("); out.print(count); out.print(" filters)");
}
out.println();
}
@Override
public ServiceInfo getServiceInfo(ComponentName component, int flags, int userId) {
checkUserId(userId);
synchronized (mPackages) {
PackageParser.Service s = mServices.mServices.get(component);
if (s != null) {
ServiceInfo serviceInfo = PackageParserCompat.generateServiceInfo(s, flags);
PackageParser.Package p = mPackages.get(serviceInfo.packageName);
AppSetting settings = (AppSetting) p.mExtras;
ComponentFixer.fixComponentInfo(settings, serviceInfo, userId);
return serviceInfo;
}
}
return null;
}
public final void addService(PackageParser.Service s) {
mServices.put(s.getComponentName(), s);
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " " + (s.info.nonLocalizedLabel != null ? s.info.nonLocalizedLabel : s.info.name) + ":");
Log.v(TAG, " Class=" + s.info.name);
}
final int NI = s.intents.size();
int j;
for (j = 0; j < NI; j++) {
PackageParser.ServiceIntentInfo intent = s.intents.get(j);
addFilter(intent);
}
}
public final void removeService(PackageParser.Service s) {
mServices.remove(s.getComponentName());
if (DEBUG_SHOW_INFO) {
Log.v(TAG, " " + (s.info.nonLocalizedLabel != null ? s.info.nonLocalizedLabel : s.info.name) + ":");
Log.v(TAG, " Class=" + s.info.name);
}
final int NI = s.intents.size();
int j;
for (j = 0; j < NI; j++) {
PackageParser.ServiceIntentInfo intent = s.intents.get(j);
removeFilter(intent);
}
}
public final void addService(PackageParser.Service s) {
mServices.put(s.getComponentName(), s);
int NI = s.intents.size();
for (int j = 0; j < NI; j++) {
PackageParser.ServiceIntentInfo intent = s.intents.get(j);
addFilter(intent);
}
}
public final void removeService(PackageParser.Service s) {
mServices.remove(s.getComponentName());
int NI = s.intents.size();
for (int j = 0; j < NI; j++) {
PackageParser.ServiceIntentInfo intent = s.intents.get(j);
removeFilter(intent);
}
}
@Override
protected boolean allowFilterResult(
PackageParser.ServiceIntentInfo filter, List<PackageParser.Service> dest) {
for (int i = dest.size() - 1; i >= 0; i--) {
PackageParser.Service destAi = dest.get(i);
if (destAi == filter.service) {
return false;
}
}
return true;
}
/** Returns the given service */
PackageParser.Service getService(ComponentName component) {
synchronized (mLock) {
return mServices.mServices.get(component);
}
}
List<ResolveInfo> queryServices(Intent intent, String resolvedType, int flags,
List<PackageParser.Service> services, int userId) {
synchronized (mLock) {
return mServices.queryIntentForPackage(intent, resolvedType, flags, services, userId);
}
}
public LoadedPlugin(PluginManager pluginManager, Context context, File apk) throws Exception {
this.mPluginManager = pluginManager;
this.mHostContext = context;
this.mLocation = apk.getAbsolutePath();
this.mPackage = PackageParserCompat.parsePackage(context, apk, PackageParser.PARSE_MUST_BE_APK);
this.mPackage.applicationInfo.metaData = this.mPackage.mAppMetaData;
this.mPackageInfo = new PackageInfo();
this.mPackageInfo.applicationInfo = this.mPackage.applicationInfo;
this.mPackageInfo.applicationInfo.sourceDir = apk.getAbsolutePath();
if (Build.VERSION.SDK_INT >= 28
|| (Build.VERSION.SDK_INT == 27 && Build.VERSION.PREVIEW_SDK_INT != 0)) { // Android P Preview
try {
this.mPackageInfo.signatures = this.mPackage.mSigningDetails.signatures;
} catch (Throwable e) {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
this.mPackageInfo.signatures = info.signatures;
}
} else {
this.mPackageInfo.signatures = this.mPackage.mSignatures;
}
this.mPackageInfo.packageName = this.mPackage.packageName;
if (pluginManager.getLoadedPlugin(mPackageInfo.packageName) != null) {
throw new RuntimeException("plugin has already been loaded : " + mPackageInfo.packageName);
}
this.mPackageInfo.versionCode = this.mPackage.mVersionCode;
this.mPackageInfo.versionName = this.mPackage.mVersionName;
this.mPackageInfo.permissions = new PermissionInfo[0];
this.mPackageManager = createPluginPackageManager();
this.mPluginContext = createPluginContext(null);
this.mNativeLibDir = getDir(context, Constants.NATIVE_DIR);
this.mPackage.applicationInfo.nativeLibraryDir = this.mNativeLibDir.getAbsolutePath();
this.mResources = createResources(context, getPackageName(), apk);
this.mClassLoader = createClassLoader(context, apk, this.mNativeLibDir, context.getClassLoader());
tryToCopyNativeLib(apk);
// Cache instrumentations
Map<ComponentName, InstrumentationInfo> instrumentations = new HashMap<ComponentName, InstrumentationInfo>();
for (PackageParser.Instrumentation instrumentation : this.mPackage.instrumentation) {
instrumentations.put(instrumentation.getComponentName(), instrumentation.info);
}
this.mInstrumentationInfos = Collections.unmodifiableMap(instrumentations);
this.mPackageInfo.instrumentation = instrumentations.values().toArray(new InstrumentationInfo[instrumentations.size()]);
// Cache activities
Map<ComponentName, ActivityInfo> activityInfos = new HashMap<ComponentName, ActivityInfo>();
for (PackageParser.Activity activity : this.mPackage.activities) {
activity.info.metaData = activity.metaData;
activityInfos.put(activity.getComponentName(), activity.info);
}
this.mActivityInfos = Collections.unmodifiableMap(activityInfos);
this.mPackageInfo.activities = activityInfos.values().toArray(new ActivityInfo[activityInfos.size()]);
// Cache services
Map<ComponentName, ServiceInfo> serviceInfos = new HashMap<ComponentName, ServiceInfo>();
for (PackageParser.Service service : this.mPackage.services) {
serviceInfos.put(service.getComponentName(), service.info);
}
this.mServiceInfos = Collections.unmodifiableMap(serviceInfos);
this.mPackageInfo.services = serviceInfos.values().toArray(new ServiceInfo[serviceInfos.size()]);
// Cache providers
Map<String, ProviderInfo> providers = new HashMap<String, ProviderInfo>();
Map<ComponentName, ProviderInfo> providerInfos = new HashMap<ComponentName, ProviderInfo>();
for (PackageParser.Provider provider : this.mPackage.providers) {
providers.put(provider.info.authority, provider.info);
providerInfos.put(provider.getComponentName(), provider.info);
}
this.mProviders = Collections.unmodifiableMap(providers);
this.mProviderInfos = Collections.unmodifiableMap(providerInfos);
this.mPackageInfo.providers = providerInfos.values().toArray(new ProviderInfo[providerInfos.size()]);
// Register broadcast receivers dynamically
Map<ComponentName, ActivityInfo> receivers = new HashMap<ComponentName, ActivityInfo>();
for (PackageParser.Activity receiver : this.mPackage.receivers) {
receivers.put(receiver.getComponentName(), receiver.info);
BroadcastReceiver br = BroadcastReceiver.class.cast(getClassLoader().loadClass(receiver.getComponentName().getClassName()).newInstance());
for (PackageParser.ActivityIntentInfo aii : receiver.intents) {
this.mHostContext.registerReceiver(br, aii);
}
}
this.mReceiverInfos = Collections.unmodifiableMap(receivers);
this.mPackageInfo.receivers = receivers.values().toArray(new ActivityInfo[receivers.size()]);
// try to invoke plugin's application
invokeApplication();
}
@Override
protected PackageParser.Service newResult(PackageParser.ServiceIntentInfo info,
int match) {
return info.service;
}
@Override
protected void sortResults(List<PackageParser.Service> results) {
}
/**
* 解析APK的manifest
*
* @param info
* 插件信息
*/
private void getPackageInfo(PluginInfo info) {
int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_CONFIGURATIONS
| PackageManager.GET_INSTRUMENTATION | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
| PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES | PackageManager.GET_SIGNATURES;
// 需要获取Package对象,主要是处理隐式启动插件中的activity
PackageParser parser = new PackageParser(info.apkPath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
File sourceFile = new File(info.apkPath);
PackageParser.Package pack = parser.parsePackage(sourceFile, info.apkPath, metrics, 0);
// 因为PackagePaser的generatePackageInfo方法不同版本参数相差太多,所以还是用packagemanager的api
// 但这样导致APK被解析了两次,上面获取Package是一次
PackageInfo packageInfo = mContext.getPackageManager().getPackageArchiveInfo(info.apkPath, flags);
info.packageName = packageInfo.packageName;
info.mPackageObj = pack;
info.mPackageInfo = packageInfo;
ArrayList<PackageParser.Activity> activitys = pack.activities;
int size = activitys.size();
for (int i = 0; i < size; i++) {
mActivitys.addActivity(activitys.get(i));
}
ArrayList<PackageParser.Service> services = pack.services;
size = services.size();
for (int i = 0; i < size; i++) {
mServices.addService(services.get(i));
}
ArrayList<PackageParser.Provider> providers = pack.providers;
size = providers.size();
for (int i = 0; i < size; i++) {
Provider p = providers.get(i);
String names[] = PATTERN_SEMICOLON.split(p.info.authority);
for (int j = 0; j < names.length; j++) {
mProviderInfoMap.put(names[i], p);
}
}
}