blob: b9a65aed4666dfe0f1b274457b45ec2be77de777 [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() {
Bai Taoba344222010-07-28 17:50:23 -0700271 mDigits.addTextChangedListener(
272 new PhoneNumberFormattingTextWatcher(ContactsUtils.getCurrentCountryIso(this)));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800273 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700274
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800275 /**
Eric Laurentd9efc872009-07-17 11:52:06 -0700276 * Overridden by subclasses to control the resource used by the content view.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800277 */
278 protected int getContentViewResource() {
279 return R.layout.twelve_key_dialer;
280 }
281
282 private boolean resolveIntent() {
283 boolean ignoreState = false;
284
285 // Find the proper intent
286 final Intent intent;
287 if (isChild()) {
288 intent = getParent().getIntent();
289 ignoreState = intent.getBooleanExtra(DialtactsActivity.EXTRA_IGNORE_STATE, false);
290 } else {
291 intent = getIntent();
292 }
293 // Log.i(TAG, "==> resolveIntent(): intent: " + intent);
294
295 // by default we are not adding a call.
296 mIsAddCallMode = false;
297
298 // By default we don't show the "dialpad chooser" UI.
299 boolean needToShowDialpadChooser = false;
300
301 // Resolve the intent
302 final String action = intent.getAction();
303 if (Intent.ACTION_DIAL.equals(action) || Intent.ACTION_VIEW.equals(action)) {
304 // see if we are "adding a call" from the InCallScreen; false by default.
305 mIsAddCallMode = intent.getBooleanExtra(ADD_CALL_MODE_KEY, false);
David Brown4dd082b2010-08-11 15:46:59 -0700306
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800307 Uri uri = intent.getData();
308 if (uri != null) {
309 if ("tel".equals(uri.getScheme())) {
310 // Put the requested number into the input area
Virgil Kingd8831122010-03-10 13:44:11 -0800311 String data = uri.getSchemeSpecificPart();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800312 setFormattedDigits(data);
313 } else {
314 String type = intent.getType();
315 if (People.CONTENT_ITEM_TYPE.equals(type)
316 || Phones.CONTENT_ITEM_TYPE.equals(type)) {
317 // Query the phone number
318 Cursor c = getContentResolver().query(intent.getData(),
319 new String[] {PhonesColumns.NUMBER}, null, null, null);
320 if (c != null) {
321 if (c.moveToFirst()) {
322 // Put the number into the input area
323 setFormattedDigits(c.getString(0));
324 }
325 c.close();
326 }
327 }
328 }
Bernd Holzhey7ca5e4d2010-08-04 17:26:03 +0200329 } else {
David Brown4dd082b2010-08-11 15:46:59 -0700330 // ACTION_DIAL or ACTION_VIEW with no data.
331 // This behaves basically like ACTION_MAIN: If there's
332 // already an active call, bring up an intermediate UI to
333 // make the user confirm what they really want to do.
334 // Be sure *not* to show the dialpad chooser if this is an
335 // explicit "Add call" action, though.
336 if (!mIsAddCallMode && phoneIsInUse()) {
337 needToShowDialpadChooser = true;
338 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800339 }
340 } else if (Intent.ACTION_MAIN.equals(action)) {
341 // The MAIN action means we're bringing up a blank dialer
342 // (e.g. by selecting the Home shortcut, or tabbing over from
343 // Contacts or Call log.)
344 //
345 // At this point, IF there's already an active call, there's a
346 // good chance that the user got here accidentally (but really
347 // wanted the in-call dialpad instead). So we bring up an
348 // intermediate UI to make the user confirm what they really
349 // want to do.
350 if (phoneIsInUse()) {
351 // Log.i(TAG, "resolveIntent(): phone is in use; showing dialpad chooser!");
352 needToShowDialpadChooser = true;
353 }
354 }
355
356 // Bring up the "dialpad chooser" IFF we need to make the user
357 // confirm which dialpad they really want.
358 showDialpadChooser(needToShowDialpadChooser);
359
360 return ignoreState;
361 }
362
363 protected void setFormattedDigits(String data) {
364 // strip the non-dialable numbers out of the data string.
365 String dialString = PhoneNumberUtils.extractNetworkPortion(data);
366 dialString = PhoneNumberUtils.formatNumber(dialString);
367 if (!TextUtils.isEmpty(dialString)) {
368 Editable digits = mDigits.getText();
369 digits.replace(0, digits.length(), dialString);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700370 // for some reason this isn't getting called in the digits.replace call above..
371 // but in any case, this will make sure the background drawable looks right
372 afterTextChanged(digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800373 }
374 }
375
376 @Override
377 protected void onNewIntent(Intent newIntent) {
378 setIntent(newIntent);
379 resolveIntent();
380 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700381
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800382 @Override
383 protected void onPostCreate(Bundle savedInstanceState) {
384 super.onPostCreate(savedInstanceState);
385
386 // This can't be done in onCreate(), since the auto-restoring of the digits
387 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
388 // is called. This method will be called every time the activity is created, and
389 // will always happen after onRestoreSavedInstanceState().
390 mDigits.addTextChangedListener(this);
391 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700392
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800393 private void setupKeypad() {
394 // Setup the listeners for the buttons
395 View view = findViewById(R.id.one);
396 view.setOnClickListener(this);
397 view.setOnLongClickListener(this);
398
399 findViewById(R.id.two).setOnClickListener(this);
400 findViewById(R.id.three).setOnClickListener(this);
401 findViewById(R.id.four).setOnClickListener(this);
402 findViewById(R.id.five).setOnClickListener(this);
403 findViewById(R.id.six).setOnClickListener(this);
404 findViewById(R.id.seven).setOnClickListener(this);
405 findViewById(R.id.eight).setOnClickListener(this);
406 findViewById(R.id.nine).setOnClickListener(this);
407 findViewById(R.id.star).setOnClickListener(this);
408
409 view = findViewById(R.id.zero);
410 view.setOnClickListener(this);
411 view.setOnLongClickListener(this);
412
413 findViewById(R.id.pound).setOnClickListener(this);
414 }
415
416 @Override
417 protected void onResume() {
418 super.onResume();
David Browndc1dfe22010-03-01 14:34:57 -0800419
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800420 // Query the last dialed number. Do it first because hitting
421 // the DB is 'slow'. This call is asynchronous.
422 queryLastOutgoingCall();
David Brownc29c7ab2009-07-07 16:00:18 -0700423
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800424 // retrieve the DTMF tone play back setting.
425 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
426 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
427
Nicolas Catania905e7622009-12-01 08:51:20 -0800428 // Retrieve the haptic feedback setting.
429 mHaptic.checkSystemSetting();
430
Eric Laurentd9efc872009-07-17 11:52:06 -0700431 // if the mToneGenerator creation fails, just continue without it. It is
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800432 // a local audio signal, and is not as important as the dtmf tone itself.
433 synchronized(mToneGeneratorLock) {
434 if (mToneGenerator == null) {
435 try {
Jean-Michel Trividd44f8c2009-11-10 13:00:45 -0800436 // we want the user to be able to control the volume of the dial tones
437 // outside of a call, so we use the stream type that is also mapped to the
438 // volume control keys for this activity
439 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
440 setVolumeControlStream(DIAL_TONE_STREAM_TYPE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800441 } catch (RuntimeException e) {
442 Log.w(TAG, "Exception caught while creating local tone generator: " + e);
443 mToneGenerator = null;
444 }
445 }
446 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700447
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800448 Activity parent = getParent();
449 // See if we were invoked with a DIAL intent. If we were, fill in the appropriate
450 // digits in the dialer field.
451 if (parent != null && parent instanceof DialtactsActivity) {
452 Uri dialUri = ((DialtactsActivity) parent).getAndClearDialUri();
453 if (dialUri != null) {
454 resolveIntent();
455 }
456 }
457
458 // While we're in the foreground, listen for phone state changes,
459 // purely so that we can take down the "dialpad chooser" if the
460 // phone becomes idle while the chooser UI is visible.
461 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
462 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
463
464 // Potentially show hint text in the mDigits field when the user
465 // hasn't typed any digits yet. (If there's already an active call,
466 // this hint text will remind the user that he's about to add a new
467 // call.)
468 //
469 // TODO: consider adding better UI for the case where *both* lines
470 // are currently in use. (Right now we let the user try to add
471 // another call, but that call is guaranteed to fail. Perhaps the
472 // entire dialer UI should be disabled instead.)
473 if (phoneIsInUse()) {
474 mDigits.setHint(R.string.dialerDialpadHintText);
475 } else {
476 // Common case; no hint necessary.
477 mDigits.setHint(null);
478
479 // Also, a sanity-check: the "dialpad chooser" UI should NEVER
480 // be visible if the phone is idle!
481 showDialpadChooser(false);
482 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700483
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800484 updateDialAndDeleteButtonEnabledState();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800485 }
486
487 @Override
Karl Rosaenf46bc312009-03-24 18:20:48 -0700488 public void onWindowFocusChanged(boolean hasFocus) {
489 if (hasFocus) {
490 // Hide soft keyboard, if visible (it's fugly over button dialer).
491 // The only known case where this will be true is when launching the dialer with
492 // ACTION_DIAL via a soft keyboard. we dismiss it here because we don't
493 // have a window token yet in onCreate / onNewIntent
494 InputMethodManager inputMethodManager = (InputMethodManager)
495 getSystemService(Context.INPUT_METHOD_SERVICE);
Eric Laurentd9efc872009-07-17 11:52:06 -0700496 inputMethodManager.hideSoftInputFromWindow(mDigits.getWindowToken(), 0);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700497 }
498 }
499
500 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 protected void onPause() {
502 super.onPause();
503
504 // Stop listening for phone state changes.
505 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
506 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
507
508 synchronized(mToneGeneratorLock) {
509 if (mToneGenerator != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800510 mToneGenerator.release();
511 mToneGenerator = null;
512 }
513 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800514 // TODO: I wonder if we should not check if the AsyncTask that
515 // lookup the last dialed number has completed.
516 mLastNumberDialed = EMPTY_NUMBER; // Since we are going to query again, free stale number.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800517 }
518
519 @Override
520 public boolean onCreateOptionsMenu(Menu menu) {
Reli Talc2a2a512009-06-10 16:48:00 -0400521 mAddToContactMenuItem = menu.add(0, MENU_ADD_CONTACTS, 0, R.string.recentCalls_addToContact)
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800522 .setIcon(android.R.drawable.ic_menu_add);
Reli Talc2a2a512009-06-10 16:48:00 -0400523 m2SecPauseMenuItem = menu.add(0, MENU_2S_PAUSE, 0, R.string.add_2sec_pause)
524 .setIcon(R.drawable.ic_menu_2sec_pause);
525 mWaitMenuItem = menu.add(0, MENU_WAIT, 0, R.string.add_wait)
526 .setIcon(R.drawable.ic_menu_wait);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800527 return true;
528 }
529
530 @Override
531 public boolean onPrepareOptionsMenu(Menu menu) {
532 // We never show a menu if the "choose dialpad" UI is up.
533 if (dialpadChooserVisible()) {
534 return false;
535 }
536
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800537 if (isDigitsEmpty()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800538 mAddToContactMenuItem.setVisible(false);
Reli Talc2a2a512009-06-10 16:48:00 -0400539 m2SecPauseMenuItem.setVisible(false);
540 mWaitMenuItem.setVisible(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800541 } else {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800542 CharSequence digits = mDigits.getText();
543
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800544 // Put the current digits string into an intent
545 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800546 intent.putExtra(Insert.PHONE, digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800547 intent.setType(People.CONTENT_ITEM_TYPE);
548 mAddToContactMenuItem.setIntent(intent);
549 mAddToContactMenuItem.setVisible(true);
Reli Talc2a2a512009-06-10 16:48:00 -0400550
551 // Check out whether to show Pause & Wait option menu items
552 int selectionStart;
553 int selectionEnd;
554 String strDigits = digits.toString();
555
556 selectionStart = mDigits.getSelectionStart();
557 selectionEnd = mDigits.getSelectionEnd();
558
559 if (selectionStart != -1) {
560 if (selectionStart > selectionEnd) {
561 // swap it as we want start to be less then end
562 int tmp = selectionStart;
563 selectionStart = selectionEnd;
564 selectionEnd = tmp;
565 }
566
567 if (selectionStart != 0) {
568 // Pause can be visible if cursor is not in the begining
569 m2SecPauseMenuItem.setVisible(true);
570
571 // For Wait to be visible set of condition to meet
572 mWaitMenuItem.setVisible(showWait(selectionStart,
573 selectionEnd, strDigits));
574 } else {
575 // cursor in the beginning both pause and wait to be invisible
576 m2SecPauseMenuItem.setVisible(false);
577 mWaitMenuItem.setVisible(false);
578 }
579 } else {
580 // cursor is not selected so assume new digit is added to the end
581 int strLength = strDigits.length();
582 mWaitMenuItem.setVisible(showWait(strLength,
583 strLength, strDigits));
584 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800585 }
586 return true;
587 }
588
589 @Override
590 public boolean onKeyDown(int keyCode, KeyEvent event) {
591 switch (keyCode) {
592 case KeyEvent.KEYCODE_CALL: {
593 long callPressDiff = SystemClock.uptimeMillis() - event.getDownTime();
594 if (callPressDiff >= ViewConfiguration.getLongPressTimeout()) {
595 // Launch voice dialer
596 Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND);
597 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
598 try {
599 startActivity(intent);
600 } catch (ActivityNotFoundException e) {
601 }
602 }
603 return true;
604 }
605 case KeyEvent.KEYCODE_1: {
Eric Laurentd9efc872009-07-17 11:52:06 -0700606 long timeDiff = SystemClock.uptimeMillis() - event.getDownTime();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800607 if (timeDiff >= ViewConfiguration.getLongPressTimeout()) {
608 // Long press detected, call voice mail
609 callVoicemail();
610 }
611 return true;
612 }
613 }
614 return super.onKeyDown(keyCode, event);
615 }
616
617 @Override
618 public boolean onKeyUp(int keyCode, KeyEvent event) {
619 switch (keyCode) {
620 case KeyEvent.KEYCODE_CALL: {
Nicolas Catania998763d2010-01-14 14:03:53 -0800621 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800622 return true;
623 }
624 }
625 return super.onKeyUp(keyCode, event);
626 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700627
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800628 private void keyPressed(int keyCode) {
Nicolas Catania905e7622009-12-01 08:51:20 -0800629 mHaptic.vibrate();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800630 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
631 mDigits.onKeyDown(keyCode, event);
632 }
633
634 public boolean onKey(View view, int keyCode, KeyEvent event) {
635 switch (view.getId()) {
636 case R.id.digits:
637 if (keyCode == KeyEvent.KEYCODE_ENTER) {
Nicolas Catania998763d2010-01-14 14:03:53 -0800638 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800639 return true;
640 }
641 break;
642 }
643 return false;
644 }
645
646 public void onClick(View view) {
647 switch (view.getId()) {
648 case R.id.one: {
649 playTone(ToneGenerator.TONE_DTMF_1);
650 keyPressed(KeyEvent.KEYCODE_1);
651 return;
652 }
653 case R.id.two: {
654 playTone(ToneGenerator.TONE_DTMF_2);
655 keyPressed(KeyEvent.KEYCODE_2);
656 return;
657 }
658 case R.id.three: {
659 playTone(ToneGenerator.TONE_DTMF_3);
660 keyPressed(KeyEvent.KEYCODE_3);
661 return;
662 }
663 case R.id.four: {
664 playTone(ToneGenerator.TONE_DTMF_4);
665 keyPressed(KeyEvent.KEYCODE_4);
666 return;
667 }
668 case R.id.five: {
669 playTone(ToneGenerator.TONE_DTMF_5);
670 keyPressed(KeyEvent.KEYCODE_5);
671 return;
672 }
673 case R.id.six: {
674 playTone(ToneGenerator.TONE_DTMF_6);
675 keyPressed(KeyEvent.KEYCODE_6);
676 return;
677 }
678 case R.id.seven: {
679 playTone(ToneGenerator.TONE_DTMF_7);
680 keyPressed(KeyEvent.KEYCODE_7);
681 return;
682 }
683 case R.id.eight: {
684 playTone(ToneGenerator.TONE_DTMF_8);
685 keyPressed(KeyEvent.KEYCODE_8);
686 return;
687 }
688 case R.id.nine: {
689 playTone(ToneGenerator.TONE_DTMF_9);
690 keyPressed(KeyEvent.KEYCODE_9);
691 return;
692 }
693 case R.id.zero: {
694 playTone(ToneGenerator.TONE_DTMF_0);
695 keyPressed(KeyEvent.KEYCODE_0);
696 return;
697 }
698 case R.id.pound: {
699 playTone(ToneGenerator.TONE_DTMF_P);
700 keyPressed(KeyEvent.KEYCODE_POUND);
701 return;
702 }
703 case R.id.star: {
704 playTone(ToneGenerator.TONE_DTMF_S);
705 keyPressed(KeyEvent.KEYCODE_STAR);
706 return;
707 }
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700708 case R.id.deleteButton: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800709 keyPressed(KeyEvent.KEYCODE_DEL);
710 return;
711 }
Nicolas Catania3040fa32009-10-01 13:00:53 -0700712 case R.id.dialButton: {
Nicolas Catania905e7622009-12-01 08:51:20 -0800713 mHaptic.vibrate(); // Vibrate here too, just like we do for the regular keys
Nicolas Catania998763d2010-01-14 14:03:53 -0800714 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800715 return;
716 }
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700717 case R.id.voicemailButton: {
718 callVoicemail();
Nicolas Catania905e7622009-12-01 08:51:20 -0800719 mHaptic.vibrate();
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700720 return;
721 }
Nicolas Catania3040fa32009-10-01 13:00:53 -0700722 case R.id.digits: {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800723 if (!isDigitsEmpty()) {
Nicolas Catania3040fa32009-10-01 13:00:53 -0700724 mDigits.setCursorVisible(true);
725 }
726 return;
727 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800728 }
729 }
730
731 public boolean onLongClick(View view) {
732 final Editable digits = mDigits.getText();
733 int id = view.getId();
734 switch (id) {
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700735 case R.id.deleteButton: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800736 digits.clear();
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700737 // TODO: The framework forgets to clear the pressed
738 // status of disabled button. Until this is fixed,
739 // clear manually the pressed status. b/2133127
740 mDelete.setPressed(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800741 return true;
742 }
743 case R.id.one: {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800744 if (isDigitsEmpty()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800745 callVoicemail();
746 return true;
747 }
748 return false;
749 }
750 case R.id.zero: {
751 keyPressed(KeyEvent.KEYCODE_PLUS);
752 return true;
753 }
754 }
755 return false;
756 }
757
758 void callVoicemail() {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700759 startActivity(newVoicemailIntent());
760 mDigits.getText().clear(); // TODO: Fix bug 1745781
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800761 finish();
762 }
763
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700764 /**
765 * In most cases, when the dial button is pressed, there is a
766 * number in digits area. Pack it in the intent, start the
767 * outgoing call broadcast as a separate task and finish this
768 * activity.
769 *
770 * When there is no digit and the phone is CDMA and off hook,
771 * we're sending a blank flash for CDMA. CDMA networks use Flash
772 * messages when special processing needs to be done, mainly for
773 * 3-way or call waiting scenarios. Presumably, here we're in a
774 * special 3-way scenario where the network needs a blank flash
775 * before being able to add the new participant. (This is not the
776 * case with all 3-way calls, just certain CDMA infrastructures.)
777 *
778 * Otherwise, there is no digit, display the last dialed
779 * number. Don't finish since the user may want to edit it. The
780 * user needs to press the dial button again, to dial it (general
781 * case described above).
782 */
Nicolas Catania998763d2010-01-14 14:03:53 -0800783 void dialButtonPressed() {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700784 if (isDigitsEmpty()) { // No number entered.
Paul Bermandbdcde22009-10-09 12:04:10 -0400785 if (phoneIsCdma() && phoneIsOffhook()) {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700786 // This is really CDMA specific. On GSM is it possible
787 // to be off hook and wanted to add a 3rd party using
788 // the redial feature.
789 startActivity(newFlashIntent());
Paul Bermandbdcde22009-10-09 12:04:10 -0400790 } else {
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700791 if (!TextUtils.isEmpty(mLastNumberDialed)) {
792 mDigits.setText(mLastNumberDialed);
793 } else {
794 // There's no "last number dialed" or the
795 // background query is still running. There's
796 // nothing useful for the Dial button to do in
797 // this case. Note: with a soft dial button, this
798 // can never happens since the dial button is
799 // disabled under these conditons.
800 playTone(ToneGenerator.TONE_PROP_NACK);
801 }
Paul Bermandbdcde22009-10-09 12:04:10 -0400802 }
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700803 } else {
804 final String number = mDigits.getText().toString();
Nicolas Catania998763d2010-01-14 14:03:53 -0800805
Nicolas Cataniae504f6d2010-05-21 14:06:39 -0700806 startActivity(newDialNumberIntent(number));
807 mDigits.getText().clear(); // TODO: Fix bug 1745781
Paul Bermandbdcde22009-10-09 12:04:10 -0400808 finish();
809 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800810 }
811
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800812
813 /**
David Brown22f615f2009-06-25 16:19:19 -0700814 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
815 *
816 * The tone is played locally, using the audio stream for phone calls.
817 * Tones are played only if the "Audible touch tones" user preference
818 * is checked, and are NOT played if the device is in silent mode.
819 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800820 * @param tone a tone code from {@link ToneGenerator}
821 */
822 void playTone(int tone) {
823 // if local tone playback is disabled, just return.
824 if (!mDTMFToneEnabled) {
825 return;
826 }
David Brown22f615f2009-06-25 16:19:19 -0700827
828 // Also do nothing if the phone is in silent mode.
829 // We need to re-check the ringer mode for *every* playTone()
830 // call, rather than keeping a local flag that's updated in
831 // onResume(), since it's possible to toggle silent mode without
832 // leaving the current activity (via the ENDCALL-longpress menu.)
833 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
David Brownd5a15302009-07-20 16:39:47 -0700834 int ringerMode = audioManager.getRingerMode();
835 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
836 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
David Brown22f615f2009-06-25 16:19:19 -0700837 return;
838 }
839
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800840 synchronized(mToneGeneratorLock) {
841 if (mToneGenerator == null) {
842 Log.w(TAG, "playTone: mToneGenerator == null, tone: "+tone);
843 return;
844 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700845
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800846 // Start the new tone (will stop any playing tone)
Eric Laurent8487fed2009-09-07 08:45:14 -0700847 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800848 }
849 }
850
851 /**
852 * Brings up the "dialpad chooser" UI in place of the usual Dialer
853 * elements (the textfield/button and the dialpad underneath).
854 *
855 * We show this UI if the user brings up the Dialer while a call is
856 * already in progress, since there's a good chance we got here
857 * accidentally (and the user really wanted the in-call dialpad instead).
858 * So in this situation we display an intermediate UI that lets the user
859 * explicitly choose between the in-call dialpad ("Use touch tone
860 * keypad") and the regular Dialer ("Add call"). (Or, the option "Return
861 * to call in progress" just goes back to the in-call UI with no dialpad
862 * at all.)
863 *
864 * @param enabled If true, show the "dialpad chooser" instead
865 * of the regular Dialer UI
866 */
867 private void showDialpadChooser(boolean enabled) {
868 if (enabled) {
869 // Log.i(TAG, "Showing dialpad chooser!");
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700870 mDigits.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800871 if (mDialpad != null) mDialpad.setVisibility(View.GONE);
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700872 mVoicemailDialAndDeleteRow.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800873 mDialpadChooser.setVisibility(View.VISIBLE);
874
875 // Instantiate the DialpadChooserAdapter and hook it up to the
876 // ListView. We do this only once.
877 if (mDialpadChooserAdapter == null) {
878 mDialpadChooserAdapter = new DialpadChooserAdapter(this);
879 mDialpadChooser.setAdapter(mDialpadChooserAdapter);
880 }
881 } else {
882 // Log.i(TAG, "Displaying normal Dialer UI.");
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700883 mDigits.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800884 if (mDialpad != null) mDialpad.setVisibility(View.VISIBLE);
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700885 mVoicemailDialAndDeleteRow.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800886 mDialpadChooser.setVisibility(View.GONE);
887 }
888 }
889
890 /**
891 * @return true if we're currently showing the "dialpad chooser" UI.
892 */
893 private boolean dialpadChooserVisible() {
894 return mDialpadChooser.getVisibility() == View.VISIBLE;
895 }
896
897 /**
898 * Simple list adapter, binding to an icon + text label
899 * for each item in the "dialpad chooser" list.
900 */
901 private static class DialpadChooserAdapter extends BaseAdapter {
902 private LayoutInflater mInflater;
903
904 // Simple struct for a single "choice" item.
905 static class ChoiceItem {
906 String text;
907 Bitmap icon;
908 int id;
909
910 public ChoiceItem(String s, Bitmap b, int i) {
911 text = s;
912 icon = b;
913 id = i;
914 }
915 }
916
917 // IDs for the possible "choices":
918 static final int DIALPAD_CHOICE_USE_DTMF_DIALPAD = 101;
919 static final int DIALPAD_CHOICE_RETURN_TO_CALL = 102;
920 static final int DIALPAD_CHOICE_ADD_NEW_CALL = 103;
921
922 private static final int NUM_ITEMS = 3;
923 private ChoiceItem mChoiceItems[] = new ChoiceItem[NUM_ITEMS];
924
925 public DialpadChooserAdapter(Context context) {
926 // Cache the LayoutInflate to avoid asking for a new one each time.
927 mInflater = LayoutInflater.from(context);
928
929 // Initialize the possible choices.
930 // TODO: could this be specified entirely in XML?
931
932 // - "Use touch tone keypad"
933 mChoiceItems[0] = new ChoiceItem(
934 context.getString(R.string.dialer_useDtmfDialpad),
935 BitmapFactory.decodeResource(context.getResources(),
936 R.drawable.ic_dialer_fork_tt_keypad),
937 DIALPAD_CHOICE_USE_DTMF_DIALPAD);
938
939 // - "Return to call in progress"
940 mChoiceItems[1] = new ChoiceItem(
941 context.getString(R.string.dialer_returnToInCallScreen),
942 BitmapFactory.decodeResource(context.getResources(),
943 R.drawable.ic_dialer_fork_current_call),
944 DIALPAD_CHOICE_RETURN_TO_CALL);
945
946 // - "Add call"
947 mChoiceItems[2] = new ChoiceItem(
948 context.getString(R.string.dialer_addAnotherCall),
949 BitmapFactory.decodeResource(context.getResources(),
950 R.drawable.ic_dialer_fork_add_call),
951 DIALPAD_CHOICE_ADD_NEW_CALL);
952 }
953
954 public int getCount() {
955 return NUM_ITEMS;
956 }
957
958 /**
959 * Return the ChoiceItem for a given position.
960 */
961 public Object getItem(int position) {
962 return mChoiceItems[position];
963 }
964
965 /**
966 * Return a unique ID for each possible choice.
967 */
968 public long getItemId(int position) {
969 return position;
970 }
971
972 /**
973 * Make a view for each row.
974 */
975 public View getView(int position, View convertView, ViewGroup parent) {
976 // When convertView is non-null, we can reuse it (there's no need
977 // to reinflate it.)
978 if (convertView == null) {
979 convertView = mInflater.inflate(R.layout.dialpad_chooser_list_item, null);
980 }
981
982 TextView text = (TextView) convertView.findViewById(R.id.text);
983 text.setText(mChoiceItems[position].text);
984
985 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
986 icon.setImageBitmap(mChoiceItems[position].icon);
987
988 return convertView;
989 }
990 }
991
992 /**
993 * Handle clicks from the dialpad chooser.
994 */
995 public void onItemClick(AdapterView parent, View v, int position, long id) {
996 DialpadChooserAdapter.ChoiceItem item =
997 (DialpadChooserAdapter.ChoiceItem) parent.getItemAtPosition(position);
998 int itemId = item.id;
999 switch (itemId) {
1000 case DialpadChooserAdapter.DIALPAD_CHOICE_USE_DTMF_DIALPAD:
1001 // Log.i(TAG, "DIALPAD_CHOICE_USE_DTMF_DIALPAD");
1002 // Fire off an intent to go back to the in-call UI
1003 // with the dialpad visible.
1004 returnToInCallScreen(true);
1005 break;
1006
1007 case DialpadChooserAdapter.DIALPAD_CHOICE_RETURN_TO_CALL:
1008 // Log.i(TAG, "DIALPAD_CHOICE_RETURN_TO_CALL");
1009 // Fire off an intent to go back to the in-call UI
1010 // (with the dialpad hidden).
1011 returnToInCallScreen(false);
1012 break;
1013
1014 case DialpadChooserAdapter.DIALPAD_CHOICE_ADD_NEW_CALL:
1015 // Log.i(TAG, "DIALPAD_CHOICE_ADD_NEW_CALL");
1016 // Ok, guess the user really did want to be here (in the
1017 // regular Dialer) after all. Bring back the normal Dialer UI.
1018 showDialpadChooser(false);
1019 break;
1020
1021 default:
1022 Log.w(TAG, "onItemClick: unexpected itemId: " + itemId);
1023 break;
1024 }
1025 }
1026
1027 /**
1028 * Returns to the in-call UI (where there's presumably a call in
1029 * progress) in response to the user selecting "use touch tone keypad"
1030 * or "return to call" from the dialpad chooser.
1031 */
1032 private void returnToInCallScreen(boolean showDialpad) {
1033 try {
1034 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1035 if (phone != null) phone.showCallScreenWithDialpad(showDialpad);
1036 } catch (RemoteException e) {
1037 Log.w(TAG, "phone.showCallScreenWithDialpad() failed", e);
1038 }
1039
1040 // Finally, finish() ourselves so that we don't stay on the
1041 // activity stack.
1042 // Note that we do this whether or not the showCallScreenWithDialpad()
1043 // call above had any effect or not! (That call is a no-op if the
1044 // phone is idle, which can happen if the current call ends while
1045 // the dialpad chooser is up. In this case we can't show the
1046 // InCallScreen, and there's no point staying here in the Dialer,
1047 // so we just take the user back where he came from...)
1048 finish();
1049 }
1050
1051 /**
1052 * @return true if the phone is "in use", meaning that at least one line
1053 * is active (ie. off hook or ringing or dialing).
1054 */
1055 private boolean phoneIsInUse() {
1056 boolean phoneInUse = false;
1057 try {
1058 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1059 if (phone != null) phoneInUse = !phone.isIdle();
1060 } catch (RemoteException e) {
1061 Log.w(TAG, "phone.isIdle() failed", e);
1062 }
1063 return phoneInUse;
1064 }
David Brownc29c7ab2009-07-07 16:00:18 -07001065
1066 /**
Paul Bermandbdcde22009-10-09 12:04:10 -04001067 * @return true if the phone is a CDMA phone type
1068 */
1069 private boolean phoneIsCdma() {
1070 boolean isCdma = false;
1071 try {
1072 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1073 if (phone != null) {
1074 isCdma = (phone.getActivePhoneType() == TelephonyManager.PHONE_TYPE_CDMA);
1075 }
1076 } catch (RemoteException e) {
1077 Log.w(TAG, "phone.getActivePhoneType() failed", e);
1078 }
1079 return isCdma;
1080 }
1081
1082 /**
1083 * @return true if the phone state is OFFHOOK
1084 */
1085 private boolean phoneIsOffhook() {
1086 boolean phoneOffhook = false;
1087 try {
1088 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1089 if (phone != null) phoneOffhook = phone.isOffhook();
1090 } catch (RemoteException e) {
1091 Log.w(TAG, "phone.isOffhook() failed", e);
1092 }
1093 return phoneOffhook;
1094 }
1095
Reli Talc2a2a512009-06-10 16:48:00 -04001096
1097 /**
1098 * Returns true whenever any one of the options from the menu is selected.
1099 * Code changes to support dialpad options
1100 */
1101 @Override
1102 public boolean onOptionsItemSelected(MenuItem item) {
1103 switch (item.getItemId()) {
1104 case MENU_2S_PAUSE:
1105 updateDialString(",");
1106 return true;
1107 case MENU_WAIT:
1108 updateDialString(";");
1109 return true;
1110 }
1111 return false;
1112 }
1113
1114 /**
1115 * Updates the dial string (mDigits) after inserting a Pause character (,)
1116 * or Wait character (;).
1117 */
1118 private void updateDialString(String newDigits) {
1119 int selectionStart;
1120 int selectionEnd;
1121
1122 // SpannableStringBuilder editable_text = new SpannableStringBuilder(mDigits.getText());
Eric Fischer686782e2009-09-10 17:57:45 -07001123 int anchor = mDigits.getSelectionStart();
1124 int point = mDigits.getSelectionEnd();
1125
1126 selectionStart = Math.min(anchor, point);
1127 selectionEnd = Math.max(anchor, point);
Reli Talc2a2a512009-06-10 16:48:00 -04001128
1129 Editable digits = mDigits.getText();
1130 if (selectionStart != -1 ) {
1131 if (selectionStart == selectionEnd) {
1132 // then there is no selection. So insert the pause at this
1133 // position and update the mDigits.
1134 digits.replace(selectionStart, selectionStart, newDigits);
1135 } else {
Eric Fischer1e2d3a22009-09-17 10:53:10 -07001136 digits.replace(selectionStart, selectionEnd, newDigits);
Nicolas Catania7edbd0c2009-09-28 20:37:33 -07001137 // Unselect: back to a regular cursor, just pass the character inserted.
1138 mDigits.setSelection(selectionStart + 1);
Reli Talc2a2a512009-06-10 16:48:00 -04001139 }
1140 } else {
1141 int len = mDigits.length();
1142 digits.replace(len, len, newDigits);
1143 }
1144 }
1145
1146 /**
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001147 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001148 */
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001149 private void updateDialAndDeleteButtonEnabledState() {
1150 final boolean digitsNotEmpty = !isDigitsEmpty();
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001151
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001152 if (mDialButton != null) {
David Browndc1dfe22010-03-01 14:34:57 -08001153 // On CDMA phones, if we're already on a call, we *always*
1154 // enable the Dial button (since you can press it without
1155 // entering any digits to send an empty flash.)
1156 if (phoneIsCdma() && phoneIsOffhook()) {
1157 mDialButton.setEnabled(true);
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001158 } else {
David Browndc1dfe22010-03-01 14:34:57 -08001159 // Common case: GSM, or CDMA but not on a call.
1160 // Enable the Dial button if some digits have
1161 // been entered, or if there is a last dialed number
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001162 // that could be redialed.
1163 mDialButton.setEnabled(digitsNotEmpty ||
1164 !TextUtils.isEmpty(mLastNumberDialed));
Paul Bermandbdcde22009-10-09 12:04:10 -04001165 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001166 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001167 mDelete.setEnabled(digitsNotEmpty);
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001168 }
1169
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001170
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001171 /**
Nicolas Catania80bda0f2009-09-19 09:17:14 -07001172 * Check if voicemail is enabled/accessible.
1173 */
1174 private void initVoicemailButton() {
1175 boolean hasVoicemail = false;
1176 try {
1177 hasVoicemail = TelephonyManager.getDefault().getVoiceMailNumber() != null;
1178 } catch (SecurityException se) {
1179 // Possibly no READ_PHONE_STATE privilege.
1180 }
1181
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001182 mVoicemailButton = mVoicemailDialAndDeleteRow.findViewById(R.id.voicemailButton);
Nicolas Catania80bda0f2009-09-19 09:17:14 -07001183 if (hasVoicemail) {
1184 mVoicemailButton.setOnClickListener(this);
1185 } else {
1186 mVoicemailButton.setEnabled(false);
1187 }
1188 }
1189
1190 /**
Reli Talc2a2a512009-06-10 16:48:00 -04001191 * This function return true if Wait menu item can be shown
1192 * otherwise returns false. Assumes the passed string is non-empty
1193 * and the 0th index check is not required.
1194 */
1195 private boolean showWait(int start, int end, String digits) {
1196 if (start == end) {
1197 // visible false in this case
1198 if (start > digits.length()) return false;
1199
1200 // preceding char is ';', so visible should be false
1201 if (digits.charAt(start-1) == ';') return false;
1202
1203 // next char is ';', so visible should be false
1204 if ((digits.length() > start) && (digits.charAt(start) == ';')) return false;
1205 } else {
1206 // visible false in this case
1207 if (start > digits.length() || end > digits.length()) return false;
1208
1209 // In this case we need to just check for ';' preceding to start
1210 // or next to end
1211 if (digits.charAt(start-1) == ';') return false;
1212 }
1213 return true;
1214 }
Nicolas Cataniabe8821e2010-01-15 09:28:13 -08001215
1216 /**
1217 * @return true if the widget with the phone number digits is empty.
1218 */
1219 private boolean isDigitsEmpty() {
Nicolas Catania941b76f2010-01-19 14:09:40 -08001220 return mDigits.length() == 0;
Nicolas Cataniabe8821e2010-01-15 09:28:13 -08001221 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001222
1223 /**
1224 * Starts the asyn query to get the last dialed/outgoing
1225 * number. When the background query finishes, mLastNumberDialed
1226 * is set to the last dialed number or an empty string if none
1227 * exists yet.
1228 */
1229 private void queryLastOutgoingCall() {
1230 mLastNumberDialed = EMPTY_NUMBER;
1231 CallLogAsync.GetLastOutgoingCallArgs lastCallArgs =
1232 new CallLogAsync.GetLastOutgoingCallArgs(
1233 this,
1234 new CallLogAsync.OnLastOutgoingCallComplete() {
1235 public void lastOutgoingCall(String number) {
1236 // TODO: Filter out emergency numbers if
1237 // the carrier does not want redial for
1238 // these.
1239 mLastNumberDialed = number;
1240 updateDialAndDeleteButtonEnabledState();
1241 }
1242 });
1243 mCallLog.getLastOutgoingCall(lastCallArgs);
1244 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -08001245
1246 @Override
1247 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
1248 boolean globalSearch) {
1249 if (globalSearch) {
1250 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1251 } else {
1252 ContactsSearchManager.startSearch(this, initialQuery);
1253 }
1254 }
Nicolas Cataniae504f6d2010-05-21 14:06:39 -07001255
1256 // Helpers for the call intents.
1257 private Intent newVoicemailIntent() {
1258 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1259 Uri.fromParts("voicemail", EMPTY_NUMBER, null));
1260 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1261 return intent;
1262 }
1263
1264 private Intent newFlashIntent() {
1265 final Intent intent = newDialNumberIntent(EMPTY_NUMBER);
1266 intent.putExtra(EXTRA_SEND_EMPTY_FLASH, true);
1267 return intent;
1268 }
1269
1270 private Intent newDialNumberIntent(String number) {
1271 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1272 Uri.fromParts("tel", number, null));
1273 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1274 return intent;
1275 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001276}