summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/dialer/ui/activecall/InCallFragment.java
blob: 0f74255d337ac01f41b1052e6e6744ee31ba11e0 (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
/*
 * Copyright (C) 2019 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.activecall;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.telecom.Call;
import android.text.TextUtils;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment;

import com.android.car.apps.common.BackgroundImageView;
import com.android.car.apps.common.LetterTileDrawable;
import com.android.car.dialer.R;
import com.android.car.dialer.log.L;
import com.android.car.dialer.ui.view.ContactAvatarOutputlineProvider;
import com.android.car.telephony.common.CallDetail;
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;

import java.util.concurrent.CompletableFuture;

/** A fragment that displays information about a call with actions. */
public abstract class InCallFragment extends Fragment {
    private static final String TAG = "CD.InCallFragment";

    private View mUserProfileContainerView;
    private TextView mPhoneNumberView;
    private Chronometer mUserProfileCallStateText;
    private TextView mNameView;
    private ImageView mAvatarView;
    private BackgroundImageView mBackgroundImage;
    private LetterTileDrawable mDefaultAvatar;
    private CompletableFuture<Void> mPhoneNumberInfoFuture;
    private String mCurrentNumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDefaultAvatar = TelecomUtils.createLetterTile(getContext(), null, null);
    }

    /**
     * Shared UI elements between ongoing call and incoming call page: {@link BackgroundImageView}
     * and {@link R.layout#user_profile_large}.
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mUserProfileContainerView = view.findViewById(R.id.user_profile_container);
        mNameView = mUserProfileContainerView.findViewById(R.id.user_profile_title);
        mAvatarView = mUserProfileContainerView.findViewById(R.id.user_profile_avatar);
        mAvatarView.setOutlineProvider(ContactAvatarOutputlineProvider.get());
        mPhoneNumberView = mUserProfileContainerView.findViewById(R.id.user_profile_phone_number);
        mUserProfileCallStateText = mUserProfileContainerView.findViewById(
                R.id.user_profile_call_state);
        mBackgroundImage = view.findViewById(R.id.background_image);
    }

    /** Presents the user profile. */
    protected void bindUserProfileView(@Nullable CallDetail callDetail) {
        L.i(TAG, "bindUserProfileView: %s", callDetail);
        if (callDetail == null) {
            return;
        }

        String number = callDetail.getNumber();
        if (mCurrentNumber != null && mCurrentNumber.equals(number)) {
            return;
        }
        mCurrentNumber = number;

        if (mPhoneNumberInfoFuture != null) {
            mPhoneNumberInfoFuture.cancel(true);
        }

        mNameView.setText(TelecomUtils.getFormattedNumber(getContext(), number));
        mPhoneNumberView.setVisibility(View.GONE);
        mAvatarView.setImageDrawable(mDefaultAvatar);

        mPhoneNumberInfoFuture = TelecomUtils.getPhoneNumberInfo(getContext(), number)
            .thenAcceptAsync((info) -> {
                if (getContext() == null) {
                    return;
                }

                mNameView.setText(info.getDisplayName());

                String phoneNumberLabel = info.getTypeLabel();
                if (!phoneNumberLabel.isEmpty()) {
                    phoneNumberLabel += " ";
                }
                phoneNumberLabel += TelecomUtils.getFormattedNumber(getContext(), number);
                if (!TextUtils.isEmpty(phoneNumberLabel)
                        && !phoneNumberLabel.equals(info.getDisplayName())) {
                    mPhoneNumberView.setText(phoneNumberLabel);
                    mPhoneNumberView.setVisibility(View.VISIBLE);
                } else {
                    mPhoneNumberView.setVisibility(View.GONE);
                }

                LetterTileDrawable letterTile = TelecomUtils.createLetterTile(
                        getContext(), info.getInitials(), info.getDisplayName());

                Glide.with(this)
                        .load(info.getAvatarUri())
                        .apply(new RequestOptions().centerCrop().error(letterTile))
                        .into(new SimpleTarget<Drawable>() {
                            @Override
                            public void onResourceReady(Drawable resource,
                                    Transition<? super Drawable> glideAnimation) {
                                mBackgroundImage.setAlpha(getResources().getFloat(
                                        R.dimen.config_background_image_alpha));
                                mBackgroundImage.setBackgroundDrawable(resource, false);
                                mAvatarView.setImageDrawable(resource);
                            }

                            @Override
                            public void onLoadFailed(Drawable errorDrawable) {
                                mBackgroundImage.setAlpha(getResources().getFloat(
                                        R.dimen.config_background_image_error_alpha));
                                mBackgroundImage.setBackgroundColor(letterTile.getColor());
                                mAvatarView.setImageDrawable(letterTile);
                            }
                        });
            }, getContext().getMainExecutor());
    }

    /** Presents the call state and call duration. */
    protected void updateCallDescription(@Nullable Pair<Integer, Long> callStateAndConnectTime) {
        if (callStateAndConnectTime == null || callStateAndConnectTime.first == null) {
            mUserProfileCallStateText.stop();
            mUserProfileCallStateText.setText("");
            return;
        }
        if (callStateAndConnectTime.first == Call.STATE_ACTIVE) {
            mUserProfileCallStateText.setBase(callStateAndConnectTime.second
                    - System.currentTimeMillis() + SystemClock.elapsedRealtime());
            mUserProfileCallStateText.start();
        } else {
            mUserProfileCallStateText.stop();
            mUserProfileCallStateText.setText(
                    TelecomUtils.callStateToUiString(getContext(),
                            callStateAndConnectTime.first));
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mPhoneNumberInfoFuture != null) {
            mPhoneNumberInfoFuture.cancel(true);
        }
    }
}