summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/dialpadview/DialpadView.java
blob: 5e79cb59fd636ddc124ca559c046ae644be93825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.dialpadview;

import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.style.TtsSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.accessibility.AccessibilityManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.dialer.animation.AnimUtils;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.compat.CompatUtils;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

/** View that displays a twelve-key phone dialpad. */
public class DialpadView extends LinearLayout {

  private static final String TAG = DialpadView.class.getSimpleName();

  // Parameters for animation
  private static final double DELAY_MULTIPLIER = 0.66;
  private static final double DURATION_MULTIPLIER = 0.8;
  private static final int KEY_FRAME_DURATION = 33;

  // Resource IDs for buttons (0-9, *, and #)
  private static final int[] BUTTON_IDS =
      new int[] {
        R.id.zero,
        R.id.one,
        R.id.two,
        R.id.three,
        R.id.four,
        R.id.five,
        R.id.six,
        R.id.seven,
        R.id.eight,
        R.id.nine,
        R.id.star,
        R.id.pound
      };

  // Resource IDs for the button-letter mapping
  private static final int[] LETTER_MAPPING_IDS =
      new int[] {
        R.string.dialpad_0_letters,
        R.string.dialpad_1_letters,
        R.string.dialpad_2_letters,
        R.string.dialpad_3_letters,
        R.string.dialpad_4_letters,
        R.string.dialpad_5_letters,
        R.string.dialpad_6_letters,
        R.string.dialpad_7_letters,
        R.string.dialpad_8_letters,
        R.string.dialpad_9_letters,
        R.string.dialpad_star_letters,
        R.string.dialpad_pound_letters
      };

  // Whether the device is in landscape mode
  private final boolean mIsLandscape;

  // Whether the dialpad is shown in a right-to-left locale
  private final boolean mIsRtl;

  private EditText mDigits;
  private ImageButton mDelete;
  private View mOverflowMenuButton;
  private ColorStateList mRippleColor;
  private ViewGroup mRateContainer;
  private TextView mIldCountry;
  private TextView mIldRate;
  private boolean mCanDigitsBeEdited;
  private int mTranslateDistance;

  public DialpadView(Context context) {
    this(context, null);
  }

  public DialpadView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public DialpadView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Dialpad);
    mRippleColor = a.getColorStateList(R.styleable.Dialpad_dialpad_key_button_touch_tint);
    a.recycle();

    mTranslateDistance =
        getResources().getDimensionPixelSize(R.dimen.dialpad_key_button_translate_y);

    mIsLandscape =
        getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    mIsRtl =
        TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL;
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();

    setupKeypad();
    mDigits = (EditText) findViewById(R.id.digits);
    mDelete = (ImageButton) findViewById(R.id.deleteButton);
    mOverflowMenuButton = findViewById(R.id.dialpad_overflow);
    mRateContainer = (ViewGroup) findViewById(R.id.rate_container);
    mIldCountry = (TextView) mRateContainer.findViewById(R.id.ild_country);
    mIldRate = (TextView) mRateContainer.findViewById(R.id.ild_rate);

    AccessibilityManager accessibilityManager =
        (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
      // The text view must be selected to send accessibility events.
      mDigits.setSelected(true);
    }
  }

  private void setupKeypad() {
    final Resources resources = getContext().getResources();

    final Locale currentLocale = resources.getConfiguration().locale;
    final NumberFormat nf;
    // We translate dialpad numbers only for "fa" and not any other locale
    // ("ar" anybody ?).
    if ("fa".equals(currentLocale.getLanguage())) {
      nf = DecimalFormat.getInstance(CompatUtils.getLocale(getContext()));
    } else {
      nf = DecimalFormat.getInstance(Locale.ENGLISH);
    }

    for (int i = 0; i < BUTTON_IDS.length; i++) {
      DialpadKeyButton dialpadKey = (DialpadKeyButton) findViewById(BUTTON_IDS[i]);
      TextView numberView = (TextView) dialpadKey.findViewById(R.id.dialpad_key_number);

      final String numberString;
      final CharSequence numberContentDescription;
      if (BUTTON_IDS[i] == R.id.pound) {
        numberString = resources.getString(R.string.dialpad_pound_number);
        numberContentDescription = numberString;
      } else if (BUTTON_IDS[i] == R.id.star) {
        numberString = resources.getString(R.string.dialpad_star_number);
        numberContentDescription = numberString;
      } else {
        numberString = nf.format(i);
        // The content description is used for Talkback key presses. The number is
        // separated by a "," to introduce a slight delay. Convert letters into a verbatim
        // span so that they are read as letters instead of as one word.
        String letters = resources.getString(LETTER_MAPPING_IDS[i]);
        Spannable spannable =
            Spannable.Factory.getInstance().newSpannable(numberString + "," + letters);
        spannable.setSpan(
            (new TtsSpan.VerbatimBuilder(letters)).build(),
            numberString.length() + 1,
            numberString.length() + 1 + letters.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        numberContentDescription = spannable;
      }

      final RippleDrawable rippleBackground =
          (RippleDrawable) getDrawableCompat(getContext(), R.drawable.btn_dialpad_key);
      if (mRippleColor != null) {
        rippleBackground.setColor(mRippleColor);
      }

      numberView.setText(numberString);
      numberView.setElegantTextHeight(false);
      dialpadKey.setContentDescription(numberContentDescription);
      dialpadKey.setBackground(rippleBackground);

      TextView lettersView = (TextView) dialpadKey.findViewById(R.id.dialpad_key_letters);
      if (lettersView != null) {
        lettersView.setText(resources.getString(LETTER_MAPPING_IDS[i]));
      }
    }

    final DialpadKeyButton one = (DialpadKeyButton) findViewById(R.id.one);
    one.setLongHoverContentDescription(resources.getText(R.string.description_voicemail_button));

    final DialpadKeyButton zero = (DialpadKeyButton) findViewById(R.id.zero);
    zero.setLongHoverContentDescription(resources.getText(R.string.description_image_button_plus));
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    if (changed) {
      if (mIsLandscape) {
        adjustKeyWidths();
      } else {
        adjustDigitKeyHeights();
      }
    }
  }

  /**
   * Make the heights of all digit keys the same.
   *
   * <p>When the device is in portrait mode, we first find the maximum height among digit key
   * layouts. Then for each key, we adjust the height of the layout containing letters/the voice
   * mail icon to ensure the height of each digit key is the same.
   *
   * <p>This method should be called after the sizes of related layouts have been calculated by the
   * framework.
   */
  private void adjustDigitKeyHeights() {
    Assert.checkState(!mIsLandscape);

    int maxHeight = 0;
    for (int i = 0; i <= 9; i++) {
      DialpadKeyButton dialpadKey = (DialpadKeyButton) findViewById(BUTTON_IDS[i]);
      LinearLayout keyLayout = (LinearLayout) dialpadKey.findViewById(R.id.dialpad_key_layout);
      maxHeight = Math.max(maxHeight, keyLayout.getHeight());
    }

    for (int i = 0; i <= 9; i++) {
      DialpadKeyButton dialpadKey = (DialpadKeyButton) findViewById(BUTTON_IDS[i]);
      LinearLayout keyLayout = (LinearLayout) dialpadKey.findViewById(R.id.dialpad_key_layout);

      DialpadTextView numberView =
          (DialpadTextView) keyLayout.findViewById(R.id.dialpad_key_number);
      MarginLayoutParams numberViewLayoutParams = (MarginLayoutParams) numberView.getLayoutParams();

      LinearLayout iconOrLettersLayout =
          (LinearLayout) keyLayout.findViewById(R.id.dialpad_key_icon_or_letters_layout);
      iconOrLettersLayout.setLayoutParams(
          new LayoutParams(
              LayoutParams.WRAP_CONTENT /* width */,
              maxHeight
                  - numberView.getHeight()
                  - numberViewLayoutParams.topMargin
                  - numberViewLayoutParams.bottomMargin /* height */));
    }
  }

  /**
   * Adjust key widths to align keys in each column.
   *
   * <p>When the device is in landscape mode, we first find the maximum among a pre-defined width
   * and the width of each key layout. Then we adjust the width of each layout's horizontal
   * placeholder to align keys in each column. This is to accommodate the scenario where not all
   * letters associated with a key can be displayed in one line due to large font size.
   *
   * <p>This method should be called after the sizes of related layouts have been calculated by the
   * framework.
   */
  private void adjustKeyWidths() {
    Assert.checkState(mIsLandscape);

    // A pre-defined minimum width for the letters shown beside a key.
    final int minimumKeyLettersWidth =
        getContext().getResources().getDimensionPixelSize(R.dimen.dialpad_key_text_width);

    // The maximum width of the key layouts. A key layout includes both the number and the letters.
    int maxWidth = 0;

    for (int buttonId : BUTTON_IDS) {
      DialpadKeyButton dialpadKey = (DialpadKeyButton) findViewById(buttonId);
      LinearLayout keyLayout = (LinearLayout) dialpadKey.findViewById(R.id.dialpad_key_layout);
      TextView keyLettersView = (TextView) keyLayout.findViewById(R.id.dialpad_key_letters);
      if (keyLettersView != null && keyLettersView.getWidth() < minimumKeyLettersWidth) {
        // If the width of the letters is less than the pre-defined minimum, use the pre-defined
        // minimum to obtain the maximum width.
        maxWidth =
            Math.max(
                maxWidth,
                keyLayout.getWidth() - keyLettersView.getWidth() + minimumKeyLettersWidth);
      } else {
        maxWidth = Math.max(maxWidth, keyLayout.getWidth());
      }
    }

    for (int buttonId : BUTTON_IDS) {
      DialpadKeyButton dialpadKey = (DialpadKeyButton) findViewById(buttonId);
      LinearLayout keyLayout = (LinearLayout) dialpadKey.findViewById(R.id.dialpad_key_layout);
      View horizontalPlaceholder = keyLayout.findViewById(R.id.dialpad_key_horizontal_placeholder);
      horizontalPlaceholder.setLayoutParams(
          new LayoutParams(
              maxWidth - keyLayout.getWidth() /* width */, LayoutParams.MATCH_PARENT /* height */));
    }
  }

  private Drawable getDrawableCompat(Context context, int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      return context.getDrawable(id);
    } else {
      return context.getResources().getDrawable(id);
    }
  }

  public void setShowVoicemailButton(boolean show) {
    View view = findViewById(R.id.dialpad_key_voicemail);
    if (view != null) {
      view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }
  }

  /**
   * Whether or not the digits above the dialer can be edited.
   *
   * @param canBeEdited If true, the backspace button will be shown and the digits EditText will be
   *     configured to allow text manipulation.
   */
  public void setCanDigitsBeEdited(boolean canBeEdited) {
    View deleteButton = findViewById(R.id.deleteButton);
    deleteButton.setVisibility(canBeEdited ? View.VISIBLE : View.INVISIBLE);
    View overflowMenuButton = findViewById(R.id.dialpad_overflow);
    overflowMenuButton.setVisibility(canBeEdited ? View.VISIBLE : View.GONE);

    EditText digits = (EditText) findViewById(R.id.digits);
    digits.setClickable(canBeEdited);
    digits.setLongClickable(canBeEdited);
    digits.setFocusableInTouchMode(canBeEdited);
    digits.setCursorVisible(false);

    mCanDigitsBeEdited = canBeEdited;
  }

  public void setCallRateInformation(String countryName, String displayRate) {
    if (TextUtils.isEmpty(countryName) && TextUtils.isEmpty(displayRate)) {
      mRateContainer.setVisibility(View.GONE);
      return;
    }
    mRateContainer.setVisibility(View.VISIBLE);
    mIldCountry.setText(countryName);
    mIldRate.setText(displayRate);
  }

  public boolean canDigitsBeEdited() {
    return mCanDigitsBeEdited;
  }

  /**
   * Always returns true for onHoverEvent callbacks, to fix problems with accessibility due to the
   * dialpad overlaying other fragments.
   */
  @Override
  public boolean onHoverEvent(MotionEvent event) {
    return true;
  }

  public void animateShow() {
    // This is a hack; without this, the setTranslationY is delayed in being applied, and the
    // numbers appear at their original position (0) momentarily before animating.
    final AnimatorListenerAdapter showListener = new AnimatorListenerAdapter() {};

    for (int i = 0; i < BUTTON_IDS.length; i++) {
      int delay = (int) (getKeyButtonAnimationDelay(BUTTON_IDS[i]) * DELAY_MULTIPLIER);
      int duration = (int) (getKeyButtonAnimationDuration(BUTTON_IDS[i]) * DURATION_MULTIPLIER);
      final DialpadKeyButton dialpadKey = (DialpadKeyButton) findViewById(BUTTON_IDS[i]);

      ViewPropertyAnimator animator = dialpadKey.animate();
      if (mIsLandscape) {
        // Landscape orientation requires translation along the X axis.
        // For RTL locales, ensure we translate negative on the X axis.
        dialpadKey.setTranslationX((mIsRtl ? -1 : 1) * mTranslateDistance);
        animator.translationX(0);
      } else {
        // Portrait orientation requires translation along the Y axis.
        dialpadKey.setTranslationY(mTranslateDistance);
        animator.translationY(0);
      }
      animator
          .setInterpolator(AnimUtils.EASE_OUT_EASE_IN)
          .setStartDelay(delay)
          .setDuration(duration)
          .setListener(showListener)
          .start();
    }
  }

  public EditText getDigits() {
    return mDigits;
  }

  public ImageButton getDeleteButton() {
    return mDelete;
  }

  public View getOverflowMenuButton() {
    return mOverflowMenuButton;
  }

  /**
   * Get the animation delay for the buttons, taking into account whether the dialpad is in
   * landscape left-to-right, landscape right-to-left, or portrait.
   *
   * @param buttonId The button ID.
   * @return The animation delay.
   */
  private int getKeyButtonAnimationDelay(int buttonId) {
    if (mIsLandscape) {
      if (mIsRtl) {
        if (buttonId == R.id.three) {
          return KEY_FRAME_DURATION * 1;
        } else if (buttonId == R.id.six) {
          return KEY_FRAME_DURATION * 2;
        } else if (buttonId == R.id.nine) {
          return KEY_FRAME_DURATION * 3;
        } else if (buttonId == R.id.pound) {
          return KEY_FRAME_DURATION * 4;
        } else if (buttonId == R.id.two) {
          return KEY_FRAME_DURATION * 5;
        } else if (buttonId == R.id.five) {
          return KEY_FRAME_DURATION * 6;
        } else if (buttonId == R.id.eight) {
          return KEY_FRAME_DURATION * 7;
        } else if (buttonId == R.id.zero) {
          return KEY_FRAME_DURATION * 8;
        } else if (buttonId == R.id.one) {
          return KEY_FRAME_DURATION * 9;
        } else if (buttonId == R.id.four) {
          return KEY_FRAME_DURATION * 10;
        } else if (buttonId == R.id.seven || buttonId == R.id.star) {
          return KEY_FRAME_DURATION * 11;
        }
      } else {
        if (buttonId == R.id.one) {
          return KEY_FRAME_DURATION * 1;
        } else if (buttonId == R.id.four) {
          return KEY_FRAME_DURATION * 2;
        } else if (buttonId == R.id.seven) {
          return KEY_FRAME_DURATION * 3;
        } else if (buttonId == R.id.star) {
          return KEY_FRAME_DURATION * 4;
        } else if (buttonId == R.id.two) {
          return KEY_FRAME_DURATION * 5;
        } else if (buttonId == R.id.five) {
          return KEY_FRAME_DURATION * 6;
        } else if (buttonId == R.id.eight) {
          return KEY_FRAME_DURATION * 7;
        } else if (buttonId == R.id.zero) {
          return KEY_FRAME_DURATION * 8;
        } else if (buttonId == R.id.three) {
          return KEY_FRAME_DURATION * 9;
        } else if (buttonId == R.id.six) {
          return KEY_FRAME_DURATION * 10;
        } else if (buttonId == R.id.nine || buttonId == R.id.pound) {
          return KEY_FRAME_DURATION * 11;
        }
      }
    } else {
      if (buttonId == R.id.one) {
        return KEY_FRAME_DURATION * 1;
      } else if (buttonId == R.id.two) {
        return KEY_FRAME_DURATION * 2;
      } else if (buttonId == R.id.three) {
        return KEY_FRAME_DURATION * 3;
      } else if (buttonId == R.id.four) {
        return KEY_FRAME_DURATION * 4;
      } else if (buttonId == R.id.five) {
        return KEY_FRAME_DURATION * 5;
      } else if (buttonId == R.id.six) {
        return KEY_FRAME_DURATION * 6;
      } else if (buttonId == R.id.seven) {
        return KEY_FRAME_DURATION * 7;
      } else if (buttonId == R.id.eight) {
        return KEY_FRAME_DURATION * 8;
      } else if (buttonId == R.id.nine) {
        return KEY_FRAME_DURATION * 9;
      } else if (buttonId == R.id.star) {
        return KEY_FRAME_DURATION * 10;
      } else if (buttonId == R.id.zero || buttonId == R.id.pound) {
        return KEY_FRAME_DURATION * 11;
      }
    }

    LogUtil.e(TAG, "Attempted to get animation delay for invalid key button id.");
    return 0;
  }

  /**
   * Get the button animation duration, taking into account whether the dialpad is in landscape
   * left-to-right, landscape right-to-left, or portrait.
   *
   * @param buttonId The button ID.
   * @return The animation duration.
   */
  private int getKeyButtonAnimationDuration(int buttonId) {
    if (mIsLandscape) {
      if (mIsRtl) {
        if (buttonId == R.id.one
            || buttonId == R.id.four
            || buttonId == R.id.seven
            || buttonId == R.id.star) {
          return KEY_FRAME_DURATION * 8;
        } else if (buttonId == R.id.two
            || buttonId == R.id.five
            || buttonId == R.id.eight
            || buttonId == R.id.zero) {
          return KEY_FRAME_DURATION * 9;
        } else if (buttonId == R.id.three
            || buttonId == R.id.six
            || buttonId == R.id.nine
            || buttonId == R.id.pound) {
          return KEY_FRAME_DURATION * 10;
        }
      } else {
        if (buttonId == R.id.one
            || buttonId == R.id.four
            || buttonId == R.id.seven
            || buttonId == R.id.star) {
          return KEY_FRAME_DURATION * 10;
        } else if (buttonId == R.id.two
            || buttonId == R.id.five
            || buttonId == R.id.eight
            || buttonId == R.id.zero) {
          return KEY_FRAME_DURATION * 9;
        } else if (buttonId == R.id.three
            || buttonId == R.id.six
            || buttonId == R.id.nine
            || buttonId == R.id.pound) {
          return KEY_FRAME_DURATION * 8;
        }
      }
    } else {
      if (buttonId == R.id.one
          || buttonId == R.id.two
          || buttonId == R.id.three
          || buttonId == R.id.four
          || buttonId == R.id.five
          || buttonId == R.id.six) {
        return KEY_FRAME_DURATION * 10;
      } else if (buttonId == R.id.seven || buttonId == R.id.eight || buttonId == R.id.nine) {
        return KEY_FRAME_DURATION * 9;
      } else if (buttonId == R.id.star || buttonId == R.id.zero || buttonId == R.id.pound) {
        return KEY_FRAME_DURATION * 8;
      }
    }

    LogUtil.e(TAG, "Attempted to get animation duration for invalid key button id.");
    return 0;
  }
}