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 android.content.ContentValues; 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.database.sqlite.SQLiteDatabase; 22 import android.provider.ContactsContract.CommonDataKinds.Nickname; 23 import android.text.TextUtils; 24 25 import com.android.providers.contacts.SearchIndexManager.IndexBuilder; 26 import com.android.providers.contacts.aggregation.ContactAggregator; 27 28 /** 29 * Handler for nickname data rows. 30 */ 31 public class DataRowHandlerForNickname extends DataRowHandlerForCommonDataKind { 32 33 public DataRowHandlerForNickname( 34 Context context, ContactsDatabaseHelper dbHelper, ContactAggregator aggregator) { 35 super(context, dbHelper, aggregator, Nickname.CONTENT_ITEM_TYPE, Nickname.TYPE, 36 Nickname.LABEL); 37 } 38 39 @Override 40 public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId, 41 ContentValues values) { 42 String nickname = values.getAsString(Nickname.NAME); 43 44 long dataId = super.insert(db, txContext, rawContactId, values); 45 46 if (!TextUtils.isEmpty(nickname)) { 47 fixRawContactDisplayName(db, txContext, rawContactId); 48 mDbHelper.insertNameLookupForNickname(rawContactId, dataId, nickname); 49 triggerAggregation(txContext, rawContactId); 50 } 51 return dataId; 52 } 53 54 @Override 55 public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values, 56 Cursor c, boolean callerIsSyncAdapter) { 57 long dataId = c.getLong(DataUpdateQuery._ID); 58 long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID); 59 60 if (!super.update(db, txContext, values, c, callerIsSyncAdapter)) { 61 return false; 62 } 63 64 if (values.containsKey(Nickname.NAME)) { 65 String nickname = values.getAsString(Nickname.NAME); 66 mDbHelper.deleteNameLookup(dataId); 67 mDbHelper.insertNameLookupForNickname(rawContactId, dataId, nickname); 68 fixRawContactDisplayName(db, txContext, rawContactId); 69 triggerAggregation(txContext, rawContactId); 70 } 71 72 return true; 73 } 74 75 @Override 76 public int delete(SQLiteDatabase db, TransactionContext txContext, Cursor c) { 77 long dataId = c.getLong(DataDeleteQuery._ID); 78 long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID); 79 80 int count = super.delete(db, txContext, c); 81 82 mDbHelper.deleteNameLookup(dataId); 83 fixRawContactDisplayName(db, txContext, rawContactId); 84 triggerAggregation(txContext, rawContactId); 85 return count; 86 } 87 88 @Override 89 public boolean containsSearchableColumns(ContentValues values) { 90 return values.containsKey(Nickname.NAME); 91 } 92 93 @Override 94 public void appendSearchableData(IndexBuilder builder) { 95 builder.appendContentFromColumn(Nickname.NAME); 96 } 97 } 98