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;
23import com.android.providers.contacts.SearchIndexManager.IndexBuilder;
24import com.android.providers.contacts.aggregation.AbstractContactAggregator;
25
26/**
27 * Handler for email address data rows.
28 */
29public class DataRowHandlerForEmail extends DataRowHandlerForCommonDataKind {
30
31    public DataRowHandlerForEmail(
32            Context context, ContactsDatabaseHelper dbHelper, AbstractContactAggregator aggregator) {
33        super(context, dbHelper, aggregator, Email.CONTENT_ITEM_TYPE, Email.TYPE, Email.LABEL);
34    }
35
36    @Override
37    public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId,
38            ContentValues values) {
39        String email = values.getAsString(Email.DATA);
40
41        long dataId = super.insert(db, txContext, rawContactId, values);
42
43        fixRawContactDisplayName(db, txContext, rawContactId);
44        String address = mDbHelper.insertNameLookupForEmail(rawContactId, dataId, email);
45        if (address != null) {
46            triggerAggregation(txContext, rawContactId);
47        }
48        return dataId;
49    }
50
51    @Override
52    public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values,
53            Cursor c, boolean callerIsSyncAdapter) {
54        if (!super.update(db, txContext, values, c, callerIsSyncAdapter)) {
55            return false;
56        }
57
58        if (values.containsKey(Email.DATA)) {
59            long dataId = c.getLong(DataUpdateQuery._ID);
60            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
61
62            String address = values.getAsString(Email.DATA);
63            mDbHelper.deleteNameLookup(dataId);
64            mDbHelper.insertNameLookupForEmail(rawContactId, dataId, address);
65            fixRawContactDisplayName(db, txContext, rawContactId);
66            triggerAggregation(txContext, rawContactId);
67        }
68
69        return true;
70    }
71
72    @Override
73    public int delete(SQLiteDatabase db, TransactionContext txContext, Cursor c) {
74        long dataId = c.getLong(DataDeleteQuery._ID);
75        long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
76
77        int count = super.delete(db, txContext, c);
78
79        mDbHelper.deleteNameLookup(dataId);
80        fixRawContactDisplayName(db, txContext, rawContactId);
81        triggerAggregation(txContext, rawContactId);
82        return count;
83    }
84
85    @Override
86    protected int getTypeRank(int type) {
87        switch (type) {
88            case Email.TYPE_HOME: return 0;
89            case Email.TYPE_WORK: return 1;
90            case Email.TYPE_CUSTOM: return 2;
91            case Email.TYPE_OTHER: return 3;
92            default: return 1000;
93        }
94    }
95
96    @Override
97    public boolean containsSearchableColumns(ContentValues values) {
98        return values.containsKey(Email.ADDRESS);
99    }
100
101    @Override
102    public void appendSearchableData(IndexBuilder builder) {
103        builder.appendContentFromColumn(Email.ADDRESS);
104    }
105}
106