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.dialpad;
18 
19 import android.os.Bundle;
20 import android.os.SystemClock;
21 import android.telecom.Call;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.Chronometer;
26 import android.widget.TextView;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.lifecycle.LiveData;
31 import androidx.lifecycle.ViewModelProviders;
32 
33 import com.android.car.dialer.R;
34 import com.android.car.dialer.log.L;
35 import com.android.car.dialer.ui.activecall.InCallViewModel;
36 import com.android.car.telephony.common.TelecomUtils;
37 import com.android.car.ui.toolbar.ToolbarController;
38 
39 /** Dialpad fragment used in the ongoing call page. */
40 public class InCallDialpadFragment extends AbstractDialpadFragment {
41     private static final String TAG = "CD.InCallDialpadFragment";
42 
43     private TextView mTitleView;
44     @Nullable
45     private Chronometer mCallStateView;
46 
47     /** An active call which this fragment is serving for. */
48     private LiveData<Call> mActiveCall;
49 
50     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)51     public View onCreateView(LayoutInflater inflater, ViewGroup container,
52             Bundle savedInstanceState) {
53 
54         ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.incall_dialpad_fragment,
55                 container,
56                 false);
57 
58         mTitleView = rootView.findViewById(R.id.title);
59         mCallStateView = rootView.findViewById(R.id.call_state);
60 
61         InCallViewModel viewModel = ViewModelProviders.of(getActivity()).get(InCallViewModel.class);
62         viewModel.getCallStateAndConnectTime().observe(this, (pair) -> {
63             if (mCallStateView == null) {
64                 return;
65             }
66 
67             if (pair == null) {
68                 mCallStateView.stop();
69                 mCallStateView.setText("");
70                 return;
71             }
72             if (pair.first == Call.STATE_ACTIVE) {
73                 mCallStateView.setBase(pair.second
74                         - System.currentTimeMillis() + SystemClock.elapsedRealtime());
75                 mCallStateView.start();
76             } else {
77                 mCallStateView.stop();
78                 mCallStateView.setText(TelecomUtils.callStateToUiString(
79                         getContext(), pair.first));
80             }
81         });
82         mActiveCall = viewModel.getPrimaryCall();
83 
84         return rootView;
85     }
86 
87     @Override
presentDialedNumber(@onNull StringBuffer number)88     void presentDialedNumber(@NonNull StringBuffer number) {
89         if (getActivity() == null) {
90             return;
91         }
92 
93         if (number.length() == 0) {
94             mTitleView.setText("");
95         } else {
96             mTitleView.setText(number);
97         }
98     }
99 
100     @Override
playTone(int keycode)101     void playTone(int keycode) {
102         L.d(TAG, "start DTMF tone for %s", keycode);
103         if (mActiveCall.getValue() != null) {
104             mActiveCall.getValue().playDtmfTone(sDialValueMap.get(keycode));
105         }
106     }
107 
108     @Override
stopAllTones()109     void stopAllTones() {
110         if (mActiveCall.getValue() != null) {
111             L.d(TAG, "stop DTMF tone");
112             mActiveCall.getValue().stopDtmfTone();
113         }
114     }
115 
116     @Override
setupToolbar(ToolbarController toolbar)117     protected void setupToolbar(ToolbarController toolbar) {
118         // No-op
119     }
120 
121     @Override
onKeypadKeyLongPressed(int keycode)122     public void onKeypadKeyLongPressed(int keycode) {
123         // No-op
124     }
125 }
126