1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License
15 */
16package com.android.providers.contacts;
17
18import android.content.ContentValues;
19import android.content.Context;
20import android.database.Cursor;
21import android.database.sqlite.SQLiteDatabase;
22import android.provider.ContactsContract.CommonDataKinds.Email;
23
24import com.android.providers.contacts.SearchIndexManager.IndexBuilder;
25import com.android.providers.contacts.aggregation.ContactAggregator;
26
27/**
28 * Handler for email address data rows.
29 */
30public class DataRowHandlerForEmail extends DataRowHandlerForCommonDataKind {
31
32    public DataRowHandlerForEmail(
33            Context context, ContactsDatabaseHelper dbHelper, ContactAggregator aggregator) {
34        super(context, dbHelper, aggregator, Email.CONTENT_ITEM_TYPE, Email.TYPE, Email.LABEL);
35    }
36
37    @Override
38    public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId,
39            ContentValues values) {
40        String email = values.getAsString(Email.DATA);
41
42        long dataId = super.insert(db, txContext, rawContactId, values);
43
44        fixRawContactDisplayName(db, txContext, rawContactId);
45        String address = mDbHelper.insertNameLookupForEmail(rawContactId, dataId, email);
46        if (address != null) {
47            triggerAggregation(txContext, rawContactId);
48        }
49        return dataId;
50    }
51
52    @Override
53    public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values,
54            Cursor c, boolean callerIsSyncAdapter) {
55        if (!super.update(db, txContext, values, c, callerIsSyncAdapter)) {
56            return false;
57        }
58
59        if (values.containsKey(Email.DATA)) {
60            long dataId = c.getLong(DataUpdateQuery._ID);
61            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
62
63            String address = values.getAsString(Email.DATA);
64            mDbHelper.deleteNameLookup(dataId);
65            mDbHelper.insertNameLookupForEmail(rawContactId, dataId, address);
66            fixRawContactDisplayName(db, txContext, rawContactId);
67            triggerAggregation(txContext, rawContactId);
68        }
69
70        return true;
71    }
72
73    @Override
74    public int delete(SQLiteDatabase db, TransactionContext txContext, Cursor c) {
75        long dataId = c.getLong(DataDeleteQuery._ID);
76        long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
77
78        int count = super.delete(db, txContext, c);
79
80        mDbHelper.deleteNameLookup(dataId);
81        fixRawContactDisplayName(db, txContext, rawContactId);
82        triggerAggregation(txContext, rawContactId);
83        return count;
84    }
85
86    @Override
87    protected int getTypeRank(int type) {
88        switch (type) {
89            case Email.TYPE_HOME: return 0;
90            case Email.TYPE_WORK: return 1;
91            case Email.TYPE_CUSTOM: return 2;
92            case Email.TYPE_OTHER: return 3;
93            default: return 1000;
94        }
95    }
96
97    @Override
98    public boolean containsSearchableColumns(ContentValues values) {
99        return values.containsKey(Email.ADDRESS);
100    }
101
102    @Override
103    public void appendSearchableData(IndexBuilder builder) {
104        builder.appendContentFromColumn(Email.ADDRESS);
105    }
106}
107