void generateServicesMap() { Iterator i$; PackageManager pm = this.mContext.getPackageManager(); ArrayList<ServiceInfo<AuthenticatorDescription>> serviceInfos = new ArrayList(); for (ResolveInfo resolveInfo : pm.queryIntentServices(new Intent(this.mInterfaceName), 128)) { ServiceInfo<AuthenticatorDescription> info; try { info = parseServiceInfo(resolveInfo); if (info == null) { Log.w(TAG, "Unable to load service info " + resolveInfo.toString()); } else { serviceInfos.add(info); } } catch (XmlPullParserException e) { Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e); } catch (IOException e2) { Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e2); } } synchronized (this.mServicesLock) { this.mServices = Maps.newHashMap(); i$ = serviceInfos.iterator(); while (i$.hasNext()) { info = (ServiceInfo) i$.next(); this.mServices.put(info.type, info); } } }
/** * Indicates whether the specified action can be used as an intent. This method queries the * package manager for installed packages that can respond to an intent with the specified action. * If no suitable package is found, this method returns false. * * @param context The application's environment. * @param action The Intent action to check for availability. * @return True if an Intent with the specified action can be sent and responded to, false * otherwise. */ public static boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> list = packageManager.queryIntentServices(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }
Intent buildServiceIntent() { Intent result = null; if (verify.isChecked()) { PackageManager mgr = getPackageManager(); for (ResolveInfo info : mgr.queryIntentServices(INTENT_SERVICE, 0)) { try { if (validate(info.serviceInfo.packageName, R.raw.valid_signature)) { result = new Intent(INTENT_SERVICE); result.setComponent( new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name)); break; } } catch (Exception e) { Log.e(getClass().getSimpleName(), "Exception finding valid service", e); } } } else { result = INTENT_SERVICE; } return (result); }
private static ComponentName findRecognizerByPackage( final Context context, final String prefPackage) { final PackageManager pm = context.getPackageManager(); final List<ResolveInfo> available = pm != null ? pm.queryIntentServices(new Intent(RecognitionService.SERVICE_INTERFACE), 0) : new LinkedList<ResolveInfo>(); final int numAvailable = available.size(); if (numAvailable == 0) { // no available voice recognition services found return null; } else { if (prefPackage != null) { for (final ResolveInfo anAvailable : available) { final ServiceInfo serviceInfo = anAvailable.serviceInfo; if (serviceInfo != null && prefPackage.equals(serviceInfo.packageName)) { return new ComponentName(serviceInfo.packageName, serviceInfo.name); } } } // Do not pick up first available, but use default one return null; } }
/** * Queries the {@link android.content.pm.PackageManager} for any installed {@link * com.battlelancer.seriesguide.api.SeriesGuideExtension} extensions. Their info is extracted into * {@link com.battlelancer.seriesguide.extensions.ExtensionManager.Extension} objects. */ public List<Extension> queryAllAvailableExtensions() { Intent queryIntent = new Intent(SeriesGuideExtension.ACTION_SERIESGUIDE_EXTENSION); PackageManager pm = mContext.getPackageManager(); List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, PackageManager.GET_META_DATA); List<Extension> extensions = new ArrayList<>(); for (ResolveInfo info : resolveInfos) { Extension extension = new Extension(); // get label, icon and component name extension.label = info.loadLabel(pm).toString(); extension.icon = info.loadIcon(pm); extension.componentName = new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name); // get description Context packageContext; try { packageContext = mContext.createPackageContext(extension.componentName.getPackageName(), 0); Resources packageRes = packageContext.getResources(); extension.description = packageRes.getString(info.serviceInfo.descriptionRes); } catch (SecurityException | PackageManager.NameNotFoundException | Resources.NotFoundException e) { Timber.e(e, "Reading description for extension " + extension.componentName + " failed"); extension.description = ""; } // get (optional) settings activity Bundle metaData = info.serviceInfo.metaData; if (metaData != null) { String settingsActivity = metaData.getString("settingsActivity"); if (!TextUtils.isEmpty(settingsActivity)) { extension.settingsActivity = ComponentName.unflattenFromString( info.serviceInfo.packageName + "/" + settingsActivity); } } Timber.d( "queryAllAvailableExtensions: found extension " + extension.label + " " + extension.componentName); extensions.add(extension); } return extensions; }
@Override public void onReceive(Context context, Intent intent) { Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); queryIntent.setPackage(context.getPackageName()); PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, 0); if (resolveInfos.size() != 1) { throw new IllegalStateException( "Expected 1 Service that handles " + Intent.ACTION_MEDIA_BUTTON + ", found " + resolveInfos.size()); } ResolveInfo resolveInfo = resolveInfos.get(0); ComponentName componentName = new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); intent.setComponent(componentName); context.startService(intent); }
/** Service 将隐式启动转换为显示启动 */ public static Intent getExplicitIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; }
/** * Returns an explicit Intent for a service that accepts the given Intent or null if no such * service was found. * * @param context The application's environment. * @param action The Intent action to check for availability. * @return The explicit service Intent or null if no service was found. */ public static Intent getPrestoServiceIntent(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent actionIntent = new Intent(action); List<ResolveInfo> list = packageManager.queryIntentServices(actionIntent, PackageManager.MATCH_DEFAULT_ONLY); if (list.size() > 0) { ResolveInfo first = list.get(0); if (first.serviceInfo != null) { Intent intent = new Intent(); intent.setComponent( new ComponentName(first.serviceInfo.packageName, first.serviceInfo.name)); Log.i(TAG, "Returning intent:" + intent.toString()); return intent; } else { Log.e(TAG, "Found service that accepts " + action + ", but serviceInfo was null"); return null; } } else { return null; } }
public static Intent updateIntentExplicitness(Context context, Intent implicitIntent) { if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { // API 21 creates the need for explicit Intents, // so make sure only one can answer PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); if (resolveInfo == null) { return null; } ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); Intent explicitIntent = new Intent(implicitIntent); explicitIntent.setComponent(component); return explicitIntent; } else { return implicitIntent; } }
public static Intent getExplicitIapIntent(String actionString, Context context) { PackageManager pm = context.getPackageManager(); Intent implicitIntent = new Intent(actionString); List<ResolveInfo> resolveInfos = pm.queryIntentServices(implicitIntent, 0); // Is somebody else trying to intercept our IAP call? if (resolveInfos == null || resolveInfos.size() == 0) { return null; } for (ResolveInfo resolveInfo : resolveInfos) { String packageName = resolveInfo.serviceInfo.packageName; if (packageName.equals(context.getApplicationInfo().packageName)) { String className = resolveInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); Intent iapIntent = new Intent(); iapIntent.setComponent(component); return iapIntent; } } return null; }
String[] getPluginDirectories() { ArrayList<String> directories = new ArrayList<String>(); PackageManager pm = mContext.getPackageManager(); List<ResolveInfo> plugins = pm.queryIntentServices( new Intent(PLUGIN_ACTION), PackageManager.GET_SERVICES | PackageManager.GET_META_DATA); synchronized (mPackageInfoCache) { // clear the list of existing packageInfo objects mPackageInfoCache.clear(); for (ResolveInfo info : plugins) { // retrieve the plugin's service information ServiceInfo serviceInfo = info.serviceInfo; if (serviceInfo == null) { Log.w(LOGTAG, "Ignore bad plugin"); continue; } // retrieve information from the plugin's manifest PackageInfo pkgInfo; try { pkgInfo = pm.getPackageInfo( serviceInfo.packageName, PackageManager.GET_PERMISSIONS | PackageManager.GET_SIGNATURES); } catch (NameNotFoundException e) { Log.w(LOGTAG, "Can't find plugin: " + serviceInfo.packageName); continue; } if (pkgInfo == null) { continue; } /* * find the location of the plugin's shared library. The default * is to assume the app is either a user installed app or an * updated system app. In both of these cases the library is * stored in the app's data directory. */ String directory = pkgInfo.applicationInfo.dataDir + "/lib"; final int appFlags = pkgInfo.applicationInfo.flags; final int updatedSystemFlags = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP; // preloaded system app with no user updates if ((appFlags & updatedSystemFlags) == ApplicationInfo.FLAG_SYSTEM) { directory = PLUGIN_SYSTEM_LIB + pkgInfo.packageName; } // check if the plugin has the required permissions and // signatures if (!containsPluginPermissionAndSignatures(pkgInfo)) { continue; } // determine the type of plugin from the manifest if (serviceInfo.metaData == null) { Log.e(LOGTAG, "The plugin '" + serviceInfo.name + "' has no type defined"); continue; } String pluginType = serviceInfo.metaData.getString(PLUGIN_TYPE); if (!TYPE_NATIVE.equals(pluginType)) { Log.e(LOGTAG, "Unrecognized plugin type: " + pluginType); continue; } try { Class<?> cls = getPluginClass(serviceInfo.packageName, serviceInfo.name); // TODO implement any requirements of the plugin class here! boolean classFound = true; if (!classFound) { Log.e( LOGTAG, "The plugin's class' " + serviceInfo.name + "' does not extend the appropriate class."); continue; } } catch (NameNotFoundException e) { Log.e(LOGTAG, "Can't find plugin: " + serviceInfo.packageName); continue; } catch (ClassNotFoundException e) { Log.e(LOGTAG, "Can't find plugin's class: " + serviceInfo.name); continue; } // if all checks have passed then make the plugin available mPackageInfoCache.add(pkgInfo); directories.add(directory); } } return directories.toArray(new String[directories.size()]); }