Example usage for android.content.pm PackageManager queryIntentServices

List of usage examples for android.content.pm PackageManager queryIntentServices

Introduction

In this page you can find the example usage for android.content.pm PackageManager queryIntentServices.

Prototype

public abstract List<ResolveInfo> queryIntentServices(Intent intent, @ResolveInfoFlags int flags);

Source Link

Document

Retrieve all services that can match the given intent.

Usage

From source file:androidx.media.SessionToken2.java

private static String getSessionIdFromService(PackageManager manager, String serviceInterface,
        ComponentName serviceComponent) {
    Intent serviceIntent = new Intent(serviceInterface);
    // Use queryIntentServices to find services with MediaLibraryService2.SERVICE_INTERFACE.
    // We cannot use resolveService with intent specified class name, because resolveService
    // ignores actions if Intent.setClassName() is specified.
    serviceIntent.setPackage(serviceComponent.getPackageName());

    List<ResolveInfo> list = manager.queryIntentServices(serviceIntent, PackageManager.GET_META_DATA);
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            ResolveInfo resolveInfo = list.get(i);
            if (resolveInfo == null || resolveInfo.serviceInfo == null) {
                continue;
            }/*w ww.  j av  a2s. co  m*/
            if (TextUtils.equals(resolveInfo.serviceInfo.name, serviceComponent.getClassName())) {
                return getSessionId(resolveInfo);
            }
        }
    }
    return null;
}

From source file:com.androidzeitgeist.dashwatch.muzei.SourceManager.java

public List<SourceListing> getAvailableSources() {
    List<SourceListing> sources = new ArrayList<SourceListing>();

    PackageManager pm = mApplicationContext.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(MuzeiArtSource.ACTION_MUZEI_ART_SOURCE),
            PackageManager.GET_META_DATA);

    for (ResolveInfo resolveInfo : resolveInfos) {
        SourceListing listing = new SourceListing();

        listing.componentName = new ComponentName(resolveInfo.serviceInfo.packageName,
                resolveInfo.serviceInfo.name);
        listing.title = resolveInfo.loadLabel(pm).toString();

        sources.add(listing);/* w  ww  . ja v a 2 s .c  o m*/
    }

    return sources;
}

From source file:org.opensilk.music.ui2.loader.PluginLoader.java

public List<PluginInfo> getPluginInfos(boolean wantIcon) {
    final List<ComponentName> disabledPlugins = readDisabledPlugins();
    final PackageManager pm = context.getPackageManager();
    final List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(API_PLUGIN_SERVICE),
            PackageManager.GET_META_DATA);
    final List<PluginInfo> pluginInfos = new ArrayList<PluginInfo>(resolveInfos.size() + 1);
    for (final ResolveInfo resolveInfo : resolveInfos) {
        if (resolveInfo.serviceInfo == null)
            continue;
        final PluginInfo pi = readResolveInfo(pm, disabledPlugins, resolveInfo);
        if (!wantIcon)
            pi.icon = null;//from  w ww. j  av  a  2 s.  c  o m
        pluginInfos.add(pi);
    }
    Collections.sort(pluginInfos);
    // folders is special and is always first
    List<ResolveInfo> folderResolveInfos = pm
            .queryIntentServices(new Intent(context, FolderLibraryService.class), PackageManager.GET_META_DATA);
    if (folderResolveInfos != null && folderResolveInfos.size() > 0) {
        final ResolveInfo resolveInfo = folderResolveInfos.get(0);
        if (resolveInfo.serviceInfo != null) {
            final PluginInfo pi = readResolveInfo(pm, disabledPlugins, resolveInfo);
            if (!wantIcon)
                pi.icon = null;
            pluginInfos.add(0, pi);
        }
    }
    return pluginInfos;
}

From source file:androidx.media.MediaSession2ImplBase.java

private static String getServiceName(Context context, String serviceAction, String id) {
    PackageManager manager = context.getPackageManager();
    Intent serviceIntent = new Intent(serviceAction);
    serviceIntent.setPackage(context.getPackageName());
    List<ResolveInfo> services = manager.queryIntentServices(serviceIntent, PackageManager.GET_META_DATA);
    String serviceName = null;/*  ww  w . j a  v  a2s.  c om*/
    if (services != null) {
        for (int i = 0; i < services.size(); i++) {
            String serviceId = SessionToken2.getSessionId(services.get(i));
            if (serviceId != null && TextUtils.equals(id, serviceId)) {
                if (services.get(i).serviceInfo == null) {
                    continue;
                }
                if (serviceName != null) {
                    throw new IllegalArgumentException(
                            "Ambiguous session type. Multiple" + " session services define the same id=" + id);
                }
                serviceName = services.get(i).serviceInfo.name;
            }
        }
    }
    return serviceName;
}

From source file:com.battlelancer.seriesguide.extensions.ExtensionManager.java

/**
 * 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.
 *//* w w w. j  a  v  a 2s.  co m*/
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;
}

From source file:com.androidzeitgeist.dashwatch.dashclock.ExtensionManager.java

/**
 * Returns a listing of all available (installed) extensions.
 *///from  w  w  w .j a  va  2 s .  c o m
public List<ExtensionListing> getAvailableExtensions() {
    List<ExtensionListing> availableExtensions = new ArrayList<ExtensionListing>();
    PackageManager pm = mApplicationContext.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(DashClockExtension.ACTION_EXTENSION),
            PackageManager.GET_META_DATA);
    for (ResolveInfo resolveInfo : resolveInfos) {
        ExtensionListing listing = new ExtensionListing();
        listing.componentName = new ComponentName(resolveInfo.serviceInfo.packageName,
                resolveInfo.serviceInfo.name);
        listing.title = resolveInfo.loadLabel(pm).toString();
        Bundle metaData = resolveInfo.serviceInfo.metaData;
        if (metaData != null) {
            listing.protocolVersion = metaData.getInt("protocolVersion");
            listing.worldReadable = metaData.getBoolean("worldReadable", false);
            listing.description = metaData.getString("description");
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                listing.settingsActivity = ComponentName
                        .unflattenFromString(resolveInfo.serviceInfo.packageName + "/" + settingsActivity);
            }
        }

        listing.icon = resolveInfo.loadIcon(pm);
        availableExtensions.add(listing);
    }

    return availableExtensions;
}

From source file:com.google.android.apps.dashclock.ExtensionManager.java

/**
 * Returns a listing of all available (installed) extensions, including those that aren't
 * world-readable./*from  w  ww.ja va  2s.  com*/
 */
public List<ExtensionListing> getAvailableExtensions() {
    List<ExtensionListing> availableExtensions = new ArrayList<>();
    PackageManager pm = mApplicationContext.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(DashClockExtension.ACTION_EXTENSION),
            PackageManager.GET_META_DATA);
    for (ResolveInfo resolveInfo : resolveInfos) {
        ExtensionListing info = new ExtensionListing();
        info.componentName(
                new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name));
        info.title(resolveInfo.loadLabel(pm).toString());
        Bundle metaData = resolveInfo.serviceInfo.metaData;
        if (metaData != null) {
            info.compatible(ExtensionHost.supportsProtocolVersion(metaData.getInt("protocolVersion")));
            info.worldReadable(metaData.getBoolean("worldReadable", false));
            info.description(metaData.getString("description"));
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                info.settingsActivity(ComponentName
                        .unflattenFromString(resolveInfo.serviceInfo.packageName + "/" + settingsActivity));
            }
        }

        info.icon(resolveInfo.getIconResource());
        availableExtensions.add(info);
    }

    return availableExtensions;
}

From source file:com.github.michalbednarski.intentslab.editor.ComponentPickerDialogFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);// www  . j a  v a2 s  . c  om

    // Get edited intent
    IntentEditorActivity intentEditor = (IntentEditorActivity) getActivity();
    Intent intent = new Intent(intentEditor.getEditedIntent());
    intent.setComponent(null);

    // Get components
    PackageManager pm = intentEditor.getPackageManager();
    List<ResolveInfo> ri = null;

    switch (intentEditor.getComponentType()) {
    case IntentEditorConstants.ACTIVITY:
        ri = pm.queryIntentActivities(intent, PackageManager.GET_DISABLED_COMPONENTS);
        break;
    case IntentEditorConstants.BROADCAST:
        ri = pm.queryBroadcastReceivers(intent, PackageManager.GET_DISABLED_COMPONENTS);
        break;
    case IntentEditorConstants.SERVICE:
        ri = pm.queryIntentServices(intent, PackageManager.GET_DISABLED_COMPONENTS);
        break;
    }

    // Cancel if no components
    if (ri.isEmpty()) {
        Toast.makeText(getActivity(), R.string.no_matching_components_found, Toast.LENGTH_SHORT).show();
        dismiss();
        return;
    }

    // Split enabled and disabled choices
    ArrayList<ResolveInfo> choices = new ArrayList<ResolveInfo>();
    ArrayList<ResolveInfo> disabledChoices = new ArrayList<ResolveInfo>();
    for (ResolveInfo resolveInfo : ri) {
        (isComponentEnabled(pm, resolveInfo) ? choices : disabledChoices).add(resolveInfo);
    }

    mEnabledChoicesCount = choices.size();
    choices.addAll(disabledChoices);
    mChoices = choices.toArray(new ResolveInfo[choices.size()]);
}

From source file:com.google.android.apps.muzei.settings.SettingsChooseSourceFragment.java

public void updateSources() {
    mSelectedSource = null;//  w  ww  .j  a  v a2  s  . co  m
    Intent queryIntent = new Intent(ACTION_MUZEI_ART_SOURCE);
    PackageManager pm = getActivity().getPackageManager();
    mSources.clear();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, PackageManager.GET_META_DATA);

    for (ResolveInfo ri : resolveInfos) {
        Source source = new Source();
        source.label = ri.loadLabel(pm).toString();
        source.icon = new BitmapDrawable(getResources(), generateSourceImage(ri.loadIcon(pm)));
        source.componentName = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
        if (ri.serviceInfo.descriptionRes != 0) {
            try {
                Context packageContext = getActivity()
                        .createPackageContext(source.componentName.getPackageName(), 0);
                Resources packageRes = packageContext.getResources();
                source.description = packageRes.getString(ri.serviceInfo.descriptionRes);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Can't read package resources for source " + source.componentName);
            }
        }
        Bundle metaData = ri.serviceInfo.metaData;
        source.color = Color.WHITE;
        if (metaData != null) {
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                source.settingsActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + settingsActivity);
            }

            String setupActivity = metaData.getString("setupActivity");
            if (!TextUtils.isEmpty(setupActivity)) {
                source.setupActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + setupActivity);
            }

            source.color = metaData.getInt("color", source.color);

            try {
                float[] hsv = new float[3];
                Color.colorToHSV(source.color, hsv);
                boolean adjust = false;
                if (hsv[2] < 0.8f) {
                    hsv[2] = 0.8f;
                    adjust = true;
                }
                if (hsv[1] > 0.4f) {
                    hsv[1] = 0.4f;
                    adjust = true;
                }
                if (adjust) {
                    source.color = Color.HSVToColor(hsv);
                }
                if (Color.alpha(source.color) != 255) {
                    source.color = Color.argb(255, Color.red(source.color), Color.green(source.color),
                            Color.blue(source.color));
                }
            } catch (IllegalArgumentException ignored) {
            }
        }

        mSources.add(source);
    }

    final String appPackage = getActivity().getPackageName();
    Collections.sort(mSources, new Comparator<Source>() {
        @Override
        public int compare(Source s1, Source s2) {
            String pn1 = s1.componentName.getPackageName();
            String pn2 = s2.componentName.getPackageName();
            if (!pn1.equals(pn2)) {
                if (appPackage.equals(pn1)) {
                    return -1;
                } else if (appPackage.equals(pn2)) {
                    return 1;
                }
            }
            return s1.label.compareTo(s2.label);
        }
    });

    redrawSources();
}

From source file:com.google.android.apps.muzei.settings.ChooseSourceFragment.java

public void updateSources() {
    mSelectedSource = null;/*from  w w  w.j a  va  2  s . co m*/
    Intent queryIntent = new Intent(ACTION_MUZEI_ART_SOURCE);
    PackageManager pm = getContext().getPackageManager();
    mSources.clear();
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, PackageManager.GET_META_DATA);

    for (ResolveInfo ri : resolveInfos) {
        Source source = new Source();
        source.label = ri.loadLabel(pm).toString();
        source.icon = new BitmapDrawable(getResources(), generateSourceImage(ri.loadIcon(pm)));
        source.targetSdkVersion = ri.serviceInfo.applicationInfo.targetSdkVersion;
        source.componentName = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
        if (ri.serviceInfo.descriptionRes != 0) {
            try {
                Context packageContext = getContext()
                        .createPackageContext(source.componentName.getPackageName(), 0);
                Resources packageRes = packageContext.getResources();
                source.description = packageRes.getString(ri.serviceInfo.descriptionRes);
            } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
                Log.e(TAG, "Can't read package resources for source " + source.componentName);
            }
        }
        Bundle metaData = ri.serviceInfo.metaData;
        source.color = Color.WHITE;
        if (metaData != null) {
            String settingsActivity = metaData.getString("settingsActivity");
            if (!TextUtils.isEmpty(settingsActivity)) {
                source.settingsActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + settingsActivity);
            }

            String setupActivity = metaData.getString("setupActivity");
            if (!TextUtils.isEmpty(setupActivity)) {
                source.setupActivity = ComponentName
                        .unflattenFromString(ri.serviceInfo.packageName + "/" + setupActivity);
            }

            source.color = metaData.getInt("color", source.color);

            try {
                float[] hsv = new float[3];
                Color.colorToHSV(source.color, hsv);
                boolean adjust = false;
                if (hsv[2] < 0.8f) {
                    hsv[2] = 0.8f;
                    adjust = true;
                }
                if (hsv[1] > 0.4f) {
                    hsv[1] = 0.4f;
                    adjust = true;
                }
                if (adjust) {
                    source.color = Color.HSVToColor(hsv);
                }
                if (Color.alpha(source.color) != 255) {
                    source.color = Color.argb(255, Color.red(source.color), Color.green(source.color),
                            Color.blue(source.color));
                }
            } catch (IllegalArgumentException ignored) {
            }
        }

        mSources.add(source);
    }

    final String appPackage = getContext().getPackageName();
    Collections.sort(mSources, new Comparator<Source>() {
        @Override
        public int compare(Source s1, Source s2) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                boolean target1IsO = s1.targetSdkVersion >= Build.VERSION_CODES.O;
                boolean target2IsO = s2.targetSdkVersion >= Build.VERSION_CODES.O;
                if (target1IsO && !target2IsO) {
                    return 1;
                } else if (!target1IsO && target2IsO) {
                    return -1;
                }
            }
            String pn1 = s1.componentName.getPackageName();
            String pn2 = s2.componentName.getPackageName();
            if (!pn1.equals(pn2)) {
                if (appPackage.equals(pn1)) {
                    return -1;
                } else if (appPackage.equals(pn2)) {
                    return 1;
                }
            }
            return s1.label.compareTo(s2.label);
        }
    });

    redrawSources();
}