1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.util;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.provider.ContactsContract.DisplayNameSources;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.text.TextUtils;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.ex.chips.RecipientEntry;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.Factory;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.BugleRecipientEntry;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ParticipantData;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Provides utility methods around creating RecipientEntry instance specific to Bugle's needs.
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ContactRecipientEntryUtils {
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * A special contact id for generated contacts with no display name (number only) and avatar.
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * By default, the chips UI doesn't load any avatar for chips with no display name, or where
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * the display name is the same as phone number (which is true for unknown contacts).
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Since Bugle always generate a default avatar for all contacts, this is used to replace
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * those default generated chips with a phone number and no avatars.
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final long CONTACT_ID_NUMBER_WITH_AVATAR = -1000;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * A generated special contact which says "Send to xxx" in the contact list, which allows
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * a user to direct send an SMS to a number that was manually typed in.
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final long CONTACT_ID_SENDTO_DESTINATION = -1001;
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Construct a special "Send to xxx" entry for a given destination.
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static RecipientEntry constructSendToDestinationEntry(final String destination) {
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return constructSpecialRecipientEntry(destination, CONTACT_ID_SENDTO_DESTINATION);
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Construct a generated contact entry but with rendered avatar.
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static RecipientEntry constructNumberWithAvatarEntry(final String destination) {
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return constructSpecialRecipientEntry(destination, CONTACT_ID_NUMBER_WITH_AVATAR);
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static RecipientEntry constructSpecialRecipientEntry(final String destination,
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final long contactId) {
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // For the send-to-destination (e.g. "Send to xxx" in the auto-complete drop-down)
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // we want to show a default avatar with a static background so that it doesn't flicker
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // as the user types.
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Uri avatarUri = contactId == CONTACT_ID_SENDTO_DESTINATION ?
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                AvatarUriUtil.DEFAULT_BACKGROUND_AVATAR : null;
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return BugleRecipientEntry.constructTopLevelEntry(null, DisplayNameSources.STRUCTURED_NAME,
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                destination, RecipientEntry.INVALID_DESTINATION_TYPE, null, contactId,
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                null, contactId, avatarUri, true, null);
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Gets the display name for contact list only. For most cases this is the same as the normal
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * contact name, but there are cases where these two differ. For example, for the
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * send to typed number item, we'd like to show "Send to xxx" in the contact list. However,
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * when this item is actually added to the chips edit box, we would like to show just the
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * phone number (i.e. no display name).
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static String getDisplayNameForContactList(final RecipientEntry entry) {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (entry.getContactId() == CONTACT_ID_SENDTO_DESTINATION) {
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return Factory.get().getApplicationContext().getResources().getString(
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    R.string.contact_list_send_to_text, formatDestination(entry));
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else if (!TextUtils.isEmpty(entry.getDisplayName())) {
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return entry.getDisplayName();
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return formatDestination(entry);
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static String formatDestination(final RecipientEntry entry) {
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return PhoneUtils.getDefault().formatForDisplay(entry.getDestination());
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Returns true if the given entry has only avatar and number
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static boolean isAvatarAndNumberOnlyContact(final RecipientEntry entry) {
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return entry.getContactId() == CONTACT_ID_NUMBER_WITH_AVATAR;
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Returns true if the given entry is a special send to number item.
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static boolean isSendToDestinationContact(final RecipientEntry entry) {
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return entry.getContactId() == CONTACT_ID_SENDTO_DESTINATION;
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Returns true if the given participant is a special send to number item.
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static boolean isSendToDestinationContact(final ParticipantData participant) {
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return participant.getContactId() == CONTACT_ID_SENDTO_DESTINATION;
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
116