1 /* 2 * Copyright (C) 2015 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.car.dialer; 17 18 import android.content.Context; 19 import android.database.Cursor; 20 import android.provider.ContactsContract; 21 import android.support.annotation.Nullable; 22 import android.text.TextUtils; 23 import com.android.car.dialer.telecom.PhoneLoader; 24 import com.android.car.dialer.telecom.TelecomUtils; 25 26 /** 27 * Encapsulates data about a phone Contact entry. Typically loaded from the local Contact store. 28 */ 29 public class ContactEntry implements Comparable<ContactEntry> { 30 private final Context mContext; 31 32 @Nullable 33 public String name; 34 public String number; 35 public boolean isStarred; 36 public int pinnedPosition; 37 38 /** 39 * Parses a Contact entry for a Cursor loaded from the OS Strequents DB. 40 */ 41 public static ContactEntry fromCursor(Cursor cursor, Context context) { 42 int nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 43 int starredColumn = cursor.getColumnIndex(ContactsContract.Contacts.STARRED); 44 int pinnedColumn = cursor.getColumnIndex("pinned"); 45 46 String name = cursor.getString(nameColumn); 47 String number = PhoneLoader.getPhoneNumber(cursor, context.getContentResolver()); 48 int starred = cursor.getInt(starredColumn); 49 int pinnedPosition = cursor.getInt(pinnedColumn); 50 return new ContactEntry(context, name, number, starred > 0, pinnedPosition); 51 } 52 53 public ContactEntry( 54 Context context, String name, String number, boolean isStarred, int pinnedPosition) { 55 mContext = context; 56 this.name = name; 57 this.number = number; 58 this.isStarred = isStarred; 59 this.pinnedPosition = pinnedPosition; 60 } 61 62 /** 63 * Retrieves a best-effort contact name ready for display to the user. 64 * It takes into account the number associated with a name for fail cases. 65 */ 66 public String getDisplayName() { 67 if (!TextUtils.isEmpty(name)) { 68 return name; 69 } 70 if (isVoicemail()) { 71 return mContext.getResources().getString(R.string.voicemail); 72 } else { 73 String displayName = TelecomUtils.getFormattedNumber(mContext, number); 74 if (TextUtils.isEmpty(displayName)) { 75 displayName = mContext.getString(R.string.unknown); 76 } 77 return displayName; 78 } 79 } 80 81 public boolean isVoicemail() { 82 return number.equals(TelecomUtils.getVoicemailNumber(mContext)); 83 } 84 85 @Override 86 public int compareTo(ContactEntry strequentContactEntry) { 87 if (isStarred == strequentContactEntry.isStarred) { 88 if (pinnedPosition == strequentContactEntry.pinnedPosition) { 89 if (name == strequentContactEntry.name) { 90 return compare(number, strequentContactEntry.number); 91 } 92 return compare(name, strequentContactEntry.name); 93 } else { 94 if (pinnedPosition > 0 && strequentContactEntry.pinnedPosition > 0) { 95 return pinnedPosition - strequentContactEntry.pinnedPosition; 96 } 97 98 if (pinnedPosition > 0) { 99 return -1; 100 } 101 102 return 1; 103 } 104 } 105 106 if (isStarred) { 107 return -1; 108 } 109 110 return 1; 111 } 112 113 @Override 114 public boolean equals(Object obj) { 115 if (obj instanceof ContactEntry) { 116 ContactEntry other = (ContactEntry) obj; 117 if (compare(name, other.name) == 0 118 && compare(number, other.number) == 0 119 && isStarred == other.isStarred 120 && pinnedPosition == other.pinnedPosition) { 121 return true; 122 } 123 } 124 return false; 125 } 126 127 @Override 128 public int hashCode() { 129 int result = 17; 130 result = 31 * result + (isStarred ? 1 : 0); 131 result = 31 * result + pinnedPosition; 132 result = 31 * result + (name == null ? 0 : name.hashCode()); 133 result = 31 * result + (number == null ? 0 : number.hashCode()); 134 return result; 135 } 136 137 private int compare(final String one, final String two) { 138 if (one == null ^ two == null) { 139 return (one == null) ? -1 : 1; 140 } 141 142 if (one == null && two == null) { 143 return 0; 144 } 145 146 return one.compareTo(two); 147 } 148 } 149