summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/contact/ContactDropdownLayouter.java
blob: 7df62de2a814f3e1fc5b5696774dafc72f0fab82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.messaging.ui.contact;

import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.support.v4.text.BidiFormatter;
import android.support.v4.text.TextDirectionHeuristicsCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.ex.chips.DropdownChipLayouter;
import com.android.ex.chips.RecipientEntry;
import com.android.messaging.R;
import com.android.messaging.datamodel.data.ContactListItemData;
import com.android.messaging.datamodel.data.ParticipantData;
import com.android.messaging.ui.ContactIconView;
import com.android.messaging.util.Assert;
import com.android.messaging.util.AvatarUriUtil;
import com.android.messaging.util.ContactRecipientEntryUtils;

/**
 * An implementation for {@link DropdownChipLayouter}. Layouts the dropdown
 * list in the ContactRecipientAutoCompleteView in Material style.
 */
public class ContactDropdownLayouter extends DropdownChipLayouter {
    private final ContactListItemView.HostInterface mClivHostInterface;

    public ContactDropdownLayouter(final LayoutInflater inflater, final Context context,
            final ContactListItemView.HostInterface clivHostInterface) {
        super(inflater, context);
        mClivHostInterface = new ContactListItemView.HostInterface() {

            @Override
            public void onContactListItemClicked(final ContactListItemData item,
                    final ContactListItemView view) {
                // The chips UI will handle auto-complete item click events, so No-op here.
            }

            @Override
            public boolean isContactSelected(final ContactListItemData item) {
                // In chips drop down we don't show any selected checkmark per design.
                return false;
            }
        };
    }

    /**
     * Bind a drop down view to a RecipientEntry. We'd like regular dropdown items (BASE_RECIPIENT)
     * to behave the same as regular ContactListItemViews, while using the chips library's
     * item styling for alternates dropdown items (happens when you click on a chip).
     */
    @Override
    public View bindView(final View convertView, final ViewGroup parent, final RecipientEntry entry,
            final int position, AdapterType type, final String substring,
            final StateListDrawable deleteDrawable) {
        if (type != AdapterType.BASE_RECIPIENT) {
            if (type == AdapterType.SINGLE_RECIPIENT) {
                // Treat single recipients the same way as alternates. The base implementation of
                // single recipients would try to simplify the destination by tokenizing. We'd
                // like to always show the full destination address per design request.
                type = AdapterType.RECIPIENT_ALTERNATES;
            }
            return super.bindView(convertView, parent, entry, position, type, substring,
                    deleteDrawable);
        }

        // Default to show all the information
        // RTL : To format contact name and detail if they happen to be phone numbers.
        final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
        final String displayName = bidiFormatter.unicodeWrap(
                ContactRecipientEntryUtils.getDisplayNameForContactList(entry),
                TextDirectionHeuristicsCompat.LTR);
        final String destination = bidiFormatter.unicodeWrap(
                ContactRecipientEntryUtils.formatDestination(entry),
                TextDirectionHeuristicsCompat.LTR);
        final View itemView = reuseOrInflateView(convertView, parent, type);

        // Bold the string that is matched.
        final CharSequence[] styledResults =
                getStyledResults(substring, displayName, destination);

        Assert.isTrue(itemView instanceof ContactListItemView);
        final ContactListItemView contactListItemView = (ContactListItemView) itemView;
        contactListItemView.setImageClickHandlerDisabled(true);
        contactListItemView.bind(entry, styledResults[0], styledResults[1],
                mClivHostInterface, (type == AdapterType.SINGLE_RECIPIENT));
        return itemView;
    }

    @Override
    protected void bindIconToView(boolean showImage, RecipientEntry entry, ImageView view,
            AdapterType type) {
        if (showImage && view instanceof ContactIconView) {
            final ContactIconView contactView = (ContactIconView) view;
            // These show contact cards by default, but that isn't what we want here
            contactView.setImageClickHandlerDisabled(true);
            final Uri avatarUri = AvatarUriUtil.createAvatarUri(
                    ParticipantData.getFromRecipientEntry(entry));
            contactView.setImageResourceUri(avatarUri);
        } else {
            super.bindIconToView(showImage, entry, view, type);
        }
    }

    @Override
    protected int getItemLayoutResId(AdapterType type) {
        switch (type) {
            case BASE_RECIPIENT:
                return R.layout.contact_list_item_view;
            case RECIPIENT_ALTERNATES:
                return R.layout.chips_alternates_dropdown_item;
            default:
                return R.layout.chips_alternates_dropdown_item;
        }
    }

    @Override
    protected int getAlternateItemLayoutResId(AdapterType type) {
        return getItemLayoutResId(type);
    }
}