Code example for Uri

Methods: getLastPathSegment, getPathSegments, getQueryParameter

0
        return true; 
    } 
 
    private static int matchUri(final Uri uri) {
        int protocolVersion = 1;
        final String protocolVersionArg = uri.getQueryParameter(QUERY_PARAMETER_PROTOCOL_VERSION);
        if ("2".equals(protocolVersionArg)) protocolVersion = 2;
        switch (protocolVersion) {
            case 1: return sUriMatcherV1.match(uri);
            case 2: return sUriMatcherV2.match(uri);
            default: return NO_MATCH;
        } 
    } 
 
    private static String getClientId(final Uri uri) {
        int protocolVersion = 1;
        final String protocolVersionArg = uri.getQueryParameter(QUERY_PARAMETER_PROTOCOL_VERSION);
        if ("2".equals(protocolVersionArg)) protocolVersion = 2;
        switch (protocolVersion) {
            case 1: return null; // In protocol 1, the client ID is always null. 
            case 2: return uri.getPathSegments().get(0);
            default: return null; 
        } 
    } 
 
    /** 
     * Returns the MIME type of the content associated with an Uri 
     * 
     * @see android.content.ContentProvider#getType(android.net.Uri) 
     * 
     * @param uri the URI of the content the type of which should be returned. 
     * @return the MIME type, or null if the URL is not recognized. 
     */ 
    @Override 
    public String getType(final Uri uri) {
        PrivateLog.log("Asked for type of : " + uri);
        final int match = matchUri(uri);
        switch (match) {
            case NO_MATCH: return null;
            case DICTIONARY_V1_WHOLE_LIST:
            case DICTIONARY_V1_DICT_INFO:
            case DICTIONARY_V2_WHOLE_LIST:
            case DICTIONARY_V2_DICT_INFO: return DICT_LIST_MIME_TYPE;
            case DICTIONARY_V2_DATAFILE: return DICT_DATAFILE_MIME_TYPE;
            default: return null; 
        } 
    } 
 
    /** 
     * Query the provider for dictionary files. 
     * 
     * This version dispatches the query according to the protocol version found in the 
     * ?protocol= query parameter. If absent or not well-formed, it defaults to 1. 
     * @see android.content.ContentProvider#query(Uri, String[], String, String[], String) 
     * 
     * @param uri a content uri (see sUriMatcherV{1,2} at the top of this file for format) 
     * @param projection ignored. All columns are always returned. 
     * @param selection ignored. 
     * @param selectionArgs ignored. 
     * @param sortOrder ignored. The results are always returned in no particular order. 
     * @return a cursor matching the uri, or null if the URI was not recognized. 
     */ 
    @Override 
    public Cursor query(final Uri uri, final String[] projection, final String selection,
            final String[] selectionArgs, final String sortOrder) {
        Utils.l("Uri =", uri);
        PrivateLog.log("Query : " + uri);
        final String clientId = getClientId(uri);
        final int match = matchUri(uri);
        switch (match) {
            case DICTIONARY_V1_WHOLE_LIST:
            case DICTIONARY_V2_WHOLE_LIST:
                final Cursor c = MetadataDbHelper.queryDictionaries(getContext(), clientId);
                Utils.l("List of dictionaries with count", c.getCount());
                PrivateLog.log("Returned a list of " + c.getCount() + " items");
                return c;
            case DICTIONARY_V2_DICT_INFO:
                // In protocol version 2, we return null if the client is unknown. Otherwise 
                // we behave exactly like for protocol 1. 
                if (!MetadataDbHelper.isClientKnown(getContext(), clientId)) return null;
                // Fall through 
            case DICTIONARY_V1_DICT_INFO:
                final String locale = uri.getLastPathSegment();
                // If LatinIME does not have a dictionary for this locale at all, it will 
                // send us true for this value. In this case, we may prompt the user for 
                // a decision about downloading a dictionary even over a metered connection. 
                final String mayPromptValue =
                        uri.getQueryParameter(QUERY_PARAMETER_MAY_PROMPT_USER);
                final boolean mayPrompt = QUERY_PARAMETER_TRUE.equals(mayPromptValue);
                final Collection<WordListInfo> dictFiles =
                        getDictionaryWordListsForLocale(clientId, locale, mayPrompt);
                // TODO: pass clientId to the following function