• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.providers.contacts.aggregation.AbstractContactAggregator;
25 
26 /**
27  * Superclass for data row handlers that deal with types (e.g. Home, Work, Other) and
28  * labels, which are custom types.
29  */
30 public class DataRowHandlerForCommonDataKind extends DataRowHandler {
31 
32     private final String mTypeColumn;
33     private final String mLabelColumn;
34 
DataRowHandlerForCommonDataKind(Context context, ContactsDatabaseHelper dbHelper, AbstractContactAggregator aggregator, String mimetype, String typeColumn, String labelColumn)35     public DataRowHandlerForCommonDataKind(Context context, ContactsDatabaseHelper dbHelper,
36             AbstractContactAggregator aggregator, String mimetype, String typeColumn,
37             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, boolean callerIsMetadataSyncAdapter)51     public boolean update(SQLiteDatabase db, TransactionContext txContext, ContentValues values,
52             Cursor c, boolean callerIsSyncAdapter, boolean callerIsMetadataSyncAdapter) {
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                 callerIsMetadataSyncAdapter);
61     }
62 
63     /**
64      * If the given {@link ContentValues} defines {@link #mTypeColumn},
65      * enforce that {@link #mLabelColumn} only appears when type is
66      * {@link BaseTypes#TYPE_CUSTOM}. Exception is thrown otherwise.
67      */
enforceTypeAndLabel(ContentValues augmented)68     private void enforceTypeAndLabel(ContentValues augmented) {
69         final boolean hasType = !TextUtils.isEmpty(augmented.getAsString(mTypeColumn));
70         final boolean hasLabel = !TextUtils.isEmpty(augmented.getAsString(mLabelColumn));
71 
72         if (hasLabel && !hasType) {
73             // When label exists, assert that some type is defined
74             throw new IllegalArgumentException(mTypeColumn + " must be specified when "
75                     + mLabelColumn + " is defined.");
76         }
77     }
78 
79     @Override
hasSearchableData()80     public boolean hasSearchableData() {
81         return true;
82     }
83 }
84