ProfileAggregator.java revision c5fdfb79564e8b46d6aae213a312d4e1560f3a3c
15d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro/*
25d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * Copyright (C) 2011 The Android Open Source Project
35d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro *
45d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * Licensed under the Apache License, Version 2.0 (the "License");
55d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * you may not use this file except in compliance with the License.
65d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * You may obtain a copy of the License at
75d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro *
85d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro *      http://www.apache.org/licenses/LICENSE-2.0
95d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro *
105d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * Unless required by applicable law or agreed to in writing, software
115d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * distributed under the License is distributed on an "AS IS" BASIS,
125d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * See the License for the specific language governing permissions and
145d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * limitations under the License
155d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro */
165d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoropackage com.android.providers.contacts;
175d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
185d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoroimport android.database.sqlite.SQLiteDatabase;
195d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoroimport android.database.sqlite.SQLiteDoneException;
205d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoroimport android.database.sqlite.SQLiteStatement;
215d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoroimport android.provider.ContactsContract.Contacts;
225d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoroimport com.android.providers.contacts.ContactsDatabaseHelper.Tables;
235d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
245d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro/**
255d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro * A version of the ContactAggregator for use against the profile database.
265d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro */
275d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoropublic class ProfileAggregator extends ContactAggregator {
285d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
295d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    private SQLiteStatement mProfileContactIdLookup;
305d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
315d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    public ProfileAggregator(ContactsProvider2 contactsProvider,
325d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            ContactsDatabaseHelper contactsDatabaseHelper,
335d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            PhotoPriorityResolver photoPriorityResolver, NameSplitter nameSplitter,
345d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            CommonNicknameCache commonNicknameCache) {
355d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        super(contactsProvider, contactsDatabaseHelper, photoPriorityResolver, nameSplitter,
365d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro                commonNicknameCache);
375d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
385d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        SQLiteDatabase db = contactsDatabaseHelper.getReadableDatabase();
395d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        mProfileContactIdLookup = db.compileStatement(
405d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro                "SELECT " + Contacts._ID +
415d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro                " FROM " + Tables.CONTACTS +
425d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro                " ORDER BY " + Contacts._ID +
435d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro                " LIMIT 1");
445d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    }
455d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
465d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    @Override
475d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    public String computeLookupKeyForContact(SQLiteDatabase db, long contactId) {
485d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        return ContactLookupKey.PROFILE_LOOKUP_KEY;
495d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    }
505d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro
515d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    @Override
52c5fdfb79564e8b46d6aae213a312d4e1560f3a3cDave Santoro    protected String buildLookupKey(String accountTypeWithDataSet, String accountName,
53c5fdfb79564e8b46d6aae213a312d4e1560f3a3cDave Santoro            long rawContactId, String sourceId, String displayName) {
54c5fdfb79564e8b46d6aae213a312d4e1560f3a3cDave Santoro        return ContactLookupKey.PROFILE_LOOKUP_KEY;
55c5fdfb79564e8b46d6aae213a312d4e1560f3a3cDave Santoro    }
56c5fdfb79564e8b46d6aae213a312d4e1560f3a3cDave Santoro
57c5fdfb79564e8b46d6aae213a312d4e1560f3a3cDave Santoro    @Override
585d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    public long onRawContactInsert(TransactionContext txContext, SQLiteDatabase db,
595d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            long rawContactId) {
605d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        // Profile aggregation on raw contact insert is simple - find the single contact in the
615d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        // database and attach to that.
625d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        long contactId = -1;
635d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        try {
645d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            contactId = mProfileContactIdLookup.simpleQueryForLong();
655d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            updateAggregateData(txContext, contactId);
665d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        } catch (SQLiteDoneException e) {
675d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            // No valid contact ID found, so create one.
685d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro            contactId = insertContact(db, rawContactId);
695d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        }
705d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        setContactId(rawContactId, contactId);
715d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro        return contactId;
725d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro    }
735d0a768b56ed4bd0dfef81b8389247ba74766659Dave Santoro}
74