Example usage for android.content IntentFilter countDataAuthorities

List of usage examples for android.content IntentFilter countDataAuthorities

Introduction

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

Prototype

public final int countDataAuthorities() 

Source Link

Document

Return the number of data authorities 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.jav  a  2s  .co  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.
 *///from   ww w. ja  va 2s  .  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  w w  w  . j  a  va 2s .c o m
    }

    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//w  ww.  ja  va  2s  .  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;
}

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

/**
 * Update IntentFilter of categories and data
 * @param isInit//  ww w .ja v a  2 s  .c o m
 */
private void updateNonActionIntentFilter(boolean isInit) {
    // If we don't have any IntentFilter use non-filtering editors
    if (mAvailbleActions == null) {
        setFreeFormCategoryEditor();
        mDataTextWrapper.setVisibility(View.VISIBLE);
        mDataTextHeader.setVisibility(View.VISIBLE);
        mUriAutocompleteAdapter.setIntentFilters(null);
        setupUnfilteredDataTypeFields();
        return;
    }

    // Update edited intent
    if (!isInit) {
        updateEditedIntent(mEditedIntent);
    }

    // Get all IntentFilters
    final IntentFilter[] allIntentFilters = getIntentEditor().getAttachedIntentFilters();

    // Get selected action
    String action = (String) mActionsSpinner.getSelectedItem();

    HashSet<String> availableCategories = new HashSet<String>();

    HashSet<String> availableMimeTypes = new HashSet<String>();
    boolean acceptsAnyDataType = false;
    boolean acceptsUntypedData = false;
    boolean mayAutoDetectType = false;

    boolean acceptsUris = false;

    ArrayList<IntentFilter> selectedIntentFilters = new ArrayList<IntentFilter>();

    // Iterate over intent filters that has selected action
    for (IntentFilter filter : allIntentFilters) {
        if (filter.hasAction(action)) {
            // List categories
            if (filter.countCategories() != 0) {
                for (Iterator<String> iterator = filter.categoriesIterator(); iterator.hasNext();) {
                    availableCategories.add(iterator.next());
                }
            }

            // List available types or set flag that we don't need them
            if (filter.countDataTypes() == 0) {
                acceptsUntypedData = true;
            } else {
                for (Iterator<String> iterator = filter.typesIterator(); iterator.hasNext();) {
                    final String type = iterator.next();
                    if ("*".equals(type)) {
                        acceptsAnyDataType = true;
                    } else {
                        availableMimeTypes.add(type);
                    }
                }
            }

            // Scan schemes to see if system can auto detect type
            if (!mayAutoDetectType) {
                if (filter.countDataSchemes() != 0) {
                    for (Iterator<String> iterator = filter.schemesIterator(); iterator.hasNext();) {
                        String scheme = iterator.next();
                        if ("content".equals(scheme) || "file".equals(scheme)) {
                            mayAutoDetectType = true;
                            break;
                        }
                    }
                } else if ( // No schemes declared
                filter.countDataTypes() != 0 && ( // There's at least one
                !NO_IMPLICIT_URI_ACTIONS.contains(action) || // Action is not on list
                        filter.countDataAuthorities() != 0 // There is host specified
                )) {

                    // Intent will match empty, content: or file: scheme
                    acceptsUris = true;
                    mayAutoDetectType = true;
                }
            }

            // Check if we have data
            if (filter.countDataSchemes() != 0) {
                acceptsUris = true;
            }

            // Save used IntentFilter to list because UriAutocompleteAdapter scans them on his own
            selectedIntentFilters.add(filter);
        }
    }

    // Setup categories
    setupCategoryCheckBoxes(availableCategories);

    // Setup data type
    if (acceptsAnyDataType) {
        setupUnfilteredDataTypeFields();
    } else {
        setupFilteredDataTypeFields(acceptsUntypedData, mayAutoDetectType, availableMimeTypes);
    }

    // Setup data uri
    mDataTextWrapper.setVisibility(acceptsUris ? View.VISIBLE : View.GONE);
    mDataTextHeader.setVisibility(acceptsUris ? View.VISIBLE : View.GONE);
    if (!acceptsUris) {
        mDataText.setText("");
    }
    mUriAutocompleteAdapter
            .setIntentFilters(selectedIntentFilters.toArray(new IntentFilter[selectedIntentFilters.size()]));
}