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.BaseTypes; 23 import android.text.TextUtils; 24 25 import com.android.providers.contacts.aggregation.ContactAggregator; 26 27 /** 28 * Superclass for data row handlers that deal with types (e.g. Home, Work, Other) and 29 * labels, which are custom types. 30 */ 31 public class DataRowHandlerForCommonDataKind extends DataRowHandler { 32 33 private final String mTypeColumn; 34 private final String mLabelColumn; 35 DataRowHandlerForCommonDataKind(Context context, ContactsDatabaseHelper dbHelper, ContactAggregator aggregator, String mimetype, String typeColumn, String labelColumn)36 public DataRowHandlerForCommonDataKind(Context context, ContactsDatabaseHelper dbHelper, 37 ContactAggregator aggregator, String mimetype, String typeColumn, String labelColumn) { 38 super(context, dbHelper, aggregator, mimetype); 39 mTypeColumn = typeColumn; 40 mLabelColumn = labelColumn; 41 } 42 43 @Override insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId, ContentValues values)44 public long insert(SQLiteDatabase db, TransactionContext txContext, long rawContactId, 45 ContentValues values) { 46 enforceTypeAndLabel(values); 47 return super.insert(db, txContext, rawContactId, values); 48 } 49 50 @Override update(SQLiteDatabase db, TransactionContext txContext, ContentValues values, Cursor c, boolean callerIsSyncAdapter)51 public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values, 52 Cursor c, boolean callerIsSyncAdapter) { 53 final long dataId = c.getLong(DataUpdateQuery._ID); 54 final ContentValues augmented = getAugmentedValues(db, dataId, values); 55 if (augmented == null) { // No change 56 return false; 57 } 58 enforceTypeAndLabel(augmented); 59 return super.update(db, txContext, values, c, callerIsSyncAdapter); 60 } 61 62 /** 63 * If the given {@link ContentValues} defines {@link #mTypeColumn}, 64 * enforce that {@link #mLabelColumn} only appears when type is 65 * {@link BaseTypes#TYPE_CUSTOM}. Exception is thrown otherwise. 66 */ enforceTypeAndLabel(ContentValues augmented)67 private void enforceTypeAndLabel(ContentValues augmented) { 68 final boolean hasType = !TextUtils.isEmpty(augmented.getAsString(mTypeColumn)); 69 final boolean hasLabel = !TextUtils.isEmpty(augmented.getAsString(mLabelColumn)); 70 71 if (hasLabel && !hasType) { 72 // When label exists, assert that some type is defined 73 throw new IllegalArgumentException(mTypeColumn + " must be specified when " 74 + mLabelColumn + " is defined."); 75 } 76 } 77 78 @Override hasSearchableData()79 public boolean hasSearchableData() { 80 return true; 81 } 82 } 83