12644d947574240b71c427d457f7a775dc160ec09Yorke Lee/*
22644d947574240b71c427d457f7a775dc160ec09Yorke Lee * Copyright (C) 2011 The Android Open Source Project
32644d947574240b71c427d457f7a775dc160ec09Yorke Lee *
42644d947574240b71c427d457f7a775dc160ec09Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
52644d947574240b71c427d457f7a775dc160ec09Yorke Lee * you may not use this file except in compliance with the License.
62644d947574240b71c427d457f7a775dc160ec09Yorke Lee * You may obtain a copy of the License at
72644d947574240b71c427d457f7a775dc160ec09Yorke Lee *
82644d947574240b71c427d457f7a775dc160ec09Yorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
92644d947574240b71c427d457f7a775dc160ec09Yorke Lee *
102644d947574240b71c427d457f7a775dc160ec09Yorke Lee * Unless required by applicable law or agreed to in writing, software
112644d947574240b71c427d457f7a775dc160ec09Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
122644d947574240b71c427d457f7a775dc160ec09Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132644d947574240b71c427d457f7a775dc160ec09Yorke Lee * See the License for the specific language governing permissions and
142644d947574240b71c427d457f7a775dc160ec09Yorke Lee * limitations under the License.
152644d947574240b71c427d457f7a775dc160ec09Yorke Lee */
162644d947574240b71c427d457f7a775dc160ec09Yorke Lee
1769c182afb0e6d82a341a28b4317aa703af768906Gary Maipackage com.android.contacts.util;
182644d947574240b71c427d457f7a775dc160ec09Yorke Lee
192644d947574240b71c427d457f7a775dc160ec09Yorke Leeimport android.content.ContentResolver;
202644d947574240b71c427d457f7a775dc160ec09Yorke Leeimport android.content.ContentUris;
212644d947574240b71c427d457f7a775dc160ec09Yorke Leeimport android.net.Uri;
222644d947574240b71c427d457f7a775dc160ec09Yorke Leeimport android.provider.Contacts;
232644d947574240b71c427d457f7a775dc160ec09Yorke Leeimport android.provider.ContactsContract;
242644d947574240b71c427d457f7a775dc160ec09Yorke Leeimport android.provider.ContactsContract.RawContacts;
252644d947574240b71c427d457f7a775dc160ec09Yorke Lee
262644d947574240b71c427d457f7a775dc160ec09Yorke Lee/**
272644d947574240b71c427d457f7a775dc160ec09Yorke Lee * Utility methods for the {@link ContactLoader}.
282644d947574240b71c427d457f7a775dc160ec09Yorke Lee */
292644d947574240b71c427d457f7a775dc160ec09Yorke Leepublic final class ContactLoaderUtils {
302644d947574240b71c427d457f7a775dc160ec09Yorke Lee
312644d947574240b71c427d457f7a775dc160ec09Yorke Lee    /** Static helper, not instantiable. */
322644d947574240b71c427d457f7a775dc160ec09Yorke Lee    private ContactLoaderUtils() {}
332644d947574240b71c427d457f7a775dc160ec09Yorke Lee
342644d947574240b71c427d457f7a775dc160ec09Yorke Lee    /**
352644d947574240b71c427d457f7a775dc160ec09Yorke Lee     * Transforms the given Uri and returns a Lookup-Uri that represents the contact.
362644d947574240b71c427d457f7a775dc160ec09Yorke Lee     * For legacy contacts, a raw-contact lookup is performed. An {@link IllegalArgumentException}
372644d947574240b71c427d457f7a775dc160ec09Yorke Lee     * can be thrown if the URI is null or the authority is not recognized.
382644d947574240b71c427d457f7a775dc160ec09Yorke Lee     *
392644d947574240b71c427d457f7a775dc160ec09Yorke Lee     * Do not call from the UI thread.
402644d947574240b71c427d457f7a775dc160ec09Yorke Lee     */
412644d947574240b71c427d457f7a775dc160ec09Yorke Lee    @SuppressWarnings("deprecation")
422644d947574240b71c427d457f7a775dc160ec09Yorke Lee    public static Uri ensureIsContactUri(final ContentResolver resolver, final Uri uri)
432644d947574240b71c427d457f7a775dc160ec09Yorke Lee            throws IllegalArgumentException {
442644d947574240b71c427d457f7a775dc160ec09Yorke Lee        if (uri == null) throw new IllegalArgumentException("uri must not be null");
452644d947574240b71c427d457f7a775dc160ec09Yorke Lee
462644d947574240b71c427d457f7a775dc160ec09Yorke Lee        final String authority = uri.getAuthority();
472644d947574240b71c427d457f7a775dc160ec09Yorke Lee
482644d947574240b71c427d457f7a775dc160ec09Yorke Lee        // Current Style Uri?
492644d947574240b71c427d457f7a775dc160ec09Yorke Lee        if (ContactsContract.AUTHORITY.equals(authority)) {
502644d947574240b71c427d457f7a775dc160ec09Yorke Lee            final String type = resolver.getType(uri);
512644d947574240b71c427d457f7a775dc160ec09Yorke Lee            // Contact-Uri? Good, return it
522644d947574240b71c427d457f7a775dc160ec09Yorke Lee            if (ContactsContract.Contacts.CONTENT_ITEM_TYPE.equals(type)) {
532644d947574240b71c427d457f7a775dc160ec09Yorke Lee                return uri;
542644d947574240b71c427d457f7a775dc160ec09Yorke Lee            }
552644d947574240b71c427d457f7a775dc160ec09Yorke Lee
562644d947574240b71c427d457f7a775dc160ec09Yorke Lee            // RawContact-Uri? Transform it to ContactUri
572644d947574240b71c427d457f7a775dc160ec09Yorke Lee            if (RawContacts.CONTENT_ITEM_TYPE.equals(type)) {
582644d947574240b71c427d457f7a775dc160ec09Yorke Lee                final long rawContactId = ContentUris.parseId(uri);
592644d947574240b71c427d457f7a775dc160ec09Yorke Lee                return RawContacts.getContactLookupUri(resolver,
602644d947574240b71c427d457f7a775dc160ec09Yorke Lee                        ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
612644d947574240b71c427d457f7a775dc160ec09Yorke Lee            }
622644d947574240b71c427d457f7a775dc160ec09Yorke Lee
632644d947574240b71c427d457f7a775dc160ec09Yorke Lee            // Anything else? We don't know what this is
642644d947574240b71c427d457f7a775dc160ec09Yorke Lee            throw new IllegalArgumentException("uri format is unknown");
652644d947574240b71c427d457f7a775dc160ec09Yorke Lee        }
662644d947574240b71c427d457f7a775dc160ec09Yorke Lee
672644d947574240b71c427d457f7a775dc160ec09Yorke Lee        // Legacy Style? Convert to RawContact
682644d947574240b71c427d457f7a775dc160ec09Yorke Lee        final String OBSOLETE_AUTHORITY = Contacts.AUTHORITY;
692644d947574240b71c427d457f7a775dc160ec09Yorke Lee        if (OBSOLETE_AUTHORITY.equals(authority)) {
702644d947574240b71c427d457f7a775dc160ec09Yorke Lee            // Legacy Format. Convert to RawContact-Uri and then lookup the contact
712644d947574240b71c427d457f7a775dc160ec09Yorke Lee            final long rawContactId = ContentUris.parseId(uri);
722644d947574240b71c427d457f7a775dc160ec09Yorke Lee            return RawContacts.getContactLookupUri(resolver,
732644d947574240b71c427d457f7a775dc160ec09Yorke Lee                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
742644d947574240b71c427d457f7a775dc160ec09Yorke Lee        }
752644d947574240b71c427d457f7a775dc160ec09Yorke Lee
762644d947574240b71c427d457f7a775dc160ec09Yorke Lee        throw new IllegalArgumentException("uri authority is unknown");
772644d947574240b71c427d457f7a775dc160ec09Yorke Lee    }
782644d947574240b71c427d457f7a775dc160ec09Yorke Lee}
79