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 android.content.ContentValues;
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.database.DatabaseUtils;
     22 import android.database.sqlite.SQLiteDatabase;
     23 import android.provider.ContactsContract.CommonDataKinds.Organization;
     24 import android.provider.ContactsContract.Data;
     25 
     26 import com.android.providers.contacts.ContactsDatabaseHelper.Tables;
     27 import com.android.providers.contacts.SearchIndexManager.IndexBuilder;
     28 import com.android.providers.contacts.aggregation.ContactAggregator;
     29 
     30 /**
     31  * Handler for organization data rows.
     32  */
     33 public class DataRowHandlerForOrganization extends DataRowHandlerForCommonDataKind {
     34 
     35     public DataRowHandlerForOrganization(Context context, ContactsDatabaseHelper dbHelper,
     36             ContactAggregator aggregator) {
     37         super(context, dbHelper, aggregator,
     38                 Organization.CONTENT_ITEM_TYPE, Organization.TYPE, Organization.LABEL);
     39     }
     40 
     41     @Override
     42     public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId,
     43             ContentValues values) {
     44         String company = values.getAsString(Organization.COMPANY);
     45         String title = values.getAsString(Organization.TITLE);
     46 
     47         long dataId = super.insert(db, txContext, rawContactId, values);
     48 
     49         fixRawContactDisplayName(db, txContext, rawContactId);
     50         return dataId;
     51     }
     52 
     53     @Override
     54     public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values,
     55             Cursor c, boolean callerIsSyncAdapter) {
     56         if (!super.update(db, txContext, values, c, callerIsSyncAdapter)) {
     57             return false;
     58         }
     59 
     60         boolean containsCompany = values.containsKey(Organization.COMPANY);
     61         boolean containsTitle = values.containsKey(Organization.TITLE);
     62         if (containsCompany || containsTitle) {
     63             long dataId = c.getLong(DataUpdateQuery._ID);
     64             long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
     65 
     66             String company;
     67 
     68             if (containsCompany) {
     69                 company = values.getAsString(Organization.COMPANY);
     70             } else {
     71                 mSelectionArgs1[0] = String.valueOf(dataId);
     72                 company = DatabaseUtils.stringForQuery(db,
     73                         "SELECT " + Organization.COMPANY +
     74                         " FROM " + Tables.DATA +
     75                         " WHERE " + Data._ID + "=?", mSelectionArgs1);
     76             }
     77 
     78             String title;
     79             if (containsTitle) {
     80                 title = values.getAsString(Organization.TITLE);
     81             } else {
     82                 mSelectionArgs1[0] = String.valueOf(dataId);
     83                 title = DatabaseUtils.stringForQuery(db,
     84                         "SELECT " + Organization.TITLE +
     85                         " FROM " + Tables.DATA +
     86                         " WHERE " + Data._ID + "=?", mSelectionArgs1);
     87             }
     88 
     89             mDbHelper.deleteNameLookup(dataId);
     90             fixRawContactDisplayName(db, txContext, rawContactId);
     91         }
     92         return true;
     93     }
     94 
     95     @Override
     96     public int delete(SQLiteDatabase db, TransactionContext txContext, Cursor c) {
     97         long dataId = c.getLong(DataUpdateQuery._ID);
     98         long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
     99 
    100         int count = super.delete(db, txContext, c);
    101         fixRawContactDisplayName(db, txContext, rawContactId);
    102         mDbHelper.deleteNameLookup(dataId);
    103         return count;
    104     }
    105 
    106     @Override
    107     protected int getTypeRank(int type) {
    108         switch (type) {
    109             case Organization.TYPE_WORK: return 0;
    110             case Organization.TYPE_CUSTOM: return 1;
    111             case Organization.TYPE_OTHER: return 2;
    112             default: return 1000;
    113         }
    114     }
    115 
    116     @Override
    117     public boolean containsSearchableColumns(ContentValues values) {
    118         return values.containsKey(Organization.COMPANY)
    119                 || values.containsKey(Organization.DEPARTMENT)
    120                 || values.containsKey(Organization.JOB_DESCRIPTION)
    121                 || values.containsKey(Organization.OFFICE_LOCATION)
    122                 || values.containsKey(Organization.PHONETIC_NAME)
    123                 || values.containsKey(Organization.SYMBOL)
    124                 || values.containsKey(Organization.TITLE);
    125     }
    126 
    127     @Override
    128     public void appendSearchableData(IndexBuilder builder) {
    129         builder.appendContentFromColumn(Organization.TITLE);
    130         builder.appendContentFromColumn(Organization.COMPANY, IndexBuilder.SEPARATOR_COMMA);
    131         builder.appendContentFromColumn(Organization.PHONETIC_NAME,
    132                 IndexBuilder.SEPARATOR_PARENTHESES);
    133         builder.appendContentFromColumn(Organization.SYMBOL, IndexBuilder.SEPARATOR_PARENTHESES);
    134         builder.appendContentFromColumn(Organization.DEPARTMENT, IndexBuilder.SEPARATOR_SLASH);
    135         builder.appendContentFromColumn(Organization.OFFICE_LOCATION, IndexBuilder.SEPARATOR_SLASH);
    136         builder.appendContentFromColumn(Organization.JOB_DESCRIPTION, IndexBuilder.SEPARATOR_SLASH);
    137     }
    138 }
    139