summaryrefslogtreecommitdiff
path: root/src/com/android/car/dialer/ui/contact/ContactDetailsViewHolder.java
blob: 1eb1d4a8a61b9316b0d79f1b4512b0440b0356e0 (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
/*
 * Copyright (C) 2018 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.car.dialer.ui.contact;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.android.car.apps.common.BackgroundImageView;
import com.android.car.apps.common.LetterTileDrawable;
import com.android.car.apps.common.util.ViewUtils;
import com.android.car.dialer.R;
import com.android.car.dialer.telecom.UiCallManager;
import com.android.car.dialer.ui.view.ContactAvatarOutputlineProvider;
import com.android.car.telephony.common.Contact;
import com.android.car.telephony.common.PhoneNumber;
import com.android.car.telephony.common.PostalAddress;
import com.android.car.telephony.common.TelecomUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * ViewHolder for {@link ContactDetailsFragment}.
 */
class ContactDetailsViewHolder extends RecyclerView.ViewHolder {
    // Applies to all
    @NonNull
    private final TextView mTitle;

    // Applies to header
    @Nullable
    private final ImageView mAvatarView;
    @Nullable
    private final BackgroundImageView mBackgroundImageView;

    // Applies to phone number items and address items
    @Nullable
    private final TextView mText;

    // Applies to phone number items
    @Nullable
    private final View mCallActionView;
    @Nullable
    private final ImageView mFavoriteActionView;

    // Applies to address items
    @Nullable
    private final View mAddressView;
    @Nullable
    private final View mNavigationButton;

    @NonNull
    private final ContactDetailsAdapter.PhoneNumberPresenter mPhoneNumberPresenter;

    ContactDetailsViewHolder(
            View v,
            @NonNull ContactDetailsAdapter.PhoneNumberPresenter phoneNumberPresenter) {
        super(v);
        mCallActionView = v.findViewById(R.id.call_action_id);
        mFavoriteActionView = v.findViewById(R.id.contact_details_favorite_button);
        mAddressView = v.findViewById(R.id.address_button);
        mNavigationButton = v.findViewById(R.id.navigation_button);
        mTitle = v.findViewById(R.id.title);
        mText = v.findViewById(R.id.text);
        mAvatarView = v.findViewById(R.id.avatar);
        if (mAvatarView != null) {
            mAvatarView.setOutlineProvider(ContactAvatarOutputlineProvider.get());
        }
        mBackgroundImageView = v.findViewById(R.id.background_image);

        mPhoneNumberPresenter = phoneNumberPresenter;
    }

    public void bind(Context context, Contact contact) {
        if (contact == null) {
            ViewUtils.setText(mTitle, R.string.error_contact_deleted);
            LetterTileDrawable letterTile = TelecomUtils.createLetterTile(context, null, null);
            if (mAvatarView != null) {
                mAvatarView.setImageDrawable(letterTile);
            }
            if (mBackgroundImageView != null) {
                mBackgroundImageView.setAlpha(context.getResources().getFloat(
                        R.dimen.config_background_image_error_alpha));
                mBackgroundImageView.setBackgroundColor(letterTile.getColor());
            }
            return;
        }

        ViewUtils.setText(mTitle, contact.getDisplayName());

        if (mAvatarView == null && mBackgroundImageView == null) {
            return;
        }

        LetterTileDrawable letterTile = TelecomUtils.createLetterTile(context,
                contact.getInitials(), contact.getDisplayName());
        Glide.with(context)
                .load(contact.getAvatarUri())
                .apply(new RequestOptions().centerCrop().error(letterTile))
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource,
                            Transition<? super Drawable> glideAnimation) {
                        if (mAvatarView != null) {
                            mAvatarView.setImageDrawable(resource);
                        }
                        if (mBackgroundImageView != null) {
                            mBackgroundImageView.setAlpha(context.getResources().getFloat(
                                    R.dimen.config_background_image_alpha));
                            mBackgroundImageView.setBackgroundDrawable(resource, false);
                        }
                    }

                    @Override
                    public void onLoadFailed(Drawable errorDrawable) {
                        if (mAvatarView != null) {
                            mAvatarView.setImageDrawable(letterTile);
                        }
                        if (mBackgroundImageView != null) {
                            mBackgroundImageView.setAlpha(context.getResources().getFloat(
                                    R.dimen.config_background_image_error_alpha));
                            mBackgroundImageView.setBackgroundColor(letterTile.getColor());
                        }
                    }
                });
    }

    public void bind(Context context, Contact contact, PhoneNumber phoneNumber) {
        mTitle.setText(phoneNumber.getRawNumber());

        // Present the phone number type and local favorite state.
        CharSequence readableLabel = phoneNumber.getReadableLabel(context.getResources());
        CharSequence favoriteLabel = phoneNumber.isFavorite() ? context.getString(
                R.string.local_favorite_number_description, readableLabel) : readableLabel;
        if (phoneNumber.isPrimary()) {
            mText.setText(context.getString(R.string.primary_number_description, favoriteLabel));
            mText.setTextAppearance(R.style.TextAppearance_DefaultNumberLabel);
        } else {
            mText.setText(favoriteLabel);
            mText.setTextAppearance(R.style.TextAppearance_ContactDetailsListSubtitle);
        }

        mCallActionView.setOnClickListener(
                v -> UiCallManager.get().placeCall(phoneNumber.getRawNumber()));

        if (phoneNumber.isFavorite() || !contact.isStarred()) {
            // If the phone number is marked as favorite locally, enable the action button to
            // allow users to remove from local favorites.
            // If the phone number is not a local favorite or a favorite from phone, allow users
            // to mark it as favorite locally.
            ViewUtils.setActivated(mFavoriteActionView, phoneNumber.isFavorite());
            ViewUtils.setEnabled(mFavoriteActionView, true);
            ViewUtils.setOnClickListener(mFavoriteActionView, v -> {
                mPhoneNumberPresenter.onClick(contact, phoneNumber);
                mFavoriteActionView.setActivated(!mFavoriteActionView.isActivated());
            });
        } else {
            // The contact is favorite contact downloaded from phone. Disable the favorite action
            // button so the phone numbers can not be marked as favorite locally as it is not
            // necessary.
            ViewUtils.setActivated(mFavoriteActionView, false);
            ViewUtils.setEnabled(mFavoriteActionView, false);
            ViewUtils.setOnClickListener(mFavoriteActionView, null);
        }
    }

    public void bind(Context context, @NonNull PostalAddress postalAddress) {
        String address = postalAddress.getFormattedAddress();
        Resources resources = context.getResources();

        mTitle.setText(address);
        ViewUtils.setText(mText, postalAddress.getReadableLabel(resources));

        mAddressView.setOnClickListener(
                v -> openMapWithMapIntent(context, postalAddress.getAddressIntent(resources)));
        mNavigationButton.setOnClickListener(
                v -> openMapWithMapIntent(context, postalAddress.getNavigationIntent(resources)));
    }

    private void openMapWithMapIntent(Context context, Intent mapIntent) {
        context.startActivity(mapIntent);
    }
}