Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.common.util;
     18 
     19 import android.content.ContentResolver;
     20 import android.content.ContentUris;
     21 import android.net.Uri;
     22 import android.provider.Contacts;
     23 import android.provider.ContactsContract;
     24 import android.provider.ContactsContract.RawContacts;
     25 
     26 /**
     27  * Utility methods for the {@link ContactLoader}.
     28  */
     29 public final class ContactLoaderUtils {
     30 
     31     /** Static helper, not instantiable. */
     32     private ContactLoaderUtils() {}
     33 
     34     /**
     35      * Transforms the given Uri and returns a Lookup-Uri that represents the contact.
     36      * For legacy contacts, a raw-contact lookup is performed. An {@link IllegalArgumentException}
     37      * can be thrown if the URI is null or the authority is not recognized.
     38      *
     39      * Do not call from the UI thread.
     40      */
     41     @SuppressWarnings("deprecation")
     42     public static Uri ensureIsContactUri(final ContentResolver resolver, final Uri uri)
     43             throws IllegalArgumentException {
     44         if (uri == null) throw new IllegalArgumentException("uri must not be null");
     45 
     46         final String authority = uri.getAuthority();
     47 
     48         // Current Style Uri?
     49         if (ContactsContract.AUTHORITY.equals(authority)) {
     50             final String type = resolver.getType(uri);
     51             // Contact-Uri? Good, return it
     52             if (ContactsContract.Contacts.CONTENT_ITEM_TYPE.equals(type)) {
     53                 return uri;
     54             }
     55 
     56             // RawContact-Uri? Transform it to ContactUri
     57             if (RawContacts.CONTENT_ITEM_TYPE.equals(type)) {
     58                 final long rawContactId = ContentUris.parseId(uri);
     59                 return RawContacts.getContactLookupUri(resolver,
     60                         ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
     61             }
     62 
     63             // Anything else? We don't know what this is
     64             throw new IllegalArgumentException("uri format is unknown");
     65         }
     66 
     67         // Legacy Style? Convert to RawContact
     68         final String OBSOLETE_AUTHORITY = Contacts.AUTHORITY;
     69         if (OBSOLETE_AUTHORITY.equals(authority)) {
     70             // Legacy Format. Convert to RawContact-Uri and then lookup the contact
     71             final long rawContactId = ContentUris.parseId(uri);
     72             return RawContacts.getContactLookupUri(resolver,
     73                     ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
     74         }
     75 
     76         throw new IllegalArgumentException("uri authority is unknown");
     77     }
     78 }
     79