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.ui.contact;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.drawable.StateListDrawable;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v4.text.BidiFormatter;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v4.text.TextDirectionHeuristicsCompat;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.LayoutInflater;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.ViewGroup;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.ImageView;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.ex.chips.DropdownChipLayouter;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.ex.chips.RecipientEntry;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ContactListItemData;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ParticipantData;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.ContactIconView;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.AvatarUriUtil;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ContactRecipientEntryUtils;
37c4073ff4ce1a342daad01a99d3fce84c54bc4776Tony Makimport com.android.messaging.util.ContactUtil;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * An implementation for {@link DropdownChipLayouter}. Layouts the dropdown
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * list in the ContactRecipientAutoCompleteView in Material style.
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ContactDropdownLayouter extends DropdownChipLayouter {
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final ContactListItemView.HostInterface mClivHostInterface;
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public ContactDropdownLayouter(final LayoutInflater inflater, final Context context,
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final ContactListItemView.HostInterface clivHostInterface) {
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super(inflater, context);
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mClivHostInterface = new ContactListItemView.HostInterface() {
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public void onContactListItemClicked(final ContactListItemData item,
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final ContactListItemView view) {
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // The chips UI will handle auto-complete item click events, so No-op here.
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public boolean isContactSelected(final ContactListItemData item) {
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // In chips drop down we don't show any selected checkmark per design.
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return false;
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        };
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Bind a drop down view to a RecipientEntry. We'd like regular dropdown items (BASE_RECIPIENT)
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * to behave the same as regular ContactListItemViews, while using the chips library's
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * item styling for alternates dropdown items (happens when you click on a chip).
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public View bindView(final View convertView, final ViewGroup parent, final RecipientEntry entry,
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int position, AdapterType type, final String substring,
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final StateListDrawable deleteDrawable) {
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (type != AdapterType.BASE_RECIPIENT) {
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (type == AdapterType.SINGLE_RECIPIENT) {
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // Treat single recipients the same way as alternates. The base implementation of
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // single recipients would try to simplify the destination by tokenizing. We'd
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // like to always show the full destination address per design request.
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                type = AdapterType.RECIPIENT_ALTERNATES;
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return super.bindView(convertView, parent, entry, position, type, substring,
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    deleteDrawable);
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Default to show all the information
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // RTL : To format contact name and detail if they happen to be phone numbers.
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String displayName = bidiFormatter.unicodeWrap(
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                ContactRecipientEntryUtils.getDisplayNameForContactList(entry),
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                TextDirectionHeuristicsCompat.LTR);
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String destination = bidiFormatter.unicodeWrap(
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                ContactRecipientEntryUtils.formatDestination(entry),
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                TextDirectionHeuristicsCompat.LTR);
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View itemView = reuseOrInflateView(convertView, parent, type);
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Bold the string that is matched.
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final CharSequence[] styledResults =
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getStyledResults(substring, displayName, destination);
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.isTrue(itemView instanceof ContactListItemView);
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ContactListItemView contactListItemView = (ContactListItemView) itemView;
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        contactListItemView.setImageClickHandlerDisabled(true);
103c4073ff4ce1a342daad01a99d3fce84c54bc4776Tony Mak        boolean isWorkContact = ContactUtil.isEnterpriseContactId(entry.getContactId());
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        contactListItemView.bind(entry, styledResults[0], styledResults[1],
105c4073ff4ce1a342daad01a99d3fce84c54bc4776Tony Mak                mClivHostInterface, (type == AdapterType.SINGLE_RECIPIENT), isWorkContact);
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return itemView;
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected void bindIconToView(boolean showImage, RecipientEntry entry, ImageView view,
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            AdapterType type) {
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (showImage && view instanceof ContactIconView) {
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final ContactIconView contactView = (ContactIconView) view;
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // These show contact cards by default, but that isn't what we want here
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            contactView.setImageClickHandlerDisabled(true);
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Uri avatarUri = AvatarUriUtil.createAvatarUri(
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    ParticipantData.getFromRecipientEntry(entry));
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            contactView.setImageResourceUri(avatarUri);
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            super.bindIconToView(showImage, entry, view, type);
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected int getItemLayoutResId(AdapterType type) {
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (type) {
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BASE_RECIPIENT:
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return R.layout.contact_list_item_view;
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case RECIPIENT_ALTERNATES:
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return R.layout.chips_alternates_dropdown_item;
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return R.layout.chips_alternates_dropdown_item;
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected int getAlternateItemLayoutResId(AdapterType type) {
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return getItemLayoutResId(type);
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
141