Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.contacts.common.model; |
| 17 | |
| 18 | import android.content.ContentProviderOperation; |
| 19 | import android.database.MatrixCursor; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.provider.ContactsContract; |
| 24 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 25 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 26 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
| 27 | |
| 28 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ComparisonChain; |
| 30 | import com.google.common.collect.Ordering; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.Arrays; |
| 33 | import java.util.Collection; |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 34 | import java.util.Collections; |
| 35 | import java.util.Comparator; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 36 | import java.util.List; |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 37 | import java.util.Objects; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Holds data for contacts loaded from the SIM card. |
| 41 | */ |
| 42 | public 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 Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 49 | mId = id; |
| 50 | mName = name; |
Marcus Hagerott | 19d7eca | 2016-11-21 16:05:31 -0800 | [diff] [blame] | 51 | mPhone = phone == null ? "" : phone.trim(); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 52 | mEmails = emails; |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 53 | } |
Marcus Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 54 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 55 | 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 Hagerott | 8e9e782 | 2016-11-22 10:29:40 -0800 | [diff] [blame] | 73 | // There is nothing to save so skip it. |
| 74 | if (!hasName() && !hasPhone() && !hasEmails()) return; |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 75 | |
| 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 Hagerott | 19d7eca | 2016-11-21 16:05:31 -0800 | [diff] [blame] | 87 | if (!mPhone.isEmpty()) { |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 88 | 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 Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 114 | public boolean hasName() { |
| 115 | return mName != null; |
| 116 | } |
| 117 | |
| 118 | public boolean hasPhone() { |
Marcus Hagerott | 19d7eca | 2016-11-21 16:05:31 -0800 | [diff] [blame] | 119 | return !mPhone.isEmpty(); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | public boolean hasEmails() { |
| 123 | return mEmails != null && mEmails.length > 0; |
| 124 | } |
| 125 | |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 126 | /** |
| 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 Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 158 | return mId == that.mId && Objects.equals(mName, that.mName) && |
| 159 | Objects.equals(mPhone, that.mPhone) && Arrays.equals(mEmails, that.mEmails); |
Marcus Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 160 | } |
| 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 Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 184 | 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 Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 200 | /** |
| 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 Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 216 | /** |
| 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 Hagerott | 6c42b4c | 2016-10-31 14:59:53 -0700 | [diff] [blame] | 224 | return Collections.binarySearch(contacts, new SimContact(-1, name, phone, null), |
| 225 | compareByPhoneThenName()); |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 226 | } |
| 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 Hagerott | 19d7eca | 2016-11-21 16:05:31 -0800 | [diff] [blame] | 233 | .compare(lhs.mPhone, rhs.mPhone) |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 234 | .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 Hagerott | 819214d | 2016-09-29 14:58:27 -0700 | [diff] [blame] | 249 | } |