DataRowHandlerForPhoneNumber.java revision f6d4922f664127d0455b45b1f7444c4553581282
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 com.android.providers.contacts.ContactsDatabaseHelper.PhoneColumns;
19import com.android.providers.contacts.ContactsDatabaseHelper.PhoneLookupColumns;
20import com.android.providers.contacts.ContactsDatabaseHelper.Tables;
21
22import android.content.ContentValues;
23import android.database.Cursor;
24import android.database.sqlite.SQLiteDatabase;
25import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.telephony.PhoneNumberUtils;
27import android.text.TextUtils;
28
29/**
30 * Handler for phone number data rows.
31 */
32public class DataRowHandlerForPhoneNumber extends DataRowHandlerForCommonDataKind {
33
34    public DataRowHandlerForPhoneNumber(
35            ContactsDatabaseHelper dbHelper, ContactAggregator aggregator) {
36        super(dbHelper, aggregator, Phone.CONTENT_ITEM_TYPE, Phone.TYPE, Phone.LABEL);
37    }
38
39    @Override
40    public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId,
41            ContentValues values) {
42        long dataId;
43        if (values.containsKey(Phone.NUMBER)) {
44            String number = values.getAsString(Phone.NUMBER);
45
46            String numberE164 =
47                    PhoneNumberUtils.formatNumberToE164(number, mDbHelper.getCurrentCountryIso());
48            if (numberE164 != null) {
49                values.put(PhoneColumns.NORMALIZED_NUMBER, numberE164);
50            }
51            dataId = super.insert(db, txContext, rawContactId, values);
52
53            updatePhoneLookup(db, rawContactId, dataId, number, numberE164);
54            mContactAggregator.updateHasPhoneNumber(db, rawContactId);
55            fixRawContactDisplayName(db, txContext, rawContactId);
56            if (numberE164 != null) {
57                triggerAggregation(rawContactId);
58            }
59        } else {
60            dataId = super.insert(db, txContext, rawContactId, values);
61        }
62        return dataId;
63    }
64
65    @Override
66    public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values,
67            Cursor c, boolean callerIsSyncAdapter) {
68        String number = null;
69        String numberE164 = null;
70        if (values.containsKey(Phone.NUMBER)) {
71            number = values.getAsString(Phone.NUMBER);
72            if (number != null) {
73                numberE164 = PhoneNumberUtils.formatNumberToE164(number,
74                        mDbHelper.getCurrentCountryIso());
75            }
76            if (numberE164 != null) {
77                values.put(PhoneColumns.NORMALIZED_NUMBER, numberE164);
78            }
79        }
80
81        if (!super.update(db, txContext, values, c, callerIsSyncAdapter)) {
82            return false;
83        }
84
85        if (values.containsKey(Phone.NUMBER)) {
86            long dataId = c.getLong(DataUpdateQuery._ID);
87            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
88            updatePhoneLookup(db, rawContactId, dataId, number, numberE164);
89            mContactAggregator.updateHasPhoneNumber(db, rawContactId);
90            fixRawContactDisplayName(db, txContext, rawContactId);
91            triggerAggregation(rawContactId);
92        }
93        return true;
94    }
95
96    @Override
97    public int delete(SQLiteDatabase db, TransactionContext txContext, Cursor c) {
98        long dataId = c.getLong(DataDeleteQuery._ID);
99        long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
100
101        int count = super.delete(db, txContext, c);
102
103        updatePhoneLookup(db, rawContactId, dataId, null, null);
104        mContactAggregator.updateHasPhoneNumber(db, rawContactId);
105        fixRawContactDisplayName(db, txContext, rawContactId);
106        triggerAggregation(rawContactId);
107        return count;
108    }
109
110    private void updatePhoneLookup(SQLiteDatabase db, long rawContactId, long dataId,
111            String number, String numberE164) {
112        mSelectionArgs1[0] = String.valueOf(dataId);
113        db.delete(Tables.PHONE_LOOKUP, PhoneLookupColumns.DATA_ID + "=?", mSelectionArgs1);
114        if (number != null) {
115            String normalizedNumber = PhoneNumberUtils.normalizeNumber(number);
116            if (!TextUtils.isEmpty(normalizedNumber)) {
117                ContentValues phoneValues = new ContentValues();
118                phoneValues.put(PhoneLookupColumns.RAW_CONTACT_ID, rawContactId);
119                phoneValues.put(PhoneLookupColumns.DATA_ID, dataId);
120                phoneValues.put(PhoneLookupColumns.NORMALIZED_NUMBER, normalizedNumber);
121                phoneValues.put(PhoneLookupColumns.MIN_MATCH,
122                        PhoneNumberUtils.toCallerIDMinMatch(normalizedNumber));
123                db.insert(Tables.PHONE_LOOKUP, null, phoneValues);
124
125                if (numberE164 != null && !numberE164.equals(normalizedNumber)) {
126                    phoneValues.put(PhoneLookupColumns.NORMALIZED_NUMBER, numberE164);
127                    phoneValues.put(PhoneLookupColumns.MIN_MATCH,
128                            PhoneNumberUtils.toCallerIDMinMatch(numberE164));
129                    db.insert(Tables.PHONE_LOOKUP, null, phoneValues);
130                }
131            }
132        }
133    }
134
135    @Override
136    protected int getTypeRank(int type) {
137        switch (type) {
138            case Phone.TYPE_MOBILE: return 0;
139            case Phone.TYPE_WORK: return 1;
140            case Phone.TYPE_HOME: return 2;
141            case Phone.TYPE_PAGER: return 3;
142            case Phone.TYPE_CUSTOM: return 4;
143            case Phone.TYPE_OTHER: return 5;
144            case Phone.TYPE_FAX_WORK: return 6;
145            case Phone.TYPE_FAX_HOME: return 7;
146            default: return 1000;
147        }
148    }
149}