RecipientEntry.java revision 4bb6a342f5aa1f38b0e0083d014e538e937eccce
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
26    public static final int ENTRY_TYPE_PERSON = 0;
27    public static final int ENTRY_TYPE_SEP_NORMAL = 1;
28    public static final int ENTRY_TYPE_SEP_WITHIN_GROUP = 2;
29    public static final int ENTRY_TYPE_WAITING_FOR_DIRECTORY_SEARCH = 3;
30
31    public static final int ENTRY_TYPE_SIZE = 4;
32
33    /** Separator entry dividing two persons or groups. */
34    public static final RecipientEntry SEP_NORMAL =
35            new RecipientEntry(ENTRY_TYPE_SEP_NORMAL);
36    /** Separator entry dividing two entries inside a person or a group. */
37    public static final RecipientEntry SEP_WITHIN_GROUP =
38            new RecipientEntry(ENTRY_TYPE_SEP_WITHIN_GROUP);
39    public static final RecipientEntry WAITING_FOR_DIRECTORY_SEARCH =
40            new RecipientEntry(ENTRY_TYPE_WAITING_FOR_DIRECTORY_SEARCH);
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    /** Destination for this contact entry. Would be an email address or a phone number. */
51    private final String mDestination;
52    /** ID for the person */
53    private final long mContactId;
54    /** ID for the destination */
55    private final long mDataId;
56    private final boolean mIsDivider;
57
58    private final Uri mPhotoThumbnailUri;
59
60    /**
61     * This can be updated after this object being constructed, when the photo is fetched
62     * from remote directories.
63     */
64    private byte[] mPhotoBytes;
65
66    private RecipientEntry(int entryType) {
67        mEntryType = entryType;
68        mDisplayName = null;
69        mDestination = null;
70        mContactId = -1;
71        mDataId = -1;
72        mPhotoThumbnailUri = null;
73        mPhotoBytes = null;
74        mIsDivider = true;
75    }
76
77    private RecipientEntry(
78            int entryType, String displayName, String destination, long contactId, long dataId,
79            Uri photoThumbnailUri, boolean isFirstLevel) {
80        mEntryType = entryType;
81        mIsFirstLevel = isFirstLevel;
82        mDisplayName = displayName;
83        mDestination = destination;
84        mContactId = contactId;
85        mDataId = dataId;
86        mPhotoThumbnailUri = photoThumbnailUri;
87        mPhotoBytes = null;
88        mIsDivider = false;
89    }
90
91    /**
92     * Construct a RecipientEntry from just an address that has been entered.
93     * This address has not been resolved to a contact and therefore does not
94     * have a contact id or photo.
95     */
96    public static RecipientEntry constructFakeEntry(String address) {
97        return new RecipientEntry(ENTRY_TYPE_PERSON, address, address, -1, -1, null, true);
98    }
99
100    public static RecipientEntry constructTopLevelEntry(
101            String displayName, String destination, long contactId, long dataId,
102            Uri photoThumbnailUri) {
103        return new RecipientEntry(ENTRY_TYPE_PERSON, displayName, destination, contactId, dataId,
104                photoThumbnailUri, true);
105    }
106
107    public static RecipientEntry constructTopLevelEntry(
108            String displayName, String destination, long contactId, long dataId,
109            String thumbnailUriAsString) {
110        return new RecipientEntry(
111                ENTRY_TYPE_PERSON, displayName, destination, contactId, dataId,
112                (thumbnailUriAsString != null ? Uri.parse(thumbnailUriAsString) : null), true);
113    }
114
115    public static RecipientEntry constructSecondLevelEntry(
116            String displayName, String destination, long contactId, long dataId,
117            String thumbnailUriAsString) {
118        return new RecipientEntry(
119                ENTRY_TYPE_PERSON, displayName, destination, contactId, dataId,
120                (thumbnailUriAsString != null ? Uri.parse(thumbnailUriAsString) : null), false);
121    }
122
123    public int getEntryType() {
124        return mEntryType;
125    }
126
127    public String getDisplayName() {
128        return mDisplayName;
129    }
130
131    public String getDestination() {
132        return mDestination;
133    }
134
135    public long getContactId() {
136        return mContactId;
137    }
138
139    public long getDataId() {
140        return mDataId;
141    }
142
143    public boolean isFirstLevel() {
144        return mIsFirstLevel;
145    }
146
147    public Uri getPhotoThumbnailUri() {
148        return mPhotoThumbnailUri;
149    }
150
151    /** This can be called outside main Looper thread. */
152    public synchronized void setPhotoBytes(byte[] photoBytes) {
153        mPhotoBytes = photoBytes;
154    }
155
156    /** This can be called outside main Looper thread. */
157    public synchronized byte[] getPhotoBytes() {
158        return mPhotoBytes;
159    }
160
161    public boolean isSeparator() {
162        return mIsDivider;
163    }
164}