summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/PersonItemView.java
blob: 51f8c0e1c3aba4ff622ce96460c3ae0fda3f4601 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * 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;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.text.BidiFormatter;
import android.support.v4.text.TextDirectionHeuristicsCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.messaging.BugleApplication;
import com.android.messaging.R;
import com.android.messaging.datamodel.binding.BindingBase;
import com.android.messaging.datamodel.binding.DetachableBinding;
import com.android.messaging.datamodel.data.PersonItemData;
import com.android.messaging.datamodel.data.PersonItemData.PersonItemDataListener;
import com.android.messaging.util.AccessibilityUtil;
import com.android.messaging.util.UiUtils;
import com.cyanogen.lookup.phonenumber.response.LookupResponse;
import com.cyanogenmod.messaging.lookup.LookupProviderManager.LookupProviderListener;
import com.cyanogenmod.messaging.ui.AttributionContactIconView;

/**
 * Shows a view for a "person" - could be a contact or a participant. This always shows a
 * contact icon on the left, and the person's display name on the right.
 *
 * This view is always bound to an abstract PersonItemData class, so to use it for a specific
 * scenario, all you need to do is to create a concrete PersonItemData subclass that bridges
 * between the underlying data (e.g. ParticipantData) and what the UI wants (e.g. display name).
 */
public class PersonItemView extends LinearLayout implements PersonItemDataListener,
        OnLayoutChangeListener, LookupProviderListener {

    public interface PersonItemViewListener {
        void onPersonClicked(PersonItemData data);
        boolean onPersonLongClicked(PersonItemData data);
    }

    protected final DetachableBinding<PersonItemData> mBinding;
    private TextView mNameTextView;
    private TextView mDetailsTextView;
    private AttributionContactIconView mContactIconView;
    private View mDetailsContainer;
    private PersonItemViewListener mListener;
    private boolean mAvatarOnly;
    private String mLastNormalizedNumber;
    private String mCachedLookupDisplayName;

    public PersonItemView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        mBinding = BindingBase.createDetachableBinding(this);
        LayoutInflater.from(getContext()).inflate(R.layout.person_item_view, this, true);
    }

    @Override
    protected void onFinishInflate() {
        mNameTextView = (TextView) findViewById(R.id.name);
        mDetailsTextView = (TextView) findViewById(R.id.details);
        mContactIconView = (AttributionContactIconView) findViewById(R.id.contact_icon);
        mDetailsContainer = findViewById(R.id.details_container);
        mNameTextView.addOnLayoutChangeListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mBinding.isBound()) {
            mBinding.detach();
        }
        if (!TextUtils.isEmpty(mLastNormalizedNumber)) {
            BugleApplication.getLookupProvider()
                    .removeLookupProviderListener(mLastNormalizedNumber, this);
            mLastNormalizedNumber = null;
        }
        mCachedLookupDisplayName = null;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mBinding.reAttachIfPossible();
    }

    /**
     * Binds to a person item data which will provide us info to be displayed.
     * @param personData the PersonItemData to be bound to.
     */
    public void bind(final PersonItemData personData) {
        if (mBinding.isBound()) {
            if (mBinding.getData().equals(personData)) {
                // Don't rebind if we are requesting the same data.
                return;
            }
            mBinding.unbind();
        }
        if (!TextUtils.isEmpty(mLastNormalizedNumber)) {
            BugleApplication.getLookupProvider()
                    .removeLookupProviderListener(mLastNormalizedNumber, this);
            mLastNormalizedNumber = null;
        }

        if (personData != null) {
            mBinding.bind(personData);
            mBinding.getData().setListener(this);

            // Accessibility reason : in case phone numbers are mixed in the display name,
            // we need to vocalize it for talkback.
            final String vocalizedDisplayName = AccessibilityUtil.getVocalizedPhoneNumber(
                    getResources(), getDisplayName());
            mNameTextView.setContentDescription(vocalizedDisplayName);
            mLastNormalizedNumber = personData.getNormalizedDestination();
        }
        updateViewAppearance();
        BugleApplication.getLookupProvider()
                .addLookupProviderListener(mLastNormalizedNumber, this);
        BugleApplication.getLookupProvider()
                .lookupInfoForPhoneNumber(mLastNormalizedNumber);
    }

    /**
     * @return Display name, possibly comma-ellipsized.
     */
    private String getDisplayName() {
        final int width = mNameTextView.getMeasuredWidth();
        final String displayName = mBinding.getData().getDisplayName();
        if (width == 0 || TextUtils.isEmpty(displayName) || !displayName.contains(",")) {
            return displayName;
        }
        final String plusOneString = getContext().getString(R.string.plus_one);
        final String plusNString = getContext().getString(R.string.plus_n);
        return BidiFormatter.getInstance().unicodeWrap(
                UiUtils.commaEllipsize(
                        displayName,
                        mNameTextView.getPaint(),
                        width,
                        plusOneString,
                        plusNString).toString(),
                TextDirectionHeuristicsCompat.LTR);
    }

    @Override
    public void onLayoutChange(final View v, final int left, final int top, final int right,
            final int bottom, final int oldLeft, final int oldTop, final int oldRight,
            final int oldBottom) {
        if (mBinding.isBound() && v == mNameTextView) {
            // TODO: figure out the reason for having this round about way of setting text
            // and fix this hack to get
            if (!TextUtils.isEmpty(mCachedLookupDisplayName)) {
                mNameTextView.setText(mCachedLookupDisplayName);
            } else {
                setNameTextView();
            }
        }
    }

    /**
     * When set to true, we display only the avatar of the person and hide everything else.
     */
    public void setAvatarOnly(final boolean avatarOnly) {
        mAvatarOnly = avatarOnly;
        mDetailsContainer.setVisibility(avatarOnly ? GONE : VISIBLE);
    }

    public boolean isAvatarOnly() {
        return mAvatarOnly;
    }

    public void setNameTextColor(final int color) {
        mNameTextView.setTextColor(color);
    }

    public void setDetailsTextColor(final int color) {
        mDetailsTextView.setTextColor(color);
    }

    public void setListener(final PersonItemViewListener listener) {
        mListener = listener;
        if (mListener == null) {
            return;
        }
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mListener != null && mBinding.isBound()) {
                    mListener.onPersonClicked(mBinding.getData());
                }
            }
        });
        final OnLongClickListener onLongClickListener = new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mListener != null && mBinding.isBound()) {
                    return mListener.onPersonLongClicked(mBinding.getData());
                }
                return false;
            }
        };
        setOnLongClickListener(onLongClickListener);
        mContactIconView.setOnLongClickListener(onLongClickListener);
    }

    public void performClickOnAvatar() {
        mContactIconView.performClick();
    }

    protected void updateViewAppearance() {
        if (mBinding.isBound()) {
            setNameTextView();

            final String details = mBinding.getData().getDetails();
            if (TextUtils.isEmpty(details)) {
                mDetailsTextView.setVisibility(GONE);
            } else {
                mDetailsTextView.setVisibility(VISIBLE);
                mDetailsTextView.setText(details);
            }

            mContactIconView.setImageResourceUri(mBinding.getData().getAvatarUri(),
                    mBinding.getData().getContactId(), mBinding.getData().getLookupKey(),
                    mBinding.getData().getNormalizedDestination());
        } else {
            mNameTextView.setText("");
            mContactIconView.setImageResourceUri(null);
        }
    }

    private void setNameTextView() {
        final String displayName = getDisplayName();
        if (TextUtils.isEmpty(displayName)) {
            mNameTextView.setVisibility(GONE);
        } else {
            mNameTextView.setVisibility(VISIBLE);
            mNameTextView.setText(displayName);
        }
    }

    @Override
    public void onPersonDataUpdated(final PersonItemData data) {
        mBinding.ensureBound(data);
        updateViewAppearance();
    }

    @Override
    public void onPersonDataFailed(final PersonItemData data, final Exception exception) {
        mBinding.ensureBound(data);
        updateViewAppearance();
    }

    public Intent getClickIntent() {
        return mBinding.getData().getClickIntent();
    }

    @Override
    public void onNewInfoAvailable(LookupResponse response) {
        if (response != null) {
            if (mLastNormalizedNumber == null || mLastNormalizedNumber.equals(response.mNumber)) {
                // always check for null if in a callback
                if (mContactIconView != null) {
                    if (!TextUtils.isEmpty(response.mPhotoUrl)) {
                        mContactIconView.setImageResourceUri(Uri.parse(response.mPhotoUrl));
                    }
                    if (response.mAttributionLogo != null) {
                        mContactIconView.setAttributionDrawable(response.mAttributionLogo);
                    }
                    mContactIconView.setLookupResponse(response);
                }
                if (!TextUtils.isEmpty(response.mName)) {
                    mCachedLookupDisplayName = response.mName;
                    mNameTextView.setText(mCachedLookupDisplayName);
                }
            }
        }

    }

}