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; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 21 | import android.util.ArrayMap; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.KeyEvent; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.View.OnClickListener; |
| 27 | import android.view.View.OnKeyListener; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.EditText; |
| 30 | import android.widget.LinearLayout; |
| 31 | import android.widget.TextView; |
| 32 | import com.android.contacts.common.compat.PhoneNumberUtilsCompat; |
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 */ |
| 48 | private static final Map<Integer, Character> mDisplayMap = new ArrayMap<>(); |
| 49 | |
| 50 | /** Set up the static maps */ |
| 51 | static { |
| 52 | // Map the buttons to the display characters |
| 53 | mDisplayMap.put(R.id.one, '1'); |
| 54 | mDisplayMap.put(R.id.two, '2'); |
| 55 | mDisplayMap.put(R.id.three, '3'); |
| 56 | mDisplayMap.put(R.id.four, '4'); |
| 57 | mDisplayMap.put(R.id.five, '5'); |
| 58 | mDisplayMap.put(R.id.six, '6'); |
| 59 | mDisplayMap.put(R.id.seven, '7'); |
| 60 | mDisplayMap.put(R.id.eight, '8'); |
| 61 | mDisplayMap.put(R.id.nine, '9'); |
| 62 | mDisplayMap.put(R.id.zero, '0'); |
| 63 | mDisplayMap.put(R.id.pound, '#'); |
| 64 | mDisplayMap.put(R.id.star, '*'); |
| 65 | } |
| 66 | |
| 67 | private final int[] mButtonIds = |
| 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 | }; |
| 82 | private EditText mDtmfDialerField; |
| 83 | // KeyListener used with the "dialpad digits" EditText widget. |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame^] | 84 | private DtmfKeyListener mDtmfKeyListener; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 85 | private DialpadView mDialpadView; |
| 86 | private int mCurrentTextColor; |
| 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(); |
| 103 | if (mDisplayMap.containsKey(viewId)) { |
| 104 | switch (event.getAction()) { |
| 105 | case KeyEvent.ACTION_DOWN: |
| 106 | if (event.getRepeatCount() == 0) { |
| 107 | getPresenter().processDtmf(mDisplayMap.get(viewId)); |
| 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); |
| 138 | mDialpadView = (DialpadView) parent.findViewById(R.id.dialpad_view); |
| 139 | mDialpadView.setCanDigitsBeEdited(false); |
| 140 | mDialpadView.setBackgroundResource(R.color.incall_dialpad_background); |
| 141 | mDtmfDialerField = (EditText) parent.findViewById(R.id.digits); |
| 142 | if (mDtmfDialerField != null) { |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame^] | 143 | LogUtil.i("DialpadFragment.onCreateView", "creating dtmfKeyListener"); |
| 144 | mDtmfKeyListener = new DtmfKeyListener(getPresenter()); |
| 145 | mDtmfDialerField.setKeyListener(mDtmfKeyListener); |
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. |
| 148 | mDtmfDialerField.setLongClickable(false); |
| 149 | mDtmfDialerField.setElegantTextHeight(false); |
| 150 | configureKeypadListeners(); |
| 151 | } |
| 152 | View backButton = mDialpadView.findViewById(R.id.dialpad_back); |
| 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 | |
| 168 | if (mCurrentTextColor == textColor) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | DialpadKeyButton dialpadKey; |
| 173 | for (int i = 0; i < mButtonIds.length; i++) { |
| 174 | dialpadKey = (DialpadKeyButton) mDialpadView.findViewById(mButtonIds[i]); |
| 175 | ((TextView) dialpadKey.findViewById(R.id.dialpad_key_number)).setTextColor(textColor); |
| 176 | } |
| 177 | |
| 178 | mCurrentTextColor = textColor; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void onDestroyView() { |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame^] | 183 | mDtmfKeyListener = 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() { |
| 193 | return mDtmfDialerField.getText().toString(); |
| 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) { |
| 202 | mDtmfDialerField.setText(PhoneNumberUtilsCompat.createTtsSpannable(text)); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void setVisible(boolean on) { |
| 207 | if (on) { |
| 208 | getView().setVisibility(View.VISIBLE); |
| 209 | } else { |
| 210 | getView().setVisibility(View.INVISIBLE); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** Starts the slide up animation for the Dialpad keys when the Dialpad is revealed. */ |
| 215 | public void animateShowDialpad() { |
| 216 | final DialpadView dialpadView = (DialpadView) getView().findViewById(R.id.dialpad_view); |
| 217 | dialpadView.animateShow(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void appendDigitsToField(char digit) { |
| 222 | if (mDtmfDialerField != null) { |
| 223 | // TODO: maybe *don't* manually append this digit if |
| 224 | // mDialpadDigits is focused and this key came from the HW |
| 225 | // keyboard, since in that case the EditText field will |
| 226 | // get the key event directly and automatically appends |
| 227 | // whetever the user types. |
| 228 | // (Or, a cleaner fix would be to just make mDialpadDigits |
| 229 | // *not* handle HW key presses. That seems to be more |
| 230 | // complicated than just setting focusable="false" on it, |
| 231 | // though.) |
| 232 | mDtmfDialerField.getText().append(digit); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** Called externally (from InCallScreen) to play a DTMF Tone. */ |
| 237 | /* package */ boolean onDialerKeyDown(KeyEvent event) { |
| 238 | Log.d(this, "Notifying dtmf key down."); |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame^] | 239 | if (mDtmfKeyListener != null) { |
| 240 | return mDtmfKeyListener.onKeyDown(event); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 241 | } else { |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** Called externally (from InCallScreen) to cancel the last DTMF Tone played. */ |
| 247 | public boolean onDialerKeyUp(KeyEvent event) { |
| 248 | Log.d(this, "Notifying dtmf key up."); |
maxwelb | 5e22e80 | 2017-11-27 17:01:07 -0800 | [diff] [blame^] | 249 | if (mDtmfKeyListener != null) { |
| 250 | return mDtmfKeyListener.onKeyUp(event); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 251 | } else { |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | private void configureKeypadListeners() { |
| 257 | DialpadKeyButton dialpadKey; |
| 258 | for (int i = 0; i < mButtonIds.length; i++) { |
| 259 | dialpadKey = (DialpadKeyButton) mDialpadView.findViewById(mButtonIds[i]); |
| 260 | dialpadKey.setOnKeyListener(this); |
| 261 | dialpadKey.setOnClickListener(this); |
| 262 | dialpadKey.setOnPressedListener(this); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onPressed(View view, boolean pressed) { |
| 268 | if (pressed && mDisplayMap.containsKey(view.getId())) { |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 269 | Logger.get(getContext()) |
| 270 | .logImpression(DialerImpression.Type.IN_CALL_DIALPAD_NUMBER_BUTTON_PRESSED); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 271 | Log.d(this, "onPressed: " + pressed + " " + mDisplayMap.get(view.getId())); |
| 272 | getPresenter().processDtmf(mDisplayMap.get(view.getId())); |
| 273 | } |
| 274 | if (!pressed) { |
| 275 | Log.d(this, "onPressed: " + pressed); |
| 276 | getPresenter().stopDtmf(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * LinearLayout with getter and setter methods for the translationY property using floats, for |
| 282 | * animation purposes. |
| 283 | */ |
| 284 | public static class DialpadSlidingLinearLayout extends LinearLayout { |
| 285 | |
| 286 | public DialpadSlidingLinearLayout(Context context) { |
| 287 | super(context); |
| 288 | } |
| 289 | |
| 290 | public DialpadSlidingLinearLayout(Context context, AttributeSet attrs) { |
| 291 | super(context, attrs); |
| 292 | } |
| 293 | |
| 294 | public DialpadSlidingLinearLayout(Context context, AttributeSet attrs, int defStyle) { |
| 295 | super(context, attrs, defStyle); |
| 296 | } |
| 297 | |
| 298 | public float getYFraction() { |
| 299 | final int height = getHeight(); |
| 300 | if (height == 0) { |
| 301 | return 0; |
| 302 | } |
| 303 | return getTranslationY() / height; |
| 304 | } |
| 305 | |
| 306 | public void setYFraction(float yFraction) { |
| 307 | setTranslationY(yFraction * getHeight()); |
| 308 | } |
| 309 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 310 | } |