blob: 44eaf212aadf3a13415fce119fa33f4a52804c9b [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2013 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
17package com.android.incallui;
18
19import android.content.Context;
20import android.os.Bundle;
linyuhb06d0092018-03-01 15:05:36 -080021import android.telephony.PhoneNumberUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080022import android.util.ArrayMap;
23import android.util.AttributeSet;
24import android.view.KeyEvent;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.view.View.OnKeyListener;
29import android.view.ViewGroup;
30import android.widget.EditText;
31import android.widget.LinearLayout;
32import android.widget.TextView;
maxwelb5e22e802017-11-27 17:01:07 -080033import com.android.dialer.common.LogUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080034import com.android.dialer.dialpadview.DialpadKeyButton;
35import com.android.dialer.dialpadview.DialpadKeyButton.OnPressedListener;
36import com.android.dialer.dialpadview.DialpadView;
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070037import com.android.dialer.logging.DialerImpression;
38import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import com.android.incallui.DialpadPresenter.DialpadUi;
40import com.android.incallui.baseui.BaseFragment;
41import java.util.Map;
42
43/** Fragment for call control buttons */
44public class DialpadFragment extends BaseFragment<DialpadPresenter, DialpadUi>
45 implements DialpadUi, OnKeyListener, OnClickListener, OnPressedListener {
46
47 /** Hash Map to map a view id to a character */
linyuh183cb712017-12-27 17:02:37 -080048 private static final Map<Integer, Character> displayMap = new ArrayMap<>();
Eric Erfanianccca3152017-02-22 16:32:36 -080049
50 /** Set up the static maps */
51 static {
52 // Map the buttons to the display characters
linyuh183cb712017-12-27 17:02:37 -080053 displayMap.put(R.id.one, '1');
54 displayMap.put(R.id.two, '2');
55 displayMap.put(R.id.three, '3');
56 displayMap.put(R.id.four, '4');
57 displayMap.put(R.id.five, '5');
58 displayMap.put(R.id.six, '6');
59 displayMap.put(R.id.seven, '7');
60 displayMap.put(R.id.eight, '8');
61 displayMap.put(R.id.nine, '9');
62 displayMap.put(R.id.zero, '0');
63 displayMap.put(R.id.pound, '#');
64 displayMap.put(R.id.star, '*');
Eric Erfanianccca3152017-02-22 16:32:36 -080065 }
66
linyuh183cb712017-12-27 17:02:37 -080067 private final int[] buttonIds =
Eric Erfanianccca3152017-02-22 16:32:36 -080068 new int[] {
69 R.id.zero,
70 R.id.one,
71 R.id.two,
72 R.id.three,
73 R.id.four,
74 R.id.five,
75 R.id.six,
76 R.id.seven,
77 R.id.eight,
78 R.id.nine,
79 R.id.star,
80 R.id.pound
81 };
linyuh183cb712017-12-27 17:02:37 -080082 private EditText dtmfDialerField;
Eric Erfanianccca3152017-02-22 16:32:36 -080083 // KeyListener used with the "dialpad digits" EditText widget.
linyuh183cb712017-12-27 17:02:37 -080084 private DtmfKeyListener dtmfKeyListener;
85 private DialpadView dialpadView;
86 private int currentTextColor;
Eric Erfanianccca3152017-02-22 16:32:36 -080087
88 @Override
89 public void onClick(View v) {
90 if (v.getId() == R.id.dialpad_back) {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070091 Logger.get(getContext())
92 .logImpression(DialerImpression.Type.IN_CALL_DIALPAD_CLOSE_BUTTON_PRESSED);
Eric Erfanianccca3152017-02-22 16:32:36 -080093 getActivity().onBackPressed();
94 }
95 }
96
97 @Override
98 public boolean onKey(View v, int keyCode, KeyEvent event) {
99 Log.d(this, "onKey: keyCode " + keyCode + ", view " + v);
100
101 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
102 int viewId = v.getId();
linyuh183cb712017-12-27 17:02:37 -0800103 if (displayMap.containsKey(viewId)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800104 switch (event.getAction()) {
105 case KeyEvent.ACTION_DOWN:
106 if (event.getRepeatCount() == 0) {
linyuh183cb712017-12-27 17:02:37 -0800107 getPresenter().processDtmf(displayMap.get(viewId));
Eric Erfanianccca3152017-02-22 16:32:36 -0800108 }
109 break;
110 case KeyEvent.ACTION_UP:
111 getPresenter().stopDtmf();
112 break;
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700113 default: // fall out
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 }
115 // do not return true [handled] here, since we want the
116 // press / click animation to be handled by the framework.
117 }
118 }
119 return false;
120 }
121
122 @Override
123 public DialpadPresenter createPresenter() {
124 return new DialpadPresenter();
125 }
126
127 @Override
128 public DialpadPresenter.DialpadUi getUi() {
129 return this;
130 }
131
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700132 // TODO(klp) Adds hardware keyboard listener
Eric Erfanianccca3152017-02-22 16:32:36 -0800133
134 @Override
135 public View onCreateView(
136 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
137 final View parent = inflater.inflate(R.layout.incall_dialpad_fragment, container, false);
linyuh183cb712017-12-27 17:02:37 -0800138 dialpadView = (DialpadView) parent.findViewById(R.id.dialpad_view);
139 dialpadView.setCanDigitsBeEdited(false);
140 dialpadView.setBackgroundResource(R.color.incall_dialpad_background);
141 dtmfDialerField = (EditText) parent.findViewById(R.id.digits);
142 if (dtmfDialerField != null) {
maxwelb5e22e802017-11-27 17:01:07 -0800143 LogUtil.i("DialpadFragment.onCreateView", "creating dtmfKeyListener");
linyuh183cb712017-12-27 17:02:37 -0800144 dtmfKeyListener = new DtmfKeyListener(getPresenter());
145 dtmfDialerField.setKeyListener(dtmfKeyListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800146 // remove the long-press context menus that support
147 // the edit (copy / paste / select) functions.
linyuh183cb712017-12-27 17:02:37 -0800148 dtmfDialerField.setLongClickable(false);
149 dtmfDialerField.setElegantTextHeight(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800150 configureKeypadListeners();
151 }
linyuh183cb712017-12-27 17:02:37 -0800152 View backButton = dialpadView.findViewById(R.id.dialpad_back);
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 backButton.setVisibility(View.VISIBLE);
154 backButton.setOnClickListener(this);
155
156 return parent;
157 }
158
159 @Override
160 public void onResume() {
161 super.onResume();
162 updateColors();
163 }
164
165 public void updateColors() {
166 int textColor = InCallPresenter.getInstance().getThemeColorManager().getPrimaryColor();
167
linyuh183cb712017-12-27 17:02:37 -0800168 if (currentTextColor == textColor) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800169 return;
170 }
171
172 DialpadKeyButton dialpadKey;
linyuh183cb712017-12-27 17:02:37 -0800173 for (int i = 0; i < buttonIds.length; i++) {
174 dialpadKey = (DialpadKeyButton) dialpadView.findViewById(buttonIds[i]);
Eric Erfanianccca3152017-02-22 16:32:36 -0800175 ((TextView) dialpadKey.findViewById(R.id.dialpad_key_number)).setTextColor(textColor);
176 }
177
linyuh183cb712017-12-27 17:02:37 -0800178 currentTextColor = textColor;
Eric Erfanianccca3152017-02-22 16:32:36 -0800179 }
180
181 @Override
182 public void onDestroyView() {
linyuh183cb712017-12-27 17:02:37 -0800183 dtmfKeyListener = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 super.onDestroyView();
185 }
186
187 /**
188 * Getter for Dialpad text.
189 *
190 * @return String containing current Dialpad EditText text.
191 */
192 public String getDtmfText() {
linyuh183cb712017-12-27 17:02:37 -0800193 return dtmfDialerField.getText().toString();
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 }
195
196 /**
197 * Sets the Dialpad text field with some text.
198 *
199 * @param text Text to set Dialpad EditText to.
200 */
201 public void setDtmfText(String text) {
linyuhb06d0092018-03-01 15:05:36 -0800202 dtmfDialerField.setText(PhoneNumberUtils.createTtsSpannable(text));
Eric Erfanianccca3152017-02-22 16:32:36 -0800203 }
204
Eric Erfanianccca3152017-02-22 16:32:36 -0800205 /** Starts the slide up animation for the Dialpad keys when the Dialpad is revealed. */
206 public void animateShowDialpad() {
207 final DialpadView dialpadView = (DialpadView) getView().findViewById(R.id.dialpad_view);
208 dialpadView.animateShow();
209 }
210
211 @Override
212 public void appendDigitsToField(char digit) {
linyuh183cb712017-12-27 17:02:37 -0800213 if (dtmfDialerField != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800214 // TODO: maybe *don't* manually append this digit if
215 // mDialpadDigits is focused and this key came from the HW
216 // keyboard, since in that case the EditText field will
217 // get the key event directly and automatically appends
218 // whetever the user types.
219 // (Or, a cleaner fix would be to just make mDialpadDigits
220 // *not* handle HW key presses. That seems to be more
221 // complicated than just setting focusable="false" on it,
222 // though.)
linyuh183cb712017-12-27 17:02:37 -0800223 dtmfDialerField.getText().append(digit);
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 }
225 }
226
227 /** Called externally (from InCallScreen) to play a DTMF Tone. */
228 /* package */ boolean onDialerKeyDown(KeyEvent event) {
229 Log.d(this, "Notifying dtmf key down.");
linyuh183cb712017-12-27 17:02:37 -0800230 if (dtmfKeyListener != null) {
231 return dtmfKeyListener.onKeyDown(event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800232 } else {
233 return false;
234 }
235 }
236
237 /** Called externally (from InCallScreen) to cancel the last DTMF Tone played. */
238 public boolean onDialerKeyUp(KeyEvent event) {
239 Log.d(this, "Notifying dtmf key up.");
linyuh183cb712017-12-27 17:02:37 -0800240 if (dtmfKeyListener != null) {
241 return dtmfKeyListener.onKeyUp(event);
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 } else {
243 return false;
244 }
245 }
246
247 private void configureKeypadListeners() {
248 DialpadKeyButton dialpadKey;
linyuh183cb712017-12-27 17:02:37 -0800249 for (int i = 0; i < buttonIds.length; i++) {
250 dialpadKey = (DialpadKeyButton) dialpadView.findViewById(buttonIds[i]);
Eric Erfanianccca3152017-02-22 16:32:36 -0800251 dialpadKey.setOnKeyListener(this);
252 dialpadKey.setOnClickListener(this);
253 dialpadKey.setOnPressedListener(this);
254 }
255 }
256
257 @Override
258 public void onPressed(View view, boolean pressed) {
linyuh183cb712017-12-27 17:02:37 -0800259 if (pressed && displayMap.containsKey(view.getId())) {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700260 Logger.get(getContext())
261 .logImpression(DialerImpression.Type.IN_CALL_DIALPAD_NUMBER_BUTTON_PRESSED);
linyuh183cb712017-12-27 17:02:37 -0800262 Log.d(this, "onPressed: " + pressed + " " + displayMap.get(view.getId()));
263 getPresenter().processDtmf(displayMap.get(view.getId()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800264 }
265 if (!pressed) {
266 Log.d(this, "onPressed: " + pressed);
267 getPresenter().stopDtmf();
268 }
269 }
270
271 /**
272 * LinearLayout with getter and setter methods for the translationY property using floats, for
273 * animation purposes.
274 */
275 public static class DialpadSlidingLinearLayout extends LinearLayout {
276
277 public DialpadSlidingLinearLayout(Context context) {
278 super(context);
279 }
280
281 public DialpadSlidingLinearLayout(Context context, AttributeSet attrs) {
282 super(context, attrs);
283 }
284
285 public DialpadSlidingLinearLayout(Context context, AttributeSet attrs, int defStyle) {
286 super(context, attrs, defStyle);
287 }
288
289 public float getYFraction() {
290 final int height = getHeight();
291 if (height == 0) {
292 return 0;
293 }
294 return getTranslationY() / height;
295 }
296
297 public void setYFraction(float yFraction) {
298 setTranslationY(yFraction * getHeight());
299 }
300 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800301}