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