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.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.fragment.app.Fragment;
27 import androidx.lifecycle.MutableLiveData;
28 import androidx.lifecycle.ViewModelProviders;
29 
30 import com.android.car.apps.common.BackgroundImageView;
31 import com.android.car.dialer.R;
32 
33 import com.google.common.annotations.VisibleForTesting;
34 
35 /**
36  * A fragment that displays information about an on-going call with options to hang up.
37  */
38 public class OngoingCallFragment extends InCallFragment {
39     private Fragment mDialpadFragment;
40     private Fragment mOnholdCallFragment;
41     private View mUserProfileContainerView;
42     private BackgroundImageView mBackgroundImage;
43     private MutableLiveData<Boolean> mDialpadState;
44 
45     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)46     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
47             @Nullable Bundle savedInstanceState) {
48         View fragmentView = inflater.inflate(R.layout.ongoing_call_fragment, container, false);
49 
50         mUserProfileContainerView = fragmentView.findViewById(R.id.user_profile_container);
51         mBackgroundImage = fragmentView.findViewById(R.id.background_image);
52         mOnholdCallFragment = getChildFragmentManager().findFragmentById(R.id.onhold_user_profile);
53         mDialpadFragment = getChildFragmentManager().findFragmentById(R.id.incall_dialpad_fragment);
54 
55         InCallViewModel inCallViewModel = ViewModelProviders.of(getActivity()).get(
56                 InCallViewModel.class);
57 
58         inCallViewModel.getPrimaryCallDetail().observe(this, this::bindUserProfileView);
59         inCallViewModel.getCallStateAndConnectTime().observe(this, this::updateCallDescription);
60 
61         mDialpadState = inCallViewModel.getDialpadOpenState();
62         mDialpadState.setValue(savedInstanceState == null ? false : !mDialpadFragment.isHidden());
63         mDialpadState.observe(this, isDialpadOpen -> {
64             if (isDialpadOpen) {
65                 onOpenDialpad();
66             } else {
67                 onCloseDialpad();
68             }
69         });
70 
71         inCallViewModel.shouldShowOnholdCall().observe(this,
72                 this::maybeShowOnholdCallFragment);
73 
74         return fragmentView;
75     }
76 
77     @VisibleForTesting
onOpenDialpad()78     void onOpenDialpad() {
79         getChildFragmentManager().beginTransaction()
80                 .show(mDialpadFragment)
81                 .commit();
82         mUserProfileContainerView.setVisibility(View.GONE);
83         mBackgroundImage.setDimmed(true);
84     }
85 
86     @VisibleForTesting
onCloseDialpad()87     void onCloseDialpad() {
88         getChildFragmentManager().beginTransaction()
89                 .hide(mDialpadFragment)
90                 .commit();
91         mUserProfileContainerView.setVisibility(View.VISIBLE);
92         mBackgroundImage.setDimmed(false);
93     }
94 
maybeShowOnholdCallFragment(Boolean showOnholdCall)95     private void maybeShowOnholdCallFragment(Boolean showOnholdCall) {
96         if (showOnholdCall) {
97             getChildFragmentManager().beginTransaction().show(mOnholdCallFragment).commit();
98         } else {
99             getChildFragmentManager().beginTransaction().hide(mOnholdCallFragment).commit();
100         }
101     }
102 }
103