RecipientEntry.java revision ccb8e237ec80934d1c983bb61f66b75541786ddc
1/*
2 * Copyright (C) 2011 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
17package com.android.ex.chips;
18
19import android.net.Uri;
20import android.provider.ContactsContract.CommonDataKinds.Email;
21import android.provider.ContactsContract.DisplayNameSources;
22
23/**
24 * Represents one entry inside recipient auto-complete list.
25 */
26public class RecipientEntry {
27    /* package */ static final int INVALID_CONTACT = -1;
28    /**
29     * A GENERATED_CONTACT is one that was created based entirely on
30     * information passed in to the RecipientEntry from an external source
31     * that is not a real contact.
32     */
33    /* package */ static final int GENERATED_CONTACT = -2;
34
35    /** Used when {@link #mDestinationType} is invalid and thus shouldn't be used for display. */
36    /* package */ static final int INVALID_DESTINATION_TYPE = -1;
37
38    public static final int ENTRY_TYPE_PERSON = 0;
39
40    public static final int ENTRY_TYPE_SIZE = 1;
41
42    private final int mEntryType;
43
44    /**
45     * True when this entry is the first entry in a group, which should have a photo and display
46     * name, while the second or later entries won't.
47     */
48    private boolean mIsFirstLevel;
49    private final String mDisplayName;
50
51    /** Destination for this contact entry. Would be an email address or a phone number. */
52    private final String mDestination;
53    /** Type of the destination like {@link Email#TYPE_HOME} */
54    private final int mDestinationType;
55    /**
56     * Label of the destination which will be used when type was {@link Email#TYPE_CUSTOM}.
57     * Can be null when {@link #mDestinationType} is {@link #INVALID_DESTINATION_TYPE}.
58     */
59    private final String mDestinationLabel;
60    /** ID for the person */
61    private final long mContactId;
62    /** ID for the destination */
63    private final long mDataId;
64    private final boolean mIsDivider;
65
66    private final Uri mPhotoThumbnailUri;
67
68    private boolean mIsValid;
69    /**
70     * This can be updated after this object being constructed, when the photo is fetched
71     * from remote directories.
72     */
73    private byte[] mPhotoBytes;
74
75    private RecipientEntry(int entryType, String displayName, String destination,
76            int destinationType, String destinationLabel, long contactId, long dataId,
77            Uri photoThumbnailUri, boolean isFirstLevel, boolean isValid) {
78        mEntryType = entryType;
79        mIsFirstLevel = isFirstLevel;
80        mDisplayName = displayName;
81        mDestination = destination;
82        mDestinationType = destinationType;
83        mDestinationLabel = destinationLabel;
84        mContactId = contactId;
85        mDataId = dataId;
86        mPhotoThumbnailUri = photoThumbnailUri;
87        mPhotoBytes = null;
88        mIsDivider = false;
89        mIsValid = isValid;
90    }
91
92    public boolean isValid() {
93        return mIsValid;
94    }
95
96    /**
97     * Determine if this was a RecipientEntry created from recipient info or
98     * an entry from contacts.
99     */
100    public static boolean isCreatedRecipient(long id) {
101        return id == RecipientEntry.INVALID_CONTACT || id == RecipientEntry.GENERATED_CONTACT;
102    }
103
104    /**
105     * Construct a RecipientEntry from just an address that has been entered.
106     * This address has not been resolved to a contact and therefore does not
107     * have a contact id or photo.
108     */
109    public static RecipientEntry constructFakeEntry(String address, boolean isValid) {
110        return new RecipientEntry(ENTRY_TYPE_PERSON, address, address,
111                INVALID_DESTINATION_TYPE, null,
112                INVALID_CONTACT, INVALID_CONTACT, null, true, isValid);
113    }
114
115    /**
116     * @return the display name for the entry.  If the display name source is larger than
117     * {@link DisplayNameSources#PHONE} we use the contact's display name, but if not,
118     * i.e. the display name came from an email address or a phone number, we don't use it
119     * to avoid confusion and just use the destination instead.
120     */
121    private static String pickDisplayName(int displayNameSource, String displayName,
122            String destination) {
123        return (displayNameSource > DisplayNameSources.PHONE) ? displayName : destination;
124    }
125
126    /**
127     * Construct a RecipientEntry from just an address that has been entered
128     * with both an associated display name. This address has not been resolved
129     * to a contact and therefore does not have a contact id or photo.
130     */
131    public static RecipientEntry constructGeneratedEntry(String display, String address,
132            boolean isValid) {
133        return new RecipientEntry(ENTRY_TYPE_PERSON, display, address, INVALID_DESTINATION_TYPE,
134                null, GENERATED_CONTACT, GENERATED_CONTACT, null, true, isValid);
135    }
136
137    public static RecipientEntry constructTopLevelEntry(String displayName, int displayNameSource,
138            String destination, int destinationType, String destinationLabel, long contactId,
139            long dataId, Uri photoThumbnailUri, boolean isValid) {
140        return new RecipientEntry(ENTRY_TYPE_PERSON, pickDisplayName(displayNameSource,
141                displayName, destination), destination, destinationType, destinationLabel,
142                contactId, dataId, photoThumbnailUri, true, isValid);
143    }
144
145    public static RecipientEntry constructTopLevelEntry(String displayName, int displayNameSource,
146            String destination, int destinationType, String destinationLabel, long contactId,
147            long dataId, String thumbnailUriAsString, boolean isValid) {
148        return new RecipientEntry(ENTRY_TYPE_PERSON, pickDisplayName(displayNameSource,
149                displayName, destination), destination, destinationType, destinationLabel,
150                contactId, dataId, (thumbnailUriAsString != null ? Uri.parse(thumbnailUriAsString)
151                        : null), true, isValid);
152    }
153
154    public static RecipientEntry constructSecondLevelEntry(String displayName,
155            int displayNameSource, String destination, int destinationType,
156            String destinationLabel, long contactId, long dataId, String thumbnailUriAsString,
157            boolean isValid) {
158        return new RecipientEntry(ENTRY_TYPE_PERSON, pickDisplayName(displayNameSource,
159                displayName, destination), destination, destinationType, destinationLabel,
160                contactId, dataId, (thumbnailUriAsString != null ? Uri.parse(thumbnailUriAsString)
161                        : null), false, isValid);
162    }
163
164    public int getEntryType() {
165        return mEntryType;
166    }
167
168    public String getDisplayName() {
169        return mDisplayName;
170    }
171
172    public String getDestination() {
173        return mDestination;
174    }
175
176    public int getDestinationType() {
177        return mDestinationType;
178    }
179
180    public String getDestinationLabel() {
181        return mDestinationLabel;
182    }
183
184    public long getContactId() {
185        return mContactId;
186    }
187
188    public long getDataId() {
189        return mDataId;
190    }
191
192    public boolean isFirstLevel() {
193        return mIsFirstLevel;
194    }
195
196    public Uri getPhotoThumbnailUri() {
197        return mPhotoThumbnailUri;
198    }
199
200    /** This can be called outside main Looper thread. */
201    public synchronized void setPhotoBytes(byte[] photoBytes) {
202        mPhotoBytes = photoBytes;
203    }
204
205    /** This can be called outside main Looper thread. */
206    public synchronized byte[] getPhotoBytes() {
207        return mPhotoBytes;
208    }
209
210    public boolean isSeparator() {
211        return mIsDivider;
212    }
213
214    public boolean isSelectable() {
215        return mEntryType == ENTRY_TYPE_PERSON;
216    }
217}