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