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