blob: bb4f49623501082e7f42e57a75d1c3e7091ed8e1 [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
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080019import com.android.internal.telephony.ITelephony;
20import com.android.phone.CallLogAsync;
21import com.android.phone.HapticFeedback;
22
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080023import android.app.Activity;
24import android.content.ActivityNotFoundException;
25import android.content.Context;
26import android.content.Intent;
Bernd Holzheyd0bfafc2010-03-02 09:00:02 +010027import android.content.res.Configuration;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import android.content.res.Resources;
29import android.database.Cursor;
30import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
32import android.graphics.drawable.Drawable;
33import android.media.AudioManager;
34import android.media.ToneGenerator;
35import android.net.Uri;
36import android.os.Bundle;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080037import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.os.SystemClock;
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080040import android.provider.Settings;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.provider.Contacts.People;
42import android.provider.Contacts.Phones;
43import android.provider.Contacts.PhonesColumns;
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080044import android.provider.Contacts.Intents.Insert;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045import android.telephony.PhoneNumberFormattingTextWatcher;
46import android.telephony.PhoneNumberUtils;
47import android.telephony.PhoneStateListener;
48import android.telephony.TelephonyManager;
49import android.text.Editable;
50import android.text.TextUtils;
51import android.text.TextWatcher;
52import android.text.method.DialerKeyListener;
53import android.util.Log;
54import android.view.KeyEvent;
55import android.view.LayoutInflater;
56import android.view.Menu;
57import android.view.MenuItem;
58import android.view.View;
59import android.view.ViewConfiguration;
60import android.view.ViewGroup;
Bernd Holzheyd0bfafc2010-03-02 09:00:02 +010061import android.view.Window;
Karl Rosaenf46bc312009-03-24 18:20:48 -070062import android.view.inputmethod.InputMethodManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063import android.widget.AdapterView;
64import android.widget.BaseAdapter;
65import android.widget.EditText;
66import android.widget.ImageView;
67import android.widget.ListView;
68import android.widget.TextView;
69
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070/**
71 * Dialer activity that displays the typical twelve key interface.
72 */
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080073@SuppressWarnings("deprecation")
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074public class TwelveKeyDialer extends Activity implements View.OnClickListener,
75 View.OnLongClickListener, View.OnKeyListener,
76 AdapterView.OnItemClickListener, TextWatcher {
Nicolas Cataniac3be69e2010-01-14 14:03:53 -080077 private static final String EMPTY_NUMBER = "";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078 private static final String TAG = "TwelveKeyDialer";
Eric Laurentd9efc872009-07-17 11:52:06 -070079
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080 /** The length of DTMF tones in milliseconds */
81 private static final int TONE_LENGTH_MS = 150;
Eric Laurentd9efc872009-07-17 11:52:06 -070082
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080083 /** The DTMF tone volume relative to other sounds in the stream */
Jean-Michel Trividd44f8c2009-11-10 13:00:45 -080084 private static final int TONE_RELATIVE_VOLUME = 80;
85
86 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
87 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_MUSIC;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088
89 private EditText mDigits;
90 private View mDelete;
91 private MenuItem mAddToContactMenuItem;
92 private ToneGenerator mToneGenerator;
93 private Object mToneGeneratorLock = new Object();
94 private Drawable mDigitsBackground;
95 private Drawable mDigitsEmptyBackground;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 private View mDialpad;
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -070097 private View mVoicemailDialAndDeleteRow;
Nicolas Catania80bda0f2009-09-19 09:17:14 -070098 private View mVoicemailButton;
99 private View mDialButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100 private ListView mDialpadChooser;
101 private DialpadChooserAdapter mDialpadChooserAdapter;
Reli Talc2a2a512009-06-10 16:48:00 -0400102 //Member variables for dialpad options
103 private MenuItem m2SecPauseMenuItem;
104 private MenuItem mWaitMenuItem;
105 private static final int MENU_ADD_CONTACTS = 1;
106 private static final int MENU_2S_PAUSE = 2;
107 private static final int MENU_WAIT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800108
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800109 // Last number dialed, retrieved asynchronously from the call DB
110 // in onCreate. This number is displayed when the user hits the
111 // send key and cleared in onPause.
112 CallLogAsync mCallLog = new CallLogAsync();
113 private String mLastNumberDialed = EMPTY_NUMBER;
114
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 // determines if we want to playback local DTMF tones.
116 private boolean mDTMFToneEnabled;
David Brownc29c7ab2009-07-07 16:00:18 -0700117
118 // Vibration (haptic feedback) for dialer key presses.
Nicolas Catania905e7622009-12-01 08:51:20 -0800119 private HapticFeedback mHaptic = new HapticFeedback();
Eric Laurentd9efc872009-07-17 11:52:06 -0700120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 /** Identifier for the "Add Call" intent extra. */
122 static final String ADD_CALL_MODE_KEY = "add_call_mode";
Paul Bermandbdcde22009-10-09 12:04:10 -0400123
124 /**
125 * Identifier for intent extra for sending an empty Flash message for
126 * CDMA networks. This message is used by the network to simulate a
127 * press/depress of the "hookswitch" of a landline phone. Aka "empty flash".
128 *
129 * TODO: Using an intent extra to tell the phone to send this flash is a
130 * temporary measure. To be replaced with an ITelephony call in the future.
131 * TODO: Keep in sync with the string defined in OutgoingCallBroadcaster.java
132 * in Phone app until this is replaced with the ITelephony API.
133 */
134 static final String EXTRA_SEND_EMPTY_FLASH
135 = "com.android.phone.extra.SEND_EMPTY_FLASH";
136
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137 /** Indicates if we are opening this dialer to add a call from the InCallScreen. */
138 private boolean mIsAddCallMode;
139
140 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
141 /**
142 * Listen for phone state changes so that we can take down the
143 * "dialpad chooser" if the phone becomes idle while the
144 * chooser UI is visible.
145 */
146 @Override
147 public void onCallStateChanged(int state, String incomingNumber) {
148 // Log.i(TAG, "PhoneStateListener.onCallStateChanged: "
149 // + state + ", '" + incomingNumber + "'");
150 if ((state == TelephonyManager.CALL_STATE_IDLE) && dialpadChooserVisible()) {
151 // Log.i(TAG, "Call ended with dialpad chooser visible! Taking it down...");
152 // Note there's a race condition in the UI here: the
153 // dialpad chooser could conceivably disappear (on its
154 // own) at the exact moment the user was trying to select
155 // one of the choices, which would be confusing. (But at
156 // least that's better than leaving the dialpad chooser
157 // onscreen, but useless...)
158 showDialpadChooser(false);
159 }
160 }
161 };
162
163 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
164 // Do nothing
165 }
166
167 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
168 // Do nothing
Eric Laurentd9efc872009-07-17 11:52:06 -0700169 // DTMF Tones do not need to be played here any longer -
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 // the DTMF dialer handles that functionality now.
171 }
172
173 public void afterTextChanged(Editable input) {
174 if (SpecialCharSequenceMgr.handleChars(this, input.toString(), mDigits)) {
175 // A special sequence was entered, clear the digits
176 mDigits.getText().clear();
177 }
178
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800179 if (!isDigitsEmpty()) {
Nicolas Catania75993762009-09-21 16:42:00 -0700180 mDigits.setBackgroundDrawable(mDigitsBackground);
181 } else {
Nicolas Catania3040fa32009-10-01 13:00:53 -0700182 mDigits.setCursorVisible(false);
Nicolas Catania75993762009-09-21 16:42:00 -0700183 mDigits.setBackgroundDrawable(mDigitsEmptyBackground);
184 }
185
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800186 updateDialAndDeleteButtonEnabledState();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800187 }
188
189 @Override
190 protected void onCreate(Bundle icicle) {
191 super.onCreate(icicle);
192
Bernd Holzheyd0bfafc2010-03-02 09:00:02 +0100193 Resources r = getResources();
194 // Do not show title in the case the device is in carmode.
195 if ((r.getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK) ==
196 Configuration.UI_MODE_TYPE_CAR) {
197 requestWindowFeature(Window.FEATURE_NO_TITLE);
198 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800199 // Set the content view
200 setContentView(getContentViewResource());
201
Nicolas Catania75993762009-09-21 16:42:00 -0700202 // Load up the resources for the text field.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800203 mDigitsBackground = r.getDrawable(R.drawable.btn_dial_textfield_active);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800204 mDigitsEmptyBackground = r.getDrawable(R.drawable.btn_dial_textfield);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800205
206 mDigits = (EditText) findViewById(R.id.digits);
207 mDigits.setKeyListener(DialerKeyListener.getInstance());
208 mDigits.setOnClickListener(this);
209 mDigits.setOnKeyListener(this);
Nicolas Catania3040fa32009-10-01 13:00:53 -0700210
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800211 maybeAddNumberFormatting();
212
213 // Check for the presence of the keypad
214 View view = findViewById(R.id.one);
215 if (view != null) {
216 setupKeypad();
217 }
218
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700219 mVoicemailDialAndDeleteRow = findViewById(R.id.voicemailAndDialAndDelete);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700220
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700221 initVoicemailButton();
222
David Brown3d07e6d2009-08-04 20:30:09 -0700223 // Check whether we should show the onscreen "Dial" button.
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700224 mDialButton = mVoicemailDialAndDeleteRow.findViewById(R.id.dialButton);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700225
David Brown3d07e6d2009-08-04 20:30:09 -0700226 if (r.getBoolean(R.bool.config_show_onscreen_dial_button)) {
David Brown3d07e6d2009-08-04 20:30:09 -0700227 mDialButton.setOnClickListener(this);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700228 } else {
229 mDialButton.setVisibility(View.GONE); // It's VISIBLE by default
230 mDialButton = null;
David Brown3d07e6d2009-08-04 20:30:09 -0700231 }
232
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700233 view = mVoicemailDialAndDeleteRow.findViewById(R.id.deleteButton);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800234 view.setOnClickListener(this);
235 view.setOnLongClickListener(this);
236 mDelete = view;
237
Nicolas Catania901f8562009-10-09 11:09:45 -0700238 mDialpad = findViewById(R.id.dialpad); // This is null in landscape mode.
239
240 // In landscape we put the keyboard in phone mode.
241 // In portrait we prevent the soft keyboard to show since the
242 // dialpad acts as one already.
243 if (null == mDialpad) {
244 mDigits.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
245 } else {
246 mDigits.setInputType(android.text.InputType.TYPE_NULL);
247 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248
249 // Set up the "dialpad chooser" UI; see showDialpadChooser().
250 mDialpadChooser = (ListView) findViewById(R.id.dialpadChooser);
251 mDialpadChooser.setOnItemClickListener(this);
252
253 if (!resolveIntent() && icicle != null) {
254 super.onRestoreInstanceState(icicle);
255 }
256
Nicolas Catania905e7622009-12-01 08:51:20 -0800257 try {
258 mHaptic.init(this, r.getBoolean(R.bool.config_enable_dialer_key_vibration));
259 } catch (Resources.NotFoundException nfe) {
260 Log.e(TAG, "Vibrate control bool missing.", nfe);
261 }
262
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800263 }
264
265 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800266 protected void onRestoreInstanceState(Bundle icicle) {
267 // Do nothing, state is restored in onCreate() if needed
268 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700269
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800270 protected void maybeAddNumberFormatting() {
271 mDigits.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
272 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700273
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800274 /**
Eric Laurentd9efc872009-07-17 11:52:06 -0700275 * Overridden by subclasses to control the resource used by the content view.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800276 */
277 protected int getContentViewResource() {
278 return R.layout.twelve_key_dialer;
279 }
280
281 private boolean resolveIntent() {
282 boolean ignoreState = false;
283
284 // Find the proper intent
285 final Intent intent;
286 if (isChild()) {
287 intent = getParent().getIntent();
288 ignoreState = intent.getBooleanExtra(DialtactsActivity.EXTRA_IGNORE_STATE, false);
289 } else {
290 intent = getIntent();
291 }
292 // Log.i(TAG, "==> resolveIntent(): intent: " + intent);
293
294 // by default we are not adding a call.
295 mIsAddCallMode = false;
296
297 // By default we don't show the "dialpad chooser" UI.
298 boolean needToShowDialpadChooser = false;
299
300 // Resolve the intent
301 final String action = intent.getAction();
302 if (Intent.ACTION_DIAL.equals(action) || Intent.ACTION_VIEW.equals(action)) {
303 // see if we are "adding a call" from the InCallScreen; false by default.
304 mIsAddCallMode = intent.getBooleanExtra(ADD_CALL_MODE_KEY, false);
305 Uri uri = intent.getData();
306 if (uri != null) {
307 if ("tel".equals(uri.getScheme())) {
308 // Put the requested number into the input area
Virgil Kingd8831122010-03-10 13:44:11 -0800309 String data = uri.getSchemeSpecificPart();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800310 setFormattedDigits(data);
311 } else {
312 String type = intent.getType();
313 if (People.CONTENT_ITEM_TYPE.equals(type)
314 || Phones.CONTENT_ITEM_TYPE.equals(type)) {
315 // Query the phone number
316 Cursor c = getContentResolver().query(intent.getData(),
317 new String[] {PhonesColumns.NUMBER}, null, null, null);
318 if (c != null) {
319 if (c.moveToFirst()) {
320 // Put the number into the input area
321 setFormattedDigits(c.getString(0));
322 }
323 c.close();
324 }
325 }
326 }
327 }
328 } else if (Intent.ACTION_MAIN.equals(action)) {
329 // The MAIN action means we're bringing up a blank dialer
330 // (e.g. by selecting the Home shortcut, or tabbing over from
331 // Contacts or Call log.)
332 //
333 // At this point, IF there's already an active call, there's a
334 // good chance that the user got here accidentally (but really
335 // wanted the in-call dialpad instead). So we bring up an
336 // intermediate UI to make the user confirm what they really
337 // want to do.
338 if (phoneIsInUse()) {
339 // Log.i(TAG, "resolveIntent(): phone is in use; showing dialpad chooser!");
340 needToShowDialpadChooser = true;
341 }
342 }
343
344 // Bring up the "dialpad chooser" IFF we need to make the user
345 // confirm which dialpad they really want.
346 showDialpadChooser(needToShowDialpadChooser);
347
348 return ignoreState;
349 }
350
351 protected void setFormattedDigits(String data) {
352 // strip the non-dialable numbers out of the data string.
353 String dialString = PhoneNumberUtils.extractNetworkPortion(data);
354 dialString = PhoneNumberUtils.formatNumber(dialString);
355 if (!TextUtils.isEmpty(dialString)) {
356 Editable digits = mDigits.getText();
357 digits.replace(0, digits.length(), dialString);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700358 // for some reason this isn't getting called in the digits.replace call above..
359 // but in any case, this will make sure the background drawable looks right
360 afterTextChanged(digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800361 }
362 }
363
364 @Override
365 protected void onNewIntent(Intent newIntent) {
366 setIntent(newIntent);
367 resolveIntent();
368 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700369
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800370 @Override
371 protected void onPostCreate(Bundle savedInstanceState) {
372 super.onPostCreate(savedInstanceState);
373
374 // This can't be done in onCreate(), since the auto-restoring of the digits
375 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
376 // is called. This method will be called every time the activity is created, and
377 // will always happen after onRestoreSavedInstanceState().
378 mDigits.addTextChangedListener(this);
379 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700380
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381 private void setupKeypad() {
382 // Setup the listeners for the buttons
383 View view = findViewById(R.id.one);
384 view.setOnClickListener(this);
385 view.setOnLongClickListener(this);
386
387 findViewById(R.id.two).setOnClickListener(this);
388 findViewById(R.id.three).setOnClickListener(this);
389 findViewById(R.id.four).setOnClickListener(this);
390 findViewById(R.id.five).setOnClickListener(this);
391 findViewById(R.id.six).setOnClickListener(this);
392 findViewById(R.id.seven).setOnClickListener(this);
393 findViewById(R.id.eight).setOnClickListener(this);
394 findViewById(R.id.nine).setOnClickListener(this);
395 findViewById(R.id.star).setOnClickListener(this);
396
397 view = findViewById(R.id.zero);
398 view.setOnClickListener(this);
399 view.setOnLongClickListener(this);
400
401 findViewById(R.id.pound).setOnClickListener(this);
402 }
403
404 @Override
405 protected void onResume() {
406 super.onResume();
David Browndc1dfe22010-03-01 14:34:57 -0800407
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800408 // Query the last dialed number. Do it first because hitting
409 // the DB is 'slow'. This call is asynchronous.
410 queryLastOutgoingCall();
David Brownc29c7ab2009-07-07 16:00:18 -0700411
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800412 // retrieve the DTMF tone play back setting.
413 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
414 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
415
Nicolas Catania905e7622009-12-01 08:51:20 -0800416 // Retrieve the haptic feedback setting.
417 mHaptic.checkSystemSetting();
418
Eric Laurentd9efc872009-07-17 11:52:06 -0700419 // if the mToneGenerator creation fails, just continue without it. It is
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800420 // a local audio signal, and is not as important as the dtmf tone itself.
421 synchronized(mToneGeneratorLock) {
422 if (mToneGenerator == null) {
423 try {
Jean-Michel Trividd44f8c2009-11-10 13:00:45 -0800424 // we want the user to be able to control the volume of the dial tones
425 // outside of a call, so we use the stream type that is also mapped to the
426 // volume control keys for this activity
427 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
428 setVolumeControlStream(DIAL_TONE_STREAM_TYPE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800429 } catch (RuntimeException e) {
430 Log.w(TAG, "Exception caught while creating local tone generator: " + e);
431 mToneGenerator = null;
432 }
433 }
434 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700435
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800436 Activity parent = getParent();
437 // See if we were invoked with a DIAL intent. If we were, fill in the appropriate
438 // digits in the dialer field.
439 if (parent != null && parent instanceof DialtactsActivity) {
440 Uri dialUri = ((DialtactsActivity) parent).getAndClearDialUri();
441 if (dialUri != null) {
442 resolveIntent();
443 }
444 }
445
446 // While we're in the foreground, listen for phone state changes,
447 // purely so that we can take down the "dialpad chooser" if the
448 // phone becomes idle while the chooser UI is visible.
449 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
450 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
451
452 // Potentially show hint text in the mDigits field when the user
453 // hasn't typed any digits yet. (If there's already an active call,
454 // this hint text will remind the user that he's about to add a new
455 // call.)
456 //
457 // TODO: consider adding better UI for the case where *both* lines
458 // are currently in use. (Right now we let the user try to add
459 // another call, but that call is guaranteed to fail. Perhaps the
460 // entire dialer UI should be disabled instead.)
461 if (phoneIsInUse()) {
462 mDigits.setHint(R.string.dialerDialpadHintText);
463 } else {
464 // Common case; no hint necessary.
465 mDigits.setHint(null);
466
467 // Also, a sanity-check: the "dialpad chooser" UI should NEVER
468 // be visible if the phone is idle!
469 showDialpadChooser(false);
470 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700471
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800472 updateDialAndDeleteButtonEnabledState();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 }
474
475 @Override
Karl Rosaenf46bc312009-03-24 18:20:48 -0700476 public void onWindowFocusChanged(boolean hasFocus) {
477 if (hasFocus) {
478 // Hide soft keyboard, if visible (it's fugly over button dialer).
479 // The only known case where this will be true is when launching the dialer with
480 // ACTION_DIAL via a soft keyboard. we dismiss it here because we don't
481 // have a window token yet in onCreate / onNewIntent
482 InputMethodManager inputMethodManager = (InputMethodManager)
483 getSystemService(Context.INPUT_METHOD_SERVICE);
Eric Laurentd9efc872009-07-17 11:52:06 -0700484 inputMethodManager.hideSoftInputFromWindow(mDigits.getWindowToken(), 0);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700485 }
486 }
487
488 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 protected void onPause() {
490 super.onPause();
491
492 // Stop listening for phone state changes.
493 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
494 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
495
496 synchronized(mToneGeneratorLock) {
497 if (mToneGenerator != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 mToneGenerator.release();
499 mToneGenerator = null;
500 }
501 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800502 // TODO: I wonder if we should not check if the AsyncTask that
503 // lookup the last dialed number has completed.
504 mLastNumberDialed = EMPTY_NUMBER; // Since we are going to query again, free stale number.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 }
506
507 @Override
508 public boolean onCreateOptionsMenu(Menu menu) {
Reli Talc2a2a512009-06-10 16:48:00 -0400509 mAddToContactMenuItem = menu.add(0, MENU_ADD_CONTACTS, 0, R.string.recentCalls_addToContact)
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800510 .setIcon(android.R.drawable.ic_menu_add);
Reli Talc2a2a512009-06-10 16:48:00 -0400511 m2SecPauseMenuItem = menu.add(0, MENU_2S_PAUSE, 0, R.string.add_2sec_pause)
512 .setIcon(R.drawable.ic_menu_2sec_pause);
513 mWaitMenuItem = menu.add(0, MENU_WAIT, 0, R.string.add_wait)
514 .setIcon(R.drawable.ic_menu_wait);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800515 return true;
516 }
517
518 @Override
519 public boolean onPrepareOptionsMenu(Menu menu) {
520 // We never show a menu if the "choose dialpad" UI is up.
521 if (dialpadChooserVisible()) {
522 return false;
523 }
524
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800525 if (isDigitsEmpty()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 mAddToContactMenuItem.setVisible(false);
Reli Talc2a2a512009-06-10 16:48:00 -0400527 m2SecPauseMenuItem.setVisible(false);
528 mWaitMenuItem.setVisible(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800529 } else {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800530 CharSequence digits = mDigits.getText();
531
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800532 // Put the current digits string into an intent
533 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800534 intent.putExtra(Insert.PHONE, digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800535 intent.setType(People.CONTENT_ITEM_TYPE);
536 mAddToContactMenuItem.setIntent(intent);
537 mAddToContactMenuItem.setVisible(true);
Reli Talc2a2a512009-06-10 16:48:00 -0400538
539 // Check out whether to show Pause & Wait option menu items
540 int selectionStart;
541 int selectionEnd;
542 String strDigits = digits.toString();
543
544 selectionStart = mDigits.getSelectionStart();
545 selectionEnd = mDigits.getSelectionEnd();
546
547 if (selectionStart != -1) {
548 if (selectionStart > selectionEnd) {
549 // swap it as we want start to be less then end
550 int tmp = selectionStart;
551 selectionStart = selectionEnd;
552 selectionEnd = tmp;
553 }
554
555 if (selectionStart != 0) {
556 // Pause can be visible if cursor is not in the begining
557 m2SecPauseMenuItem.setVisible(true);
558
559 // For Wait to be visible set of condition to meet
560 mWaitMenuItem.setVisible(showWait(selectionStart,
561 selectionEnd, strDigits));
562 } else {
563 // cursor in the beginning both pause and wait to be invisible
564 m2SecPauseMenuItem.setVisible(false);
565 mWaitMenuItem.setVisible(false);
566 }
567 } else {
568 // cursor is not selected so assume new digit is added to the end
569 int strLength = strDigits.length();
570 mWaitMenuItem.setVisible(showWait(strLength,
571 strLength, strDigits));
572 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800573 }
574 return true;
575 }
576
577 @Override
578 public boolean onKeyDown(int keyCode, KeyEvent event) {
579 switch (keyCode) {
580 case KeyEvent.KEYCODE_CALL: {
581 long callPressDiff = SystemClock.uptimeMillis() - event.getDownTime();
582 if (callPressDiff >= ViewConfiguration.getLongPressTimeout()) {
583 // Launch voice dialer
584 Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND);
585 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
586 try {
587 startActivity(intent);
588 } catch (ActivityNotFoundException e) {
589 }
590 }
591 return true;
592 }
593 case KeyEvent.KEYCODE_1: {
Eric Laurentd9efc872009-07-17 11:52:06 -0700594 long timeDiff = SystemClock.uptimeMillis() - event.getDownTime();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800595 if (timeDiff >= ViewConfiguration.getLongPressTimeout()) {
596 // Long press detected, call voice mail
597 callVoicemail();
598 }
599 return true;
600 }
601 }
602 return super.onKeyDown(keyCode, event);
603 }
604
605 @Override
606 public boolean onKeyUp(int keyCode, KeyEvent event) {
607 switch (keyCode) {
608 case KeyEvent.KEYCODE_CALL: {
Nicolas Catania998763d2010-01-14 14:03:53 -0800609 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800610 return true;
611 }
612 }
613 return super.onKeyUp(keyCode, event);
614 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700615
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800616 private void keyPressed(int keyCode) {
Nicolas Catania905e7622009-12-01 08:51:20 -0800617 mHaptic.vibrate();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800618 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
619 mDigits.onKeyDown(keyCode, event);
620 }
621
622 public boolean onKey(View view, int keyCode, KeyEvent event) {
623 switch (view.getId()) {
624 case R.id.digits:
625 if (keyCode == KeyEvent.KEYCODE_ENTER) {
Nicolas Catania998763d2010-01-14 14:03:53 -0800626 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627 return true;
628 }
629 break;
630 }
631 return false;
632 }
633
634 public void onClick(View view) {
635 switch (view.getId()) {
636 case R.id.one: {
637 playTone(ToneGenerator.TONE_DTMF_1);
638 keyPressed(KeyEvent.KEYCODE_1);
639 return;
640 }
641 case R.id.two: {
642 playTone(ToneGenerator.TONE_DTMF_2);
643 keyPressed(KeyEvent.KEYCODE_2);
644 return;
645 }
646 case R.id.three: {
647 playTone(ToneGenerator.TONE_DTMF_3);
648 keyPressed(KeyEvent.KEYCODE_3);
649 return;
650 }
651 case R.id.four: {
652 playTone(ToneGenerator.TONE_DTMF_4);
653 keyPressed(KeyEvent.KEYCODE_4);
654 return;
655 }
656 case R.id.five: {
657 playTone(ToneGenerator.TONE_DTMF_5);
658 keyPressed(KeyEvent.KEYCODE_5);
659 return;
660 }
661 case R.id.six: {
662 playTone(ToneGenerator.TONE_DTMF_6);
663 keyPressed(KeyEvent.KEYCODE_6);
664 return;
665 }
666 case R.id.seven: {
667 playTone(ToneGenerator.TONE_DTMF_7);
668 keyPressed(KeyEvent.KEYCODE_7);
669 return;
670 }
671 case R.id.eight: {
672 playTone(ToneGenerator.TONE_DTMF_8);
673 keyPressed(KeyEvent.KEYCODE_8);
674 return;
675 }
676 case R.id.nine: {
677 playTone(ToneGenerator.TONE_DTMF_9);
678 keyPressed(KeyEvent.KEYCODE_9);
679 return;
680 }
681 case R.id.zero: {
682 playTone(ToneGenerator.TONE_DTMF_0);
683 keyPressed(KeyEvent.KEYCODE_0);
684 return;
685 }
686 case R.id.pound: {
687 playTone(ToneGenerator.TONE_DTMF_P);
688 keyPressed(KeyEvent.KEYCODE_POUND);
689 return;
690 }
691 case R.id.star: {
692 playTone(ToneGenerator.TONE_DTMF_S);
693 keyPressed(KeyEvent.KEYCODE_STAR);
694 return;
695 }
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700696 case R.id.deleteButton: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800697 keyPressed(KeyEvent.KEYCODE_DEL);
698 return;
699 }
Nicolas Catania3040fa32009-10-01 13:00:53 -0700700 case R.id.dialButton: {
Nicolas Catania905e7622009-12-01 08:51:20 -0800701 mHaptic.vibrate(); // Vibrate here too, just like we do for the regular keys
Nicolas Catania998763d2010-01-14 14:03:53 -0800702 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800703 return;
704 }
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700705 case R.id.voicemailButton: {
706 callVoicemail();
Nicolas Catania905e7622009-12-01 08:51:20 -0800707 mHaptic.vibrate();
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700708 return;
709 }
Nicolas Catania3040fa32009-10-01 13:00:53 -0700710 case R.id.digits: {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800711 if (!isDigitsEmpty()) {
Nicolas Catania3040fa32009-10-01 13:00:53 -0700712 mDigits.setCursorVisible(true);
713 }
714 return;
715 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800716 }
717 }
718
719 public boolean onLongClick(View view) {
720 final Editable digits = mDigits.getText();
721 int id = view.getId();
722 switch (id) {
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700723 case R.id.deleteButton: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800724 digits.clear();
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700725 // TODO: The framework forgets to clear the pressed
726 // status of disabled button. Until this is fixed,
727 // clear manually the pressed status. b/2133127
728 mDelete.setPressed(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800729 return true;
730 }
731 case R.id.one: {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800732 if (isDigitsEmpty()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800733 callVoicemail();
734 return true;
735 }
736 return false;
737 }
738 case R.id.zero: {
739 keyPressed(KeyEvent.KEYCODE_PLUS);
740 return true;
741 }
742 }
743 return false;
744 }
745
746 void callVoicemail() {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700747 startActivity(newVoicemailIntent());
748 mDigits.getText().clear(); // TODO: Fix bug 1745781
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800749 finish();
750 }
751
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700752 /**
753 * In most cases, when the dial button is pressed, there is a
754 * number in digits area. Pack it in the intent, start the
755 * outgoing call broadcast as a separate task and finish this
756 * activity.
757 *
758 * When there is no digit and the phone is CDMA and off hook,
759 * we're sending a blank flash for CDMA. CDMA networks use Flash
760 * messages when special processing needs to be done, mainly for
761 * 3-way or call waiting scenarios. Presumably, here we're in a
762 * special 3-way scenario where the network needs a blank flash
763 * before being able to add the new participant. (This is not the
764 * case with all 3-way calls, just certain CDMA infrastructures.)
765 *
766 * Otherwise, there is no digit, display the last dialed
767 * number. Don't finish since the user may want to edit it. The
768 * user needs to press the dial button again, to dial it (general
769 * case described above).
770 */
Nicolas Catania998763d2010-01-14 14:03:53 -0800771 void dialButtonPressed() {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700772 if (isDigitsEmpty()) { // No number entered.
Paul Bermandbdcde22009-10-09 12:04:10 -0400773 if (phoneIsCdma() && phoneIsOffhook()) {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700774 // This is really CDMA specific. On GSM is it possible
775 // to be off hook and wanted to add a 3rd party using
776 // the redial feature.
777 startActivity(newFlashIntent());
Paul Bermandbdcde22009-10-09 12:04:10 -0400778 } else {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700779 if (!TextUtils.isEmpty(mLastNumberDialed)) {
780 mDigits.setText(mLastNumberDialed);
781 } else {
782 // There's no "last number dialed" or the
783 // background query is still running. There's
784 // nothing useful for the Dial button to do in
785 // this case. Note: with a soft dial button, this
786 // can never happens since the dial button is
787 // disabled under these conditons.
788 playTone(ToneGenerator.TONE_PROP_NACK);
789 }
Paul Bermandbdcde22009-10-09 12:04:10 -0400790 }
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700791 } else {
792 final String number = mDigits.getText().toString();
Nicolas Catania998763d2010-01-14 14:03:53 -0800793
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700794 startActivity(newDialNumberIntent(number));
795 mDigits.getText().clear(); // TODO: Fix bug 1745781
Paul Bermandbdcde22009-10-09 12:04:10 -0400796 finish();
797 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800798 }
799
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800800
801 /**
David Brown22f615f2009-06-25 16:19:19 -0700802 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
803 *
804 * The tone is played locally, using the audio stream for phone calls.
805 * Tones are played only if the "Audible touch tones" user preference
806 * is checked, and are NOT played if the device is in silent mode.
807 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800808 * @param tone a tone code from {@link ToneGenerator}
809 */
810 void playTone(int tone) {
811 // if local tone playback is disabled, just return.
812 if (!mDTMFToneEnabled) {
813 return;
814 }
David Brown22f615f2009-06-25 16:19:19 -0700815
816 // Also do nothing if the phone is in silent mode.
817 // We need to re-check the ringer mode for *every* playTone()
818 // call, rather than keeping a local flag that's updated in
819 // onResume(), since it's possible to toggle silent mode without
820 // leaving the current activity (via the ENDCALL-longpress menu.)
821 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
David Brownd5a15302009-07-20 16:39:47 -0700822 int ringerMode = audioManager.getRingerMode();
823 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
824 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
David Brown22f615f2009-06-25 16:19:19 -0700825 return;
826 }
827
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800828 synchronized(mToneGeneratorLock) {
829 if (mToneGenerator == null) {
830 Log.w(TAG, "playTone: mToneGenerator == null, tone: "+tone);
831 return;
832 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700833
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800834 // Start the new tone (will stop any playing tone)
Eric Laurent8487fed2009-09-07 08:45:14 -0700835 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800836 }
837 }
838
839 /**
840 * Brings up the "dialpad chooser" UI in place of the usual Dialer
841 * elements (the textfield/button and the dialpad underneath).
842 *
843 * We show this UI if the user brings up the Dialer while a call is
844 * already in progress, since there's a good chance we got here
845 * accidentally (and the user really wanted the in-call dialpad instead).
846 * So in this situation we display an intermediate UI that lets the user
847 * explicitly choose between the in-call dialpad ("Use touch tone
848 * keypad") and the regular Dialer ("Add call"). (Or, the option "Return
849 * to call in progress" just goes back to the in-call UI with no dialpad
850 * at all.)
851 *
852 * @param enabled If true, show the "dialpad chooser" instead
853 * of the regular Dialer UI
854 */
855 private void showDialpadChooser(boolean enabled) {
856 if (enabled) {
857 // Log.i(TAG, "Showing dialpad chooser!");
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700858 mDigits.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800859 if (mDialpad != null) mDialpad.setVisibility(View.GONE);
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700860 mVoicemailDialAndDeleteRow.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800861 mDialpadChooser.setVisibility(View.VISIBLE);
862
863 // Instantiate the DialpadChooserAdapter and hook it up to the
864 // ListView. We do this only once.
865 if (mDialpadChooserAdapter == null) {
866 mDialpadChooserAdapter = new DialpadChooserAdapter(this);
867 mDialpadChooser.setAdapter(mDialpadChooserAdapter);
868 }
869 } else {
870 // Log.i(TAG, "Displaying normal Dialer UI.");
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700871 mDigits.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800872 if (mDialpad != null) mDialpad.setVisibility(View.VISIBLE);
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700873 mVoicemailDialAndDeleteRow.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800874 mDialpadChooser.setVisibility(View.GONE);
875 }
876 }
877
878 /**
879 * @return true if we're currently showing the "dialpad chooser" UI.
880 */
881 private boolean dialpadChooserVisible() {
882 return mDialpadChooser.getVisibility() == View.VISIBLE;
883 }
884
885 /**
886 * Simple list adapter, binding to an icon + text label
887 * for each item in the "dialpad chooser" list.
888 */
889 private static class DialpadChooserAdapter extends BaseAdapter {
890 private LayoutInflater mInflater;
891
892 // Simple struct for a single "choice" item.
893 static class ChoiceItem {
894 String text;
895 Bitmap icon;
896 int id;
897
898 public ChoiceItem(String s, Bitmap b, int i) {
899 text = s;
900 icon = b;
901 id = i;
902 }
903 }
904
905 // IDs for the possible "choices":
906 static final int DIALPAD_CHOICE_USE_DTMF_DIALPAD = 101;
907 static final int DIALPAD_CHOICE_RETURN_TO_CALL = 102;
908 static final int DIALPAD_CHOICE_ADD_NEW_CALL = 103;
909
910 private static final int NUM_ITEMS = 3;
911 private ChoiceItem mChoiceItems[] = new ChoiceItem[NUM_ITEMS];
912
913 public DialpadChooserAdapter(Context context) {
914 // Cache the LayoutInflate to avoid asking for a new one each time.
915 mInflater = LayoutInflater.from(context);
916
917 // Initialize the possible choices.
918 // TODO: could this be specified entirely in XML?
919
920 // - "Use touch tone keypad"
921 mChoiceItems[0] = new ChoiceItem(
922 context.getString(R.string.dialer_useDtmfDialpad),
923 BitmapFactory.decodeResource(context.getResources(),
924 R.drawable.ic_dialer_fork_tt_keypad),
925 DIALPAD_CHOICE_USE_DTMF_DIALPAD);
926
927 // - "Return to call in progress"
928 mChoiceItems[1] = new ChoiceItem(
929 context.getString(R.string.dialer_returnToInCallScreen),
930 BitmapFactory.decodeResource(context.getResources(),
931 R.drawable.ic_dialer_fork_current_call),
932 DIALPAD_CHOICE_RETURN_TO_CALL);
933
934 // - "Add call"
935 mChoiceItems[2] = new ChoiceItem(
936 context.getString(R.string.dialer_addAnotherCall),
937 BitmapFactory.decodeResource(context.getResources(),
938 R.drawable.ic_dialer_fork_add_call),
939 DIALPAD_CHOICE_ADD_NEW_CALL);
940 }
941
942 public int getCount() {
943 return NUM_ITEMS;
944 }
945
946 /**
947 * Return the ChoiceItem for a given position.
948 */
949 public Object getItem(int position) {
950 return mChoiceItems[position];
951 }
952
953 /**
954 * Return a unique ID for each possible choice.
955 */
956 public long getItemId(int position) {
957 return position;
958 }
959
960 /**
961 * Make a view for each row.
962 */
963 public View getView(int position, View convertView, ViewGroup parent) {
964 // When convertView is non-null, we can reuse it (there's no need
965 // to reinflate it.)
966 if (convertView == null) {
967 convertView = mInflater.inflate(R.layout.dialpad_chooser_list_item, null);
968 }
969
970 TextView text = (TextView) convertView.findViewById(R.id.text);
971 text.setText(mChoiceItems[position].text);
972
973 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
974 icon.setImageBitmap(mChoiceItems[position].icon);
975
976 return convertView;
977 }
978 }
979
980 /**
981 * Handle clicks from the dialpad chooser.
982 */
983 public void onItemClick(AdapterView parent, View v, int position, long id) {
984 DialpadChooserAdapter.ChoiceItem item =
985 (DialpadChooserAdapter.ChoiceItem) parent.getItemAtPosition(position);
986 int itemId = item.id;
987 switch (itemId) {
988 case DialpadChooserAdapter.DIALPAD_CHOICE_USE_DTMF_DIALPAD:
989 // Log.i(TAG, "DIALPAD_CHOICE_USE_DTMF_DIALPAD");
990 // Fire off an intent to go back to the in-call UI
991 // with the dialpad visible.
992 returnToInCallScreen(true);
993 break;
994
995 case DialpadChooserAdapter.DIALPAD_CHOICE_RETURN_TO_CALL:
996 // Log.i(TAG, "DIALPAD_CHOICE_RETURN_TO_CALL");
997 // Fire off an intent to go back to the in-call UI
998 // (with the dialpad hidden).
999 returnToInCallScreen(false);
1000 break;
1001
1002 case DialpadChooserAdapter.DIALPAD_CHOICE_ADD_NEW_CALL:
1003 // Log.i(TAG, "DIALPAD_CHOICE_ADD_NEW_CALL");
1004 // Ok, guess the user really did want to be here (in the
1005 // regular Dialer) after all. Bring back the normal Dialer UI.
1006 showDialpadChooser(false);
1007 break;
1008
1009 default:
1010 Log.w(TAG, "onItemClick: unexpected itemId: " + itemId);
1011 break;
1012 }
1013 }
1014
1015 /**
1016 * Returns to the in-call UI (where there's presumably a call in
1017 * progress) in response to the user selecting "use touch tone keypad"
1018 * or "return to call" from the dialpad chooser.
1019 */
1020 private void returnToInCallScreen(boolean showDialpad) {
1021 try {
1022 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1023 if (phone != null) phone.showCallScreenWithDialpad(showDialpad);
1024 } catch (RemoteException e) {
1025 Log.w(TAG, "phone.showCallScreenWithDialpad() failed", e);
1026 }
1027
1028 // Finally, finish() ourselves so that we don't stay on the
1029 // activity stack.
1030 // Note that we do this whether or not the showCallScreenWithDialpad()
1031 // call above had any effect or not! (That call is a no-op if the
1032 // phone is idle, which can happen if the current call ends while
1033 // the dialpad chooser is up. In this case we can't show the
1034 // InCallScreen, and there's no point staying here in the Dialer,
1035 // so we just take the user back where he came from...)
1036 finish();
1037 }
1038
1039 /**
1040 * @return true if the phone is "in use", meaning that at least one line
1041 * is active (ie. off hook or ringing or dialing).
1042 */
1043 private boolean phoneIsInUse() {
1044 boolean phoneInUse = false;
1045 try {
1046 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1047 if (phone != null) phoneInUse = !phone.isIdle();
1048 } catch (RemoteException e) {
1049 Log.w(TAG, "phone.isIdle() failed", e);
1050 }
1051 return phoneInUse;
1052 }
David Brownc29c7ab2009-07-07 16:00:18 -07001053
1054 /**
Paul Bermandbdcde22009-10-09 12:04:10 -04001055 * @return true if the phone is a CDMA phone type
1056 */
1057 private boolean phoneIsCdma() {
1058 boolean isCdma = false;
1059 try {
1060 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1061 if (phone != null) {
1062 isCdma = (phone.getActivePhoneType() == TelephonyManager.PHONE_TYPE_CDMA);
1063 }
1064 } catch (RemoteException e) {
1065 Log.w(TAG, "phone.getActivePhoneType() failed", e);
1066 }
1067 return isCdma;
1068 }
1069
1070 /**
1071 * @return true if the phone state is OFFHOOK
1072 */
1073 private boolean phoneIsOffhook() {
1074 boolean phoneOffhook = false;
1075 try {
1076 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1077 if (phone != null) phoneOffhook = phone.isOffhook();
1078 } catch (RemoteException e) {
1079 Log.w(TAG, "phone.isOffhook() failed", e);
1080 }
1081 return phoneOffhook;
1082 }
1083
Reli Talc2a2a512009-06-10 16:48:00 -04001084
1085 /**
1086 * Returns true whenever any one of the options from the menu is selected.
1087 * Code changes to support dialpad options
1088 */
1089 @Override
1090 public boolean onOptionsItemSelected(MenuItem item) {
1091 switch (item.getItemId()) {
1092 case MENU_2S_PAUSE:
1093 updateDialString(",");
1094 return true;
1095 case MENU_WAIT:
1096 updateDialString(";");
1097 return true;
1098 }
1099 return false;
1100 }
1101
1102 /**
1103 * Updates the dial string (mDigits) after inserting a Pause character (,)
1104 * or Wait character (;).
1105 */
1106 private void updateDialString(String newDigits) {
1107 int selectionStart;
1108 int selectionEnd;
1109
1110 // SpannableStringBuilder editable_text = new SpannableStringBuilder(mDigits.getText());
Eric Fischer686782e2009-09-10 17:57:45 -07001111 int anchor = mDigits.getSelectionStart();
1112 int point = mDigits.getSelectionEnd();
1113
1114 selectionStart = Math.min(anchor, point);
1115 selectionEnd = Math.max(anchor, point);
Reli Talc2a2a512009-06-10 16:48:00 -04001116
1117 Editable digits = mDigits.getText();
1118 if (selectionStart != -1 ) {
1119 if (selectionStart == selectionEnd) {
1120 // then there is no selection. So insert the pause at this
1121 // position and update the mDigits.
1122 digits.replace(selectionStart, selectionStart, newDigits);
1123 } else {
Eric Fischer1e2d3a22009-09-17 10:53:10 -07001124 digits.replace(selectionStart, selectionEnd, newDigits);
Nicolas Catania7edbd0c2009-09-28 20:37:33 -07001125 // Unselect: back to a regular cursor, just pass the character inserted.
1126 mDigits.setSelection(selectionStart + 1);
Reli Talc2a2a512009-06-10 16:48:00 -04001127 }
1128 } else {
1129 int len = mDigits.length();
1130 digits.replace(len, len, newDigits);
1131 }
1132 }
1133
1134 /**
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001135 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001136 */
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001137 private void updateDialAndDeleteButtonEnabledState() {
1138 final boolean digitsNotEmpty = !isDigitsEmpty();
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001139
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001140 if (mDialButton != null) {
David Browndc1dfe22010-03-01 14:34:57 -08001141 // On CDMA phones, if we're already on a call, we *always*
1142 // enable the Dial button (since you can press it without
1143 // entering any digits to send an empty flash.)
1144 if (phoneIsCdma() && phoneIsOffhook()) {
1145 mDialButton.setEnabled(true);
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001146 } else {
David Browndc1dfe22010-03-01 14:34:57 -08001147 // Common case: GSM, or CDMA but not on a call.
1148 // Enable the Dial button if some digits have
1149 // been entered, or if there is a last dialed number
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001150 // that could be redialed.
1151 mDialButton.setEnabled(digitsNotEmpty ||
1152 !TextUtils.isEmpty(mLastNumberDialed));
Paul Bermandbdcde22009-10-09 12:04:10 -04001153 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001154 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001155 mDelete.setEnabled(digitsNotEmpty);
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001156 }
1157
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001158
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001159 /**
Nicolas Catania80bda0f2009-09-19 09:17:14 -07001160 * Check if voicemail is enabled/accessible.
1161 */
1162 private void initVoicemailButton() {
1163 boolean hasVoicemail = false;
1164 try {
1165 hasVoicemail = TelephonyManager.getDefault().getVoiceMailNumber() != null;
1166 } catch (SecurityException se) {
1167 // Possibly no READ_PHONE_STATE privilege.
1168 }
1169
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001170 mVoicemailButton = mVoicemailDialAndDeleteRow.findViewById(R.id.voicemailButton);
Nicolas Catania80bda0f2009-09-19 09:17:14 -07001171 if (hasVoicemail) {
1172 mVoicemailButton.setOnClickListener(this);
1173 } else {
1174 mVoicemailButton.setEnabled(false);
1175 }
1176 }
1177
1178 /**
Reli Talc2a2a512009-06-10 16:48:00 -04001179 * This function return true if Wait menu item can be shown
1180 * otherwise returns false. Assumes the passed string is non-empty
1181 * and the 0th index check is not required.
1182 */
1183 private boolean showWait(int start, int end, String digits) {
1184 if (start == end) {
1185 // visible false in this case
1186 if (start > digits.length()) return false;
1187
1188 // preceding char is ';', so visible should be false
1189 if (digits.charAt(start-1) == ';') return false;
1190
1191 // next char is ';', so visible should be false
1192 if ((digits.length() > start) && (digits.charAt(start) == ';')) return false;
1193 } else {
1194 // visible false in this case
1195 if (start > digits.length() || end > digits.length()) return false;
1196
1197 // In this case we need to just check for ';' preceding to start
1198 // or next to end
1199 if (digits.charAt(start-1) == ';') return false;
1200 }
1201 return true;
1202 }
Nicolas Cataniabe8821e2010-01-15 09:28:13 -08001203
1204 /**
1205 * @return true if the widget with the phone number digits is empty.
1206 */
1207 private boolean isDigitsEmpty() {
Nicolas Catania941b76f2010-01-19 14:09:40 -08001208 return mDigits.length() == 0;
Nicolas Cataniabe8821e2010-01-15 09:28:13 -08001209 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001210
1211 /**
1212 * Starts the asyn query to get the last dialed/outgoing
1213 * number. When the background query finishes, mLastNumberDialed
1214 * is set to the last dialed number or an empty string if none
1215 * exists yet.
1216 */
1217 private void queryLastOutgoingCall() {
1218 mLastNumberDialed = EMPTY_NUMBER;
1219 CallLogAsync.GetLastOutgoingCallArgs lastCallArgs =
1220 new CallLogAsync.GetLastOutgoingCallArgs(
1221 this,
1222 new CallLogAsync.OnLastOutgoingCallComplete() {
1223 public void lastOutgoingCall(String number) {
1224 // TODO: Filter out emergency numbers if
1225 // the carrier does not want redial for
1226 // these.
1227 mLastNumberDialed = number;
1228 updateDialAndDeleteButtonEnabledState();
1229 }
1230 });
1231 mCallLog.getLastOutgoingCall(lastCallArgs);
1232 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -08001233
1234 @Override
1235 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
1236 boolean globalSearch) {
1237 if (globalSearch) {
1238 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1239 } else {
1240 ContactsSearchManager.startSearch(this, initialQuery);
1241 }
1242 }
Nicolas Cataniae504f6d2010-05-21 14:06:39 -07001243
1244 // Helpers for the call intents.
1245 private Intent newVoicemailIntent() {
1246 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1247 Uri.fromParts("voicemail", EMPTY_NUMBER, null));
1248 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1249 return intent;
1250 }
1251
1252 private Intent newFlashIntent() {
1253 final Intent intent = newDialNumberIntent(EMPTY_NUMBER);
1254 intent.putExtra(EXTRA_SEND_EMPTY_FLASH, true);
1255 return intent;
1256 }
1257
1258 private Intent newDialNumberIntent(String number) {
1259 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1260 Uri.fromParts("tel", number, null));
1261 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1262 return intent;
1263 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001264}