1/*
2 * Copyright (C) 2015 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 */
16package com.android.messaging.util;
17
18import android.net.Uri;
19import android.provider.ContactsContract.DisplayNameSources;
20import android.text.TextUtils;
21
22import com.android.ex.chips.RecipientEntry;
23import com.android.messaging.Factory;
24import com.android.messaging.R;
25import com.android.messaging.datamodel.BugleRecipientEntry;
26import com.android.messaging.datamodel.data.ParticipantData;
27
28/**
29 * Provides utility methods around creating RecipientEntry instance specific to Bugle's needs.
30 */
31public class ContactRecipientEntryUtils {
32    /**
33     * A special contact id for generated contacts with no display name (number only) and avatar.
34     * By default, the chips UI doesn't load any avatar for chips with no display name, or where
35     * the display name is the same as phone number (which is true for unknown contacts).
36     * Since Bugle always generate a default avatar for all contacts, this is used to replace
37     * those default generated chips with a phone number and no avatars.
38     */
39    private static final long CONTACT_ID_NUMBER_WITH_AVATAR = -1000;
40
41    /**
42     * A generated special contact which says "Send to xxx" in the contact list, which allows
43     * a user to direct send an SMS to a number that was manually typed in.
44     */
45    private static final long CONTACT_ID_SENDTO_DESTINATION = -1001;
46
47    /**
48     * Construct a special "Send to xxx" entry for a given destination.
49     */
50    public static RecipientEntry constructSendToDestinationEntry(final String destination) {
51        return constructSpecialRecipientEntry(destination, CONTACT_ID_SENDTO_DESTINATION);
52    }
53
54    /**
55     * Construct a generated contact entry but with rendered avatar.
56     */
57    public static RecipientEntry constructNumberWithAvatarEntry(final String destination) {
58        return constructSpecialRecipientEntry(destination, CONTACT_ID_NUMBER_WITH_AVATAR);
59    }
60
61    private static RecipientEntry constructSpecialRecipientEntry(final String destination,
62            final long contactId) {
63        // For the send-to-destination (e.g. "Send to xxx" in the auto-complete drop-down)
64        // we want to show a default avatar with a static background so that it doesn't flicker
65        // as the user types.
66        final Uri avatarUri = contactId == CONTACT_ID_SENDTO_DESTINATION ?
67                AvatarUriUtil.DEFAULT_BACKGROUND_AVATAR : null;
68        return BugleRecipientEntry.constructTopLevelEntry(null, DisplayNameSources.STRUCTURED_NAME,
69                destination, RecipientEntry.INVALID_DESTINATION_TYPE, null, contactId,
70                null, contactId, avatarUri, true, null);
71    }
72
73    /**
74     * Gets the display name for contact list only. For most cases this is the same as the normal
75     * contact name, but there are cases where these two differ. For example, for the
76     * send to typed number item, we'd like to show "Send to xxx" in the contact list. However,
77     * when this item is actually added to the chips edit box, we would like to show just the
78     * phone number (i.e. no display name).
79     */
80    public static String getDisplayNameForContactList(final RecipientEntry entry) {
81        if (entry.getContactId() == CONTACT_ID_SENDTO_DESTINATION) {
82            return Factory.get().getApplicationContext().getResources().getString(
83                    R.string.contact_list_send_to_text, formatDestination(entry));
84        } else if (!TextUtils.isEmpty(entry.getDisplayName())) {
85            return entry.getDisplayName();
86        } else {
87            return formatDestination(entry);
88        }
89    }
90
91    public static String formatDestination(final RecipientEntry entry) {
92        return PhoneUtils.getDefault().formatForDisplay(entry.getDestination());
93    }
94
95    /**
96     * Returns true if the given entry has only avatar and number
97     */
98    public static boolean isAvatarAndNumberOnlyContact(final RecipientEntry entry) {
99        return entry.getContactId() == CONTACT_ID_NUMBER_WITH_AVATAR;
100    }
101
102    /**
103     * Returns true if the given entry is a special send to number item.
104     */
105    public static boolean isSendToDestinationContact(final RecipientEntry entry) {
106        return entry.getContactId() == CONTACT_ID_SENDTO_DESTINATION;
107    }
108
109    /**
110     * Returns true if the given participant is a special send to number item.
111     */
112    public static boolean isSendToDestinationContact(final ParticipantData participant) {
113        return participant.getContactId() == CONTACT_ID_SENDTO_DESTINATION;
114    }
115}
116