blob: 1ef3886c31cb8a5a58e7ed7cde28407053b56189 [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;
27import android.content.res.Resources;
28import android.database.Cursor;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
31import android.graphics.drawable.Drawable;
32import android.media.AudioManager;
33import android.media.ToneGenerator;
34import android.net.Uri;
35import android.os.Bundle;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080036import android.os.RemoteException;
37import android.os.ServiceManager;
38import android.os.SystemClock;
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080039import android.provider.Settings;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080040import android.provider.Contacts.People;
41import android.provider.Contacts.Phones;
42import android.provider.Contacts.PhonesColumns;
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080043import android.provider.Contacts.Intents.Insert;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044import android.telephony.PhoneNumberFormattingTextWatcher;
45import android.telephony.PhoneNumberUtils;
46import android.telephony.PhoneStateListener;
47import android.telephony.TelephonyManager;
48import android.text.Editable;
49import android.text.TextUtils;
50import android.text.TextWatcher;
51import android.text.method.DialerKeyListener;
52import android.util.Log;
53import android.view.KeyEvent;
54import android.view.LayoutInflater;
55import android.view.Menu;
56import android.view.MenuItem;
57import android.view.View;
58import android.view.ViewConfiguration;
59import android.view.ViewGroup;
Karl Rosaenf46bc312009-03-24 18:20:48 -070060import android.view.inputmethod.InputMethodManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080061import android.widget.AdapterView;
62import android.widget.BaseAdapter;
63import android.widget.EditText;
64import android.widget.ImageView;
65import android.widget.ListView;
66import android.widget.TextView;
67
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068/**
69 * Dialer activity that displays the typical twelve key interface.
70 */
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -080071@SuppressWarnings("deprecation")
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072public class TwelveKeyDialer extends Activity implements View.OnClickListener,
73 View.OnLongClickListener, View.OnKeyListener,
74 AdapterView.OnItemClickListener, TextWatcher {
Nicolas Cataniac3be69e2010-01-14 14:03:53 -080075 private static final String EMPTY_NUMBER = "";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076 private static final String TAG = "TwelveKeyDialer";
Eric Laurentd9efc872009-07-17 11:52:06 -070077
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078 /** The length of DTMF tones in milliseconds */
79 private static final int TONE_LENGTH_MS = 150;
Eric Laurentd9efc872009-07-17 11:52:06 -070080
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080081 /** The DTMF tone volume relative to other sounds in the stream */
Jean-Michel Trividd44f8c2009-11-10 13:00:45 -080082 private static final int TONE_RELATIVE_VOLUME = 80;
83
84 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
85 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_MUSIC;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086
87 private EditText mDigits;
88 private View mDelete;
89 private MenuItem mAddToContactMenuItem;
90 private ToneGenerator mToneGenerator;
91 private Object mToneGeneratorLock = new Object();
92 private Drawable mDigitsBackground;
93 private Drawable mDigitsEmptyBackground;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094 private View mDialpad;
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -070095 private View mVoicemailDialAndDeleteRow;
Nicolas Catania80bda0f2009-09-19 09:17:14 -070096 private View mVoicemailButton;
97 private View mDialButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098 private ListView mDialpadChooser;
99 private DialpadChooserAdapter mDialpadChooserAdapter;
Reli Talc2a2a512009-06-10 16:48:00 -0400100 //Member variables for dialpad options
101 private MenuItem m2SecPauseMenuItem;
102 private MenuItem mWaitMenuItem;
103 private static final int MENU_ADD_CONTACTS = 1;
104 private static final int MENU_2S_PAUSE = 2;
105 private static final int MENU_WAIT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800107 // Last number dialed, retrieved asynchronously from the call DB
108 // in onCreate. This number is displayed when the user hits the
109 // send key and cleared in onPause.
110 CallLogAsync mCallLog = new CallLogAsync();
111 private String mLastNumberDialed = EMPTY_NUMBER;
112
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 // determines if we want to playback local DTMF tones.
114 private boolean mDTMFToneEnabled;
David Brownc29c7ab2009-07-07 16:00:18 -0700115
116 // Vibration (haptic feedback) for dialer key presses.
Nicolas Catania905e7622009-12-01 08:51:20 -0800117 private HapticFeedback mHaptic = new HapticFeedback();
Eric Laurentd9efc872009-07-17 11:52:06 -0700118
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800119 /** Identifier for the "Add Call" intent extra. */
120 static final String ADD_CALL_MODE_KEY = "add_call_mode";
Paul Bermandbdcde22009-10-09 12:04:10 -0400121
122 /**
123 * Identifier for intent extra for sending an empty Flash message for
124 * CDMA networks. This message is used by the network to simulate a
125 * press/depress of the "hookswitch" of a landline phone. Aka "empty flash".
126 *
127 * TODO: Using an intent extra to tell the phone to send this flash is a
128 * temporary measure. To be replaced with an ITelephony call in the future.
129 * TODO: Keep in sync with the string defined in OutgoingCallBroadcaster.java
130 * in Phone app until this is replaced with the ITelephony API.
131 */
132 static final String EXTRA_SEND_EMPTY_FLASH
133 = "com.android.phone.extra.SEND_EMPTY_FLASH";
134
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 /** Indicates if we are opening this dialer to add a call from the InCallScreen. */
136 private boolean mIsAddCallMode;
137
138 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
139 /**
140 * Listen for phone state changes so that we can take down the
141 * "dialpad chooser" if the phone becomes idle while the
142 * chooser UI is visible.
143 */
144 @Override
145 public void onCallStateChanged(int state, String incomingNumber) {
146 // Log.i(TAG, "PhoneStateListener.onCallStateChanged: "
147 // + state + ", '" + incomingNumber + "'");
148 if ((state == TelephonyManager.CALL_STATE_IDLE) && dialpadChooserVisible()) {
149 // Log.i(TAG, "Call ended with dialpad chooser visible! Taking it down...");
150 // Note there's a race condition in the UI here: the
151 // dialpad chooser could conceivably disappear (on its
152 // own) at the exact moment the user was trying to select
153 // one of the choices, which would be confusing. (But at
154 // least that's better than leaving the dialpad chooser
155 // onscreen, but useless...)
156 showDialpadChooser(false);
157 }
158 }
159 };
160
161 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
162 // Do nothing
163 }
164
165 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
166 // Do nothing
Eric Laurentd9efc872009-07-17 11:52:06 -0700167 // DTMF Tones do not need to be played here any longer -
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 // the DTMF dialer handles that functionality now.
169 }
170
171 public void afterTextChanged(Editable input) {
172 if (SpecialCharSequenceMgr.handleChars(this, input.toString(), mDigits)) {
173 // A special sequence was entered, clear the digits
174 mDigits.getText().clear();
175 }
176
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800177 if (!isDigitsEmpty()) {
Nicolas Catania75993762009-09-21 16:42:00 -0700178 mDigits.setBackgroundDrawable(mDigitsBackground);
179 } else {
Nicolas Catania3040fa32009-10-01 13:00:53 -0700180 mDigits.setCursorVisible(false);
Nicolas Catania75993762009-09-21 16:42:00 -0700181 mDigits.setBackgroundDrawable(mDigitsEmptyBackground);
182 }
183
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800184 updateDialAndDeleteButtonEnabledState();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185 }
186
187 @Override
188 protected void onCreate(Bundle icicle) {
189 super.onCreate(icicle);
190
191 // Set the content view
192 setContentView(getContentViewResource());
193
Nicolas Catania75993762009-09-21 16:42:00 -0700194 // Load up the resources for the text field.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800195 Resources r = getResources();
196 mDigitsBackground = r.getDrawable(R.drawable.btn_dial_textfield_active);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800197 mDigitsEmptyBackground = r.getDrawable(R.drawable.btn_dial_textfield);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800198
199 mDigits = (EditText) findViewById(R.id.digits);
200 mDigits.setKeyListener(DialerKeyListener.getInstance());
201 mDigits.setOnClickListener(this);
202 mDigits.setOnKeyListener(this);
Nicolas Catania3040fa32009-10-01 13:00:53 -0700203
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800204 maybeAddNumberFormatting();
205
206 // Check for the presence of the keypad
207 View view = findViewById(R.id.one);
208 if (view != null) {
209 setupKeypad();
210 }
211
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700212 mVoicemailDialAndDeleteRow = findViewById(R.id.voicemailAndDialAndDelete);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700213
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700214 initVoicemailButton();
215
David Brown3d07e6d2009-08-04 20:30:09 -0700216 // Check whether we should show the onscreen "Dial" button.
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700217 mDialButton = mVoicemailDialAndDeleteRow.findViewById(R.id.dialButton);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700218
David Brown3d07e6d2009-08-04 20:30:09 -0700219 if (r.getBoolean(R.bool.config_show_onscreen_dial_button)) {
David Brown3d07e6d2009-08-04 20:30:09 -0700220 mDialButton.setOnClickListener(this);
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700221 } else {
222 mDialButton.setVisibility(View.GONE); // It's VISIBLE by default
223 mDialButton = null;
David Brown3d07e6d2009-08-04 20:30:09 -0700224 }
225
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700226 view = mVoicemailDialAndDeleteRow.findViewById(R.id.deleteButton);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800227 view.setOnClickListener(this);
228 view.setOnLongClickListener(this);
229 mDelete = view;
230
Nicolas Catania901f8562009-10-09 11:09:45 -0700231 mDialpad = findViewById(R.id.dialpad); // This is null in landscape mode.
232
233 // In landscape we put the keyboard in phone mode.
234 // In portrait we prevent the soft keyboard to show since the
235 // dialpad acts as one already.
236 if (null == mDialpad) {
237 mDigits.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
238 } else {
239 mDigits.setInputType(android.text.InputType.TYPE_NULL);
240 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800241
242 // Set up the "dialpad chooser" UI; see showDialpadChooser().
243 mDialpadChooser = (ListView) findViewById(R.id.dialpadChooser);
244 mDialpadChooser.setOnItemClickListener(this);
245
246 if (!resolveIntent() && icicle != null) {
247 super.onRestoreInstanceState(icicle);
248 }
249
Nicolas Catania905e7622009-12-01 08:51:20 -0800250 try {
251 mHaptic.init(this, r.getBoolean(R.bool.config_enable_dialer_key_vibration));
252 } catch (Resources.NotFoundException nfe) {
253 Log.e(TAG, "Vibrate control bool missing.", nfe);
254 }
255
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 }
257
258 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800259 protected void onRestoreInstanceState(Bundle icicle) {
260 // Do nothing, state is restored in onCreate() if needed
261 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700262
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800263 protected void maybeAddNumberFormatting() {
264 mDigits.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
265 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700266
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800267 /**
Eric Laurentd9efc872009-07-17 11:52:06 -0700268 * Overridden by subclasses to control the resource used by the content view.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800269 */
270 protected int getContentViewResource() {
271 return R.layout.twelve_key_dialer;
272 }
273
274 private boolean resolveIntent() {
275 boolean ignoreState = false;
276
277 // Find the proper intent
278 final Intent intent;
279 if (isChild()) {
280 intent = getParent().getIntent();
281 ignoreState = intent.getBooleanExtra(DialtactsActivity.EXTRA_IGNORE_STATE, false);
282 } else {
283 intent = getIntent();
284 }
285 // Log.i(TAG, "==> resolveIntent(): intent: " + intent);
286
287 // by default we are not adding a call.
288 mIsAddCallMode = false;
289
290 // By default we don't show the "dialpad chooser" UI.
291 boolean needToShowDialpadChooser = false;
292
293 // Resolve the intent
294 final String action = intent.getAction();
295 if (Intent.ACTION_DIAL.equals(action) || Intent.ACTION_VIEW.equals(action)) {
296 // see if we are "adding a call" from the InCallScreen; false by default.
297 mIsAddCallMode = intent.getBooleanExtra(ADD_CALL_MODE_KEY, false);
298 Uri uri = intent.getData();
299 if (uri != null) {
300 if ("tel".equals(uri.getScheme())) {
301 // Put the requested number into the input area
Nicolas Catania43094f62009-12-08 14:09:46 -0800302 String data = uri.toString().substring("tel:".length());
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800303 setFormattedDigits(data);
304 } else {
305 String type = intent.getType();
306 if (People.CONTENT_ITEM_TYPE.equals(type)
307 || Phones.CONTENT_ITEM_TYPE.equals(type)) {
308 // Query the phone number
309 Cursor c = getContentResolver().query(intent.getData(),
310 new String[] {PhonesColumns.NUMBER}, null, null, null);
311 if (c != null) {
312 if (c.moveToFirst()) {
313 // Put the number into the input area
314 setFormattedDigits(c.getString(0));
315 }
316 c.close();
317 }
318 }
319 }
320 }
321 } else if (Intent.ACTION_MAIN.equals(action)) {
322 // The MAIN action means we're bringing up a blank dialer
323 // (e.g. by selecting the Home shortcut, or tabbing over from
324 // Contacts or Call log.)
325 //
326 // At this point, IF there's already an active call, there's a
327 // good chance that the user got here accidentally (but really
328 // wanted the in-call dialpad instead). So we bring up an
329 // intermediate UI to make the user confirm what they really
330 // want to do.
331 if (phoneIsInUse()) {
332 // Log.i(TAG, "resolveIntent(): phone is in use; showing dialpad chooser!");
333 needToShowDialpadChooser = true;
334 }
335 }
336
337 // Bring up the "dialpad chooser" IFF we need to make the user
338 // confirm which dialpad they really want.
339 showDialpadChooser(needToShowDialpadChooser);
340
341 return ignoreState;
342 }
343
344 protected void setFormattedDigits(String data) {
345 // strip the non-dialable numbers out of the data string.
346 String dialString = PhoneNumberUtils.extractNetworkPortion(data);
347 dialString = PhoneNumberUtils.formatNumber(dialString);
348 if (!TextUtils.isEmpty(dialString)) {
349 Editable digits = mDigits.getText();
350 digits.replace(0, digits.length(), dialString);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700351 // for some reason this isn't getting called in the digits.replace call above..
352 // but in any case, this will make sure the background drawable looks right
353 afterTextChanged(digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800354 }
355 }
356
357 @Override
358 protected void onNewIntent(Intent newIntent) {
359 setIntent(newIntent);
360 resolveIntent();
361 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700362
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800363 @Override
364 protected void onPostCreate(Bundle savedInstanceState) {
365 super.onPostCreate(savedInstanceState);
366
367 // This can't be done in onCreate(), since the auto-restoring of the digits
368 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
369 // is called. This method will be called every time the activity is created, and
370 // will always happen after onRestoreSavedInstanceState().
371 mDigits.addTextChangedListener(this);
372 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700373
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800374 private void setupKeypad() {
375 // Setup the listeners for the buttons
376 View view = findViewById(R.id.one);
377 view.setOnClickListener(this);
378 view.setOnLongClickListener(this);
379
380 findViewById(R.id.two).setOnClickListener(this);
381 findViewById(R.id.three).setOnClickListener(this);
382 findViewById(R.id.four).setOnClickListener(this);
383 findViewById(R.id.five).setOnClickListener(this);
384 findViewById(R.id.six).setOnClickListener(this);
385 findViewById(R.id.seven).setOnClickListener(this);
386 findViewById(R.id.eight).setOnClickListener(this);
387 findViewById(R.id.nine).setOnClickListener(this);
388 findViewById(R.id.star).setOnClickListener(this);
389
390 view = findViewById(R.id.zero);
391 view.setOnClickListener(this);
392 view.setOnLongClickListener(this);
393
394 findViewById(R.id.pound).setOnClickListener(this);
395 }
396
397 @Override
398 protected void onResume() {
399 super.onResume();
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800400 // Query the last dialed number. Do it first because hitting
401 // the DB is 'slow'. This call is asynchronous.
402 queryLastOutgoingCall();
David Brownc29c7ab2009-07-07 16:00:18 -0700403
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800404 // retrieve the DTMF tone play back setting.
405 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
406 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
407
Nicolas Catania905e7622009-12-01 08:51:20 -0800408 // Retrieve the haptic feedback setting.
409 mHaptic.checkSystemSetting();
410
Eric Laurentd9efc872009-07-17 11:52:06 -0700411 // if the mToneGenerator creation fails, just continue without it. It is
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800412 // a local audio signal, and is not as important as the dtmf tone itself.
413 synchronized(mToneGeneratorLock) {
414 if (mToneGenerator == null) {
415 try {
Jean-Michel Trividd44f8c2009-11-10 13:00:45 -0800416 // we want the user to be able to control the volume of the dial tones
417 // outside of a call, so we use the stream type that is also mapped to the
418 // volume control keys for this activity
419 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
420 setVolumeControlStream(DIAL_TONE_STREAM_TYPE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800421 } catch (RuntimeException e) {
422 Log.w(TAG, "Exception caught while creating local tone generator: " + e);
423 mToneGenerator = null;
424 }
425 }
426 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700427
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800428 Activity parent = getParent();
429 // See if we were invoked with a DIAL intent. If we were, fill in the appropriate
430 // digits in the dialer field.
431 if (parent != null && parent instanceof DialtactsActivity) {
432 Uri dialUri = ((DialtactsActivity) parent).getAndClearDialUri();
433 if (dialUri != null) {
434 resolveIntent();
435 }
436 }
437
438 // While we're in the foreground, listen for phone state changes,
439 // purely so that we can take down the "dialpad chooser" if the
440 // phone becomes idle while the chooser UI is visible.
441 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
442 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
443
444 // Potentially show hint text in the mDigits field when the user
445 // hasn't typed any digits yet. (If there's already an active call,
446 // this hint text will remind the user that he's about to add a new
447 // call.)
448 //
449 // TODO: consider adding better UI for the case where *both* lines
450 // are currently in use. (Right now we let the user try to add
451 // another call, but that call is guaranteed to fail. Perhaps the
452 // entire dialer UI should be disabled instead.)
453 if (phoneIsInUse()) {
454 mDigits.setHint(R.string.dialerDialpadHintText);
455 } else {
456 // Common case; no hint necessary.
457 mDigits.setHint(null);
458
459 // Also, a sanity-check: the "dialpad chooser" UI should NEVER
460 // be visible if the phone is idle!
461 showDialpadChooser(false);
462 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -0700463
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800464 updateDialAndDeleteButtonEnabledState();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800465 }
466
467 @Override
Karl Rosaenf46bc312009-03-24 18:20:48 -0700468 public void onWindowFocusChanged(boolean hasFocus) {
469 if (hasFocus) {
470 // Hide soft keyboard, if visible (it's fugly over button dialer).
471 // The only known case where this will be true is when launching the dialer with
472 // ACTION_DIAL via a soft keyboard. we dismiss it here because we don't
473 // have a window token yet in onCreate / onNewIntent
474 InputMethodManager inputMethodManager = (InputMethodManager)
475 getSystemService(Context.INPUT_METHOD_SERVICE);
Eric Laurentd9efc872009-07-17 11:52:06 -0700476 inputMethodManager.hideSoftInputFromWindow(mDigits.getWindowToken(), 0);
Karl Rosaenf46bc312009-03-24 18:20:48 -0700477 }
478 }
479
480 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 protected void onPause() {
482 super.onPause();
483
484 // Stop listening for phone state changes.
485 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
486 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
487
488 synchronized(mToneGeneratorLock) {
489 if (mToneGenerator != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800490 mToneGenerator.release();
491 mToneGenerator = null;
492 }
493 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800494 // TODO: I wonder if we should not check if the AsyncTask that
495 // lookup the last dialed number has completed.
496 mLastNumberDialed = EMPTY_NUMBER; // Since we are going to query again, free stale number.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 }
498
499 @Override
500 public boolean onCreateOptionsMenu(Menu menu) {
Reli Talc2a2a512009-06-10 16:48:00 -0400501 mAddToContactMenuItem = menu.add(0, MENU_ADD_CONTACTS, 0, R.string.recentCalls_addToContact)
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 .setIcon(android.R.drawable.ic_menu_add);
Reli Talc2a2a512009-06-10 16:48:00 -0400503 m2SecPauseMenuItem = menu.add(0, MENU_2S_PAUSE, 0, R.string.add_2sec_pause)
504 .setIcon(R.drawable.ic_menu_2sec_pause);
505 mWaitMenuItem = menu.add(0, MENU_WAIT, 0, R.string.add_wait)
506 .setIcon(R.drawable.ic_menu_wait);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 return true;
508 }
509
510 @Override
511 public boolean onPrepareOptionsMenu(Menu menu) {
512 // We never show a menu if the "choose dialpad" UI is up.
513 if (dialpadChooserVisible()) {
514 return false;
515 }
516
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800517 if (isDigitsEmpty()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800518 mAddToContactMenuItem.setVisible(false);
Reli Talc2a2a512009-06-10 16:48:00 -0400519 m2SecPauseMenuItem.setVisible(false);
520 mWaitMenuItem.setVisible(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800521 } else {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800522 CharSequence digits = mDigits.getText();
523
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800524 // Put the current digits string into an intent
525 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800526 intent.putExtra(Insert.PHONE, digits);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800527 intent.setType(People.CONTENT_ITEM_TYPE);
528 mAddToContactMenuItem.setIntent(intent);
529 mAddToContactMenuItem.setVisible(true);
Reli Talc2a2a512009-06-10 16:48:00 -0400530
531 // Check out whether to show Pause & Wait option menu items
532 int selectionStart;
533 int selectionEnd;
534 String strDigits = digits.toString();
535
536 selectionStart = mDigits.getSelectionStart();
537 selectionEnd = mDigits.getSelectionEnd();
538
539 if (selectionStart != -1) {
540 if (selectionStart > selectionEnd) {
541 // swap it as we want start to be less then end
542 int tmp = selectionStart;
543 selectionStart = selectionEnd;
544 selectionEnd = tmp;
545 }
546
547 if (selectionStart != 0) {
548 // Pause can be visible if cursor is not in the begining
549 m2SecPauseMenuItem.setVisible(true);
550
551 // For Wait to be visible set of condition to meet
552 mWaitMenuItem.setVisible(showWait(selectionStart,
553 selectionEnd, strDigits));
554 } else {
555 // cursor in the beginning both pause and wait to be invisible
556 m2SecPauseMenuItem.setVisible(false);
557 mWaitMenuItem.setVisible(false);
558 }
559 } else {
560 // cursor is not selected so assume new digit is added to the end
561 int strLength = strDigits.length();
562 mWaitMenuItem.setVisible(showWait(strLength,
563 strLength, strDigits));
564 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565 }
566 return true;
567 }
568
569 @Override
570 public boolean onKeyDown(int keyCode, KeyEvent event) {
571 switch (keyCode) {
572 case KeyEvent.KEYCODE_CALL: {
573 long callPressDiff = SystemClock.uptimeMillis() - event.getDownTime();
574 if (callPressDiff >= ViewConfiguration.getLongPressTimeout()) {
575 // Launch voice dialer
576 Intent intent = new Intent(Intent.ACTION_VOICE_COMMAND);
577 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
578 try {
579 startActivity(intent);
580 } catch (ActivityNotFoundException e) {
581 }
582 }
583 return true;
584 }
585 case KeyEvent.KEYCODE_1: {
Eric Laurentd9efc872009-07-17 11:52:06 -0700586 long timeDiff = SystemClock.uptimeMillis() - event.getDownTime();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800587 if (timeDiff >= ViewConfiguration.getLongPressTimeout()) {
588 // Long press detected, call voice mail
589 callVoicemail();
590 }
591 return true;
592 }
593 }
594 return super.onKeyDown(keyCode, event);
595 }
596
597 @Override
598 public boolean onKeyUp(int keyCode, KeyEvent event) {
599 switch (keyCode) {
600 case KeyEvent.KEYCODE_CALL: {
Nicolas Catania998763d2010-01-14 14:03:53 -0800601 // TODO: In dialButtonPressed we do some of these
602 // tests again. We should try to consolidate them in
603 // one place.
604 if (!phoneIsCdma() && mIsAddCallMode && isDigitsEmpty()) {
605 // For CDMA phones, we always call
606 // dialButtonPressed() because we may need to send
607 // an empty flash command to the network.
608 // Otherwise, if we are adding a call from the
609 // InCallScreen and the phone number entered is
610 // empty, we just close the dialer to expose the
611 // InCallScreen under it.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800612 finish();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 }
Nicolas Catania998763d2010-01-14 14:03:53 -0800614
615 // If we're CDMA, regardless of where we are adding a call from (either
616 // InCallScreen or Dialtacts), the user may need to send an empty
617 // flash command to the network. So let's call dialButtonPressed() regardless
618 // and dialButtonPressed will handle this functionality for us.
619 // otherwise, we place the call.
620 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800621 return true;
622 }
623 }
624 return super.onKeyUp(keyCode, event);
625 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700626
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627 private void keyPressed(int keyCode) {
Nicolas Catania905e7622009-12-01 08:51:20 -0800628 mHaptic.vibrate();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800629 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
630 mDigits.onKeyDown(keyCode, event);
631 }
632
633 public boolean onKey(View view, int keyCode, KeyEvent event) {
634 switch (view.getId()) {
635 case R.id.digits:
636 if (keyCode == KeyEvent.KEYCODE_ENTER) {
Nicolas Catania998763d2010-01-14 14:03:53 -0800637 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800638 return true;
639 }
640 break;
641 }
642 return false;
643 }
644
645 public void onClick(View view) {
646 switch (view.getId()) {
647 case R.id.one: {
648 playTone(ToneGenerator.TONE_DTMF_1);
649 keyPressed(KeyEvent.KEYCODE_1);
650 return;
651 }
652 case R.id.two: {
653 playTone(ToneGenerator.TONE_DTMF_2);
654 keyPressed(KeyEvent.KEYCODE_2);
655 return;
656 }
657 case R.id.three: {
658 playTone(ToneGenerator.TONE_DTMF_3);
659 keyPressed(KeyEvent.KEYCODE_3);
660 return;
661 }
662 case R.id.four: {
663 playTone(ToneGenerator.TONE_DTMF_4);
664 keyPressed(KeyEvent.KEYCODE_4);
665 return;
666 }
667 case R.id.five: {
668 playTone(ToneGenerator.TONE_DTMF_5);
669 keyPressed(KeyEvent.KEYCODE_5);
670 return;
671 }
672 case R.id.six: {
673 playTone(ToneGenerator.TONE_DTMF_6);
674 keyPressed(KeyEvent.KEYCODE_6);
675 return;
676 }
677 case R.id.seven: {
678 playTone(ToneGenerator.TONE_DTMF_7);
679 keyPressed(KeyEvent.KEYCODE_7);
680 return;
681 }
682 case R.id.eight: {
683 playTone(ToneGenerator.TONE_DTMF_8);
684 keyPressed(KeyEvent.KEYCODE_8);
685 return;
686 }
687 case R.id.nine: {
688 playTone(ToneGenerator.TONE_DTMF_9);
689 keyPressed(KeyEvent.KEYCODE_9);
690 return;
691 }
692 case R.id.zero: {
693 playTone(ToneGenerator.TONE_DTMF_0);
694 keyPressed(KeyEvent.KEYCODE_0);
695 return;
696 }
697 case R.id.pound: {
698 playTone(ToneGenerator.TONE_DTMF_P);
699 keyPressed(KeyEvent.KEYCODE_POUND);
700 return;
701 }
702 case R.id.star: {
703 playTone(ToneGenerator.TONE_DTMF_S);
704 keyPressed(KeyEvent.KEYCODE_STAR);
705 return;
706 }
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700707 case R.id.deleteButton: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800708 keyPressed(KeyEvent.KEYCODE_DEL);
709 return;
710 }
Nicolas Catania3040fa32009-10-01 13:00:53 -0700711 case R.id.dialButton: {
Nicolas Catania905e7622009-12-01 08:51:20 -0800712 mHaptic.vibrate(); // Vibrate here too, just like we do for the regular keys
Nicolas Catania998763d2010-01-14 14:03:53 -0800713 dialButtonPressed();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800714 return;
715 }
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700716 case R.id.voicemailButton: {
717 callVoicemail();
Nicolas Catania905e7622009-12-01 08:51:20 -0800718 mHaptic.vibrate();
Nicolas Catania80bda0f2009-09-19 09:17:14 -0700719 return;
720 }
Nicolas Catania3040fa32009-10-01 13:00:53 -0700721 case R.id.digits: {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800722 if (!isDigitsEmpty()) {
Nicolas Catania3040fa32009-10-01 13:00:53 -0700723 mDigits.setCursorVisible(true);
724 }
725 return;
726 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800727 }
728 }
729
730 public boolean onLongClick(View view) {
731 final Editable digits = mDigits.getText();
732 int id = view.getId();
733 switch (id) {
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700734 case R.id.deleteButton: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800735 digits.clear();
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700736 // TODO: The framework forgets to clear the pressed
737 // status of disabled button. Until this is fixed,
738 // clear manually the pressed status. b/2133127
739 mDelete.setPressed(false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800740 return true;
741 }
742 case R.id.one: {
Nicolas Cataniabe8821e2010-01-15 09:28:13 -0800743 if (isDigitsEmpty()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800744 callVoicemail();
745 return true;
746 }
747 return false;
748 }
749 case R.id.zero: {
750 keyPressed(KeyEvent.KEYCODE_PLUS);
751 return true;
752 }
753 }
754 return false;
755 }
756
757 void callVoicemail() {
758 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800759 Uri.fromParts("voicemail", EMPTY_NUMBER, null));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800760 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
761 startActivity(intent);
762 mDigits.getText().clear();
763 finish();
764 }
765
Nicolas Catania998763d2010-01-14 14:03:53 -0800766 void dialButtonPressed() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800767 final String number = mDigits.getText().toString();
Paul Bermandbdcde22009-10-09 12:04:10 -0400768 boolean sendEmptyFlash = false;
Nicolas Catania998763d2010-01-14 14:03:53 -0800769 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED);
770 if (isDigitsEmpty()) { // There is no number entered.
Paul Bermandbdcde22009-10-09 12:04:10 -0400771 if (phoneIsCdma() && phoneIsOffhook()) {
772 // We only want to send this empty flash extra if we're CDMA and the
773 // phone is offhook (don't want to send if ringing or dialing)
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800774 intent.setData(Uri.fromParts("tel", EMPTY_NUMBER, null));
Paul Bermandbdcde22009-10-09 12:04:10 -0400775 intent.putExtra(EXTRA_SEND_EMPTY_FLASH, true);
776 sendEmptyFlash = true;
Nicolas Cataniac3be69e2010-01-14 14:03:53 -0800777 } else if (!phoneIsOffhook() && !TextUtils.isEmpty(mLastNumberDialed)) {
778 mDigits.setText(mLastNumberDialed);
Nicolas Catania998763d2010-01-14 14:03:53 -0800779 return;
Paul Bermandbdcde22009-10-09 12:04:10 -0400780 } else {
Nicolas Catania998763d2010-01-14 14:03:53 -0800781 // TODO: Is this dead code? Hit only if phoneIsOffHook
782 // and dial button is pressed. Can this happen? How
783 // does this compare to the finish() called in
784 // onKeyUp? Should this play tone be moved there?
Paul Bermandbdcde22009-10-09 12:04:10 -0400785 playTone(ToneGenerator.TONE_PROP_NACK);
786 return;
787 }
Nicolas Catania998763d2010-01-14 14:03:53 -0800788 } else { // There is a number.
789 intent.setData(Uri.fromParts("tel", number, null));
Paul Bermandbdcde22009-10-09 12:04:10 -0400790 }
Nicolas Catania998763d2010-01-14 14:03:53 -0800791
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800792 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
793 startActivity(intent);
794 mDigits.getText().clear();
Paul Bermandbdcde22009-10-09 12:04:10 -0400795 // Don't finish TwelveKeyDialer yet if we're sending a blank flash for CDMA. CDMA
796 // networks use Flash messages when special processing needs to be done, mainly for
797 // 3-way or call waiting scenarios. Presumably, here we're in a special 3-way scenario
798 // where the network needs a blank flash before being able to add the new participant.
799 // (This is not the case with all 3-way calls, just certain CDMA infrastructures.)
800 if (!sendEmptyFlash) {
801 finish();
802 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800803 }
804
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800805
806 /**
David Brown22f615f2009-06-25 16:19:19 -0700807 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
808 *
809 * The tone is played locally, using the audio stream for phone calls.
810 * Tones are played only if the "Audible touch tones" user preference
811 * is checked, and are NOT played if the device is in silent mode.
812 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800813 * @param tone a tone code from {@link ToneGenerator}
814 */
815 void playTone(int tone) {
816 // if local tone playback is disabled, just return.
817 if (!mDTMFToneEnabled) {
818 return;
819 }
David Brown22f615f2009-06-25 16:19:19 -0700820
821 // Also do nothing if the phone is in silent mode.
822 // We need to re-check the ringer mode for *every* playTone()
823 // call, rather than keeping a local flag that's updated in
824 // onResume(), since it's possible to toggle silent mode without
825 // leaving the current activity (via the ENDCALL-longpress menu.)
826 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
David Brownd5a15302009-07-20 16:39:47 -0700827 int ringerMode = audioManager.getRingerMode();
828 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
829 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
David Brown22f615f2009-06-25 16:19:19 -0700830 return;
831 }
832
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800833 synchronized(mToneGeneratorLock) {
834 if (mToneGenerator == null) {
835 Log.w(TAG, "playTone: mToneGenerator == null, tone: "+tone);
836 return;
837 }
Eric Laurentd9efc872009-07-17 11:52:06 -0700838
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800839 // Start the new tone (will stop any playing tone)
Eric Laurent8487fed2009-09-07 08:45:14 -0700840 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800841 }
842 }
843
844 /**
845 * Brings up the "dialpad chooser" UI in place of the usual Dialer
846 * elements (the textfield/button and the dialpad underneath).
847 *
848 * We show this UI if the user brings up the Dialer while a call is
849 * already in progress, since there's a good chance we got here
850 * accidentally (and the user really wanted the in-call dialpad instead).
851 * So in this situation we display an intermediate UI that lets the user
852 * explicitly choose between the in-call dialpad ("Use touch tone
853 * keypad") and the regular Dialer ("Add call"). (Or, the option "Return
854 * to call in progress" just goes back to the in-call UI with no dialpad
855 * at all.)
856 *
857 * @param enabled If true, show the "dialpad chooser" instead
858 * of the regular Dialer UI
859 */
860 private void showDialpadChooser(boolean enabled) {
861 if (enabled) {
862 // Log.i(TAG, "Showing dialpad chooser!");
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700863 mDigits.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800864 if (mDialpad != null) mDialpad.setVisibility(View.GONE);
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700865 mVoicemailDialAndDeleteRow.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800866 mDialpadChooser.setVisibility(View.VISIBLE);
867
868 // Instantiate the DialpadChooserAdapter and hook it up to the
869 // ListView. We do this only once.
870 if (mDialpadChooserAdapter == null) {
871 mDialpadChooserAdapter = new DialpadChooserAdapter(this);
872 mDialpadChooser.setAdapter(mDialpadChooserAdapter);
873 }
874 } else {
875 // Log.i(TAG, "Displaying normal Dialer UI.");
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700876 mDigits.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800877 if (mDialpad != null) mDialpad.setVisibility(View.VISIBLE);
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -0700878 mVoicemailDialAndDeleteRow.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800879 mDialpadChooser.setVisibility(View.GONE);
880 }
881 }
882
883 /**
884 * @return true if we're currently showing the "dialpad chooser" UI.
885 */
886 private boolean dialpadChooserVisible() {
887 return mDialpadChooser.getVisibility() == View.VISIBLE;
888 }
889
890 /**
891 * Simple list adapter, binding to an icon + text label
892 * for each item in the "dialpad chooser" list.
893 */
894 private static class DialpadChooserAdapter extends BaseAdapter {
895 private LayoutInflater mInflater;
896
897 // Simple struct for a single "choice" item.
898 static class ChoiceItem {
899 String text;
900 Bitmap icon;
901 int id;
902
903 public ChoiceItem(String s, Bitmap b, int i) {
904 text = s;
905 icon = b;
906 id = i;
907 }
908 }
909
910 // IDs for the possible "choices":
911 static final int DIALPAD_CHOICE_USE_DTMF_DIALPAD = 101;
912 static final int DIALPAD_CHOICE_RETURN_TO_CALL = 102;
913 static final int DIALPAD_CHOICE_ADD_NEW_CALL = 103;
914
915 private static final int NUM_ITEMS = 3;
916 private ChoiceItem mChoiceItems[] = new ChoiceItem[NUM_ITEMS];
917
918 public DialpadChooserAdapter(Context context) {
919 // Cache the LayoutInflate to avoid asking for a new one each time.
920 mInflater = LayoutInflater.from(context);
921
922 // Initialize the possible choices.
923 // TODO: could this be specified entirely in XML?
924
925 // - "Use touch tone keypad"
926 mChoiceItems[0] = new ChoiceItem(
927 context.getString(R.string.dialer_useDtmfDialpad),
928 BitmapFactory.decodeResource(context.getResources(),
929 R.drawable.ic_dialer_fork_tt_keypad),
930 DIALPAD_CHOICE_USE_DTMF_DIALPAD);
931
932 // - "Return to call in progress"
933 mChoiceItems[1] = new ChoiceItem(
934 context.getString(R.string.dialer_returnToInCallScreen),
935 BitmapFactory.decodeResource(context.getResources(),
936 R.drawable.ic_dialer_fork_current_call),
937 DIALPAD_CHOICE_RETURN_TO_CALL);
938
939 // - "Add call"
940 mChoiceItems[2] = new ChoiceItem(
941 context.getString(R.string.dialer_addAnotherCall),
942 BitmapFactory.decodeResource(context.getResources(),
943 R.drawable.ic_dialer_fork_add_call),
944 DIALPAD_CHOICE_ADD_NEW_CALL);
945 }
946
947 public int getCount() {
948 return NUM_ITEMS;
949 }
950
951 /**
952 * Return the ChoiceItem for a given position.
953 */
954 public Object getItem(int position) {
955 return mChoiceItems[position];
956 }
957
958 /**
959 * Return a unique ID for each possible choice.
960 */
961 public long getItemId(int position) {
962 return position;
963 }
964
965 /**
966 * Make a view for each row.
967 */
968 public View getView(int position, View convertView, ViewGroup parent) {
969 // When convertView is non-null, we can reuse it (there's no need
970 // to reinflate it.)
971 if (convertView == null) {
972 convertView = mInflater.inflate(R.layout.dialpad_chooser_list_item, null);
973 }
974
975 TextView text = (TextView) convertView.findViewById(R.id.text);
976 text.setText(mChoiceItems[position].text);
977
978 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
979 icon.setImageBitmap(mChoiceItems[position].icon);
980
981 return convertView;
982 }
983 }
984
985 /**
986 * Handle clicks from the dialpad chooser.
987 */
988 public void onItemClick(AdapterView parent, View v, int position, long id) {
989 DialpadChooserAdapter.ChoiceItem item =
990 (DialpadChooserAdapter.ChoiceItem) parent.getItemAtPosition(position);
991 int itemId = item.id;
992 switch (itemId) {
993 case DialpadChooserAdapter.DIALPAD_CHOICE_USE_DTMF_DIALPAD:
994 // Log.i(TAG, "DIALPAD_CHOICE_USE_DTMF_DIALPAD");
995 // Fire off an intent to go back to the in-call UI
996 // with the dialpad visible.
997 returnToInCallScreen(true);
998 break;
999
1000 case DialpadChooserAdapter.DIALPAD_CHOICE_RETURN_TO_CALL:
1001 // Log.i(TAG, "DIALPAD_CHOICE_RETURN_TO_CALL");
1002 // Fire off an intent to go back to the in-call UI
1003 // (with the dialpad hidden).
1004 returnToInCallScreen(false);
1005 break;
1006
1007 case DialpadChooserAdapter.DIALPAD_CHOICE_ADD_NEW_CALL:
1008 // Log.i(TAG, "DIALPAD_CHOICE_ADD_NEW_CALL");
1009 // Ok, guess the user really did want to be here (in the
1010 // regular Dialer) after all. Bring back the normal Dialer UI.
1011 showDialpadChooser(false);
1012 break;
1013
1014 default:
1015 Log.w(TAG, "onItemClick: unexpected itemId: " + itemId);
1016 break;
1017 }
1018 }
1019
1020 /**
1021 * Returns to the in-call UI (where there's presumably a call in
1022 * progress) in response to the user selecting "use touch tone keypad"
1023 * or "return to call" from the dialpad chooser.
1024 */
1025 private void returnToInCallScreen(boolean showDialpad) {
1026 try {
1027 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1028 if (phone != null) phone.showCallScreenWithDialpad(showDialpad);
1029 } catch (RemoteException e) {
1030 Log.w(TAG, "phone.showCallScreenWithDialpad() failed", e);
1031 }
1032
1033 // Finally, finish() ourselves so that we don't stay on the
1034 // activity stack.
1035 // Note that we do this whether or not the showCallScreenWithDialpad()
1036 // call above had any effect or not! (That call is a no-op if the
1037 // phone is idle, which can happen if the current call ends while
1038 // the dialpad chooser is up. In this case we can't show the
1039 // InCallScreen, and there's no point staying here in the Dialer,
1040 // so we just take the user back where he came from...)
1041 finish();
1042 }
1043
1044 /**
1045 * @return true if the phone is "in use", meaning that at least one line
1046 * is active (ie. off hook or ringing or dialing).
1047 */
1048 private boolean phoneIsInUse() {
1049 boolean phoneInUse = false;
1050 try {
1051 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1052 if (phone != null) phoneInUse = !phone.isIdle();
1053 } catch (RemoteException e) {
1054 Log.w(TAG, "phone.isIdle() failed", e);
1055 }
1056 return phoneInUse;
1057 }
David Brownc29c7ab2009-07-07 16:00:18 -07001058
1059 /**
Paul Bermandbdcde22009-10-09 12:04:10 -04001060 * @return true if the phone is a CDMA phone type
1061 */
1062 private boolean phoneIsCdma() {
1063 boolean isCdma = false;
1064 try {
1065 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1066 if (phone != null) {
1067 isCdma = (phone.getActivePhoneType() == TelephonyManager.PHONE_TYPE_CDMA);
1068 }
1069 } catch (RemoteException e) {
1070 Log.w(TAG, "phone.getActivePhoneType() failed", e);
1071 }
1072 return isCdma;
1073 }
1074
1075 /**
1076 * @return true if the phone state is OFFHOOK
1077 */
1078 private boolean phoneIsOffhook() {
1079 boolean phoneOffhook = false;
1080 try {
1081 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1082 if (phone != null) phoneOffhook = phone.isOffhook();
1083 } catch (RemoteException e) {
1084 Log.w(TAG, "phone.isOffhook() failed", e);
1085 }
1086 return phoneOffhook;
1087 }
1088
Reli Talc2a2a512009-06-10 16:48:00 -04001089
1090 /**
1091 * Returns true whenever any one of the options from the menu is selected.
1092 * Code changes to support dialpad options
1093 */
1094 @Override
1095 public boolean onOptionsItemSelected(MenuItem item) {
1096 switch (item.getItemId()) {
1097 case MENU_2S_PAUSE:
1098 updateDialString(",");
1099 return true;
1100 case MENU_WAIT:
1101 updateDialString(";");
1102 return true;
1103 }
1104 return false;
1105 }
1106
1107 /**
1108 * Updates the dial string (mDigits) after inserting a Pause character (,)
1109 * or Wait character (;).
1110 */
1111 private void updateDialString(String newDigits) {
1112 int selectionStart;
1113 int selectionEnd;
1114
1115 // SpannableStringBuilder editable_text = new SpannableStringBuilder(mDigits.getText());
Eric Fischer686782e2009-09-10 17:57:45 -07001116 int anchor = mDigits.getSelectionStart();
1117 int point = mDigits.getSelectionEnd();
1118
1119 selectionStart = Math.min(anchor, point);
1120 selectionEnd = Math.max(anchor, point);
Reli Talc2a2a512009-06-10 16:48:00 -04001121
1122 Editable digits = mDigits.getText();
1123 if (selectionStart != -1 ) {
1124 if (selectionStart == selectionEnd) {
1125 // then there is no selection. So insert the pause at this
1126 // position and update the mDigits.
1127 digits.replace(selectionStart, selectionStart, newDigits);
1128 } else {
Eric Fischer1e2d3a22009-09-17 10:53:10 -07001129 digits.replace(selectionStart, selectionEnd, newDigits);
Nicolas Catania7edbd0c2009-09-28 20:37:33 -07001130 // Unselect: back to a regular cursor, just pass the character inserted.
1131 mDigits.setSelection(selectionStart + 1);
Reli Talc2a2a512009-06-10 16:48:00 -04001132 }
1133 } else {
1134 int len = mDigits.length();
1135 digits.replace(len, len, newDigits);
1136 }
1137 }
1138
1139 /**
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001140 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001141 */
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001142 private void updateDialAndDeleteButtonEnabledState() {
1143 final boolean digitsNotEmpty = !isDigitsEmpty();
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001144
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001145 if (mDialButton != null) {
1146 // If we're already on a CDMA call, then we want to enable
1147 // the Call button so we can send a flash.
1148 if (phoneIsOffhook()) {
1149 mDialButton.setEnabled(phoneIsCdma());
1150 } else {
1151 // Not in a call, enable the button if digits have
1152 // been entered or if there is a last dialed number
1153 // that could be redialed.
1154 mDialButton.setEnabled(digitsNotEmpty ||
1155 !TextUtils.isEmpty(mLastNumberDialed));
Paul Bermandbdcde22009-10-09 12:04:10 -04001156 }
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001157 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001158 mDelete.setEnabled(digitsNotEmpty);
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001159 }
1160
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001161
Nicolas Cataniadea164e2009-09-18 06:26:16 -07001162 /**
Nicolas Catania80bda0f2009-09-19 09:17:14 -07001163 * Check if voicemail is enabled/accessible.
1164 */
1165 private void initVoicemailButton() {
1166 boolean hasVoicemail = false;
1167 try {
1168 hasVoicemail = TelephonyManager.getDefault().getVoiceMailNumber() != null;
1169 } catch (SecurityException se) {
1170 // Possibly no READ_PHONE_STATE privilege.
1171 }
1172
Nicolas Cataniaa7e5a5b2009-09-20 10:56:40 -07001173 mVoicemailButton = mVoicemailDialAndDeleteRow.findViewById(R.id.voicemailButton);
Nicolas Catania80bda0f2009-09-19 09:17:14 -07001174 if (hasVoicemail) {
1175 mVoicemailButton.setOnClickListener(this);
1176 } else {
1177 mVoicemailButton.setEnabled(false);
1178 }
1179 }
1180
1181 /**
Reli Talc2a2a512009-06-10 16:48:00 -04001182 * This function return true if Wait menu item can be shown
1183 * otherwise returns false. Assumes the passed string is non-empty
1184 * and the 0th index check is not required.
1185 */
1186 private boolean showWait(int start, int end, String digits) {
1187 if (start == end) {
1188 // visible false in this case
1189 if (start > digits.length()) return false;
1190
1191 // preceding char is ';', so visible should be false
1192 if (digits.charAt(start-1) == ';') return false;
1193
1194 // next char is ';', so visible should be false
1195 if ((digits.length() > start) && (digits.charAt(start) == ';')) return false;
1196 } else {
1197 // visible false in this case
1198 if (start > digits.length() || end > digits.length()) return false;
1199
1200 // In this case we need to just check for ';' preceding to start
1201 // or next to end
1202 if (digits.charAt(start-1) == ';') return false;
1203 }
1204 return true;
1205 }
Nicolas Cataniabe8821e2010-01-15 09:28:13 -08001206
1207 /**
1208 * @return true if the widget with the phone number digits is empty.
1209 */
1210 private boolean isDigitsEmpty() {
Nicolas Catania941b76f2010-01-19 14:09:40 -08001211 return mDigits.length() == 0;
Nicolas Cataniabe8821e2010-01-15 09:28:13 -08001212 }
Nicolas Cataniac3be69e2010-01-14 14:03:53 -08001213
1214 /**
1215 * Starts the asyn query to get the last dialed/outgoing
1216 * number. When the background query finishes, mLastNumberDialed
1217 * is set to the last dialed number or an empty string if none
1218 * exists yet.
1219 */
1220 private void queryLastOutgoingCall() {
1221 mLastNumberDialed = EMPTY_NUMBER;
1222 CallLogAsync.GetLastOutgoingCallArgs lastCallArgs =
1223 new CallLogAsync.GetLastOutgoingCallArgs(
1224 this,
1225 new CallLogAsync.OnLastOutgoingCallComplete() {
1226 public void lastOutgoingCall(String number) {
1227 // TODO: Filter out emergency numbers if
1228 // the carrier does not want redial for
1229 // these.
1230 mLastNumberDialed = number;
1231 updateDialAndDeleteButtonEnabledState();
1232 }
1233 });
1234 mCallLog.getLastOutgoingCall(lastCallArgs);
1235 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -08001236
1237 @Override
1238 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
1239 boolean globalSearch) {
1240 if (globalSearch) {
1241 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1242 } else {
1243 ContactsSearchManager.startSearch(this, initialQuery);
1244 }
1245 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001246}