blob: 2d260290141ccb08f48718e2cd50af2d63669145 [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 */
16package com.android.contacts.common.model;
17
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;
27
28import com.android.contacts.common.model.account.AccountWithDataSet;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070029import com.google.common.collect.ComparisonChain;
30import com.google.common.collect.Ordering;
Marcus Hagerott819214d2016-09-29 14:58:27 -070031
32import java.util.Arrays;
33import java.util.Collection;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070034import java.util.Collections;
35import java.util.Comparator;
Marcus Hagerott819214d2016-09-29 14:58:27 -070036import java.util.List;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070037import java.util.Objects;
Marcus Hagerott819214d2016-09-29 14:58:27 -070038
39/**
40 * Holds data for contacts loaded from the SIM card.
41 */
42public class SimContact implements Parcelable {
43 private final long mId;
44 private final String mName;
45 private final String mPhone;
46 private final String[] mEmails;
47
48 public SimContact(long id, String name, String phone, String[] emails) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -070049 mId = id;
50 mName = name;
Marcus Hagerott19d7eca2016-11-21 16:05:31 -080051 mPhone = phone == null ? "" : phone.trim();
Marcus Hagerott2aa31982016-10-25 14:36:25 -070052 mEmails = emails;
Marcus Hagerott2aa31982016-10-25 14:36:25 -070053 }
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -070054
Marcus Hagerott819214d2016-09-29 14:58:27 -070055 public long getId() {
56 return mId;
57 }
58
59 public String getName() {
60 return mName;
61 }
62
63 public String getPhone() {
64 return mPhone;
65 }
66
67 public String[] getEmails() {
68 return mEmails;
69 }
70
71 public void appendCreateContactOperations(List<ContentProviderOperation> ops,
72 AccountWithDataSet targetAccount) {
Marcus Hagerott8e9e7822016-11-22 10:29:40 -080073 // There is nothing to save so skip it.
74 if (!hasName() && !hasPhone() && !hasEmails()) return;
Marcus Hagerott819214d2016-09-29 14:58:27 -070075
76 final int rawContactOpIndex = ops.size();
77 ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
78 .withYieldAllowed(true)
79 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, targetAccount.name)
80 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, targetAccount.type)
81 .withValue(ContactsContract.RawContacts.DATA_SET, targetAccount.dataSet)
82 .build());
83 if (mName != null) {
84 ops.add(createInsertOp(rawContactOpIndex, StructuredName.CONTENT_ITEM_TYPE,
85 StructuredName.DISPLAY_NAME, mName));
86 }
Marcus Hagerott19d7eca2016-11-21 16:05:31 -080087 if (!mPhone.isEmpty()) {
Marcus Hagerott819214d2016-09-29 14:58:27 -070088 ops.add(createInsertOp(rawContactOpIndex, Phone.CONTENT_ITEM_TYPE,
89 Phone.NUMBER, mPhone));
90 }
91 if (mEmails != null) {
92 for (String email : mEmails) {
93 ops.add(createInsertOp(rawContactOpIndex, Email.CONTENT_ITEM_TYPE,
94 Email.ADDRESS, email));
95 }
96 }
97 }
98
99 private ContentProviderOperation createInsertOp(int rawContactOpIndex, String mimeType,
100 String column, String value) {
101 return ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
102 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactOpIndex)
103 .withValue(ContactsContract.Data.MIMETYPE, mimeType)
104 .withValue(column, value)
105 .build();
106 }
107
108 public void appendAsContactRow(MatrixCursor cursor) {
109 cursor.newRow().add(ContactsContract.Contacts._ID, mId)
110 .add(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, mName)
111 .add(ContactsContract.Contacts.LOOKUP_KEY, getLookupKey());
112 }
113
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700114 public boolean hasName() {
115 return mName != null;
116 }
117
118 public boolean hasPhone() {
Marcus Hagerott19d7eca2016-11-21 16:05:31 -0800119 return !mPhone.isEmpty();
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700120 }
121
122 public boolean hasEmails() {
123 return mEmails != null && mEmails.length > 0;
124 }
125
Marcus Hagerott819214d2016-09-29 14:58:27 -0700126 /**
127 * Generate a "fake" lookup key. This is needed because
128 * {@link com.android.contacts.common.ContactPhotoManager} will only generate a letter avatar
129 * if the contact has a lookup key.
130 */
131 private String getLookupKey() {
132 if (mName != null) {
133 return "sim-n-" + Uri.encode(mName);
134 } else if (mPhone != null) {
135 return "sim-p-" + Uri.encode(mPhone);
136 } else {
137 return null;
138 }
139 }
140
141 @Override
142 public String toString() {
143 return "SimContact{" +
144 "mId=" + mId +
145 ", mName='" + mName + '\'' +
146 ", mPhone='" + mPhone + '\'' +
147 ", mEmails=" + Arrays.toString(mEmails) +
148 '}';
149 }
150
151 @Override
152 public boolean equals(Object o) {
153 if (this == o) return true;
154 if (o == null || getClass() != o.getClass()) return false;
155
156 final SimContact that = (SimContact) o;
157
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700158 return mId == that.mId && Objects.equals(mName, that.mName) &&
159 Objects.equals(mPhone, that.mPhone) && Arrays.equals(mEmails, that.mEmails);
Marcus Hagerott819214d2016-09-29 14:58:27 -0700160 }
161
162 @Override
163 public int hashCode() {
164 int result = (int) (mId ^ (mId >>> 32));
165 result = 31 * result + (mName != null ? mName.hashCode() : 0);
166 result = 31 * result + (mPhone != null ? mPhone.hashCode() : 0);
167 result = 31 * result + Arrays.hashCode(mEmails);
168 return result;
169 }
170
171 @Override
172 public int describeContents() {
173 return 0;
174 }
175
176 @Override
177 public void writeToParcel(Parcel dest, int flags) {
178 dest.writeLong(mId);
179 dest.writeString(mName);
180 dest.writeString(mPhone);
181 dest.writeStringArray(mEmails);
182 }
183
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700184 public static final Creator<SimContact> CREATOR = new Creator<SimContact>() {
185 @Override
186 public SimContact createFromParcel(Parcel source) {
187 final long id = source.readLong();
188 final String name = source.readString();
189 final String phone = source.readString();
190 final String[] emails = source.createStringArray();
191 return new SimContact(id, name, phone, emails);
192 }
193
194 @Override
195 public SimContact[] newArray(int size) {
196 return new SimContact[size];
197 }
198 };
199
Marcus Hagerott819214d2016-09-29 14:58:27 -0700200 /**
201 * Convert a collection of SIM contacts to a Cursor matching a query from
202 * {@link android.provider.ContactsContract.Contacts#CONTENT_URI} with the provided projection.
203 *
204 * This allows a collection of SIM contacts to be displayed using the existing adapters for
205 * contacts.
206 */
207 public static final MatrixCursor convertToContactsCursor(Collection<SimContact> contacts,
208 String[] projection) {
209 final MatrixCursor result = new MatrixCursor(projection);
210 for (SimContact contact : contacts) {
211 contact.appendAsContactRow(result);
212 }
213 return result;
214 }
215
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700216 /**
217 * Returns the index of a contact with a matching name and phone
218 * @param contacts list to search. Should be sorted using
219 * {@link SimContact#compareByPhoneThenName()}
220 * @param phone the phone to search for
221 * @param name the name to search for
222 */
223 public static int findByPhoneAndName(List<SimContact> contacts, String phone, String name) {
Marcus Hagerott6c42b4c2016-10-31 14:59:53 -0700224 return Collections.binarySearch(contacts, new SimContact(-1, name, phone, null),
225 compareByPhoneThenName());
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700226 }
227
228 public static final Comparator<SimContact> compareByPhoneThenName() {
229 return new Comparator<SimContact>() {
230 @Override
231 public int compare(SimContact lhs, SimContact rhs) {
232 return ComparisonChain.start()
Marcus Hagerott19d7eca2016-11-21 16:05:31 -0800233 .compare(lhs.mPhone, rhs.mPhone)
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700234 .compare(lhs.mName, rhs.mName, Ordering.<String>natural().nullsFirst())
235 .result();
236 }
237 };
238 }
239
240 public static final Comparator<SimContact> compareById() {
241 return new Comparator<SimContact>() {
242 @Override
243 public int compare(SimContact lhs, SimContact rhs) {
244 // We assume ids are unique.
245 return Long.compare(lhs.mId, rhs.mId);
246 }
247 };
248 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700249}