Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.incallui; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Bundle; |
linyuh | b06d009 | 2018-03-01 15:05:36 -0800 | [diff] [blame^] | 21 | import android.telephony.PhoneNumberUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 22 | import android.util.ArrayMap; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.KeyEvent; |
| 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
| 28 | import android.view.View.OnKeyListener; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.widget.EditText; |
| 31 | import android.widget.LinearLayout; |
| 32 | import android.widget.TextView; |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame] | 33 | import com.android.dialer.common.LogUtil; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 34 | import com.android.dialer.dialpadview.DialpadKeyButton; |
| 35 | import com.android.dialer.dialpadview.DialpadKeyButton.OnPressedListener; |
| 36 | import com.android.dialer.dialpadview.DialpadView; |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 37 | import com.android.dialer.logging.DialerImpression; |
| 38 | import com.android.dialer.logging.Logger; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 39 | import com.android.incallui.DialpadPresenter.DialpadUi; |
| 40 | import com.android.incallui.baseui.BaseFragment; |
| 41 | import java.util.Map; |
| 42 | |
| 43 | /** Fragment for call control buttons */ |
| 44 | public class DialpadFragment extends BaseFragment<DialpadPresenter, DialpadUi> |
| 45 | implements DialpadUi, OnKeyListener, OnClickListener, OnPressedListener { |
| 46 | |
| 47 | /** Hash Map to map a view id to a character */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 48 | private static final Map<Integer, Character> displayMap = new ArrayMap<>(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 49 | |
| 50 | /** Set up the static maps */ |
| 51 | static { |
| 52 | // Map the buttons to the display characters |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 53 | 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | } |
| 66 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 67 | private final int[] buttonIds = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 68 | 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 | }; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 82 | private EditText dtmfDialerField; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 83 | // KeyListener used with the "dialpad digits" EditText widget. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 84 | private DtmfKeyListener dtmfKeyListener; |
| 85 | private DialpadView dialpadView; |
| 86 | private int currentTextColor; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 87 | |
| 88 | @Override |
| 89 | public void onClick(View v) { |
| 90 | if (v.getId() == R.id.dialpad_back) { |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 91 | Logger.get(getContext()) |
| 92 | .logImpression(DialerImpression.Type.IN_CALL_DIALPAD_CLOSE_BUTTON_PRESSED); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 93 | 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(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 103 | if (displayMap.containsKey(viewId)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 104 | switch (event.getAction()) { |
| 105 | case KeyEvent.ACTION_DOWN: |
| 106 | if (event.getRepeatCount() == 0) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 107 | getPresenter().processDtmf(displayMap.get(viewId)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 108 | } |
| 109 | break; |
| 110 | case KeyEvent.ACTION_UP: |
| 111 | getPresenter().stopDtmf(); |
| 112 | break; |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 113 | default: // fall out |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 114 | } |
| 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 Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 132 | // TODO(klp) Adds hardware keyboard listener |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 133 | |
| 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); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 138 | 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) { |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame] | 143 | LogUtil.i("DialpadFragment.onCreateView", "creating dtmfKeyListener"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 144 | dtmfKeyListener = new DtmfKeyListener(getPresenter()); |
| 145 | dtmfDialerField.setKeyListener(dtmfKeyListener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 146 | // remove the long-press context menus that support |
| 147 | // the edit (copy / paste / select) functions. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 148 | dtmfDialerField.setLongClickable(false); |
| 149 | dtmfDialerField.setElegantTextHeight(false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 150 | configureKeypadListeners(); |
| 151 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 152 | View backButton = dialpadView.findViewById(R.id.dialpad_back); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 153 | 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 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 168 | if (currentTextColor == textColor) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| 172 | DialpadKeyButton dialpadKey; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 173 | for (int i = 0; i < buttonIds.length; i++) { |
| 174 | dialpadKey = (DialpadKeyButton) dialpadView.findViewById(buttonIds[i]); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 175 | ((TextView) dialpadKey.findViewById(R.id.dialpad_key_number)).setTextColor(textColor); |
| 176 | } |
| 177 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 178 | currentTextColor = textColor; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void onDestroyView() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 183 | dtmfKeyListener = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 184 | super.onDestroyView(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Getter for Dialpad text. |
| 189 | * |
| 190 | * @return String containing current Dialpad EditText text. |
| 191 | */ |
| 192 | public String getDtmfText() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 193 | return dtmfDialerField.getText().toString(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 194 | } |
| 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) { |
linyuh | b06d009 | 2018-03-01 15:05:36 -0800 | [diff] [blame^] | 202 | dtmfDialerField.setText(PhoneNumberUtils.createTtsSpannable(text)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 205 | /** 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) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 213 | if (dtmfDialerField != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 214 | // 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.) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 223 | dtmfDialerField.getText().append(digit); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 224 | } |
| 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."); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 230 | if (dtmfKeyListener != null) { |
| 231 | return dtmfKeyListener.onKeyDown(event); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 232 | } 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."); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 240 | if (dtmfKeyListener != null) { |
| 241 | return dtmfKeyListener.onKeyUp(event); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 242 | } else { |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private void configureKeypadListeners() { |
| 248 | DialpadKeyButton dialpadKey; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 249 | for (int i = 0; i < buttonIds.length; i++) { |
| 250 | dialpadKey = (DialpadKeyButton) dialpadView.findViewById(buttonIds[i]); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 251 | dialpadKey.setOnKeyListener(this); |
| 252 | dialpadKey.setOnClickListener(this); |
| 253 | dialpadKey.setOnPressedListener(this); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void onPressed(View view, boolean pressed) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 259 | if (pressed && displayMap.containsKey(view.getId())) { |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 260 | Logger.get(getContext()) |
| 261 | .logImpression(DialerImpression.Type.IN_CALL_DIALPAD_NUMBER_BUTTON_PRESSED); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 262 | Log.d(this, "onPressed: " + pressed + " " + displayMap.get(view.getId())); |
| 263 | getPresenter().processDtmf(displayMap.get(view.getId())); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 264 | } |
| 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 301 | } |