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 package com.android.providers.contacts.aggregation; 17 18 import android.database.sqlite.SQLiteDatabase; 19 import android.database.sqlite.SQLiteDoneException; 20 import android.database.sqlite.SQLiteStatement; 21 import android.provider.ContactsContract.Contacts; 22 23 import com.android.providers.contacts.ContactLookupKey; 24 import com.android.providers.contacts.ContactsDatabaseHelper; 25 import com.android.providers.contacts.ContactsDatabaseHelper.Tables; 26 import com.android.providers.contacts.aggregation.util.CommonNicknameCache; 27 import com.android.providers.contacts.ContactsProvider2; 28 import com.android.providers.contacts.NameSplitter; 29 import com.android.providers.contacts.PhotoPriorityResolver; 30 import com.android.providers.contacts.TransactionContext; 31 32 /** 33 * A version of the ContactAggregator for use against the profile database. 34 */ 35 public class ProfileAggregator extends ContactAggregator { 36 37 private long mContactId; 38 39 public ProfileAggregator(ContactsProvider2 contactsProvider, 40 ContactsDatabaseHelper contactsDatabaseHelper, 41 PhotoPriorityResolver photoPriorityResolver, NameSplitter nameSplitter, 42 CommonNicknameCache commonNicknameCache) { 43 super(contactsProvider, contactsDatabaseHelper, photoPriorityResolver, nameSplitter, 44 commonNicknameCache); 45 } 46 47 @Override 48 protected String computeLookupKeyForContact(SQLiteDatabase db, long contactId) { 49 return ContactLookupKey.PROFILE_LOOKUP_KEY; 50 } 51 52 @Override 53 protected void appendLookupKey(StringBuilder sb, String accountTypeWithDataSet, 54 String accountName, long rawContactId, String sourceId, String displayName) { 55 56 // The profile's lookup key should always be "profile". 57 sb.setLength(0); 58 sb.append(ContactLookupKey.PROFILE_LOOKUP_KEY); 59 } 60 61 @Override 62 public long onRawContactInsert(TransactionContext txContext, SQLiteDatabase db, 63 long rawContactId) { 64 aggregateContact(txContext, db, rawContactId); 65 return mContactId; 66 } 67 68 @Override 69 public void aggregateInTransaction(TransactionContext txContext, SQLiteDatabase db) { 70 // Do nothing. The contact should already be aggregated. 71 } 72 73 @Override 74 public void aggregateContact(TransactionContext txContext, SQLiteDatabase db, 75 long rawContactId) { 76 // Profile aggregation is simple - find the single contact in the database and attach to 77 // that. We look it up each time in case the profile was deleted by a previous operation 78 // and needs re-creation. 79 SQLiteStatement profileContactIdLookup = db.compileStatement( 80 "SELECT " + Contacts._ID + 81 " FROM " + Tables.CONTACTS + 82 " ORDER BY " + Contacts._ID + 83 " LIMIT 1"); 84 try { 85 mContactId = profileContactIdLookup.simpleQueryForLong(); 86 updateAggregateData(txContext, mContactId); 87 } catch (SQLiteDoneException e) { 88 // No valid contact ID found, so create one. 89 mContactId = insertContact(db, rawContactId); 90 } finally { 91 profileContactIdLookup.close(); 92 } 93 setContactId(rawContactId, mContactId); 94 } 95 } 96