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
|
/*
* Copyright (C) 2013 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.incallui;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.DialerKeyListener;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.accessibility.AccessibilityManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.contacts.common.dialpad.DialpadKeyButton;
import com.android.contacts.common.dialpad.DialpadView;
import java.util.HashMap;
/**
* Fragment for call control buttons
*/
public class DialpadFragment extends BaseFragment<DialpadPresenter, DialpadPresenter.DialpadUi>
implements DialpadPresenter.DialpadUi, View.OnTouchListener, View.OnKeyListener,
View.OnHoverListener, View.OnClickListener {
private static final float DIALPAD_SLIDE_FRACTION = 1.0f;
/**
* LinearLayout with getter and setter methods for the translationY property using floats,
* for animation purposes.
*/
public static class DialpadSlidingLinearLayout extends LinearLayout {
public DialpadSlidingLinearLayout(Context context) {
super(context);
}
public DialpadSlidingLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DialpadSlidingLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public float getYFraction() {
final int height = getHeight();
if (height == 0) return 0;
return getTranslationY() / height;
}
public void setYFraction(float yFraction) {
setTranslationY(yFraction * getHeight());
}
}
/**
* LinearLayout that always returns true for onHoverEvent callbacks, to fix
* problems with accessibility due to the dialpad overlaying other fragments.
*/
public static class HoverIgnoringLinearLayout extends LinearLayout {
public HoverIgnoringLinearLayout(Context context) {
super(context);
}
public HoverIgnoringLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HoverIgnoringLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onHoverEvent(MotionEvent event) {
return true;
}
}
private EditText mDtmfDialerField;
/** Hash Map to map a view id to a character*/
private static final HashMap<Integer, Character> mDisplayMap =
new HashMap<Integer, Character>();
/** Set up the static maps*/
static {
// Map the buttons to the display characters
mDisplayMap.put(R.id.one, '1');
mDisplayMap.put(R.id.two, '2');
mDisplayMap.put(R.id.three, '3');
mDisplayMap.put(R.id.four, '4');
mDisplayMap.put(R.id.five, '5');
mDisplayMap.put(R.id.six, '6');
mDisplayMap.put(R.id.seven, '7');
mDisplayMap.put(R.id.eight, '8');
mDisplayMap.put(R.id.nine, '9');
mDisplayMap.put(R.id.zero, '0');
mDisplayMap.put(R.id.pound, '#');
mDisplayMap.put(R.id.star, '*');
}
// KeyListener used with the "dialpad digits" EditText widget.
private DTMFKeyListener mDialerKeyListener;
private DialpadView mDialpadView;
/**
* Our own key listener, specialized for dealing with DTMF codes.
* 1. Ignore the backspace since it is irrelevant.
* 2. Allow ONLY valid DTMF characters to generate a tone and be
* sent as a DTMF code.
* 3. All other remaining characters are handled by the superclass.
*
* This code is purely here to handle events from the hardware keyboard
* while the DTMF dialpad is up.
*/
private class DTMFKeyListener extends DialerKeyListener {
private DTMFKeyListener() {
super();
}
/**
* Overriden to return correct DTMF-dialable characters.
*/
@Override
protected char[] getAcceptedChars(){
return DTMF_CHARACTERS;
}
/** special key listener ignores backspace. */
@Override
public boolean backspace(View view, Editable content, int keyCode,
KeyEvent event) {
return false;
}
/**
* Return true if the keyCode is an accepted modifier key for the
* dialer (ALT or SHIFT).
*/
private boolean isAcceptableModifierKey(int keyCode) {
switch (keyCode) {
case KeyEvent.KEYCODE_ALT_LEFT:
case KeyEvent.KEYCODE_ALT_RIGHT:
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
return true;
default:
return false;
}
}
/**
* Overriden so that with each valid button press, we start sending
* a dtmf code and play a local dtmf tone.
*/
@Override
public boolean onKeyDown(View view, Editable content,
int keyCode, KeyEvent event) {
// if (DBG) log("DTMFKeyListener.onKeyDown, keyCode " + keyCode + ", view " + view);
// find the character
char c = (char) lookup(event, content);
// if not a long press, and parent onKeyDown accepts the input
if (event.getRepeatCount() == 0 && super.onKeyDown(view, content, keyCode, event)) {
boolean keyOK = ok(getAcceptedChars(), c);
// if the character is a valid dtmf code, start playing the tone and send the
// code.
if (keyOK) {
Log.d(this, "DTMFKeyListener reading '" + c + "' from input.");
getPresenter().processDtmf(c);
} else {
Log.d(this, "DTMFKeyListener rejecting '" + c + "' from input.");
}
return true;
}
return false;
}
/**
* Overriden so that with each valid button up, we stop sending
* a dtmf code and the dtmf tone.
*/
@Override
public boolean onKeyUp(View view, Editable content,
int keyCode, KeyEvent event) {
// if (DBG) log("DTMFKeyListener.onKeyUp, keyCode " + keyCode + ", view " + view);
super.onKeyUp(view, content, keyCode, event);
// find the character
char c = (char) lookup(event, content);
boolean keyOK = ok(getAcceptedChars(), c);
if (keyOK) {
Log.d(this, "Stopping the tone for '" + c + "'");
getPresenter().stopTone();
return true;
}
return false;
}
/**
* Handle individual keydown events when we DO NOT have an Editable handy.
*/
public boolean onKeyDown(KeyEvent event) {
char c = lookup(event);
Log.d(this, "DTMFKeyListener.onKeyDown: event '" + c + "'");
// if not a long press, and parent onKeyDown accepts the input
if (event.getRepeatCount() == 0 && c != 0) {
// if the character is a valid dtmf code, start playing the tone and send the
// code.
if (ok(getAcceptedChars(), c)) {
Log.d(this, "DTMFKeyListener reading '" + c + "' from input.");
getPresenter().processDtmf(c);
return true;
} else {
Log.d(this, "DTMFKeyListener rejecting '" + c + "' from input.");
}
}
return false;
}
/**
* Handle individual keyup events.
*
* @param event is the event we are trying to stop. If this is null,
* then we just force-stop the last tone without checking if the event
* is an acceptable dialer event.
*/
public boolean onKeyUp(KeyEvent event) {
if (event == null) {
//the below piece of code sends stopDTMF event unnecessarily even when a null event
//is received, hence commenting it.
/*if (DBG) log("Stopping the last played tone.");
stopTone();*/
return true;
}
char c = lookup(event);
Log.d(this, "DTMFKeyListener.onKeyUp: event '" + c + "'");
// TODO: stopTone does not take in character input, we may want to
// consider checking for this ourselves.
if (ok(getAcceptedChars(), c)) {
Log.d(this, "Stopping the tone for '" + c + "'");
getPresenter().stopTone();
return true;
}
return false;
}
/**
* Find the Dialer Key mapped to this event.
*
* @return The char value of the input event, otherwise
* 0 if no matching character was found.
*/
private char lookup(KeyEvent event) {
// This code is similar to {@link DialerKeyListener#lookup(KeyEvent, Spannable) lookup}
int meta = event.getMetaState();
int number = event.getNumber();
if (!((meta & (KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)) == 0) || (number == 0)) {
int match = event.getMatch(getAcceptedChars(), meta);
number = (match != 0) ? match : number;
}
return (char) number;
}
/**
* Check to see if the keyEvent is dialable.
*/
boolean isKeyEventAcceptable (KeyEvent event) {
return (ok(getAcceptedChars(), lookup(event)));
}
/**
* Overrides the characters used in {@link DialerKeyListener#CHARACTERS}
* These are the valid dtmf characters.
*/
public final char[] DTMF_CHARACTERS = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '#', '*'
};
}
@Override
public void onClick(View v) {
final AccessibilityManager accessibilityManager = (AccessibilityManager)
v.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
// When accessibility is on, simulate press and release to preserve the
// semantic meaning of performClick(). Required for Braille support.
if (accessibilityManager.isEnabled()) {
final int id = v.getId();
// Checking the press state prevents double activation.
if (!v.isPressed() && mDisplayMap.containsKey(id)) {
getPresenter().processDtmf(mDisplayMap.get(id));
}
}
}
@Override
public boolean onHover(View v, MotionEvent event) {
// When touch exploration is turned on, lifting a finger while inside
// the button's hover target bounds should perform a click action.
final AccessibilityManager accessibilityManager = (AccessibilityManager)
v.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
if (accessibilityManager.isEnabled()
&& accessibilityManager.isTouchExplorationEnabled()) {
final int left = v.getPaddingLeft();
final int right = (v.getWidth() - v.getPaddingRight());
final int top = v.getPaddingTop();
final int bottom = (v.getHeight() - v.getPaddingBottom());
switch (event.getActionMasked()) {
case MotionEvent.ACTION_HOVER_ENTER:
// Lift-to-type temporarily disables double-tap activation.
v.setClickable(false);
break;
case MotionEvent.ACTION_HOVER_EXIT:
final int x = (int) event.getX();
final int y = (int) event.getY();
if ((x > left) && (x < right) && (y > top) && (y < bottom)) {
v.performClick();
}
v.setClickable(true);
break;
}
}
return false;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(this, "onKey: keyCode " + keyCode + ", view " + v);
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
int viewId = v.getId();
if (mDisplayMap.containsKey(viewId)) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (event.getRepeatCount() == 0) {
getPresenter().processDtmf(mDisplayMap.get(viewId));
}
break;
case KeyEvent.ACTION_UP:
getPresenter().stopTone();
break;
}
// do not return true [handled] here, since we want the
// press / click animation to be handled by the framework.
}
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(this, "onTouch");
int viewId = v.getId();
// if the button is recognized
if (mDisplayMap.containsKey(viewId)) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Append the character mapped to this button, to the display.
// start the tone
getPresenter().processDtmf(mDisplayMap.get(viewId));
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// stop the tone on ANY other event, except for MOVE.
getPresenter().stopTone();
break;
}
// do not return true [handled] here, since we want the
// press / click animation to be handled by the framework.
}
return false;
}
// TODO(klp) Adds hardware keyboard listener
@Override
DialpadPresenter createPresenter() {
return new DialpadPresenter();
}
@Override
DialpadPresenter.DialpadUi getUi() {
return this;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View parent = inflater.inflate(
com.android.incallui.R.layout.dtmf_twelve_key_dialer_view, container, false);
mDialpadView = (DialpadView) parent.findViewById(R.id.dialpad_view);
mDialpadView.setCanDigitsBeEdited(false);
mDialpadView.setBackgroundResource(R.color.incall_dialpad_background);
mDtmfDialerField = (EditText) parent.findViewById(R.id.digits);
if (mDtmfDialerField != null) {
mDialerKeyListener = new DTMFKeyListener();
mDtmfDialerField.setKeyListener(mDialerKeyListener);
// remove the long-press context menus that support
// the edit (copy / paste / select) functions.
mDtmfDialerField.setLongClickable(false);
mDtmfDialerField.setElegantTextHeight(false);
configureKeypadListeners(mDialpadView);
}
final ViewTreeObserver vto = parent.getViewTreeObserver();
// Adjust the translation of the DialpadFragment in a preDrawListener instead of in
// DialtactsActivity, because at the point in time when the DialpadFragment is added,
// its views have not been laid out yet.
final ViewTreeObserver.OnPreDrawListener
preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (isHidden()) return true;
if (parent.getTranslationY() == 0) {
((DialpadSlidingLinearLayout) parent)
.setYFraction(DIALPAD_SLIDE_FRACTION);
}
final ViewTreeObserver vto = parent.getViewTreeObserver();
vto.removeOnPreDrawListener(this);
return true;
}
};
vto.addOnPreDrawListener(preDrawListener);
return parent;
}
@Override
public void onDestroyView() {
mDialerKeyListener = null;
super.onDestroyView();
}
@Override
public void setVisible(boolean on) {
if (on) {
getView().setVisibility(View.VISIBLE);
} else {
getView().setVisibility(View.INVISIBLE);
}
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
final DialpadView dialpadView = (DialpadView) getView().findViewById(R.id.dialpad_view);
dialpadView.animateShow();
}
}
@Override
public void appendDigitsToField(char digit) {
if (mDtmfDialerField != null) {
// TODO: maybe *don't* manually append this digit if
// mDialpadDigits is focused and this key came from the HW
// keyboard, since in that case the EditText field will
// get the key event directly and automatically appends
// whetever the user types.
// (Or, a cleaner fix would be to just make mDialpadDigits
// *not* handle HW key presses. That seems to be more
// complicated than just setting focusable="false" on it,
// though.)
mDtmfDialerField.getText().append(digit);
}
}
/**
* Called externally (from InCallScreen) to play a DTMF Tone.
*/
/* package */ boolean onDialerKeyDown(KeyEvent event) {
Log.d(this, "Notifying dtmf key down.");
if (mDialerKeyListener != null) {
return mDialerKeyListener.onKeyDown(event);
} else {
return false;
}
}
/**
* Called externally (from InCallScreen) to cancel the last DTMF Tone played.
*/
public boolean onDialerKeyUp(KeyEvent event) {
Log.d(this, "Notifying dtmf key up.");
if (mDialerKeyListener != null) {
return mDialerKeyListener.onKeyUp(event);
} else {
return false;
}
}
private void configureKeypadListeners(View fragmentView) {
final int[] buttonIds = 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};
DialpadKeyButton dialpadKey;
for (int i = 0; i < buttonIds.length; i++) {
dialpadKey = (DialpadKeyButton) fragmentView.findViewById(buttonIds[i]);
dialpadKey.setBackgroundResource(R.drawable.incall_dialpad_key_colors);
dialpadKey.setOnTouchListener(this);
dialpadKey.setOnKeyListener(this);
dialpadKey.setOnHoverListener(this);
dialpadKey.setOnClickListener(this);
}
}
}
|