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