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