Example usage for android.content IntentFilter countDataPaths

List of usage examples for android.content IntentFilter countDataPaths

Introduction

In this page you can find the example usage for android.content IntentFilter countDataPaths.

Prototype

public final int countDataPaths() 

Source Link

Document

Return the number of data paths in the filter.

Usage

From source file:Main.java

/**
 * Used to check whether there is a specialized handler for a given intent.
 * @param intent The intent to check with.
 * @return Whether there is a specialized handler for the given intent.
 *//*from  w ww  .  ja v a2  s  . c o  m*/
private static boolean hasSpecializedHandlerIntents(Context context, Intent intent) {
    try {
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> handlers = pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
        if (handlers == null || handlers.size() == 0) {
            return false;
        }
        for (ResolveInfo resolveInfo : handlers) {
            IntentFilter filter = resolveInfo.filter;
            if (filter == null)
                continue;
            if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0)
                continue;
            if (resolveInfo.activityInfo == null)
                continue;
            return true;
        }
    } catch (RuntimeException e) {
        Log.e(TAG, "Runtime exception while getting specialized handlers");
    }
    return false;
}

From source file:Main.java

/**
 * Check whether the given package is a specialized handler for the given intent
 *
 * @param context {@link Context} to use for getting the {@link PackageManager}.
 * @param packageName Package name to check against. Can be null or empty.
 * @param intent The intent to resolve for.
 * @return Whether the given package is a specialized handler for the given intent. If there is
 *         no package name given checks whether there is any specialized handler.
 *//*  w w w .  j  a v a2s .  com*/
public static boolean isPackageSpecializedHandler(Context context, String packageName, Intent intent) {
    try {
        List<ResolveInfo> handlers = context.getPackageManager().queryIntentActivities(intent,
                PackageManager.GET_RESOLVED_FILTER);
        if (handlers == null || handlers.size() == 0)
            return false;
        for (ResolveInfo resolveInfo : handlers) {
            IntentFilter filter = resolveInfo.filter;
            if (filter == null) {
                // No intent filter matches this intent?
                // Error on the side of staying in the browser, ignore
                continue;
            }
            if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) {
                // Generic handler, skip
                continue;
            }
            if (TextUtils.isEmpty(packageName))
                return true;
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            if (activityInfo == null)
                continue;
            if (!activityInfo.packageName.equals(packageName))
                continue;
            return true;
        }
    } catch (RuntimeException e) {
        Log.e(TAG, "isPackageSpecializedHandler e=" + e);
    }
    return false;
}

From source file:com.github.michalbednarski.intentslab.browser.ComponentInfoFragment.java

static CharSequence dumpIntentFilter(IntentFilter filter, Resources res, boolean isBroadcast) {
    FormattedTextBuilder ftb = new FormattedTextBuilder();
    int tagColor = res.getColor(R.color.xml_tag);
    int attributeNameColor = res.getColor(R.color.xml_attr_name);
    int attributeValueColor = res.getColor(R.color.xml_attr_value);
    int commentColor = res.getColor(R.color.xml_comment);
    final String protectedComment = " <!-- " + res.getString(R.string.broadcast_action_protected_comment)
            + " -->";

    ftb.appendColoured("\n<intent-filter>", tagColor);

    for (int i = 0, j = filter.countActions(); i < j; i++) {
        final String action = filter.getAction(i);
        ftb.appendColoured("\n  <action", tagColor);
        ftb.appendColoured(" a:name=", attributeNameColor);
        ftb.appendColoured("\"" + action + "\"", attributeValueColor);
        ftb.appendColoured(">", tagColor);

        if (isBroadcast && Utils.isProtectedBroadcast(action)) {
            ftb.appendColoured(protectedComment, commentColor);
        }//from   ww  w  . ja va 2  s  .c  om
    }

    for (int i = 0, j = filter.countCategories(); i < j; i++) {
        ftb.appendColoured("\n  <category", tagColor);
        ftb.appendColoured(" a:name=", attributeNameColor);
        ftb.appendColoured("\"" + filter.getCategory(i) + "\"", attributeValueColor);
        ftb.appendColoured(">", tagColor);
    }

    for (int i = 0, j = filter.countDataSchemes(); i < j; i++) {
        ftb.appendColoured("\n  <data", tagColor);
        ftb.appendColoured(" a:scheme=", attributeNameColor);
        ftb.appendColoured("\"" + filter.getDataScheme(i) + "\"", attributeValueColor);
        ftb.appendColoured(">", tagColor);
    }

    for (int i = 0, j = filter.countDataAuthorities(); i < j; i++) {
        IntentFilter.AuthorityEntry authority = filter.getDataAuthority(i);
        ftb.appendColoured("\n  <data", tagColor);
        ftb.appendColoured(" a:host=", attributeNameColor);
        ftb.appendColoured("\"" + authority.getHost() + "\"", attributeValueColor);
        if (authority.getPort() != -1) {
            ftb.appendColoured(" a:port=", attributeNameColor);
            ftb.appendColoured("\"" + authority.getPort() + "\"", attributeValueColor);
        }
        ftb.appendColoured(">", tagColor);
    }

    for (int i = 0, j = filter.countDataPaths(); i < j; i++) {
        PatternMatcher pathMatcher = filter.getDataPath(i);
        int type = pathMatcher.getType();
        ftb.appendColoured("\n  <data", tagColor);
        ftb.appendColoured(
                " a:path"
                        + (type == PatternMatcher.PATTERN_LITERAL ? ""
                                : type == PatternMatcher.PATTERN_PREFIX ? "Prefix"
                                        : type == PatternMatcher.PATTERN_SIMPLE_GLOB ? "Pattern" : "[unknown]")
                        + "=",
                attributeNameColor);
        ftb.appendColoured("\"" + pathMatcher.getPath() + "\"", attributeValueColor);
        ftb.appendColoured(">", tagColor);
    }

    for (int i = 0, j = filter.countDataTypes(); i < j; i++) {
        String dataType = filter.getDataType(i);
        if (!dataType.contains("/")) {
            // IntentFilter for partial types don't store "/*" at end
            // e.g. "image" instead of "image/*".
            // We display it in full form here
            dataType += "/*";
        }
        ftb.appendColoured("\n  <data", tagColor);
        ftb.appendColoured(" a:mimeType=", attributeNameColor);
        ftb.appendColoured("\"" + dataType + "\"", attributeValueColor);
        ftb.appendColoured(">", tagColor);
    }

    ftb.appendColoured("\n</intent-filter>", tagColor);
    return ftb.getText();
}

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

/**
 * Get info on the preferred (launch by default) applications.
 * @return//from w ww . j  ava 2  s  .  c  o m
 */
public String getPreferredAppInfo() {
    List<PackageInfo> packages = getActivity().getPackageManager().getInstalledPackages(0);
    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    List<ComponentName> activities = new ArrayList<ComponentName>();
    String info = "";
    int nPref = 0, nFilters = 0, nActivities = 0;
    PackageInfo packInfo = null;
    // int orderCnt = 0;
    for (int i = 0; i < packages.size(); i++) {
        packInfo = packages.get(i);
        nPref = getActivity().getPackageManager().getPreferredActivities(filters, activities,
                packInfo.packageName);
        nFilters = filters.size();
        nActivities = activities.size();
        if (nPref > 0 || nFilters > 0 || nActivities > 0) {
            // This is a launch by default package
            // info += "\n" + packInfo.packageName + "\n";
            ArrayListPairString pkgList = new ArrayListPairString();

            for (IntentFilter filter : filters) {
                info += "IntentFilter:\n";
                for (int j = 0; j < filter.countActions(); j++) {
                    addList(pkgList, "Action", filter.getAction(j));
                }
                for (int j = 0; j < filter.countCategories(); j++) {
                    addList(pkgList, "Category", filter.getCategory(j));
                }
                for (int j = 0; j < filter.countDataTypes(); j++) {
                    addList(pkgList, "Type", filter.getDataType(j));
                }
                for (int j = 0; j < filter.countDataAuthorities(); j++) {
                    addList(pkgList, "Authority", filter.getDataAuthority(j).toString());
                }
                for (int j = 0; j < filter.countDataPaths(); j++) {
                    addList(pkgList, "Path", filter.getDataPath(j).toString());
                }
                for (int j = 0; j < filter.countDataSchemes(); j++) {
                    addList(pkgList, "Scheme", filter.getDataScheme(j));
                }
                // for (ComponentName activity : activities) {
                // info += "activity="
                // + activity.flattenToString() + "\n";
                // }
            }
            if (pkgList.size() != 0) {
                m_workList.add(new PackingItem(packInfo.packageName, pkgList, packInfo, i,
                        packInfo.applicationInfo.processName));
            }
        }
    }

    return info;
}