Example usage for android.os PatternMatcher getType

List of usage examples for android.os PatternMatcher getType

Introduction

In this page you can find the example usage for android.os PatternMatcher getType.

Prototype

public final int getType() 

Source Link

Usage

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  ww. j a  v  a  2  s .co 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.github.michalbednarski.intentslab.providerlab.ProviderInfoFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_provider_info, container, false);

    mPackageName = getArguments().getString(ComponentInfoFragment.ARG_PACKAGE_NAME);
    mComponentName = getArguments().getString(ComponentInfoFragment.ARG_COMPONENT_NAME);

    // Fill mProviderInfo
    try {/*from ww w .ja  v a 2  s  . co m*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            fillProviderInfo();
        } else {
            fillProviderInfoLegacy();
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(getActivity(), R.string.component_not_found, Toast.LENGTH_SHORT).show();
        //finish();
        return null;
    }

    PackageManager packageManager = getActivity().getPackageManager();

    // Header icon and text
    ((TextView) view.findViewById(R.id.title)).setText(mProviderInfo.loadLabel(packageManager));
    ((TextView) view.findViewById(R.id.component))
            .setText(new ComponentName(mPackageName, mComponentName).flattenToShortString());
    ((ImageView) view.findViewById(R.id.icon)).setImageDrawable(mProviderInfo.loadIcon(packageManager));

    // Start building description
    FormattedTextBuilder text = new FormattedTextBuilder();

    // Authority
    if (mProviderInfo.authority != null) {
        text.appendValue("Authority", mProviderInfo.authority);
    }

    // Permission/exported
    if (!mProviderInfo.exported) {
        text.appendHeader(getString(R.string.component_not_exported));
    } else {
        if (mProviderInfo.readPermission == null) {
            if (mProviderInfo.writePermission == null) {
                text.appendHeader(getString(R.string.provider_rw_world_accessible));
            } else {
                text.appendValue(getString(R.string.provider_w_only_permission), mProviderInfo.writePermission,
                        true, FormattedTextBuilder.ValueSemantic.PERMISSION);
            }
        } else if (mProviderInfo.readPermission.equals(mProviderInfo.writePermission)) {
            text.appendValue(getString(R.string.provider_rw_permission), mProviderInfo.readPermission, true,
                    FormattedTextBuilder.ValueSemantic.PERMISSION);
        } else {
            text.appendValue(getString(R.string.provider_r_permission), mProviderInfo.readPermission, true,
                    FormattedTextBuilder.ValueSemantic.PERMISSION);
            if (mProviderInfo.writePermission == null) {
                text.appendValuelessKeyContinuingGroup(
                        getResources().getText(R.string.provider_no_w_permission));
            } else {
                text.appendValue(getString(R.string.provider_w_permission), mProviderInfo.writePermission,
                        false, FormattedTextBuilder.ValueSemantic.PERMISSION);
            }
        }
    }

    // Permission granting
    if (mProviderInfo.grantUriPermissions) {
        if (mProviderInfo.uriPermissionPatterns != null) {
            PatternMatcher[] uriPermissionPatterns = mProviderInfo.uriPermissionPatterns;
            String[] listItems = new String[uriPermissionPatterns.length];
            for (int i = 0; i < uriPermissionPatterns.length; i++) {
                PatternMatcher pattern = uriPermissionPatterns[i];
                listItems[i] = pattern.getPath()
                        + (pattern.getType() == PatternMatcher.PATTERN_PREFIX ? "*" : "");
            }
            text.appendList(getString(R.string.provider_grant_uri_permission_for), listItems);
        } else {
            text.appendHeader(getString(R.string.provider_grant_uri_permission_for_all_paths));
        }
    }

    // <meta-data>
    text.appendFormattedText(
            ComponentInfoFragment.dumpMetaData(getActivity(), mPackageName, mProviderInfo.metaData));

    // Put description in TextView
    TextView textView = (TextView) view.findViewById(R.id.description);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(text.getText());

    // Set button action
    view.findViewById(R.id.go_to_provider_lab).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            goToProviderLab();
        }
    });

    // Return view
    return view;
}

From source file:com.tasomaniac.openwith.resolver.ResolverActivity.java

protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) {

    final ChooserHistory history = getHistory();

    if ((mAlwaysUseOption || mAdapter.hasFilteredItem()) && mAdapter.mOrigResolveList != null) {
        // Build a reasonable intent filter, based on what matched.
        IntentFilter filter = new IntentFilter();

        if (intent.getAction() != null) {
            filter.addAction(intent.getAction());
        }/*w  w w .  j a  v  a 2  s  .  c  om*/
        Set<String> categories = intent.getCategories();
        if (categories != null) {
            for (String cat : categories) {
                filter.addCategory(cat);
            }
        }
        filter.addCategory(Intent.CATEGORY_DEFAULT);

        int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
        Uri data = intent.getData();
        if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
            String mimeType = intent.resolveType(this);
            if (mimeType != null) {
                try {
                    filter.addDataType(mimeType);
                } catch (IntentFilter.MalformedMimeTypeException e) {
                    Log.w("ResolverActivity", e);
                    filter = null;
                }
            }
        }
        if (filter != null && data != null && data.getScheme() != null) {
            // We need the data specification if there was no type,
            // OR if the scheme is not one of our magical "file:"
            // or "content:" schemes (see IntentFilter for the reason).
            if (cat != IntentFilter.MATCH_CATEGORY_TYPE
                    || (!"file".equals(data.getScheme()) && !"content".equals(data.getScheme()))) {
                filter.addDataScheme(data.getScheme());

                // Look through the resolved filter to determine which part
                // of it matched the original Intent.
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();

                    if (pIt != null) {
                        String ssp = data.getSchemeSpecificPart();
                        while (ssp != null && pIt.hasNext()) {
                            PatternMatcher p = pIt.next();
                            if (p.match(ssp)) {
                                filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
                                break;
                            }
                        }
                    }
                }
                Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
                if (aIt != null) {
                    while (aIt.hasNext()) {
                        IntentFilter.AuthorityEntry a = aIt.next();
                        if (a.match(data) >= 0) {
                            int port = a.getPort();
                            filter.addDataAuthority(a.getHost(), port >= 0 ? Integer.toString(port) : null);
                            break;
                        }
                    }
                }
                Iterator<PatternMatcher> pIt = ri.filter.pathsIterator();
                if (pIt != null) {
                    String path = data.getPath();
                    while (path != null && pIt.hasNext()) {
                        PatternMatcher p = pIt.next();
                        if (p.match(path)) {
                            filter.addDataPath(p.getPath(), p.getType());
                            break;
                        }
                    }
                }
            }
        }

        if (filter != null) {
            ContentValues values = new ContentValues(3);
            values.put(HOST, mRequestedUri.getHost());
            values.put(COMPONENT, intent.getComponent().flattenToString());

            if (alwaysCheck) {
                values.put(PREFERRED, true);
            }
            values.put(LAST_CHOSEN, true);
            getContentResolver().insert(CONTENT_URI, values);

            history.add(intent.getComponent().getPackageName());
        }
    }

    if (intent != null) {
        startActivity(intent);
    }
    history.save(this);
}