1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.dialer.ui.activecall;
18 
19 import android.os.Bundle;
20 import android.telecom.Call;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.Fragment;
30 import androidx.lifecycle.LiveData;
31 import androidx.lifecycle.ViewModelProviders;
32 
33 import com.android.car.apps.common.LetterTileDrawable;
34 import com.android.car.dialer.R;
35 import com.android.car.dialer.ui.view.ContactAvatarOutputlineProvider;
36 import com.android.car.telephony.common.CallDetail;
37 import com.android.car.telephony.common.TelecomUtils;
38 
39 import java.util.concurrent.CompletableFuture;
40 
41 /**
42  * A fragment that displays information about onhold call.
43  */
44 public class OnHoldCallUserProfileFragment extends Fragment {
45 
46     private TextView mTitle;
47     private ImageView mAvatarView;
48     private View mSwapCallsView;
49     private LiveData<Call> mPrimaryCallLiveData;
50     private LiveData<Call> mSecondaryCallLiveData;
51     private CompletableFuture<Void> mPhoneNumberInfoFuture;
52     private LetterTileDrawable mDefaultAvatar;
53 
54     @Override
onCreate(Bundle savedInstanceState)55     public void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         mDefaultAvatar = TelecomUtils.createLetterTile(getContext(), null, null);
58     }
59 
60     @Nullable
61     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)62     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
63             @Nullable Bundle savedInstanceState) {
64         View fragmentView = inflater.inflate(R.layout.onhold_user_profile, container, false);
65 
66         mTitle = fragmentView.findViewById(R.id.title);
67         mAvatarView = fragmentView.findViewById(R.id.icon);
68         mAvatarView.setOutlineProvider(ContactAvatarOutputlineProvider.get());
69 
70         mSwapCallsView = fragmentView.findViewById(R.id.swap_calls_view);
71         mSwapCallsView.setOnClickListener(v -> swapCalls());
72 
73         InCallViewModel inCallViewModel = ViewModelProviders.of(getActivity()).get(
74                 InCallViewModel.class);
75         inCallViewModel.getSecondaryCallDetail().observe(this, this::updateProfile);
76         mPrimaryCallLiveData = inCallViewModel.getPrimaryCall();
77         mSecondaryCallLiveData = inCallViewModel.getSecondaryCall();
78 
79         return fragmentView;
80     }
81 
updateProfile(@ullable CallDetail callDetail)82     private void updateProfile(@Nullable CallDetail callDetail) {
83         if (callDetail == null) {
84             return;
85         }
86 
87         if (mPhoneNumberInfoFuture != null) {
88             mPhoneNumberInfoFuture.cancel(true);
89         }
90 
91         String number = callDetail.getNumber();
92         mTitle.setText(TelecomUtils.getFormattedNumber(getContext(), number));
93         mAvatarView.setImageDrawable(mDefaultAvatar);
94 
95         mPhoneNumberInfoFuture = TelecomUtils.getPhoneNumberInfo(getContext(), number)
96                 .thenAcceptAsync((info) -> {
97                     mTitle.setText(info.getDisplayName());
98                     TelecomUtils.setContactBitmapAsync(getContext(), mAvatarView,
99                             info.getAvatarUri(), info.getInitials(), info.getDisplayName());
100                 }, getContext().getMainExecutor());
101     }
102 
swapCalls()103     private void swapCalls() {
104         // Hold primary call and the secondary call will automatically come to the foreground.
105         if (mPrimaryCallLiveData.getValue().getState() != Call.STATE_HOLDING) {
106             mPrimaryCallLiveData.getValue().hold();
107         }
108     }
109 
110     @Override
onStop()111     public void onStop() {
112         super.onStop();
113         if (mPhoneNumberInfoFuture != null) {
114             mPhoneNumberInfoFuture.cancel(true);
115         }
116     }
117 }
118