blob: 2e88d892e0f83538154bbfee230ff42e405c62f7 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 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.contacts;
18
19import android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.Resources;
24import android.database.Cursor;
25import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.graphics.drawable.Drawable;
28import android.media.AudioManager;
29import android.media.ToneGenerator;
30import android.net.Uri;
31import android.os.Bundle;
32import android.os.Handler;
33import android.os.Message;
34import android.os.RemoteException;
35import android.os.ServiceManager;
36import android.os.SystemClock;
David Brownc29c7ab2009-07-07 16:00:18 -070037import android.os.Vibrator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038import android.provider.Contacts.Intents.Insert;
39import android.provider.Contacts.People;
40import android.provider.Contacts.Phones;
41import android.provider.Contacts.PhonesColumns;
42import android.provider.Settings;
43import android.telephony.PhoneNumberFormattingTextWatcher;
44import android.telephony.PhoneNumberUtils;
45import android.telephony.PhoneStateListener;
46import android.telephony.TelephonyManager;
47import android.text.Editable;
Reli Talc2a2a512009-06-10 16:48:00 -040048import android.text.SpannableStringBuilder;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.text.TextUtils;
50import android.text.TextWatcher;
51import android.text.method.DialerKeyListener;
52import android.util.Log;
53import android.view.KeyEvent;
54import android.view.LayoutInflater;
55import android.view.Menu;
56import android.view.MenuItem;
57import android.view.View;
58import android.view.ViewConfiguration;
59import android.view.ViewGroup;
Karl Rosaenf46bc312009-03-24 18:20:48 -070060import android.view.inputmethod.InputMethodManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080061import android.widget.AdapterView;
62import android.widget.BaseAdapter;
63import android.widget.EditText;
64import android.widget.ImageView;
65import android.widget.ListView;
66import android.widget.TextView;
67
68import com.android.internal.telephony.ITelephony;
69
70/**
71 * Dialer activity that displays the typical twelve key interface.
72 */
73public class TwelveKeyDialer extends Activity implements View.OnClickListener,
74 View.OnLongClickListener, View.OnKeyListener,
75 AdapterView.OnItemClickListener, TextWatcher {
76
77 private static final String TAG = "TwelveKeyDialer";
Eric Laurentd9efc872009-07-17 11:52:06 -070078
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079 /** The length of DTMF tones in milliseconds */
80 private static final int TONE_LENGTH_MS = 150;
Eric Laurentd9efc872009-07-17 11:52:06 -070081
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080082 /** The DTMF tone volume relative to other sounds in the stream */
83 private static final int TONE_RELATIVE_VOLUME = 50;
84
85 private EditText mDigits;
86 private View mDelete;
87 private MenuItem mAddToContactMenuItem;
88 private ToneGenerator mToneGenerator;
89 private Object mToneGeneratorLock = new Object();
90 private Drawable mDigitsBackground;
91 private Drawable mDigitsEmptyBackground;
92 private Drawable mDeleteBackground;
93 private Drawable mDeleteEmptyBackground;
94 private View mDigitsAndBackspace;
95 private View mDialpad;
David Brown3d07e6d2009-08-04 20:30:09 -070096 private View mDialButton;
Nicolas Cataniadea164e2009-09-18 06:26:16 -070097 private View mVoicemailDialAndBackspaceRow;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098 private ListView mDialpadChooser;
99 private DialpadChooserAdapter mDialpadChooserAdapter;
Reli Talc2a2a512009-06-10 16:48:00 -0400100 //Member variables for dialpad options
101 private MenuItem m2SecPauseMenuItem;
102 private MenuItem mWaitMenuItem;
103 private static final int MENU_ADD_CONTACTS = 1;
104 private static final int MENU_2S_PAUSE = 2;
105 private static final int MENU_WAIT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106
107 // determines if we want to playback local DTMF tones.
108 private boolean mDTMFToneEnabled;
David Brownc29c7ab2009-07-07 16:00:18 -0700109
110 // Vibration (haptic feedback) for dialer key presses.
111 private Vibrator mVibrator;
112 private boolean mVibrateOn;
113 private long mVibrateDuration;
114
Eric Laurentd9efc872009-07-17 11:52:06 -0700115
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 /** Identifier for the "Add Call" intent extra. */
117 static final String ADD_CALL_MODE_KEY = "add_call_mode";
118 /** Indicates if we are opening this dialer to add a call from the InCallScreen. */
119 private boolean mIsAddCallMode;
120
121 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
122 /**
123 * Listen for phone state changes so that we can take down the
124 * "dialpad chooser" if the phone becomes idle while the
125 * chooser UI is visible.
126 */
127 @Override
128 public void onCallStateChanged(int state, String incomingNumber) {
129 // Log.i(TAG, "PhoneStateListener.onCallStateChanged: "
130 // + state + ", '" + incomingNumber + "'");
131 if ((state == TelephonyManager.CALL_STATE_IDLE) && dialpadChooserVisible()) {
132 // Log.i(TAG, "Call ended with dialpad chooser visible! Taking it down...");
133 // Note there's a race condition in the UI here: the
134 // dialpad chooser could conceivably disappear (on its
135 // own) at the exact moment the user was trying to select
136 // one of the choices, which would be confusing. (But at
137 // least that's better than leaving the dialpad chooser
138 // onscreen, but useless...)
139 showDialpadChooser(false);
140 }
141 }
142 };
143
144 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
145 // Do nothing
146 }
147
148 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
149 // Do nothing
Eric Laurentd9efc872009-07-17 11:52:06 -0700150 // DTMF Tones do not need to be played here any longer -
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800151 // the DTMF dialer handles that functionality now.
152 }
153
154 public void afterTextChanged(Editable input) {
155 if (SpecialCharSequenceMgr.handleChars(this, input.toString(), mDigits)) {
156 // A special sequence was entered, clear the digits
157 mDigits.getText().clear();
158 }
159
160 // Set the proper background for the dial input area
161 if (mDigits.length() != 0) {
162 mDelete.setBackgroundDrawable(mDeleteBackground);
163 mDigits.setBackgroundDrawable(mDigitsBackground);
164 mDigits.setCompoundDrawablesWithIntrinsicBounds(
165 getResources().getDrawable(R.drawable.ic_dial_number), null, null, null);
166 } else {
167 mDelete.setBackgroundDrawable(mDeleteEmptyBackground);
168 mDigits.setBackgroundDrawable(mDigitsEmptyBackground);
169 mDigits.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
170 }
David Brown3d07e6d2009-08-04 20:30:09 -0700171
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700172 updateDialButtonStateEnabledAttr();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 }
174
175 @Override
176 protected void onCreate(Bundle icicle) {
177 super.onCreate(icicle);
178
179 // Set the content view
180 setContentView(getContentViewResource());
181
182 // Load up the resources for the text field and delete button
183 Resources r = getResources();
184 mDigitsBackground = r.getDrawable(R.drawable.btn_dial_textfield_active);
185 //mDigitsBackground.setDither(true);
186 mDigitsEmptyBackground = r.getDrawable(R.drawable.btn_dial_textfield);
187 //mDigitsEmptyBackground.setDither(true);
188 mDeleteBackground = r.getDrawable(R.drawable.btn_dial_delete_active);
189 //mDeleteBackground.setDither(true);
190 mDeleteEmptyBackground = r.getDrawable(R.drawable.btn_dial_delete);
191 //mDeleteEmptyBackground.setDither(true);
192
193 mDigits = (EditText) findViewById(R.id.digits);
194 mDigits.setKeyListener(DialerKeyListener.getInstance());
195 mDigits.setOnClickListener(this);
196 mDigits.setOnKeyListener(this);
197 maybeAddNumberFormatting();
198
199 // Check for the presence of the keypad
200 View view = findViewById(R.id.one);
201 if (view != null) {
202 setupKeypad();
203 }
204
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700205 mVoicemailDialAndBackspaceRow = findViewById(R.id.voicemailAndDialAndBackspace);
206
David Brown3d07e6d2009-08-04 20:30:09 -0700207 // Check whether we should show the onscreen "Dial" button.
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700208 mDialButton = mVoicemailDialAndBackspaceRow.findViewById(R.id.dialButton);
209
David Brown3d07e6d2009-08-04 20:30:09 -0700210 if (r.getBoolean(R.bool.config_show_onscreen_dial_button)) {
David Brown3d07e6d2009-08-04 20:30:09 -0700211 mDialButton.setOnClickListener(this);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700212 } else {
213 mDialButton.setVisibility(View.GONE); // It's VISIBLE by default
214 mDialButton = null;
David Brown3d07e6d2009-08-04 20:30:09 -0700215 }
216
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800217 view = findViewById(R.id.backspace);
218 view.setOnClickListener(this);
219 view.setOnLongClickListener(this);
220 mDelete = view;
221
Dmitri Plotnikov032bb362009-05-06 17:05:39 -0700222 mDigitsAndBackspace = findViewById(R.id.digitsAndBackspace);
223 mDialpad = findViewById(R.id.dialpad); // This is null in landscape mode
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800224
225 // Set up the "dialpad chooser" UI; see showDialpadChooser().
226 mDialpadChooser = (ListView) findViewById(R.id.dialpadChooser);
227 mDialpadChooser.setOnItemClickListener(this);
228
229 if (!resolveIntent() && icicle != null) {
230 super.onRestoreInstanceState(icicle);
231 }
232
David Brownc29c7ab2009-07-07 16:00:18 -0700233 // Initialize vibration parameters.
234 // TODO: We might eventually need to make mVibrateOn come from a
235 // user preference rather than a per-platform resource, in which
236 // case we would need to update it in onResume() rather than here.
237 mVibrateOn = r.getBoolean(R.bool.config_enable_dialer_key_vibration);
238 mVibrateDuration = (long) r.getInteger(R.integer.config_dialer_key_vibrate_duration);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800239 }
240
241 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242 protected void onRestoreInstanceState(Bundle icicle) {
243 // Do nothing, state is restored in onCreate() if needed
244 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700245
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 protected void maybeAddNumberFormatting() {
247 mDigits.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
248 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700249
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 /**
Eric Laurentd9efc872009-07-17 11:52:06 -0700251 * Overridden by subclasses to control the resource used by the content view.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 */
253 protected int getContentViewResource() {
254 return R.layout.twelve_key_dialer;
255 }
256
257 private boolean resolveIntent() {
258 boolean ignoreState = false;
259
260 // Find the proper intent
261 final Intent intent;
262 if (isChild()) {
263 intent = getParent().getIntent();
264 ignoreState = intent.getBooleanExtra(DialtactsActivity.EXTRA_IGNORE_STATE, false);
265 } else {
266 intent = getIntent();
267 }
268 // Log.i(TAG, "==> resolveIntent(): intent: " + intent);
269
270 // by default we are not adding a call.
271 mIsAddCallMode = false;
272
273 // By default we don't show the "dialpad chooser" UI.
274 boolean needToShowDialpadChooser = false;
275
276 // Resolve the intent
277 final String action = intent.getAction();
278 if (Intent.ACTION_DIAL.equals(action) || Intent.ACTION_VIEW.equals(action)) {
279 // see if we are "adding a call" from the InCallScreen; false by default.
280 mIsAddCallMode = intent.getBooleanExtra(ADD_CALL_MODE_KEY, false);
281 Uri uri = intent.getData();
282 if (uri != null) {
283 if ("tel".equals(uri.getScheme())) {
284 // Put the requested number into the input area
285 String data = uri.getSchemeSpecificPart();
286 setFormattedDigits(data);
287 } else {
288 String type = intent.getType();
289 if (People.CONTENT_ITEM_TYPE.equals(type)
290 || Phones.CONTENT_ITEM_TYPE.equals(type)) {
291 // Query the phone number
292 Cursor c = getContentResolver().query(intent.getData(),
293 new String[] {PhonesColumns.NUMBER}, null, null, null);
294 if (c != null) {
295 if (c.moveToFirst()) {
296 // Put the number into the input area
297 setFormattedDigits(c.getString(0));
298 }
299 c.close();
300 }
301 }
302 }
303 }
304 } else if (Intent.ACTION_MAIN.equals(action)) {
305 // The MAIN action means we're bringing up a blank dialer
306 // (e.g. by selecting the Home shortcut, or tabbing over from
307 // Contacts or Call log.)
308 //
309 // At this point, IF there's already an active call, there's a
310 // good chance that the user got here accidentally (but really
311 // wanted the in-call dialpad instead). So we bring up an
312 // intermediate UI to make the user confirm what they really
313 // want to do.
314 if (phoneIsInUse()) {
315 // Log.i(TAG, "resolveIntent(): phone is in use; showing dialpad chooser!");
316 needToShowDialpadChooser = true;
317 }
318 }
319
320 // Bring up the "dialpad chooser" IFF we need to make the user
321 // confirm which dialpad they really want.
322 showDialpadChooser(needToShowDialpadChooser);
323
324 return ignoreState;
325 }
326
327 protected void setFormattedDigits(String data) {
328 // strip the non-dialable numbers out of the data string.
329 String dialString = PhoneNumberUtils.extractNetworkPortion(data);
330 dialString = PhoneNumberUtils.formatNumber(dialString);
331 if (!TextUtils.isEmpty(dialString)) {
332 Editable digits = mDigits.getText();
333 digits.replace(0, digits.length(), dialString);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700334 // for some reason this isn't getting called in the digits.replace call above..
335 // but in any case, this will make sure the background drawable looks right
336 afterTextChanged(digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800337 }
338 }
339
340 @Override
341 protected void onNewIntent(Intent newIntent) {
342 setIntent(newIntent);
343 resolveIntent();
344 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700345
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800346 @Override
347 protected void onPostCreate(Bundle savedInstanceState) {
348 super.onPostCreate(savedInstanceState);
349
350 // This can't be done in onCreate(), since the auto-restoring of the digits
351 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
352 // is called. This method will be called every time the activity is created, and
353 // will always happen after onRestoreSavedInstanceState().
354 mDigits.addTextChangedListener(this);
355 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700356
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800357 private void setupKeypad() {
358 // Setup the listeners for the buttons
359 View view = findViewById(R.id.one);
360 view.setOnClickListener(this);
361 view.setOnLongClickListener(this);
362
363 findViewById(R.id.two).setOnClickListener(this);
364 findViewById(R.id.three).setOnClickListener(this);
365 findViewById(R.id.four).setOnClickListener(this);
366 findViewById(R.id.five).setOnClickListener(this);
367 findViewById(R.id.six).setOnClickListener(this);
368 findViewById(R.id.seven).setOnClickListener(this);
369 findViewById(R.id.eight).setOnClickListener(this);
370 findViewById(R.id.nine).setOnClickListener(this);
371 findViewById(R.id.star).setOnClickListener(this);
372
373 view = findViewById(R.id.zero);
374 view.setOnClickListener(this);
375 view.setOnLongClickListener(this);
376
377 findViewById(R.id.pound).setOnClickListener(this);
378 }
379
380 @Override
381 protected void onResume() {
382 super.onResume();
David Brownc29c7ab2009-07-07 16:00:18 -0700383
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800384 // retrieve the DTMF tone play back setting.
385 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
386 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
387
Eric Laurentd9efc872009-07-17 11:52:06 -0700388 // if the mToneGenerator creation fails, just continue without it. It is
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800389 // a local audio signal, and is not as important as the dtmf tone itself.
390 synchronized(mToneGeneratorLock) {
391 if (mToneGenerator == null) {
392 try {
Eric Laurentd9efc872009-07-17 11:52:06 -0700393 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800394 TONE_RELATIVE_VOLUME);
395 } catch (RuntimeException e) {
396 Log.w(TAG, "Exception caught while creating local tone generator: " + e);
397 mToneGenerator = null;
398 }
399 }
400 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700401
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800402 Activity parent = getParent();
403 // See if we were invoked with a DIAL intent. If we were, fill in the appropriate
404 // digits in the dialer field.
405 if (parent != null && parent instanceof DialtactsActivity) {
406 Uri dialUri = ((DialtactsActivity) parent).getAndClearDialUri();
407 if (dialUri != null) {
408 resolveIntent();
409 }
410 }
411
412 // While we're in the foreground, listen for phone state changes,
413 // purely so that we can take down the "dialpad chooser" if the
414 // phone becomes idle while the chooser UI is visible.
415 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
416 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
417
418 // Potentially show hint text in the mDigits field when the user
419 // hasn't typed any digits yet. (If there's already an active call,
420 // this hint text will remind the user that he's about to add a new
421 // call.)
422 //
423 // TODO: consider adding better UI for the case where *both* lines
424 // are currently in use. (Right now we let the user try to add
425 // another call, but that call is guaranteed to fail. Perhaps the
426 // entire dialer UI should be disabled instead.)
427 if (phoneIsInUse()) {
428 mDigits.setHint(R.string.dialerDialpadHintText);
429 } else {
430 // Common case; no hint necessary.
431 mDigits.setHint(null);
432
433 // Also, a sanity-check: the "dialpad chooser" UI should NEVER
434 // be visible if the phone is idle!
435 showDialpadChooser(false);
436 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700437
438 updateDialButtonStateEnabledAttr();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800439 }
440
441 @Override
Karl Rosaenf46bc312009-03-24 18:20:48 -0700442 public void onWindowFocusChanged(boolean hasFocus) {
443 if (hasFocus) {
444 // Hide soft keyboard, if visible (it's fugly over button dialer).
445 // The only known case where this will be true is when launching the dialer with
446 // ACTION_DIAL via a soft keyboard. we dismiss it here because we don't
447 // have a window token yet in onCreate / onNewIntent
448 InputMethodManager inputMethodManager = (InputMethodManager)
449 getSystemService(Context.INPUT_METHOD_SERVICE);
Eric Laurentd9efc872009-07-17 11:52:06 -0700450 inputMethodManager.hideSoftInputFromWindow(mDigits.getWindowToken(), 0);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700451 }
452 }
453
454 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800455 protected void onPause() {
456 super.onPause();
457
458 // Stop listening for phone state changes.
459 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
460 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
461
462 synchronized(mToneGeneratorLock) {
463 if (mToneGenerator != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800464 mToneGenerator.release();
465 mToneGenerator = null;
466 }
467 }
468 }
469
470 @Override
471 public boolean onCreateOptionsMenu(Menu menu) {
Reli Talc2a2a512009-06-10 16:48:00 -0400472 mAddToContactMenuItem = menu.add(0, MENU_ADD_CONTACTS, 0, R.string.recentCalls_addToContact)
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 .setIcon(android.R.drawable.ic_menu_add);
Reli Talc2a2a512009-06-10 16:48:00 -0400474 m2SecPauseMenuItem = menu.add(0, MENU_2S_PAUSE, 0, R.string.add_2sec_pause)
475 .setIcon(R.drawable.ic_menu_2sec_pause);
476 mWaitMenuItem = menu.add(0, MENU_WAIT, 0, R.string.add_wait)
477 .setIcon(R.drawable.ic_menu_wait);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800478 return true;
479 }
480
481 @Override
482 public boolean onPrepareOptionsMenu(Menu menu) {
483 // We never show a menu if the "choose dialpad" UI is up.
484 if (dialpadChooserVisible()) {
485 return false;
486 }
487
488 CharSequence digits = mDigits.getText();
489 if (digits == null || !TextUtils.isGraphic(digits)) {
490 mAddToContactMenuItem.setVisible(false);
Reli Talc2a2a512009-06-10 16:48:00 -0400491 m2SecPauseMenuItem.setVisible(false);
492 mWaitMenuItem.setVisible(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800493 } else {
494 // Put the current digits string into an intent
495 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
496 intent.putExtra(Insert.PHONE, mDigits.getText());
497 intent.setType(People.CONTENT_ITEM_TYPE);
498 mAddToContactMenuItem.setIntent(intent);
499 mAddToContactMenuItem.setVisible(true);
Reli Talc2a2a512009-06-10 16:48:00 -0400500
501 // Check out whether to show Pause & Wait option menu items
502 int selectionStart;
503 int selectionEnd;
504 String strDigits = digits.toString();
505
506 selectionStart = mDigits.getSelectionStart();
507 selectionEnd = mDigits.getSelectionEnd();
508
509 if (selectionStart != -1) {
510 if (selectionStart > selectionEnd) {
511 // swap it as we want start to be less then end
512 int tmp = selectionStart;
513 selectionStart = selectionEnd;
514 selectionEnd = tmp;
515 }
516
517 if (selectionStart != 0) {
518 // Pause can be visible if cursor is not in the begining
519 m2SecPauseMenuItem.setVisible(true);
520
521 // For Wait to be visible set of condition to meet
522 mWaitMenuItem.setVisible(showWait(selectionStart,
523 selectionEnd, strDigits));
524 } else {
525 // cursor in the beginning both pause and wait to be invisible
526 m2SecPauseMenuItem.setVisible(false);
527 mWaitMenuItem.setVisible(false);
528 }
529 } else {
530 // cursor is not selected so assume new digit is added to the end
531 int strLength = strDigits.length();
532 mWaitMenuItem.setVisible(showWait(strLength,
533 strLength, strDigits));
534 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800535 }
536 return true;
537 }
538
539 @Override
540 public boolean onKeyDown(int keyCode, KeyEvent event) {
541 switch (keyCode) {
542 case KeyEvent.KEYCODE_CALL: {
543 long callPressDiff = SystemClock.uptimeMillis() - event.getDownTime();
544 if (callPressDiff >= ViewConfiguration.getLongPressTimeout()) {
545 // Launch voice dialer
546 Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND);
547 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
548 try {
549 startActivity(intent);
550 } catch (ActivityNotFoundException e) {
551 }
552 }
553 return true;
554 }
555 case KeyEvent.KEYCODE_1: {
Eric Laurentd9efc872009-07-17 11:52:06 -0700556 long timeDiff = SystemClock.uptimeMillis() - event.getDownTime();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800557 if (timeDiff >= ViewConfiguration.getLongPressTimeout()) {
558 // Long press detected, call voice mail
559 callVoicemail();
560 }
561 return true;
562 }
563 }
564 return super.onKeyDown(keyCode, event);
565 }
566
567 @Override
568 public boolean onKeyUp(int keyCode, KeyEvent event) {
569 switch (keyCode) {
570 case KeyEvent.KEYCODE_CALL: {
571 if (mIsAddCallMode && (TextUtils.isEmpty(mDigits.getText().toString()))) {
572 // if we are adding a call from the InCallScreen and the phone
573 // number entered is empty, we just close the dialer to expose
574 // the InCallScreen under it.
575 finish();
576 } else {
577 // otherwise, we place the call.
578 placeCall();
579 }
580 return true;
581 }
582 }
583 return super.onKeyUp(keyCode, event);
584 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700585
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800586 private void keyPressed(int keyCode) {
David Brownc29c7ab2009-07-07 16:00:18 -0700587 vibrate();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800588 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
589 mDigits.onKeyDown(keyCode, event);
590 }
591
592 public boolean onKey(View view, int keyCode, KeyEvent event) {
593 switch (view.getId()) {
594 case R.id.digits:
595 if (keyCode == KeyEvent.KEYCODE_ENTER) {
596 placeCall();
597 return true;
598 }
599 break;
600 }
601 return false;
602 }
603
604 public void onClick(View view) {
605 switch (view.getId()) {
606 case R.id.one: {
607 playTone(ToneGenerator.TONE_DTMF_1);
608 keyPressed(KeyEvent.KEYCODE_1);
609 return;
610 }
611 case R.id.two: {
612 playTone(ToneGenerator.TONE_DTMF_2);
613 keyPressed(KeyEvent.KEYCODE_2);
614 return;
615 }
616 case R.id.three: {
617 playTone(ToneGenerator.TONE_DTMF_3);
618 keyPressed(KeyEvent.KEYCODE_3);
619 return;
620 }
621 case R.id.four: {
622 playTone(ToneGenerator.TONE_DTMF_4);
623 keyPressed(KeyEvent.KEYCODE_4);
624 return;
625 }
626 case R.id.five: {
627 playTone(ToneGenerator.TONE_DTMF_5);
628 keyPressed(KeyEvent.KEYCODE_5);
629 return;
630 }
631 case R.id.six: {
632 playTone(ToneGenerator.TONE_DTMF_6);
633 keyPressed(KeyEvent.KEYCODE_6);
634 return;
635 }
636 case R.id.seven: {
637 playTone(ToneGenerator.TONE_DTMF_7);
638 keyPressed(KeyEvent.KEYCODE_7);
639 return;
640 }
641 case R.id.eight: {
642 playTone(ToneGenerator.TONE_DTMF_8);
643 keyPressed(KeyEvent.KEYCODE_8);
644 return;
645 }
646 case R.id.nine: {
647 playTone(ToneGenerator.TONE_DTMF_9);
648 keyPressed(KeyEvent.KEYCODE_9);
649 return;
650 }
651 case R.id.zero: {
652 playTone(ToneGenerator.TONE_DTMF_0);
653 keyPressed(KeyEvent.KEYCODE_0);
654 return;
655 }
656 case R.id.pound: {
657 playTone(ToneGenerator.TONE_DTMF_P);
658 keyPressed(KeyEvent.KEYCODE_POUND);
659 return;
660 }
661 case R.id.star: {
662 playTone(ToneGenerator.TONE_DTMF_S);
663 keyPressed(KeyEvent.KEYCODE_STAR);
664 return;
665 }
666 case R.id.backspace: {
667 keyPressed(KeyEvent.KEYCODE_DEL);
668 return;
669 }
David Brown3d07e6d2009-08-04 20:30:09 -0700670 case R.id.dialButton:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800671 case R.id.digits: {
David Brownc29c7ab2009-07-07 16:00:18 -0700672 vibrate(); // Vibrate here too, just like we do for the regular keys
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800673 placeCall();
674 return;
675 }
676 }
677 }
678
679 public boolean onLongClick(View view) {
680 final Editable digits = mDigits.getText();
681 int id = view.getId();
682 switch (id) {
683 case R.id.backspace: {
684 digits.clear();
685 return true;
686 }
687 case R.id.one: {
688 if (digits.length() == 0) {
689 callVoicemail();
690 return true;
691 }
692 return false;
693 }
694 case R.id.zero: {
695 keyPressed(KeyEvent.KEYCODE_PLUS);
696 return true;
697 }
698 }
699 return false;
700 }
701
702 void callVoicemail() {
703 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
704 Uri.fromParts("voicemail", "", null));
705 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
706 startActivity(intent);
707 mDigits.getText().clear();
708 finish();
709 }
710
711 void placeCall() {
712 final String number = mDigits.getText().toString();
713 if (number == null || !TextUtils.isGraphic(number)) {
714 // There is no number entered.
715 playTone(ToneGenerator.TONE_PROP_NACK);
716 return;
717 }
718 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
719 Uri.fromParts("tel", number, null));
720 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
721 startActivity(intent);
722 mDigits.getText().clear();
723 finish();
724 }
725
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800726
727 /**
David Brown22f615f2009-06-25 16:19:19 -0700728 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
729 *
730 * The tone is played locally, using the audio stream for phone calls.
731 * Tones are played only if the "Audible touch tones" user preference
732 * is checked, and are NOT played if the device is in silent mode.
733 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800734 * @param tone a tone code from {@link ToneGenerator}
735 */
736 void playTone(int tone) {
737 // if local tone playback is disabled, just return.
738 if (!mDTMFToneEnabled) {
739 return;
740 }
David Brown22f615f2009-06-25 16:19:19 -0700741
742 // Also do nothing if the phone is in silent mode.
743 // We need to re-check the ringer mode for *every* playTone()
744 // call, rather than keeping a local flag that's updated in
745 // onResume(), since it's possible to toggle silent mode without
746 // leaving the current activity (via the ENDCALL-longpress menu.)
747 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
David Brownd5a15302009-07-20 16:39:47 -0700748 int ringerMode = audioManager.getRingerMode();
749 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
750 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
David Brown22f615f2009-06-25 16:19:19 -0700751 return;
752 }
753
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800754 synchronized(mToneGeneratorLock) {
755 if (mToneGenerator == null) {
756 Log.w(TAG, "playTone: mToneGenerator == null, tone: "+tone);
757 return;
758 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700759
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800760 // Start the new tone (will stop any playing tone)
Eric Laurent8487fed2009-09-07 08:45:14 -0700761 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800762 }
763 }
764
765 /**
766 * Brings up the "dialpad chooser" UI in place of the usual Dialer
767 * elements (the textfield/button and the dialpad underneath).
768 *
769 * We show this UI if the user brings up the Dialer while a call is
770 * already in progress, since there's a good chance we got here
771 * accidentally (and the user really wanted the in-call dialpad instead).
772 * So in this situation we display an intermediate UI that lets the user
773 * explicitly choose between the in-call dialpad ("Use touch tone
774 * keypad") and the regular Dialer ("Add call"). (Or, the option "Return
775 * to call in progress" just goes back to the in-call UI with no dialpad
776 * at all.)
777 *
778 * @param enabled If true, show the "dialpad chooser" instead
779 * of the regular Dialer UI
780 */
781 private void showDialpadChooser(boolean enabled) {
782 if (enabled) {
783 // Log.i(TAG, "Showing dialpad chooser!");
784 mDigitsAndBackspace.setVisibility(View.GONE);
785 if (mDialpad != null) mDialpad.setVisibility(View.GONE);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700786 mVoicemailDialAndBackspaceRow.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800787 mDialpadChooser.setVisibility(View.VISIBLE);
788
789 // Instantiate the DialpadChooserAdapter and hook it up to the
790 // ListView. We do this only once.
791 if (mDialpadChooserAdapter == null) {
792 mDialpadChooserAdapter = new DialpadChooserAdapter(this);
793 mDialpadChooser.setAdapter(mDialpadChooserAdapter);
794 }
795 } else {
796 // Log.i(TAG, "Displaying normal Dialer UI.");
797 mDigitsAndBackspace.setVisibility(View.VISIBLE);
798 if (mDialpad != null) mDialpad.setVisibility(View.VISIBLE);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700799 mVoicemailDialAndBackspaceRow.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800800 mDialpadChooser.setVisibility(View.GONE);
801 }
802 }
803
804 /**
805 * @return true if we're currently showing the "dialpad chooser" UI.
806 */
807 private boolean dialpadChooserVisible() {
808 return mDialpadChooser.getVisibility() == View.VISIBLE;
809 }
810
811 /**
812 * Simple list adapter, binding to an icon + text label
813 * for each item in the "dialpad chooser" list.
814 */
815 private static class DialpadChooserAdapter extends BaseAdapter {
816 private LayoutInflater mInflater;
817
818 // Simple struct for a single "choice" item.
819 static class ChoiceItem {
820 String text;
821 Bitmap icon;
822 int id;
823
824 public ChoiceItem(String s, Bitmap b, int i) {
825 text = s;
826 icon = b;
827 id = i;
828 }
829 }
830
831 // IDs for the possible "choices":
832 static final int DIALPAD_CHOICE_USE_DTMF_DIALPAD = 101;
833 static final int DIALPAD_CHOICE_RETURN_TO_CALL = 102;
834 static final int DIALPAD_CHOICE_ADD_NEW_CALL = 103;
835
836 private static final int NUM_ITEMS = 3;
837 private ChoiceItem mChoiceItems[] = new ChoiceItem[NUM_ITEMS];
838
839 public DialpadChooserAdapter(Context context) {
840 // Cache the LayoutInflate to avoid asking for a new one each time.
841 mInflater = LayoutInflater.from(context);
842
843 // Initialize the possible choices.
844 // TODO: could this be specified entirely in XML?
845
846 // - "Use touch tone keypad"
847 mChoiceItems[0] = new ChoiceItem(
848 context.getString(R.string.dialer_useDtmfDialpad),
849 BitmapFactory.decodeResource(context.getResources(),
850 R.drawable.ic_dialer_fork_tt_keypad),
851 DIALPAD_CHOICE_USE_DTMF_DIALPAD);
852
853 // - "Return to call in progress"
854 mChoiceItems[1] = new ChoiceItem(
855 context.getString(R.string.dialer_returnToInCallScreen),
856 BitmapFactory.decodeResource(context.getResources(),
857 R.drawable.ic_dialer_fork_current_call),
858 DIALPAD_CHOICE_RETURN_TO_CALL);
859
860 // - "Add call"
861 mChoiceItems[2] = new ChoiceItem(
862 context.getString(R.string.dialer_addAnotherCall),
863 BitmapFactory.decodeResource(context.getResources(),
864 R.drawable.ic_dialer_fork_add_call),
865 DIALPAD_CHOICE_ADD_NEW_CALL);
866 }
867
868 public int getCount() {
869 return NUM_ITEMS;
870 }
871
872 /**
873 * Return the ChoiceItem for a given position.
874 */
875 public Object getItem(int position) {
876 return mChoiceItems[position];
877 }
878
879 /**
880 * Return a unique ID for each possible choice.
881 */
882 public long getItemId(int position) {
883 return position;
884 }
885
886 /**
887 * Make a view for each row.
888 */
889 public View getView(int position, View convertView, ViewGroup parent) {
890 // When convertView is non-null, we can reuse it (there's no need
891 // to reinflate it.)
892 if (convertView == null) {
893 convertView = mInflater.inflate(R.layout.dialpad_chooser_list_item, null);
894 }
895
896 TextView text = (TextView) convertView.findViewById(R.id.text);
897 text.setText(mChoiceItems[position].text);
898
899 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
900 icon.setImageBitmap(mChoiceItems[position].icon);
901
902 return convertView;
903 }
904 }
905
906 /**
907 * Handle clicks from the dialpad chooser.
908 */
909 public void onItemClick(AdapterView parent, View v, int position, long id) {
910 DialpadChooserAdapter.ChoiceItem item =
911 (DialpadChooserAdapter.ChoiceItem) parent.getItemAtPosition(position);
912 int itemId = item.id;
913 switch (itemId) {
914 case DialpadChooserAdapter.DIALPAD_CHOICE_USE_DTMF_DIALPAD:
915 // Log.i(TAG, "DIALPAD_CHOICE_USE_DTMF_DIALPAD");
916 // Fire off an intent to go back to the in-call UI
917 // with the dialpad visible.
918 returnToInCallScreen(true);
919 break;
920
921 case DialpadChooserAdapter.DIALPAD_CHOICE_RETURN_TO_CALL:
922 // Log.i(TAG, "DIALPAD_CHOICE_RETURN_TO_CALL");
923 // Fire off an intent to go back to the in-call UI
924 // (with the dialpad hidden).
925 returnToInCallScreen(false);
926 break;
927
928 case DialpadChooserAdapter.DIALPAD_CHOICE_ADD_NEW_CALL:
929 // Log.i(TAG, "DIALPAD_CHOICE_ADD_NEW_CALL");
930 // Ok, guess the user really did want to be here (in the
931 // regular Dialer) after all. Bring back the normal Dialer UI.
932 showDialpadChooser(false);
933 break;
934
935 default:
936 Log.w(TAG, "onItemClick: unexpected itemId: " + itemId);
937 break;
938 }
939 }
940
941 /**
942 * Returns to the in-call UI (where there's presumably a call in
943 * progress) in response to the user selecting "use touch tone keypad"
944 * or "return to call" from the dialpad chooser.
945 */
946 private void returnToInCallScreen(boolean showDialpad) {
947 try {
948 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
949 if (phone != null) phone.showCallScreenWithDialpad(showDialpad);
950 } catch (RemoteException e) {
951 Log.w(TAG, "phone.showCallScreenWithDialpad() failed", e);
952 }
953
954 // Finally, finish() ourselves so that we don't stay on the
955 // activity stack.
956 // Note that we do this whether or not the showCallScreenWithDialpad()
957 // call above had any effect or not! (That call is a no-op if the
958 // phone is idle, which can happen if the current call ends while
959 // the dialpad chooser is up. In this case we can't show the
960 // InCallScreen, and there's no point staying here in the Dialer,
961 // so we just take the user back where he came from...)
962 finish();
963 }
964
965 /**
966 * @return true if the phone is "in use", meaning that at least one line
967 * is active (ie. off hook or ringing or dialing).
968 */
969 private boolean phoneIsInUse() {
970 boolean phoneInUse = false;
971 try {
972 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
973 if (phone != null) phoneInUse = !phone.isIdle();
974 } catch (RemoteException e) {
975 Log.w(TAG, "phone.isIdle() failed", e);
976 }
977 return phoneInUse;
978 }
David Brownc29c7ab2009-07-07 16:00:18 -0700979
980 /**
981 * Triggers haptic feedback (if enabled) for dialer key presses.
982 */
983 private synchronized void vibrate() {
984 if (!mVibrateOn) {
985 return;
986 }
987 if (mVibrator == null) {
988 mVibrator = new Vibrator();
989 }
990 mVibrator.vibrate(mVibrateDuration);
991 }
Reli Talc2a2a512009-06-10 16:48:00 -0400992
993 /**
994 * Returns true whenever any one of the options from the menu is selected.
995 * Code changes to support dialpad options
996 */
997 @Override
998 public boolean onOptionsItemSelected(MenuItem item) {
999 switch (item.getItemId()) {
1000 case MENU_2S_PAUSE:
1001 updateDialString(",");
1002 return true;
1003 case MENU_WAIT:
1004 updateDialString(";");
1005 return true;
1006 }
1007 return false;
1008 }
1009
1010 /**
1011 * Updates the dial string (mDigits) after inserting a Pause character (,)
1012 * or Wait character (;).
1013 */
1014 private void updateDialString(String newDigits) {
1015 int selectionStart;
1016 int selectionEnd;
1017
1018 // SpannableStringBuilder editable_text = new SpannableStringBuilder(mDigits.getText());
Eric Fischer686782e2009-09-10 17:57:45 -07001019 int anchor = mDigits.getSelectionStart();
1020 int point = mDigits.getSelectionEnd();
1021
1022 selectionStart = Math.min(anchor, point);
1023 selectionEnd = Math.max(anchor, point);
Reli Talc2a2a512009-06-10 16:48:00 -04001024
1025 Editable digits = mDigits.getText();
1026 if (selectionStart != -1 ) {
1027 if (selectionStart == selectionEnd) {
1028 // then there is no selection. So insert the pause at this
1029 // position and update the mDigits.
1030 digits.replace(selectionStart, selectionStart, newDigits);
1031 } else {
Eric Fischer1e2d3a22009-09-17 10:53:10 -07001032 digits.replace(selectionStart, selectionEnd, newDigits);
Reli Talc2a2a512009-06-10 16:48:00 -04001033 }
1034 } else {
1035 int len = mDigits.length();
1036 digits.replace(len, len, newDigits);
1037 }
1038 }
1039
1040 /**
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001041 * Update the enabledness of the "Dial" button if applicable.
1042 */
1043 private void updateDialButtonStateEnabledAttr() {
1044 if (mDialButton != null) {
1045 mDialButton.setEnabled(mDigits.length() != 0);
1046 }
1047 }
1048
1049 /**
Reli Talc2a2a512009-06-10 16:48:00 -04001050 * This function return true if Wait menu item can be shown
1051 * otherwise returns false. Assumes the passed string is non-empty
1052 * and the 0th index check is not required.
1053 */
1054 private boolean showWait(int start, int end, String digits) {
1055 if (start == end) {
1056 // visible false in this case
1057 if (start > digits.length()) return false;
1058
1059 // preceding char is ';', so visible should be false
1060 if (digits.charAt(start-1) == ';') return false;
1061
1062 // next char is ';', so visible should be false
1063 if ((digits.length() > start) && (digits.charAt(start) == ';')) return false;
1064 } else {
1065 // visible false in this case
1066 if (start > digits.length() || end > digits.length()) return false;
1067
1068 // In this case we need to just check for ';' preceding to start
1069 // or next to end
1070 if (digits.charAt(start-1) == ';') return false;
1071 }
1072 return true;
1073 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001074}