summaryrefslogtreecommitdiff
path: root/java/com/android/contacts/common/model/RawContact.java
blob: 9efc8a87859048b0e3922f74e665a34260427031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.common.model;

import android.content.ContentValues;
import android.content.Context;
import android.content.Entity;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import com.android.contacts.common.model.account.AccountType;
import com.android.contacts.common.model.account.AccountWithDataSet;
import com.android.contacts.common.model.dataitem.DataItem;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * RawContact represents a single raw contact in the raw contacts database. It has specialized
 * getters/setters for raw contact items, and also contains a collection of DataItem objects. A
 * RawContact contains the information from a single account.
 *
 * <p>This allows RawContact objects to be thought of as a class with raw contact fields (like
 * account type, name, data set, sync state, etc.) and a list of DataItem objects that represent
 * contact information elements (like phone numbers, email, address, etc.).
 */
public final class RawContact implements Parcelable {

  /** Create for building the parcelable. */
  public static final Parcelable.Creator<RawContact> CREATOR =
      new Parcelable.Creator<RawContact>() {

        @Override
        public RawContact createFromParcel(Parcel parcel) {
          return new RawContact(parcel);
        }

        @Override
        public RawContact[] newArray(int i) {
          return new RawContact[i];
        }
      };

  private final ContentValues mValues;
  private final ArrayList<NamedDataItem> mDataItems;
  private AccountTypeManager mAccountTypeManager;

  /** A RawContact object can be created with or without a context. */
  public RawContact() {
    this(new ContentValues());
  }

  public RawContact(ContentValues values) {
    mValues = values;
    mDataItems = new ArrayList<NamedDataItem>();
  }

  /**
   * Constructor for the parcelable.
   *
   * @param parcel The parcel to de-serialize from.
   */
  private RawContact(Parcel parcel) {
    mValues = parcel.readParcelable(ContentValues.class.getClassLoader());
    mDataItems = new ArrayList<>();
    parcel.readTypedList(mDataItems, NamedDataItem.CREATOR);
  }

  public static RawContact createFrom(Entity entity) {
    final ContentValues values = entity.getEntityValues();
    final ArrayList<Entity.NamedContentValues> subValues = entity.getSubValues();

    RawContact rawContact = new RawContact(values);
    for (Entity.NamedContentValues subValue : subValues) {
      rawContact.addNamedDataItemValues(subValue.uri, subValue.values);
    }
    return rawContact;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel parcel, int i) {
    parcel.writeParcelable(mValues, i);
    parcel.writeTypedList(mDataItems);
  }

  public AccountTypeManager getAccountTypeManager(Context context) {
    if (mAccountTypeManager == null) {
      mAccountTypeManager = AccountTypeManager.getInstance(context);
    }
    return mAccountTypeManager;
  }

  public ContentValues getValues() {
    return mValues;
  }

  /** Returns the id of the raw contact. */
  public Long getId() {
    return getValues().getAsLong(RawContacts._ID);
  }

  /** Returns the account name of the raw contact. */
  public String getAccountName() {
    return getValues().getAsString(RawContacts.ACCOUNT_NAME);
  }

  /** Returns the account type of the raw contact. */
  public String getAccountTypeString() {
    return getValues().getAsString(RawContacts.ACCOUNT_TYPE);
  }

  /** Returns the data set of the raw contact. */
  public String getDataSet() {
    return getValues().getAsString(RawContacts.DATA_SET);
  }

  public boolean isDirty() {
    return getValues().getAsBoolean(RawContacts.DIRTY);
  }

  public String getSourceId() {
    return getValues().getAsString(RawContacts.SOURCE_ID);
  }

  public String getSync1() {
    return getValues().getAsString(RawContacts.SYNC1);
  }

  public String getSync2() {
    return getValues().getAsString(RawContacts.SYNC2);
  }

  public String getSync3() {
    return getValues().getAsString(RawContacts.SYNC3);
  }

  public String getSync4() {
    return getValues().getAsString(RawContacts.SYNC4);
  }

  public boolean isDeleted() {
    return getValues().getAsBoolean(RawContacts.DELETED);
  }

  public long getContactId() {
    return getValues().getAsLong(Contacts.Entity.CONTACT_ID);
  }

  public boolean isStarred() {
    return getValues().getAsBoolean(Contacts.STARRED);
  }

  public AccountType getAccountType(Context context) {
    return getAccountTypeManager(context).getAccountType(getAccountTypeString(), getDataSet());
  }

  /**
   * Sets the account name, account type, and data set strings. Valid combinations for account-name,
   * account-type, data-set 1) null, null, null (local account) 2) non-null, non-null, null (valid
   * account without data-set) 3) non-null, non-null, non-null (valid account with data-set)
   */
  private void setAccount(String accountName, String accountType, String dataSet) {
    final ContentValues values = getValues();
    if (accountName == null) {
      if (accountType == null && dataSet == null) {
        // This is a local account
        values.putNull(RawContacts.ACCOUNT_NAME);
        values.putNull(RawContacts.ACCOUNT_TYPE);
        values.putNull(RawContacts.DATA_SET);
        return;
      }
    } else {
      if (accountType != null) {
        // This is a valid account, either with or without a dataSet.
        values.put(RawContacts.ACCOUNT_NAME, accountName);
        values.put(RawContacts.ACCOUNT_TYPE, accountType);
        if (dataSet == null) {
          values.putNull(RawContacts.DATA_SET);
        } else {
          values.put(RawContacts.DATA_SET, dataSet);
        }
        return;
      }
    }
    throw new IllegalArgumentException(
        "Not a valid combination of account name, type, and data set.");
  }

  public void setAccount(AccountWithDataSet accountWithDataSet) {
    if (accountWithDataSet != null) {
      setAccount(accountWithDataSet.name, accountWithDataSet.type, accountWithDataSet.dataSet);
    } else {
      setAccount(null, null, null);
    }
  }

  public void setAccountToLocal() {
    setAccount(null, null, null);
  }

  /** Creates and inserts a DataItem object that wraps the content values, and returns it. */
  public void addDataItemValues(ContentValues values) {
    addNamedDataItemValues(Data.CONTENT_URI, values);
  }

  public NamedDataItem addNamedDataItemValues(Uri uri, ContentValues values) {
    final NamedDataItem namedItem = new NamedDataItem(uri, values);
    mDataItems.add(namedItem);
    return namedItem;
  }

  public ArrayList<ContentValues> getContentValues() {
    final ArrayList<ContentValues> list = new ArrayList<>(mDataItems.size());
    for (NamedDataItem dataItem : mDataItems) {
      if (Data.CONTENT_URI.equals(dataItem.mUri)) {
        list.add(dataItem.mContentValues);
      }
    }
    return list;
  }

  public List<DataItem> getDataItems() {
    final ArrayList<DataItem> list = new ArrayList<>(mDataItems.size());
    for (NamedDataItem dataItem : mDataItems) {
      if (Data.CONTENT_URI.equals(dataItem.mUri)) {
        list.add(DataItem.createFrom(dataItem.mContentValues));
      }
    }
    return list;
  }

  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("RawContact: ").append(mValues);
    for (RawContact.NamedDataItem namedDataItem : mDataItems) {
      sb.append("\n  ").append(namedDataItem.mUri);
      sb.append("\n  -> ").append(namedDataItem.mContentValues);
    }
    return sb.toString();
  }

  @Override
  public int hashCode() {
    return Objects.hash(mValues, mDataItems);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }

    RawContact other = (RawContact) obj;
    return Objects.equals(mValues, other.mValues) && Objects.equals(mDataItems, other.mDataItems);
  }

  public static final class NamedDataItem implements Parcelable {

    public static final Parcelable.Creator<NamedDataItem> CREATOR =
        new Parcelable.Creator<NamedDataItem>() {

          @Override
          public NamedDataItem createFromParcel(Parcel parcel) {
            return new NamedDataItem(parcel);
          }

          @Override
          public NamedDataItem[] newArray(int i) {
            return new NamedDataItem[i];
          }
        };
    public final Uri mUri;
    // This use to be a DataItem. DataItem creation is now delayed until the point of request
    // since there is no benefit to storing them here due to the multiple inheritance.
    // Eventually instanceof still has to be used anyways to determine which sub-class of
    // DataItem it is. And having parent DataItem's here makes it very difficult to serialize or
    // parcelable.
    //
    // Instead of having a common DataItem super class, we should refactor this to be a generic
    // Object where the object is a concrete class that no longer relies on ContentValues.
    // (this will also make the classes easier to use).
    // Since instanceof is used later anyways, having a list of Objects won't hurt and is no
    // worse than having a DataItem.
    public final ContentValues mContentValues;

    public NamedDataItem(Uri uri, ContentValues values) {
      this.mUri = uri;
      this.mContentValues = values;
    }

    public NamedDataItem(Parcel parcel) {
      this.mUri = parcel.readParcelable(Uri.class.getClassLoader());
      this.mContentValues = parcel.readParcelable(ContentValues.class.getClassLoader());
    }

    @Override
    public int describeContents() {
      return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
      parcel.writeParcelable(mUri, i);
      parcel.writeParcelable(mContentValues, i);
    }

    @Override
    public int hashCode() {
      return Objects.hash(mUri, mContentValues);
    }

    @Override
    public boolean equals(Object obj) {
      if (obj == null) {
        return false;
      }
      if (getClass() != obj.getClass()) {
        return false;
      }

      final NamedDataItem other = (NamedDataItem) obj;
      return Objects.equals(mUri, other.mUri)
          && Objects.equals(mContentValues, other.mContentValues);
    }
  }
}