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 /** Utility methods for the {@link ContactLoader}. */ 27 public final class ContactLoaderUtils { 28 29 /** Static helper, not instantiable. */ ContactLoaderUtils()30 private ContactLoaderUtils() {} 31 32 /** 33 * Transforms the given Uri and returns a Lookup-Uri that represents the contact. For legacy 34 * contacts, a raw-contact lookup is performed. An {@link IllegalArgumentException} can be thrown 35 * if the URI is null or the authority is not recognized. 36 * 37 * <p>Do not call from the UI thread. 38 */ 39 @SuppressWarnings("deprecation") ensureIsContactUri(final ContentResolver resolver, final Uri uri)40 public static Uri ensureIsContactUri(final ContentResolver resolver, final Uri uri) 41 throws IllegalArgumentException { 42 if (uri == null) { 43 throw new IllegalArgumentException("uri must not be null"); 44 } 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( 60 resolver, 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( 73 resolver, ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)); 74 } 75 76 throw new IllegalArgumentException("uri authority is unknown"); 77 } 78 } 79