blob: 7babe27ca185ef8e82a24d7a525e3a4bd3911160 [file] [log] [blame]
Marcus Hagerott819214d2016-09-29 14:58:27 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Gary Mai69c182a2016-12-05 13:07:03 -080016package com.android.contacts.model;
Marcus Hagerott819214d2016-09-29 14:58:27 -070017
18import android.content.ContentProviderOperation;
19import android.database.MatrixCursor;
20import android.net.Uri;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.provider.ContactsContract;
24import android.provider.ContactsContract.CommonDataKinds.Email;
25import android.provider.ContactsContract.CommonDataKinds.Phone;
26import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Marcus Hagerotta75206b2016-11-29 14:40:59 -080027import android.text.TextUtils;
Marcus Hagerott819214d2016-09-29 14:58:27 -070028
Gary Mai0a49afa2016-12-05 15:53:58 -080029import com.android.contacts.ContactPhotoManager;
Gary Mai69c182a2016-12-05 13:07:03 -080030import com.android.contacts.model.account.AccountWithDataSet;
Gary Mai0a49afa2016-12-05 15:53:58 -080031
Marcus Hagerott2aa31982016-10-25 14:36:25 -070032import com.google.common.collect.ComparisonChain;
33import com.google.common.collect.Ordering;
Marcus Hagerott819214d2016-09-29 14:58:27 -070034
35import java.util.Arrays;
36import java.util.Collection;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070037import java.util.Collections;
38import java.util.Comparator;
Marcus Hagerott819214d2016-09-29 14:58:27 -070039import java.util.List;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070040import java.util.Objects;
Marcus Hagerott819214d2016-09-29 14:58:27 -070041
42/**
43 * Holds data for contacts loaded from the SIM card.
44 */
45public class SimContact implements Parcelable {
46 private final long mId;
47 private final String mName;
48 private final String mPhone;
49 private final String[] mEmails;
50
Marcus Hagerotta75206b2016-11-29 14:40:59 -080051 public SimContact(long id, String name, String phone) {
52 this(id, name, phone, null);
53 }
54
Marcus Hagerott819214d2016-09-29 14:58:27 -070055 public SimContact(long id, String name, String phone, String[] emails) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -070056 mId = id;
57 mName = name;
Marcus Hagerott19d7eca2016-11-21 16:05:31 -080058 mPhone = phone == null ? "" : phone.trim();
Marcus Hagerott2aa31982016-10-25 14:36:25 -070059 mEmails = emails;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070060 }
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -070061
Marcus Hagerotta75206b2016-11-29 14:40:59 -080062 public SimContact(SimContact other) {
63 this(other.mId, other.mName, other.mPhone, other.mEmails);
64 }
65
Marcus Hagerott819214d2016-09-29 14:58:27 -070066 public long getId() {
67 return mId;
68 }
69
70 public String getName() {
71 return mName;
72 }
73
74 public String getPhone() {
75 return mPhone;
76 }
77
78 public String[] getEmails() {
79 return mEmails;
80 }
81
82 public void appendCreateContactOperations(List<ContentProviderOperation> ops,
83 AccountWithDataSet targetAccount) {
Marcus Hagerott8e9e7822016-11-22 10:29:40 -080084 // There is nothing to save so skip it.
85 if (!hasName() && !hasPhone() && !hasEmails()) return;
Marcus Hagerott819214d2016-09-29 14:58:27 -070086
87 final int rawContactOpIndex = ops.size();
88 ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
89 .withYieldAllowed(true)
90 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, targetAccount.name)
91 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, targetAccount.type)
92 .withValue(ContactsContract.RawContacts.DATA_SET, targetAccount.dataSet)
93 .build());
94 if (mName != null) {
95 ops.add(createInsertOp(rawContactOpIndex, StructuredName.CONTENT_ITEM_TYPE,
96 StructuredName.DISPLAY_NAME, mName));
97 }
Marcus Hagerott19d7eca2016-11-21 16:05:31 -080098 if (!mPhone.isEmpty()) {
Marcus Hagerott819214d2016-09-29 14:58:27 -070099 ops.add(createInsertOp(rawContactOpIndex, Phone.CONTENT_ITEM_TYPE,
100 Phone.NUMBER, mPhone));
101 }
102 if (mEmails != null) {
103 for (String email : mEmails) {
104 ops.add(createInsertOp(rawContactOpIndex, Email.CONTENT_ITEM_TYPE,
105 Email.ADDRESS, email));
106 }
107 }
108 }
109
110 private ContentProviderOperation createInsertOp(int rawContactOpIndex, String mimeType,
111 String column, String value) {
112 return ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
113 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactOpIndex)
114 .withValue(ContactsContract.Data.MIMETYPE, mimeType)
115 .withValue(column, value)
116 .build();
117 }
118
119 public void appendAsContactRow(MatrixCursor cursor) {
120 cursor.newRow().add(ContactsContract.Contacts._ID, mId)
121 .add(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, mName)
122 .add(ContactsContract.Contacts.LOOKUP_KEY, getLookupKey());
123 }
124
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700125 public boolean hasName() {
Marcus Hagerotta75206b2016-11-29 14:40:59 -0800126 return !TextUtils.isEmpty(mName);
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700127 }
128
129 public boolean hasPhone() {
Marcus Hagerott19d7eca2016-11-21 16:05:31 -0800130 return !mPhone.isEmpty();
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700131 }
132
133 public boolean hasEmails() {
134 return mEmails != null && mEmails.length > 0;
135 }
136
Marcus Hagerott819214d2016-09-29 14:58:27 -0700137 /**
138 * Generate a "fake" lookup key. This is needed because
Gary Mai0a49afa2016-12-05 15:53:58 -0800139 * {@link ContactPhotoManager} will only generate a letter avatar
Marcus Hagerott819214d2016-09-29 14:58:27 -0700140 * if the contact has a lookup key.
141 */
142 private String getLookupKey() {
143 if (mName != null) {
144 return "sim-n-" + Uri.encode(mName);
145 } else if (mPhone != null) {
146 return "sim-p-" + Uri.encode(mPhone);
147 } else {
148 return null;
149 }
150 }
151
152 @Override
153 public String toString() {
154 return "SimContact{" +
155 "mId=" + mId +
156 ", mName='" + mName + '\'' +
157 ", mPhone='" + mPhone + '\'' +
158 ", mEmails=" + Arrays.toString(mEmails) +
159 '}';
160 }
161
162 @Override
163 public boolean equals(Object o) {
164 if (this == o) return true;
165 if (o == null || getClass() != o.getClass()) return false;
166
167 final SimContact that = (SimContact) o;
168
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700169 return mId == that.mId && Objects.equals(mName, that.mName) &&
170 Objects.equals(mPhone, that.mPhone) && Arrays.equals(mEmails, that.mEmails);
Marcus Hagerott819214d2016-09-29 14:58:27 -0700171 }
172
173 @Override
174 public int hashCode() {
175 int result = (int) (mId ^ (mId >>> 32));
176 result = 31 * result + (mName != null ? mName.hashCode() : 0);
177 result = 31 * result + (mPhone != null ? mPhone.hashCode() : 0);
178 result = 31 * result + Arrays.hashCode(mEmails);
179 return result;
180 }
181
182 @Override
183 public int describeContents() {
184 return 0;
185 }
186
187 @Override
188 public void writeToParcel(Parcel dest, int flags) {
189 dest.writeLong(mId);
190 dest.writeString(mName);
191 dest.writeString(mPhone);
192 dest.writeStringArray(mEmails);
193 }
194
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700195 public static final Creator<SimContact> CREATOR = new Creator<SimContact>() {
196 @Override
197 public SimContact createFromParcel(Parcel source) {
198 final long id = source.readLong();
199 final String name = source.readString();
200 final String phone = source.readString();
201 final String[] emails = source.createStringArray();
202 return new SimContact(id, name, phone, emails);
203 }
204
205 @Override
206 public SimContact[] newArray(int size) {
207 return new SimContact[size];
208 }
209 };
210
Marcus Hagerott819214d2016-09-29 14:58:27 -0700211 /**
212 * Convert a collection of SIM contacts to a Cursor matching a query from
213 * {@link android.provider.ContactsContract.Contacts#CONTENT_URI} with the provided projection.
214 *
215 * This allows a collection of SIM contacts to be displayed using the existing adapters for
216 * contacts.
217 */
218 public static final MatrixCursor convertToContactsCursor(Collection<SimContact> contacts,
219 String[] projection) {
220 final MatrixCursor result = new MatrixCursor(projection);
221 for (SimContact contact : contacts) {
222 contact.appendAsContactRow(result);
223 }
224 return result;
225 }
226
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700227 /**
228 * Returns the index of a contact with a matching name and phone
229 * @param contacts list to search. Should be sorted using
230 * {@link SimContact#compareByPhoneThenName()}
231 * @param phone the phone to search for
232 * @param name the name to search for
233 */
234 public static int findByPhoneAndName(List<SimContact> contacts, String phone, String name) {
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700235 return Collections.binarySearch(contacts, new SimContact(-1, name, phone, null),
236 compareByPhoneThenName());
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700237 }
238
239 public static final Comparator<SimContact> compareByPhoneThenName() {
240 return new Comparator<SimContact>() {
241 @Override
242 public int compare(SimContact lhs, SimContact rhs) {
243 return ComparisonChain.start()
Marcus Hagerott19d7eca2016-11-21 16:05:31 -0800244 .compare(lhs.mPhone, rhs.mPhone)
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700245 .compare(lhs.mName, rhs.mName, Ordering.<String>natural().nullsFirst())
246 .result();
247 }
248 };
249 }
250
251 public static final Comparator<SimContact> compareById() {
252 return new Comparator<SimContact>() {
253 @Override
254 public int compare(SimContact lhs, SimContact rhs) {
255 // We assume ids are unique.
256 return Long.compare(lhs.mId, rhs.mId);
257 }
258 };
259 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700260}