Code example for ContentResolver

Methods: getType, getContactLookupUri

0
 
        final String authority = uri.getAuthority();
 
        // Current Style Uri? 
        if (ContactsContract.AUTHORITY.equals(authority)) {
            final String type = resolver.getType(uri);
            // Contact-Uri? Good, return it 
            if (ContactsContract.Contacts.CONTENT_ITEM_TYPE.equals(type)) {
                return uri;
            } 
 
            // RawContact-Uri? Transform it to ContactUri 
            if (RawContacts.CONTENT_ITEM_TYPE.equals(type)) {
                final long rawContactId = ContentUris.parseId(uri);
                return RawContacts.getContactLookupUri(resolver,
                        ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
            } 
 
            // Anything else? We don't know what this is 
            throw new IllegalArgumentException("uri format is unknown");
        } 
 
        // Legacy Style? Convert to RawContact 
        final String OBSOLETE_AUTHORITY = Contacts.AUTHORITY;
        if (OBSOLETE_AUTHORITY.equals(authority)) {
            // Legacy Format. Convert to RawContact-Uri and then lookup the contact 
            final long rawContactId = ContentUris.parseId(uri);
            return RawContacts.getContactLookupUri(resolver,
                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
        } 
 
        throw new IllegalArgumentException("uri authority is unknown");
    }