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 package android.car.cluster;
17 
18 import android.os.Bundle;
19 import android.telephony.TelephonyManager;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.fragment.app.Fragment;
29 import androidx.fragment.app.FragmentActivity;
30 import androidx.lifecycle.ViewModelProviders;
31 
32 import com.android.car.telephony.common.TelecomUtils;
33 
34 /**
35  * Displays ongoing call information.
36  */
37 public class PhoneFragment extends Fragment {
38     private View mUserProfileContainerView;
39 
40     private PhoneFragmentViewModel mViewModel;
41 
PhoneFragment()42     public PhoneFragment() {
43         // Required empty public constructor
44     }
45 
46     @Nullable
47     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)48     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
49             Bundle savedInstanceState) {
50         FragmentActivity activity = requireActivity();
51         mViewModel = ViewModelProviders.of(activity).get(
52                 PhoneFragmentViewModel.class);
53 
54         View fragmentView = inflater.inflate(R.layout.fragment_phone, container, false);
55         mUserProfileContainerView = fragmentView.findViewById(R.id.user_profile_container);
56 
57         TextView body = mUserProfileContainerView.findViewById(R.id.body);
58         ImageView avatar = mUserProfileContainerView.findViewById(R.id.avatar);
59         TextView nameView = mUserProfileContainerView.findViewById(R.id.title);
60 
61         mViewModel.getContactInfo().observe(getViewLifecycleOwner(), (contactInfo) -> {
62             nameView.setText(contactInfo.getDisplayName());
63             TelecomUtils.setContactBitmapAsync(getContext(),
64                     avatar, contactInfo.getContact(), contactInfo.getNumber());
65         });
66         mViewModel.getBody().observe(getViewLifecycleOwner(), body::setText);
67         mViewModel.getState().observe(getViewLifecycleOwner(), (state) -> {
68             if (state == TelephonyManager.CALL_STATE_IDLE) {
69                 mUserProfileContainerView.setVisibility(View.GONE);
70             } else {
71                 mUserProfileContainerView.setVisibility(View.VISIBLE);
72             }
73         });
74 
75         return fragmentView;
76     }
77 }
78