Example usage for android.content.pm PackageManager queryIntentActivities

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

Introduction

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

Prototype

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

Source Link

Document

Retrieve all activities that can be performed for the given intent.

Usage

From source file:com.android.dialer.DialtactsFragment.java

private boolean canIntentBeHandled(Intent intent) {
    final PackageManager packageManager = getActivity().getPackageManager();
    final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return resolveInfo != null && resolveInfo.size() > 0;
}

From source file:com.fsck.k9.activity.Accounts.java

private void onImport() {
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("*/*");

    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> infos = packageManager.queryIntentActivities(i, 0);

    if (infos.size() > 0) {
        startActivityForResult(Intent.createChooser(i, null), ACTIVITY_REQUEST_PICK_SETTINGS_FILE);
    } else {/*from   w  w w  . j  av a 2 s  .  c  o  m*/
        showDialog(DIALOG_NO_FILE_MANAGER);
    }
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * This function allows to know if there is an application that responds to
 * the specified action.//from   w  w w  .j  a v a 2s.  co  m
 * 
 * @param context
 * @param action   Action that requires an application.
 * @return
 */
public static boolean system_isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

From source file:org.mariotaku.twidere.util.Utils.java

public static void addIntentToSubMenu(final Context context, final SubMenu menu, final Intent query_intent) {
    if (context == null || menu == null || query_intent == null)
        return;/*ww w  . j a v  a2  s.c om*/
    final PackageManager pm = context.getPackageManager();
    final List<ResolveInfo> activities = pm.queryIntentActivities(query_intent, 0);
    for (final ResolveInfo info : activities) {
        final Intent intent = new Intent(query_intent);
        intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
        menu.add(info.loadLabel(pm)).setIcon(info.loadIcon(pm)).setIntent(intent);
    }
}

From source file:me.tb.player.SkeletonActivity.java

public void onShareClick() {
    Resources resources = getResources();
    String type = "image/*";
    String mediaPath = Environment.getExternalStorageDirectory() + "/game_icon1.png";

    // Create the URI from the media
    File media = new File(mediaPath);
    Uri uri = Uri.fromFile(media);//from ww w . ja v a2 s. co m

    Intent emailIntent = new Intent();
    emailIntent.setAction(Intent.ACTION_SEND);
    // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
    emailIntent.putExtra(Intent.EXTRA_TEXT,
            "Download in Google Play Store\nhttps://play.google.com/store/apps/details?id=me.tb.player");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Play Word Bandit - Multiplayer");
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    emailIntent.setType(type);

    PackageManager pm = getPackageManager();
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType(type);

    Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if (packageName.contains("com.google.android.gm")) {
            emailIntent.setPackage(packageName);
        } else if (packageName.contains("twitter") || packageName.contains("facebook.katana")
                || packageName.contains("com.instagram.android")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType(type);
            // Add the URI and the caption to the Intent.
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            if (packageName.contains("twitter") || packageName.contains("instagram")) {
                intent.putExtra(Intent.EXTRA_TEXT, shareMessageCombo
                        + "\nDownload now https://play.google.com/store/apps/details?id=me.tb.player");
            }
            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

    // convert intentList to array
    LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(openInChooser);
}

From source file:org.mariotaku.twidere.util.Utils.java

public static void setMenuForStatus(final Context context, final Menu menu, final ParcelableStatus status) {
    if (context == null || menu == null || status == null)
        return;/*from w ww  .ja  v  a  2s  .  co  m*/
    final int activated_color = context.getResources().getColor(R.color.holo_blue_bright);
    final MenuItem delete = menu.findItem(R.id.delete_submenu);
    if (delete != null) {
        delete.setVisible(status.account_id == status.user_id && !isMyRetweet(status));
    }
    final MenuItem retweet = menu.findItem(MENU_RETWEET);
    if (retweet != null) {
        final Drawable icon = retweet.getIcon().mutate();
        retweet.setVisible(!status.is_protected || isMyRetweet(status));
        if (isMyRetweet(status)) {
            icon.setColorFilter(activated_color, Mode.MULTIPLY);
            retweet.setTitle(R.string.cancel_retweet);
        } else {
            icon.clearColorFilter();
            retweet.setTitle(R.string.retweet);
        }
    }
    final MenuItem favorite = menu.findItem(MENU_FAVORITE);
    if (favorite != null) {
        final Drawable icon = favorite.getIcon().mutate();
        if (status.is_favorite) {
            icon.setColorFilter(activated_color, Mode.MULTIPLY);
            favorite.setTitle(R.string.unfavorite);
        } else {
            icon.clearColorFilter();
            favorite.setTitle(R.string.favorite);
        }
    }
    final MenuItem extensions = menu.findItem(MENU_EXTENSIONS_SUBMENU);
    if (extensions != null) {
        final Intent intent = new Intent(INTENT_ACTION_EXTENSION_OPEN_STATUS);
        final Bundle extras = new Bundle();
        extras.putParcelable(INTENT_KEY_STATUS, status);
        intent.putExtras(extras);
        final PackageManager pm = context.getPackageManager();
        final List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
        final boolean has_extension = !activities.isEmpty();
        extensions.setVisible(has_extension);
        if (has_extension) {
            addIntentToSubMenu(context, extensions.getSubMenu(), intent);
        }
    }
}

From source file:com.landenlabs.all_devtool.PackageFragment.java

/**
 * Load packages which are default (associated) with specific mime types.
 *
 * Use "adb shell dumpsys package r" to get full list
 *///w  w w . j a v  a 2 s .c om
void loadDefaultPackages() {
    m_workList = new ArrayList<PackingItem>();

    String[] actions = { Intent.ACTION_SEND, Intent.ACTION_SEND, Intent.ACTION_SEND, Intent.ACTION_SEND,

            Intent.ACTION_VIEW, Intent.ACTION_VIEW, Intent.ACTION_VIEW, Intent.ACTION_VIEW, Intent.ACTION_VIEW,
            Intent.ACTION_VIEW, Intent.ACTION_VIEW, Intent.ACTION_VIEW,

            MediaStore.ACTION_IMAGE_CAPTURE, MediaStore.ACTION_VIDEO_CAPTURE,

            Intent.ACTION_CREATE_SHORTCUT };

    String[] types = { "audio/*", "video/*", "image/*", "text/plain",

            "application/pdf", "application/zip", "audio/*", "video/*", "image/*", "text/html", "text/plain",
            "text/csv",

            "image/png", "video/*",

            "" };

    long orderCnt = 1;
    for (int idx = 0; idx != actions.length; idx++) {
        String type = types[idx];
        Intent resolveIntent = new Intent(actions[idx]);

        if (!TextUtils.isEmpty(type)) {
            if (type.startsWith("audio/*")) {
                Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
                resolveIntent.setDataAndType(uri, type);
            } else if (type.startsWith("video/*")) {
                Uri uri = Uri.withAppendedPath(MediaStore.Video.Media.INTERNAL_CONTENT_URI, "1");
                resolveIntent.setDataAndType(uri, type);
            } else if (type.startsWith("text/")) {
                Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath());
                resolveIntent.setDataAndType(uri, type);
            } else {
                resolveIntent.setType(type);
            }
        }

        PackageManager pm = getActivity().getPackageManager();

        // PackageManager.GET_RESOLVED_FILTER);  // or PackageManager.MATCH_DEFAULT_ONLY
        List<ResolveInfo> resolveList = pm.queryIntentActivities(resolveIntent, -1); // PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_INTENT_FILTERS);

        if (resolveList != null) {
            String actType = type = Utils.last(actions[idx].split("[.]")) + ":" + type;
            for (ResolveInfo resolveInfo : resolveList) {
                ArrayListPairString pkgList = new ArrayListPairString();
                String appName = resolveInfo.activityInfo.loadLabel(pm).toString().trim();

                addList(pkgList, "Type", actType);
                String pkgName = resolveInfo.activityInfo.packageName;
                PackageInfo packInfo = null;
                try {
                    packInfo = pm.getPackageInfo(pkgName, 0);
                    addList(pkgList, "Version", packInfo.versionName);
                    addList(pkgList, "VerCode", String.valueOf(packInfo.versionCode));
                    addList(pkgList, "TargetSDK", String.valueOf(packInfo.applicationInfo.targetSdkVersion));
                    m_date.setTime(packInfo.firstInstallTime);
                    addList(pkgList, "Install First", s_timeFormat.format(m_date));
                    m_date.setTime(packInfo.lastUpdateTime);
                    addList(pkgList, "Install Last", s_timeFormat.format(m_date));
                    if (resolveInfo.filter != null) {
                        if (resolveInfo.filter.countDataSchemes() > 0) {
                            addList(pkgList, "Intent Scheme", "");
                            for (int sIdx = 0; sIdx != resolveInfo.filter.countDataSchemes(); sIdx++)
                                addList(pkgList, " ", resolveInfo.filter.getDataScheme(sIdx));
                        }
                        if (resolveInfo.filter.countActions() > 0) {
                            addList(pkgList, "Intent Action", "");
                            for (int aIdx = 0; aIdx != resolveInfo.filter.countActions(); aIdx++)
                                addList(pkgList, " ", resolveInfo.filter.getAction(aIdx));
                        }
                        if (resolveInfo.filter.countCategories() > 0) {
                            addList(pkgList, "Intent Category", "");
                            for (int cIdx = 0; cIdx != resolveInfo.filter.countCategories(); cIdx++)
                                addList(pkgList, " ", resolveInfo.filter.getCategory(cIdx));
                        }
                        if (resolveInfo.filter.countDataTypes() > 0) {
                            addList(pkgList, "Intent DataType", "");
                            for (int dIdx = 0; dIdx != resolveInfo.filter.countDataTypes(); dIdx++)
                                addList(pkgList, " ", resolveInfo.filter.getDataType(dIdx));
                        }
                    }
                    m_workList.add(
                            new PackingItem(pkgName.trim(), pkgList, packInfo, orderCnt++, appName, actType));
                } catch (Exception ex) {
                }
            }
        }

        if (false) {
            // TODO - look into this method, see loadCachedPackages
            int flags = PackageManager.GET_PROVIDERS;
            List<PackageInfo> packList = pm.getPreferredPackages(flags);
            if (packList != null) {
                for (int pkgIdx = 0; pkgIdx < packList.size(); pkgIdx++) {
                    PackageInfo packInfo = packList.get(pkgIdx);

                    // if (((packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) == showSys) {
                    addPackageInfo(packInfo);
                    // }
                }
            }
        }
    }

    // getPreferredAppInfo();

    /*
    List<ProviderInfo> providerList = getActivity().getPackageManager().queryContentProviders(null, 0, 0);
    if (providerList != null) {
    for (ProviderInfo providerInfo : providerList) {
        String name = providerInfo.name;
        String pkg = providerInfo.packageName;
            
    }
    }
    */
}

From source file:de.mrapp.android.bottomsheet.BottomSheet.java

/**
 * Adds the apps, which are able to handle a specific intent, as items to the bottom sheet. This
 * causes all previously added items to be removed. When an item is clicked, the corresponding
 * app is started./*from  w ww.  j  a v a2  s. c  o  m*/
 *
 * @param activity
 *         The activity, the bottom sheet belongs to, as an instance of the class {@link
 *         Activity}. The activity may not be null
 * @param intent
 *         The intent as an instance of the class {@link Intent}. The intent may not be null
 */
public final void setIntent(@NonNull final Activity activity, @NonNull final Intent intent) {
    ensureNotNull(activity, "The activity may not be null");
    ensureNotNull(intent, "The intent may not be null");
    removeAllItems();
    PackageManager packageManager = activity.getPackageManager();
    List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);

    for (int i = 0; i < resolveInfos.size(); i++) {
        ResolveInfo resolveInfo = resolveInfos.get(i);
        addItem(i, resolveInfo.loadLabel(packageManager), resolveInfo.loadIcon(packageManager));
    }

    setOnItemClickListener(createIntentClickListener(activity, (Intent) intent.clone(), resolveInfos));
}

From source file:com.aimfire.gallery.GalleryActivity.java

/**
 * share only to certain apps. code based on "http://stackoverflow.com/questions/
 * 9730243/how-to-filter-specific-apps-for-action-send-intent-and-set-a-different-
 * text-for/18980872#18980872"/* w w w  . j a  v a2  s .  c o  m*/
 * 
 * "copy link" inspired by http://cketti.de/2016/06/15/share-url-to-clipboard/
 * 
 * in general, "deep linking" is supported by the apps below. Facebook, Wechat,
 * Telegram are exceptions. click on the link would bring users to the landing
 * page. 
 * 
 * Facebook doesn't take our EXTRA_TEXT so user will have to "copy link" first 
 * then paste the link
 */
private void shareMedia(Intent data) {
    /*
     * we log this as "share complete", but user can still cancel the share at this point,
     * and we wouldn't be able to know
     */
    mFirebaseAnalytics.logEvent(MainConsts.FIREBASE_CUSTOM_EVENT_SHARE_COMPLETE, null);

    Resources resources = getResources();

    /*
     * get the resource id for the shared file
     */
    String id = data.getStringExtra(MainConsts.EXTRA_ID_RESOURCE);

    /*
     * construct link
     */
    String link = "https://" + resources.getString(R.string.app_domain) + "/?id=" + id + "&name="
            + ((mPreviewName != null) ? mPreviewName : mMediaName);

    /*
     * message subject and text
     */
    String emailSubject, emailText, twitterText;

    if (MediaScanner.isPhoto(mMediaPath)) {
        emailSubject = resources.getString(R.string.emailSubjectPhoto);
        emailText = resources.getString(R.string.emailBodyPhotoPrefix) + link;
        twitterText = resources.getString(R.string.emailBodyPhotoPrefix) + link
                + resources.getString(R.string.twitterHashtagPhoto) + resources.getString(R.string.app_hashtag);
    } else if (MediaScanner.is3dMovie(mMediaPath)) {
        emailSubject = resources.getString(R.string.emailSubjectVideo);
        emailText = resources.getString(R.string.emailBodyVideoPrefix) + link;
        twitterText = resources.getString(R.string.emailBodyVideoPrefix) + link
                + resources.getString(R.string.twitterHashtagVideo) + resources.getString(R.string.app_hashtag);
    } else //if(MediaScanner.is2dMovie(mMediaPath))
    {
        emailSubject = resources.getString(R.string.emailSubjectVideo2d);
        emailText = resources.getString(R.string.emailBodyVideoPrefix2d) + link;
        twitterText = resources.getString(R.string.emailBodyVideoPrefix2d) + link
                + resources.getString(R.string.twitterHashtagVideo) + resources.getString(R.string.app_hashtag);
    }

    Intent emailIntent = new Intent();
    emailIntent.setAction(Intent.ACTION_SEND);
    // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
    //emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
    emailIntent.setType("message/rfc822");

    PackageManager pm = getPackageManager();
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");

    Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if (packageName.contains("android.email")) {
            emailIntent.setPackage(packageName);
        } else if (packageName.contains("twitter") || packageName.contains("facebook")
                || packageName.contains("whatsapp") || packageName.contains("tencent.mm") || //wechat
                packageName.contains("line") || packageName.contains("skype") || packageName.contains("viber")
                || packageName.contains("kik") || packageName.contains("sgiggle") || //tango
                packageName.contains("kakao") || packageName.contains("telegram")
                || packageName.contains("nimbuzz") || packageName.contains("hike")
                || packageName.contains("imoim") || packageName.contains("bbm")
                || packageName.contains("threema") || packageName.contains("mms")
                || packageName.contains("android.apps.messaging") || //google messenger
                packageName.contains("android.talk") || //google hangouts
                packageName.contains("android.gm")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            if (packageName.contains("twitter")) {
                intent.putExtra(Intent.EXTRA_TEXT, twitterText);
            } else if (packageName.contains("facebook")) {
                /*
                 * the warning below is wrong! at least on GS5, Facebook client does take
                 * our text, however it seems it takes only the first hyperlink in the
                 * text.
                 * 
                 * Warning: Facebook IGNORES our text. They say "These fields are intended 
                 * for users to express themselves. Pre-filling these fields erodes the 
                 * authenticity of the user voice."
                 * One workaround is to use the Facebook SDK to post, but that doesn't 
                 * allow the user to choose how they want to share. We can also make a 
                 * custom landing page, and the link will show the <meta content ="..."> 
                 * text from that page with our link in Facebook.
                 */
                intent.putExtra(Intent.EXTRA_TEXT, link);
            } else if (packageName.contains("tencent.mm")) //wechat
            {
                /*
                 * wechat appears to do this similar to Facebook
                 */
                intent.putExtra(Intent.EXTRA_TEXT, link);
            } else if (packageName.contains("android.gm")) {
                // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
                intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
                intent.putExtra(Intent.EXTRA_TEXT, emailText);
                //intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
                intent.setType("message/rfc822");
            } else if (packageName.contains("android.apps.docs")) {
                /*
                 * google drive - no reason to send link to it
                 */
                continue;
            } else {
                intent.putExtra(Intent.EXTRA_TEXT, emailText);
            }

            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

    /*
     *  create "Copy Link To Clipboard" Intent
     */
    Intent clipboardIntent = new Intent(this, CopyToClipboardActivity.class);
    clipboardIntent.setData(Uri.parse(link));
    intentList.add(new LabeledIntent(clipboardIntent, getPackageName(),
            getResources().getString(R.string.clipboard_activity_name), R.drawable.ic_copy_link));

    // convert intentList to array
    LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(openInChooser);
}

From source file:org.kontalk.ui.AbstractComposeFragment.java

/** Starts an activity for shooting a picture. */
void selectPhotoAttachment() {
    try {/*from  ww  w .ja  va 2 s  . co  m*/
        // check if camera is available
        final PackageManager packageManager = getActivity().getPackageManager();
        final Intent intent = SystemUtils.externalIntent(MediaStore.ACTION_IMAGE_CAPTURE);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (list.size() <= 0)
            throw new UnsupportedOperationException();

        mCurrentPhoto = MediaStorage.getOutgoingPhotoFile();
        Uri uri = Uri.fromFile(mCurrentPhoto);
        Intent take = SystemUtils.externalIntent(MediaStore.ACTION_IMAGE_CAPTURE);
        take.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            take.setClipData(ClipData.newUri(getContext().getContentResolver(), "Picture path", uri));
        }

        startActivityForResult(take, SELECT_ATTACHMENT_PHOTO);
    } catch (UnsupportedOperationException ue) {
        Toast.makeText(getActivity(), R.string.chooser_error_no_camera_app, Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Log.e(TAG, "error creating temp file", e);
        Toast.makeText(getActivity(), R.string.chooser_error_no_camera, Toast.LENGTH_LONG).show();
    }
}