• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 com.android.car.dialer.ui;
17 
18 import android.content.Context;
19 import android.telecom.Call;
20 import android.text.TextUtils;
21 import android.view.View;
22 import android.widget.ImageButton;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import com.android.car.apps.common.FabDrawable;
27 import com.android.car.dialer.R;
28 import com.android.car.dialer.telecom.TelecomUtils;
29 import com.android.car.dialer.telecom.UiCall;
30 import com.android.car.dialer.telecom.UiCallManager;
31 
32 /**
33  * Controls dialer information such as dialed number and shows proper action based on current call
34  * state.
35  */
36 public class DialerInfoController {
37     private static final int MAX_DIAL_NUMBER = 20;
38 
39     private TextView mTitleView;
40     private TextView mBodyView;
41 
42     private ImageButton mCallButton;
43     private ImageButton mDeleteButton;
44 
45     private ImageButton mEndCallButton;
46     private ImageButton mMuteButton;
47 
48     private Context mContext;
49 
50     private final StringBuffer mNumber = new StringBuffer(MAX_DIAL_NUMBER);
51 
DialerInfoController(Context context, View container)52     public DialerInfoController(Context context, View container) {
53         mContext = context;
54         init(container);
55     }
56 
init(View container)57     public View init(View container) {
58         mTitleView = container.findViewById(R.id.title);
59         mBodyView = container.findViewById(R.id.body);
60         mCallButton = container.findViewById(R.id.call_button);
61         mDeleteButton = container.findViewById(R.id.delete_button);
62         mEndCallButton = container.findViewById(R.id.end_call_button);
63         mMuteButton = container.findViewById(R.id.mute_button);
64 
65         FabDrawable answerCallDrawable = new FabDrawable(mContext);
66         answerCallDrawable.setFabAndStrokeColor(mContext.getColor(R.color.phone_call));
67         mCallButton.setBackground(answerCallDrawable);
68         mCallButton.setOnClickListener((unusedView) -> {
69             if (!TextUtils.isEmpty(mNumber.toString())) {
70                 UiCallManager.get().safePlaceCall(mNumber.toString(), false);
71             }
72         });
73         mDeleteButton.setOnClickListener(v -> {
74             removeLastDigit();
75         });
76         mDeleteButton.setOnLongClickListener(v -> {
77             // Clear all on long-press
78             clearDialedNumber();
79             return true;
80         });
81 
82         updateView();
83 
84         return container;
85     }
86 
87     /**
88      * Append more number to the end of dialed number.
89      */
appendDialedNumber(String number)90     public void appendDialedNumber(String number) {
91         if (mNumber.length() < MAX_DIAL_NUMBER) {
92             mNumber.append(number);
93             mTitleView.setText(getFormattedNumber(mNumber.toString()));
94         }
95     }
96 
97     /**
98      * Remove last digit of the dialed number. If there's no number left to delete, there's no
99      * operation to be take.
100      */
removeLastDigit()101     public void removeLastDigit() {
102         if (mNumber.length() != 0) {
103             mNumber.deleteCharAt(mNumber.length() - 1);
104             mTitleView.setText(getFormattedNumber(mNumber.toString()));
105         }
106         UiCall primaryCall = UiCallManager.get().getPrimaryCall();
107 
108         if (mNumber.length() == 0 && primaryCall != null
109                 && primaryCall.getState() != Call.STATE_ACTIVE) {
110             mTitleView.setText(R.string.dial_a_number);
111         }
112     }
113 
updateView()114     private void updateView() {
115         UiCall onGoingCall = UiCallManager.get().getPrimaryCall();
116         if (onGoingCall == null) {
117             showPreDialUi();
118         } else if (onGoingCall.getState() == Call.STATE_CONNECTING) {
119             showDialingUi(onGoingCall);
120         } else if (onGoingCall.getState() == Call.STATE_ACTIVE) {
121             showInCallUi();
122         }
123     }
124 
showPreDialUi()125     private void showPreDialUi() {
126         mCallButton.setVisibility(View.VISIBLE);
127         mDeleteButton.setVisibility(View.VISIBLE);
128 
129         mEndCallButton.setVisibility(View.GONE);
130         mMuteButton.setVisibility(View.GONE);
131     }
132 
showDialingUi(UiCall uiCall)133     private void showDialingUi(UiCall uiCall) {
134         if (mTitleView.getText().equals(mContext.getString(R.string.dial_a_number))) {
135             mTitleView.setText("");
136         }
137         FabDrawable endCallDrawable = new FabDrawable(mContext);
138         endCallDrawable.setFabAndStrokeColor(mContext.getColor(R.color.phone_end_call));
139         mEndCallButton.setBackground(endCallDrawable);
140         mEndCallButton.setVisibility(View.VISIBLE);
141         mMuteButton.setVisibility(View.VISIBLE);
142         mBodyView.setVisibility(View.VISIBLE);
143 
144         mDeleteButton.setVisibility(View.GONE);
145         mCallButton.setVisibility(View.GONE);
146         bindUserProfileView(uiCall);
147     }
148 
showInCallUi()149     private void showInCallUi() {
150         if (mTitleView.getText().equals(mContext.getString(R.string.dial_a_number))) {
151             mTitleView.setText("");
152         }
153         mEndCallButton.setVisibility(View.GONE);
154         mDeleteButton.setVisibility(View.GONE);
155         mCallButton.setVisibility(View.GONE);
156     }
157 
getFormattedNumber(String number)158     private String getFormattedNumber(String number) {
159         return TelecomUtils.getFormattedNumber(mContext, number);
160     }
161 
clearDialedNumber()162     private void clearDialedNumber() {
163         mNumber.setLength(0);
164         mTitleView.setText(getFormattedNumber(mNumber.toString()));
165     }
166 
bindUserProfileView(UiCall primaryCall)167     private void bindUserProfileView(UiCall primaryCall) {
168         if (primaryCall == null) {
169             return;
170         }
171         mTitleView.setText(TelecomUtils.getDisplayName(mContext, primaryCall));
172     }
173 }
174