ComposeMessageActivity.java revision 706aae13ff1d40788787dc2651defb4ba6c8ecda
1/* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.mms.ui; 19 20import static android.content.res.Configuration.KEYBOARDHIDDEN_NO; 21import static com.android.mms.transaction.ProgressCallbackEntity.PROGRESS_ABORT; 22import static com.android.mms.transaction.ProgressCallbackEntity.PROGRESS_COMPLETE; 23import static com.android.mms.transaction.ProgressCallbackEntity.PROGRESS_START; 24import static com.android.mms.transaction.ProgressCallbackEntity.PROGRESS_STATUS_ACTION; 25import static com.android.mms.ui.MessageListAdapter.COLUMN_ID; 26import static com.android.mms.ui.MessageListAdapter.COLUMN_MSG_TYPE; 27import static com.android.mms.ui.MessageListAdapter.PROJECTION; 28 29import java.io.File; 30import java.io.FileInputStream; 31import java.io.FileOutputStream; 32import java.io.IOException; 33import java.io.InputStream; 34import java.io.UnsupportedEncodingException; 35import java.net.URLDecoder; 36import java.util.ArrayList; 37import java.util.HashMap; 38import java.util.HashSet; 39import java.util.List; 40import java.util.Map; 41import java.util.regex.Pattern; 42 43import android.app.ActionBar; 44import android.app.Activity; 45import android.app.AlertDialog; 46import android.app.ProgressDialog; 47import android.content.ActivityNotFoundException; 48import android.content.BroadcastReceiver; 49import android.content.ClipData; 50import android.content.ClipboardManager; 51import android.content.ContentResolver; 52import android.content.ContentUris; 53import android.content.ContentValues; 54import android.content.Context; 55import android.content.DialogInterface; 56import android.content.DialogInterface.OnClickListener; 57import android.content.Intent; 58import android.content.IntentFilter; 59import android.content.res.Configuration; 60import android.content.res.Resources; 61import android.database.Cursor; 62import android.database.sqlite.SQLiteException; 63import android.database.sqlite.SqliteWrapper; 64import android.drm.DrmStore; 65import android.graphics.drawable.Drawable; 66import android.media.RingtoneManager; 67import android.net.Uri; 68import android.os.AsyncTask; 69import android.os.Bundle; 70import android.os.Environment; 71import android.os.Handler; 72import android.os.Message; 73import android.os.Parcelable; 74import android.os.SystemProperties; 75import android.provider.ContactsContract; 76import android.provider.ContactsContract.QuickContact; 77import android.provider.Telephony; 78import android.provider.ContactsContract.CommonDataKinds.Email; 79import android.provider.ContactsContract.CommonDataKinds.Phone; 80import android.provider.ContactsContract.Contacts; 81import android.provider.ContactsContract.Intents; 82import android.provider.MediaStore.Images; 83import android.provider.MediaStore.Video; 84import android.provider.Settings; 85import android.provider.Telephony.Mms; 86import android.provider.Telephony.Sms; 87import android.telephony.PhoneNumberUtils; 88import android.telephony.SmsMessage; 89import android.text.Editable; 90import android.text.InputFilter; 91import android.text.InputFilter.LengthFilter; 92import android.text.SpannableString; 93import android.text.Spanned; 94import android.text.TextUtils; 95import android.text.TextWatcher; 96import android.text.method.TextKeyListener; 97import android.text.style.URLSpan; 98import android.text.util.Linkify; 99import android.util.Log; 100import android.view.ContextMenu; 101import android.view.ContextMenu.ContextMenuInfo; 102import android.view.KeyEvent; 103import android.view.Menu; 104import android.view.MenuItem; 105import android.view.View; 106import android.view.View.OnCreateContextMenuListener; 107import android.view.View.OnKeyListener; 108import android.view.ViewStub; 109import android.view.WindowManager; 110import android.view.inputmethod.InputMethodManager; 111import android.webkit.MimeTypeMap; 112import android.widget.AdapterView; 113import android.widget.EditText; 114import android.widget.ImageButton; 115import android.widget.ImageView; 116import android.widget.ListView; 117import android.widget.SimpleAdapter; 118import android.widget.TextView; 119import android.widget.Toast; 120 121import com.android.internal.telephony.TelephonyIntents; 122import com.android.internal.telephony.TelephonyProperties; 123import com.android.mms.LogTag; 124import com.android.mms.MmsApp; 125import com.android.mms.MmsConfig; 126import com.android.mms.R; 127import com.android.mms.TempFileProvider; 128import com.android.mms.data.Contact; 129import com.android.mms.data.ContactList; 130import com.android.mms.data.Conversation; 131import com.android.mms.data.Conversation.ConversationQueryHandler; 132import com.android.mms.data.WorkingMessage; 133import com.android.mms.data.WorkingMessage.MessageStatusListener; 134import com.android.mms.drm.DrmUtils; 135import com.android.mms.model.SlideModel; 136import com.android.mms.model.SlideshowModel; 137import com.android.mms.transaction.MessagingNotification; 138import com.android.mms.ui.MessageListView.OnSizeChangedListener; 139import com.android.mms.ui.MessageUtils.ResizeImageResultCallback; 140import com.android.mms.ui.RecipientsEditor.RecipientContextMenuInfo; 141import com.android.mms.util.DraftCache; 142import com.android.mms.util.PhoneNumberFormatter; 143import com.android.mms.util.SendingProgressTokenManager; 144import com.android.mms.util.SmileyParser; 145import com.android.mms.widget.MmsWidgetProvider; 146import com.google.android.mms.ContentType; 147import com.google.android.mms.MmsException; 148import com.google.android.mms.pdu.EncodedStringValue; 149import com.google.android.mms.pdu.PduBody; 150import com.google.android.mms.pdu.PduPart; 151import com.google.android.mms.pdu.PduPersister; 152import com.google.android.mms.pdu.SendReq; 153 154/** 155 * This is the main UI for: 156 * 1. Composing a new message; 157 * 2. Viewing/managing message history of a conversation. 158 * 159 * This activity can handle following parameters from the intent 160 * by which it's launched. 161 * thread_id long Identify the conversation to be viewed. When creating a 162 * new message, this parameter shouldn't be present. 163 * msg_uri Uri The message which should be opened for editing in the editor. 164 * address String The addresses of the recipients in current conversation. 165 * exit_on_sent boolean Exit this activity after the message is sent. 166 */ 167public class ComposeMessageActivity extends Activity 168 implements View.OnClickListener, TextView.OnEditorActionListener, 169 MessageStatusListener, Contact.UpdateListener { 170 public static final int REQUEST_CODE_ATTACH_IMAGE = 100; 171 public static final int REQUEST_CODE_TAKE_PICTURE = 101; 172 public static final int REQUEST_CODE_ATTACH_VIDEO = 102; 173 public static final int REQUEST_CODE_TAKE_VIDEO = 103; 174 public static final int REQUEST_CODE_ATTACH_SOUND = 104; 175 public static final int REQUEST_CODE_RECORD_SOUND = 105; 176 public static final int REQUEST_CODE_CREATE_SLIDESHOW = 106; 177 public static final int REQUEST_CODE_ECM_EXIT_DIALOG = 107; 178 public static final int REQUEST_CODE_ADD_CONTACT = 108; 179 public static final int REQUEST_CODE_PICK = 109; 180 181 private static final String TAG = "Mms/compose"; 182 183 private static final boolean DEBUG = false; 184 private static final boolean TRACE = false; 185 private static final boolean LOCAL_LOGV = false; 186 187 // Menu ID 188 private static final int MENU_ADD_SUBJECT = 0; 189 private static final int MENU_DELETE_THREAD = 1; 190 private static final int MENU_ADD_ATTACHMENT = 2; 191 private static final int MENU_DISCARD = 3; 192 private static final int MENU_SEND = 4; 193 private static final int MENU_CALL_RECIPIENT = 5; 194 private static final int MENU_CONVERSATION_LIST = 6; 195 private static final int MENU_DEBUG_DUMP = 7; 196 197 // Context menu ID 198 private static final int MENU_VIEW_CONTACT = 12; 199 private static final int MENU_ADD_TO_CONTACTS = 13; 200 201 private static final int MENU_EDIT_MESSAGE = 14; 202 private static final int MENU_VIEW_SLIDESHOW = 16; 203 private static final int MENU_VIEW_MESSAGE_DETAILS = 17; 204 private static final int MENU_DELETE_MESSAGE = 18; 205 private static final int MENU_SEARCH = 19; 206 private static final int MENU_DELIVERY_REPORT = 20; 207 private static final int MENU_FORWARD_MESSAGE = 21; 208 private static final int MENU_CALL_BACK = 22; 209 private static final int MENU_SEND_EMAIL = 23; 210 private static final int MENU_COPY_MESSAGE_TEXT = 24; 211 private static final int MENU_COPY_TO_SDCARD = 25; 212 private static final int MENU_INSERT_SMILEY = 26; 213 private static final int MENU_ADD_ADDRESS_TO_CONTACTS = 27; 214 private static final int MENU_LOCK_MESSAGE = 28; 215 private static final int MENU_UNLOCK_MESSAGE = 29; 216 private static final int MENU_SAVE_RINGTONE = 30; 217 private static final int MENU_PREFERENCES = 31; 218 private static final int MENU_GROUP_PARTICIPANTS = 32; 219 220 private static final int RECIPIENTS_MAX_LENGTH = 312; 221 222 private static final int MESSAGE_LIST_QUERY_TOKEN = 9527; 223 private static final int MESSAGE_LIST_QUERY_AFTER_DELETE_TOKEN = 9528; 224 225 private static final int DELETE_MESSAGE_TOKEN = 9700; 226 227 private static final int CHARS_REMAINING_BEFORE_COUNTER_SHOWN = 10; 228 229 private static final long NO_DATE_FOR_DIALOG = -1L; 230 231 private static final String EXIT_ECM_RESULT = "exit_ecm_result"; 232 233 // When the conversation has a lot of messages and a new message is sent, the list is scrolled 234 // so the user sees the just sent message. If we have to scroll the list more than 20 items, 235 // then a scroll shortcut is invoked to move the list near the end before scrolling. 236 private static final int MAX_ITEMS_TO_INVOKE_SCROLL_SHORTCUT = 20; 237 238 // Any change in height in the message list view greater than this threshold will not 239 // cause a smooth scroll. Instead, we jump the list directly to the desired position. 240 private static final int SMOOTH_SCROLL_THRESHOLD = 200; 241 242 243 private ContentResolver mContentResolver; 244 245 private BackgroundQueryHandler mBackgroundQueryHandler; 246 247 private Conversation mConversation; // Conversation we are working in 248 249 private boolean mExitOnSent; // Should we finish() after sending a message? 250 // TODO: mExitOnSent is obsolete -- remove 251 252 private View mTopPanel; // View containing the recipient and subject editors 253 private View mBottomPanel; // View containing the text editor, send button, ec. 254 private EditText mTextEditor; // Text editor to type your message into 255 private TextView mTextCounter; // Shows the number of characters used in text editor 256 private TextView mSendButtonMms; // Press to send mms 257 private ImageButton mSendButtonSms; // Press to send sms 258 private EditText mSubjectTextEditor; // Text editor for MMS subject 259 260 private AttachmentEditor mAttachmentEditor; 261 private View mAttachmentEditorScrollView; 262 263 private MessageListView mMsgListView; // ListView for messages in this conversation 264 public MessageListAdapter mMsgListAdapter; // and its corresponding ListAdapter 265 266 private RecipientsEditor mRecipientsEditor; // UI control for editing recipients 267 private ImageButton mRecipientsPicker; // UI control for recipients picker 268 269 private boolean mIsKeyboardOpen; // Whether the hardware keyboard is visible 270 private boolean mIsLandscape; // Whether we're in landscape mode 271 272 private boolean mToastForDraftSave; // Whether to notify the user that a draft is being saved 273 274 private boolean mSentMessage; // true if the user has sent a message while in this 275 // activity. On a new compose message case, when the first 276 // message is sent is a MMS w/ attachment, the list blanks 277 // for a second before showing the sent message. But we'd 278 // think the message list is empty, thus show the recipients 279 // editor thinking it's a draft message. This flag should 280 // help clarify the situation. 281 282 private WorkingMessage mWorkingMessage; // The message currently being composed. 283 284 private AlertDialog mSmileyDialog; 285 286 private boolean mWaitingForSubActivity; 287 private int mLastRecipientCount; // Used for warning the user on too many recipients. 288 private AttachmentTypeSelectorAdapter mAttachmentTypeSelectorAdapter; 289 290 private boolean mSendingMessage; // Indicates the current message is sending, and shouldn't send again. 291 292 private Intent mAddContactIntent; // Intent used to add a new contact 293 294 private Uri mTempMmsUri; // Only used as a temporary to hold a slideshow uri 295 private long mTempThreadId; // Only used as a temporary to hold a threadId 296 297 private AsyncDialog mAsyncDialog; // Used for background tasks. 298 299 private String mDebugRecipients; 300 private int mLastSmoothScrollPosition; 301 private boolean mScrollOnSend; // Flag that we need to scroll the list to the end. 302 303 private int mSavedScrollPosition = -1; // we save the ListView's scroll position in onPause(), 304 // so we can remember it after re-entering the activity. 305 // If the value >= 0, then we jump to that line. If the 306 // value is maxint, then we jump to the end. 307 private long mLastMessageId; 308 309 /** 310 * Whether this activity is currently running (i.e. not paused) 311 */ 312 private boolean mIsRunning; 313 314 // keys for extras and icicles 315 public final static String THREAD_ID = "thread_id"; 316 private final static String RECIPIENTS = "recipients"; 317 318 @SuppressWarnings("unused") 319 public static void log(String logMsg) { 320 Thread current = Thread.currentThread(); 321 long tid = current.getId(); 322 StackTraceElement[] stack = current.getStackTrace(); 323 String methodName = stack[3].getMethodName(); 324 // Prepend current thread ID and name of calling method to the message. 325 logMsg = "[" + tid + "] [" + methodName + "] " + logMsg; 326 Log.d(TAG, logMsg); 327 } 328 329 //========================================================== 330 // Inner classes 331 //========================================================== 332 333 private void editSlideshow() { 334 // The user wants to edit the slideshow. That requires us to persist the slideshow to 335 // disk as a PDU in saveAsMms. This code below does that persisting in a background 336 // task. If the task takes longer than a half second, a progress dialog is displayed. 337 // Once the PDU persisting is done, another runnable on the UI thread get executed to start 338 // the SlideshowEditActivity. 339 getAsyncDialog().runAsync(new Runnable() { 340 @Override 341 public void run() { 342 // This runnable gets run in a background thread. 343 mTempMmsUri = mWorkingMessage.saveAsMms(false); 344 } 345 }, new Runnable() { 346 @Override 347 public void run() { 348 // Once the above background thread is complete, this runnable is run 349 // on the UI thread. 350 if (mTempMmsUri == null) { 351 return; 352 } 353 Intent intent = new Intent(ComposeMessageActivity.this, 354 SlideshowEditActivity.class); 355 intent.setData(mTempMmsUri); 356 startActivityForResult(intent, REQUEST_CODE_CREATE_SLIDESHOW); 357 } 358 }, R.string.building_slideshow_title); 359 } 360 361 private final Handler mAttachmentEditorHandler = new Handler() { 362 @Override 363 public void handleMessage(Message msg) { 364 switch (msg.what) { 365 case AttachmentEditor.MSG_EDIT_SLIDESHOW: { 366 editSlideshow(); 367 break; 368 } 369 case AttachmentEditor.MSG_SEND_SLIDESHOW: { 370 if (isPreparedForSending()) { 371 ComposeMessageActivity.this.confirmSendMessageIfNeeded(); 372 } 373 break; 374 } 375 case AttachmentEditor.MSG_VIEW_IMAGE: 376 case AttachmentEditor.MSG_PLAY_VIDEO: 377 case AttachmentEditor.MSG_PLAY_AUDIO: 378 case AttachmentEditor.MSG_PLAY_SLIDESHOW: 379 viewMmsMessageAttachment(msg.what); 380 break; 381 382 case AttachmentEditor.MSG_REPLACE_IMAGE: 383 case AttachmentEditor.MSG_REPLACE_VIDEO: 384 case AttachmentEditor.MSG_REPLACE_AUDIO: 385 showAddAttachmentDialog(true); 386 break; 387 388 case AttachmentEditor.MSG_REMOVE_ATTACHMENT: 389 mWorkingMessage.removeAttachment(true); 390 break; 391 392 default: 393 break; 394 } 395 } 396 }; 397 398 399 private void viewMmsMessageAttachment(final int requestCode) { 400 SlideshowModel slideshow = mWorkingMessage.getSlideshow(); 401 if (slideshow == null) { 402 throw new IllegalStateException("mWorkingMessage.getSlideshow() == null"); 403 } 404 if (slideshow.isSimple()) { 405 MessageUtils.viewSimpleSlideshow(this, slideshow); 406 } else { 407 // The user wants to view the slideshow. That requires us to persist the slideshow to 408 // disk as a PDU in saveAsMms. This code below does that persisting in a background 409 // task. If the task takes longer than a half second, a progress dialog is displayed. 410 // Once the PDU persisting is done, another runnable on the UI thread get executed to 411 // start the SlideshowActivity. 412 getAsyncDialog().runAsync(new Runnable() { 413 @Override 414 public void run() { 415 // This runnable gets run in a background thread. 416 mTempMmsUri = mWorkingMessage.saveAsMms(false); 417 } 418 }, new Runnable() { 419 @Override 420 public void run() { 421 // Once the above background thread is complete, this runnable is run 422 // on the UI thread. 423 if (mTempMmsUri == null) { 424 return; 425 } 426 MessageUtils.launchSlideshowActivity(ComposeMessageActivity.this, mTempMmsUri, 427 requestCode); 428 } 429 }, R.string.building_slideshow_title); 430 } 431 } 432 433 434 private final Handler mMessageListItemHandler = new Handler() { 435 @Override 436 public void handleMessage(Message msg) { 437 MessageItem msgItem = (MessageItem) msg.obj; 438 if (msgItem != null) { 439 switch (msg.what) { 440 case MessageListItem.MSG_LIST_DETAILS: 441 showMessageDetails(msgItem); 442 break; 443 444 case MessageListItem.MSG_LIST_EDIT: 445 editMessageItem(msgItem); 446 drawBottomPanel(); 447 break; 448 449 case MessageListItem.MSG_LIST_PLAY: 450 switch (msgItem.mAttachmentType) { 451 case WorkingMessage.IMAGE: 452 case WorkingMessage.VIDEO: 453 case WorkingMessage.AUDIO: 454 case WorkingMessage.SLIDESHOW: 455 MessageUtils.viewMmsMessageAttachment(ComposeMessageActivity.this, 456 msgItem.mMessageUri, msgItem.mSlideshow, 457 getAsyncDialog()); 458 break; 459 } 460 break; 461 462 default: 463 Log.w(TAG, "Unknown message: " + msg.what); 464 return; 465 } 466 } 467 } 468 }; 469 470 private boolean showMessageDetails(MessageItem msgItem) { 471 Cursor cursor = mMsgListAdapter.getCursorForItem(msgItem); 472 if (cursor == null) { 473 return false; 474 } 475 String messageDetails = MessageUtils.getMessageDetails( 476 ComposeMessageActivity.this, cursor, msgItem.mMessageSize); 477 new AlertDialog.Builder(ComposeMessageActivity.this) 478 .setTitle(R.string.message_details_title) 479 .setMessage(messageDetails) 480 .setCancelable(true) 481 .show(); 482 return true; 483 } 484 485 private final OnKeyListener mSubjectKeyListener = new OnKeyListener() { 486 @Override 487 public boolean onKey(View v, int keyCode, KeyEvent event) { 488 if (event.getAction() != KeyEvent.ACTION_DOWN) { 489 return false; 490 } 491 492 // When the subject editor is empty, press "DEL" to hide the input field. 493 if ((keyCode == KeyEvent.KEYCODE_DEL) && (mSubjectTextEditor.length() == 0)) { 494 showSubjectEditor(false); 495 mWorkingMessage.setSubject(null, true); 496 return true; 497 } 498 return false; 499 } 500 }; 501 502 /** 503 * Return the messageItem associated with the type ("mms" or "sms") and message id. 504 * @param type Type of the message: "mms" or "sms" 505 * @param msgId Message id of the message. This is the _id of the sms or pdu row and is 506 * stored in the MessageItem 507 * @param createFromCursorIfNotInCache true if the item is not found in the MessageListAdapter's 508 * cache and the code can create a new MessageItem based on the position of the current cursor. 509 * If false, the function returns null if the MessageItem isn't in the cache. 510 * @return MessageItem or null if not found and createFromCursorIfNotInCache is false 511 */ 512 private MessageItem getMessageItem(String type, long msgId, 513 boolean createFromCursorIfNotInCache) { 514 return mMsgListAdapter.getCachedMessageItem(type, msgId, 515 createFromCursorIfNotInCache ? mMsgListAdapter.getCursor() : null); 516 } 517 518 private boolean isCursorValid() { 519 // Check whether the cursor is valid or not. 520 Cursor cursor = mMsgListAdapter.getCursor(); 521 if (cursor.isClosed() || cursor.isBeforeFirst() || cursor.isAfterLast()) { 522 Log.e(TAG, "Bad cursor.", new RuntimeException()); 523 return false; 524 } 525 return true; 526 } 527 528 private void resetCounter() { 529 mTextCounter.setText(""); 530 mTextCounter.setVisibility(View.GONE); 531 } 532 533 private void updateCounter(CharSequence text, int start, int before, int count) { 534 WorkingMessage workingMessage = mWorkingMessage; 535 if (workingMessage.requiresMms()) { 536 // If we're not removing text (i.e. no chance of converting back to SMS 537 // because of this change) and we're in MMS mode, just bail out since we 538 // then won't have to calculate the length unnecessarily. 539 final boolean textRemoved = (before > count); 540 if (!textRemoved) { 541 showSmsOrMmsSendButton(workingMessage.requiresMms()); 542 return; 543 } 544 } 545 546 int[] params = SmsMessage.calculateLength(text, false); 547 /* SmsMessage.calculateLength returns an int[4] with: 548 * int[0] being the number of SMS's required, 549 * int[1] the number of code units used, 550 * int[2] is the number of code units remaining until the next message. 551 * int[3] is the encoding type that should be used for the message. 552 */ 553 int msgCount = params[0]; 554 int remainingInCurrentMessage = params[2]; 555 556 if (!MmsConfig.getMultipartSmsEnabled()) { 557 // The provider doesn't support multi-part sms's so as soon as the user types 558 // an sms longer than one segment, we have to turn the message into an mms. 559 mWorkingMessage.setLengthRequiresMms(msgCount > 1, true); 560 } else { 561 int threshold = MmsConfig.getSmsToMmsTextThreshold(); 562 mWorkingMessage.setLengthRequiresMms(threshold > 0 && msgCount > threshold, true); 563 } 564 565 // Show the counter only if: 566 // - We are not in MMS mode 567 // - We are going to send more than one message OR we are getting close 568 boolean showCounter = false; 569 if (!workingMessage.requiresMms() && 570 (msgCount > 1 || 571 remainingInCurrentMessage <= CHARS_REMAINING_BEFORE_COUNTER_SHOWN)) { 572 showCounter = true; 573 } 574 575 showSmsOrMmsSendButton(workingMessage.requiresMms()); 576 577 if (showCounter) { 578 // Update the remaining characters and number of messages required. 579 String counterText = msgCount > 1 ? remainingInCurrentMessage + " / " + msgCount 580 : String.valueOf(remainingInCurrentMessage); 581 mTextCounter.setText(counterText); 582 mTextCounter.setVisibility(View.VISIBLE); 583 } else { 584 mTextCounter.setVisibility(View.GONE); 585 } 586 } 587 588 @Override 589 public void startActivityForResult(Intent intent, int requestCode) 590 { 591 // requestCode >= 0 means the activity in question is a sub-activity. 592 if (requestCode >= 0) { 593 mWaitingForSubActivity = true; 594 } 595 // The camera and other activities take a long time to hide the keyboard so we pre-hide 596 // it here. However, if we're opening up the quick contact window while typing, don't 597 // mess with the keyboard. 598 if (mIsKeyboardOpen && !QuickContact.ACTION_QUICK_CONTACT.equals(intent.getAction())) { 599 hideKeyboard(); 600 } 601 602 super.startActivityForResult(intent, requestCode); 603 } 604 605 private void showConvertToMmsToast() { 606 Toast.makeText(this, R.string.converting_to_picture_message, Toast.LENGTH_SHORT).show(); 607 } 608 609 private class DeleteMessageListener implements OnClickListener { 610 private final MessageItem mMessageItem; 611 612 public DeleteMessageListener(MessageItem messageItem) { 613 mMessageItem = messageItem; 614 } 615 616 @Override 617 public void onClick(DialogInterface dialog, int whichButton) { 618 dialog.dismiss(); 619 620 new AsyncTask<Void, Void, Void>() { 621 protected Void doInBackground(Void... none) { 622 if (mMessageItem.isMms()) { 623 WorkingMessage.removeThumbnailsFromCache(mMessageItem.getSlideshow()); 624 625 MmsApp.getApplication().getPduLoaderManager() 626 .removePdu(mMessageItem.mMessageUri); 627 // Delete the message *after* we've removed the thumbnails because we 628 // need the pdu and slideshow for removeThumbnailsFromCache to work. 629 } 630 Boolean deletingLastItem = false; 631 Cursor cursor = mMsgListAdapter != null ? mMsgListAdapter.getCursor() : null; 632 if (cursor != null) { 633 cursor.moveToLast(); 634 long msgId = cursor.getLong(COLUMN_ID); 635 deletingLastItem = msgId == mMessageItem.mMsgId; 636 } 637 mBackgroundQueryHandler.startDelete(DELETE_MESSAGE_TOKEN, 638 deletingLastItem, mMessageItem.mMessageUri, 639 mMessageItem.mLocked ? null : "locked=0", null); 640 return null; 641 } 642 }.execute(); 643 } 644 } 645 646 private class DiscardDraftListener implements OnClickListener { 647 @Override 648 public void onClick(DialogInterface dialog, int whichButton) { 649 mWorkingMessage.discard(); 650 dialog.dismiss(); 651 finish(); 652 } 653 } 654 655 private class SendIgnoreInvalidRecipientListener implements OnClickListener { 656 @Override 657 public void onClick(DialogInterface dialog, int whichButton) { 658 sendMessage(true); 659 dialog.dismiss(); 660 } 661 } 662 663 private class CancelSendingListener implements OnClickListener { 664 @Override 665 public void onClick(DialogInterface dialog, int whichButton) { 666 if (isRecipientsEditorVisible()) { 667 mRecipientsEditor.requestFocus(); 668 } 669 dialog.dismiss(); 670 } 671 } 672 673 private void confirmSendMessageIfNeeded() { 674 if (!isRecipientsEditorVisible()) { 675 sendMessage(true); 676 return; 677 } 678 679 boolean isMms = mWorkingMessage.requiresMms(); 680 if (mRecipientsEditor.hasInvalidRecipient(isMms)) { 681 if (mRecipientsEditor.hasValidRecipient(isMms)) { 682 String title = getResourcesString(R.string.has_invalid_recipient, 683 mRecipientsEditor.formatInvalidNumbers(isMms)); 684 new AlertDialog.Builder(this) 685 .setTitle(title) 686 .setMessage(R.string.invalid_recipient_message) 687 .setPositiveButton(R.string.try_to_send, 688 new SendIgnoreInvalidRecipientListener()) 689 .setNegativeButton(R.string.no, new CancelSendingListener()) 690 .show(); 691 } else { 692 new AlertDialog.Builder(this) 693 .setTitle(R.string.cannot_send_message) 694 .setMessage(R.string.cannot_send_message_reason) 695 .setPositiveButton(R.string.yes, new CancelSendingListener()) 696 .show(); 697 } 698 } else { 699 // The recipients editor is still open. Make sure we use what's showing there 700 // as the destination. 701 ContactList contacts = mRecipientsEditor.constructContactsFromInput(false); 702 mDebugRecipients = contacts.serialize(); 703 sendMessage(true); 704 } 705 } 706 707 private final TextWatcher mRecipientsWatcher = new TextWatcher() { 708 @Override 709 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 710 } 711 712 @Override 713 public void onTextChanged(CharSequence s, int start, int before, int count) { 714 // This is a workaround for bug 1609057. Since onUserInteraction() is 715 // not called when the user touches the soft keyboard, we pretend it was 716 // called when textfields changes. This should be removed when the bug 717 // is fixed. 718 onUserInteraction(); 719 } 720 721 @Override 722 public void afterTextChanged(Editable s) { 723 // Bug 1474782 describes a situation in which we send to 724 // the wrong recipient. We have been unable to reproduce this, 725 // but the best theory we have so far is that the contents of 726 // mRecipientList somehow become stale when entering 727 // ComposeMessageActivity via onNewIntent(). This assertion is 728 // meant to catch one possible path to that, of a non-visible 729 // mRecipientsEditor having its TextWatcher fire and refreshing 730 // mRecipientList with its stale contents. 731 if (!isRecipientsEditorVisible()) { 732 IllegalStateException e = new IllegalStateException( 733 "afterTextChanged called with invisible mRecipientsEditor"); 734 // Make sure the crash is uploaded to the service so we 735 // can see if this is happening in the field. 736 Log.w(TAG, 737 "RecipientsWatcher: afterTextChanged called with invisible mRecipientsEditor"); 738 return; 739 } 740 741 List<String> numbers = mRecipientsEditor.getNumbers(); 742 mWorkingMessage.setWorkingRecipients(numbers); 743 boolean multiRecipients = numbers != null && numbers.size() > 1; 744 mMsgListAdapter.setIsGroupConversation(multiRecipients); 745 mWorkingMessage.setHasMultipleRecipients(multiRecipients, true); 746 mWorkingMessage.setHasEmail(mRecipientsEditor.containsEmail(), true); 747 748 checkForTooManyRecipients(); 749 750 // Walk backwards in the text box, skipping spaces. If the last 751 // character is a comma, update the title bar. 752 for (int pos = s.length() - 1; pos >= 0; pos--) { 753 char c = s.charAt(pos); 754 if (c == ' ') 755 continue; 756 757 if (c == ',') { 758 ContactList contacts = mRecipientsEditor.constructContactsFromInput(false); 759 updateTitle(contacts); 760 } 761 762 break; 763 } 764 765 // If we have gone to zero recipients, disable send button. 766 updateSendButtonState(); 767 } 768 }; 769 770 private void checkForTooManyRecipients() { 771 final int recipientLimit = MmsConfig.getRecipientLimit(); 772 if (recipientLimit != Integer.MAX_VALUE) { 773 final int recipientCount = recipientCount(); 774 boolean tooMany = recipientCount > recipientLimit; 775 776 if (recipientCount != mLastRecipientCount) { 777 // Don't warn the user on every character they type when they're over the limit, 778 // only when the actual # of recipients changes. 779 mLastRecipientCount = recipientCount; 780 if (tooMany) { 781 String tooManyMsg = getString(R.string.too_many_recipients, recipientCount, 782 recipientLimit); 783 Toast.makeText(ComposeMessageActivity.this, 784 tooManyMsg, Toast.LENGTH_LONG).show(); 785 } 786 } 787 } 788 } 789 790 private final OnCreateContextMenuListener mRecipientsMenuCreateListener = 791 new OnCreateContextMenuListener() { 792 @Override 793 public void onCreateContextMenu(ContextMenu menu, View v, 794 ContextMenuInfo menuInfo) { 795 if (menuInfo != null) { 796 Contact c = ((RecipientContextMenuInfo) menuInfo).recipient; 797 RecipientsMenuClickListener l = new RecipientsMenuClickListener(c); 798 799 menu.setHeaderTitle(c.getName()); 800 801 if (c.existsInDatabase()) { 802 menu.add(0, MENU_VIEW_CONTACT, 0, R.string.menu_view_contact) 803 .setOnMenuItemClickListener(l); 804 } else if (canAddToContacts(c)){ 805 menu.add(0, MENU_ADD_TO_CONTACTS, 0, R.string.menu_add_to_contacts) 806 .setOnMenuItemClickListener(l); 807 } 808 } 809 } 810 }; 811 812 private final class RecipientsMenuClickListener implements MenuItem.OnMenuItemClickListener { 813 private final Contact mRecipient; 814 815 RecipientsMenuClickListener(Contact recipient) { 816 mRecipient = recipient; 817 } 818 819 @Override 820 public boolean onMenuItemClick(MenuItem item) { 821 switch (item.getItemId()) { 822 // Context menu handlers for the recipients editor. 823 case MENU_VIEW_CONTACT: { 824 Uri contactUri = mRecipient.getUri(); 825 Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); 826 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 827 startActivity(intent); 828 return true; 829 } 830 case MENU_ADD_TO_CONTACTS: { 831 mAddContactIntent = ConversationList.createAddContactIntent( 832 mRecipient.getNumber()); 833 ComposeMessageActivity.this.startActivityForResult(mAddContactIntent, 834 REQUEST_CODE_ADD_CONTACT); 835 return true; 836 } 837 } 838 return false; 839 } 840 } 841 842 private boolean canAddToContacts(Contact contact) { 843 // There are some kind of automated messages, like STK messages, that we don't want 844 // to add to contacts. These names begin with special characters, like, "*Info". 845 final String name = contact.getName(); 846 if (!TextUtils.isEmpty(contact.getNumber())) { 847 char c = contact.getNumber().charAt(0); 848 if (isSpecialChar(c)) { 849 return false; 850 } 851 } 852 if (!TextUtils.isEmpty(name)) { 853 char c = name.charAt(0); 854 if (isSpecialChar(c)) { 855 return false; 856 } 857 } 858 if (!(Mms.isEmailAddress(name) || 859 Telephony.Mms.isPhoneNumber(name) || 860 contact.isMe())) { 861 return false; 862 } 863 return true; 864 } 865 866 private boolean isSpecialChar(char c) { 867 return c == '*' || c == '%' || c == '$'; 868 } 869 870 private void addPositionBasedMenuItems(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 871 AdapterView.AdapterContextMenuInfo info; 872 873 try { 874 info = (AdapterView.AdapterContextMenuInfo) menuInfo; 875 } catch (ClassCastException e) { 876 Log.e(TAG, "bad menuInfo"); 877 return; 878 } 879 final int position = info.position; 880 881 addUriSpecificMenuItems(menu, v, position); 882 } 883 884 private Uri getSelectedUriFromMessageList(ListView listView, int position) { 885 // If the context menu was opened over a uri, get that uri. 886 MessageListItem msglistItem = (MessageListItem) listView.getChildAt(position); 887 if (msglistItem == null) { 888 // FIXME: Should get the correct view. No such interface in ListView currently 889 // to get the view by position. The ListView.getChildAt(position) cannot 890 // get correct view since the list doesn't create one child for each item. 891 // And if setSelection(position) then getSelectedView(), 892 // cannot get corrent view when in touch mode. 893 return null; 894 } 895 896 TextView textView; 897 CharSequence text = null; 898 int selStart = -1; 899 int selEnd = -1; 900 901 //check if message sender is selected 902 textView = (TextView) msglistItem.findViewById(R.id.text_view); 903 if (textView != null) { 904 text = textView.getText(); 905 selStart = textView.getSelectionStart(); 906 selEnd = textView.getSelectionEnd(); 907 } 908 909 // Check that some text is actually selected, rather than the cursor 910 // just being placed within the TextView. 911 if (selStart != selEnd) { 912 int min = Math.min(selStart, selEnd); 913 int max = Math.max(selStart, selEnd); 914 915 URLSpan[] urls = ((Spanned) text).getSpans(min, max, 916 URLSpan.class); 917 918 if (urls.length == 1) { 919 return Uri.parse(urls[0].getURL()); 920 } 921 } 922 923 //no uri was selected 924 return null; 925 } 926 927 private void addUriSpecificMenuItems(ContextMenu menu, View v, int position) { 928 Uri uri = getSelectedUriFromMessageList((ListView) v, position); 929 930 if (uri != null) { 931 Intent intent = new Intent(null, uri); 932 intent.addCategory(Intent.CATEGORY_SELECTED_ALTERNATIVE); 933 menu.addIntentOptions(0, 0, 0, 934 new android.content.ComponentName(this, ComposeMessageActivity.class), 935 null, intent, 0, null); 936 } 937 } 938 939 private final void addCallAndContactMenuItems( 940 ContextMenu menu, MsgListMenuClickListener l, MessageItem msgItem) { 941 if (TextUtils.isEmpty(msgItem.mBody)) { 942 return; 943 } 944 SpannableString msg = new SpannableString(msgItem.mBody); 945 Linkify.addLinks(msg, Linkify.ALL); 946 ArrayList<String> uris = 947 MessageUtils.extractUris(msg.getSpans(0, msg.length(), URLSpan.class)); 948 949 // Remove any dupes so they don't get added to the menu multiple times 950 HashSet<String> collapsedUris = new HashSet<String>(); 951 for (String uri : uris) { 952 collapsedUris.add(uri.toLowerCase()); 953 } 954 for (String uriString : collapsedUris) { 955 String prefix = null; 956 int sep = uriString.indexOf(":"); 957 if (sep >= 0) { 958 prefix = uriString.substring(0, sep); 959 uriString = uriString.substring(sep + 1); 960 } 961 Uri contactUri = null; 962 boolean knownPrefix = true; 963 if ("mailto".equalsIgnoreCase(prefix)) { 964 contactUri = getContactUriForEmail(uriString); 965 } else if ("tel".equalsIgnoreCase(prefix)) { 966 contactUri = getContactUriForPhoneNumber(uriString); 967 } else { 968 knownPrefix = false; 969 } 970 if (knownPrefix && contactUri == null) { 971 Intent intent = ConversationList.createAddContactIntent(uriString); 972 973 String addContactString = getString(R.string.menu_add_address_to_contacts, 974 uriString); 975 menu.add(0, MENU_ADD_ADDRESS_TO_CONTACTS, 0, addContactString) 976 .setOnMenuItemClickListener(l) 977 .setIntent(intent); 978 } 979 } 980 } 981 982 private Uri getContactUriForEmail(String emailAddress) { 983 Cursor cursor = SqliteWrapper.query(this, getContentResolver(), 984 Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(emailAddress)), 985 new String[] { Email.CONTACT_ID, Contacts.DISPLAY_NAME }, null, null, null); 986 987 if (cursor != null) { 988 try { 989 while (cursor.moveToNext()) { 990 String name = cursor.getString(1); 991 if (!TextUtils.isEmpty(name)) { 992 return ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0)); 993 } 994 } 995 } finally { 996 cursor.close(); 997 } 998 } 999 return null; 1000 } 1001 1002 private Uri getContactUriForPhoneNumber(String phoneNumber) { 1003 Contact contact = Contact.get(phoneNumber, false); 1004 if (contact.existsInDatabase()) { 1005 return contact.getUri(); 1006 } 1007 return null; 1008 } 1009 1010 private final OnCreateContextMenuListener mMsgListMenuCreateListener = 1011 new OnCreateContextMenuListener() { 1012 @Override 1013 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 1014 if (!isCursorValid()) { 1015 return; 1016 } 1017 Cursor cursor = mMsgListAdapter.getCursor(); 1018 String type = cursor.getString(COLUMN_MSG_TYPE); 1019 long msgId = cursor.getLong(COLUMN_ID); 1020 1021 addPositionBasedMenuItems(menu, v, menuInfo); 1022 1023 MessageItem msgItem = mMsgListAdapter.getCachedMessageItem(type, msgId, cursor); 1024 if (msgItem == null) { 1025 Log.e(TAG, "Cannot load message item for type = " + type 1026 + ", msgId = " + msgId); 1027 return; 1028 } 1029 1030 menu.setHeaderTitle(R.string.message_options); 1031 1032 MsgListMenuClickListener l = new MsgListMenuClickListener(msgItem); 1033 1034 // It is unclear what would make most sense for copying an MMS message 1035 // to the clipboard, so we currently do SMS only. 1036 if (msgItem.isSms()) { 1037 // Message type is sms. Only allow "edit" if the message has a single recipient 1038 if (getRecipients().size() == 1 && 1039 (msgItem.mBoxId == Sms.MESSAGE_TYPE_OUTBOX || 1040 msgItem.mBoxId == Sms.MESSAGE_TYPE_FAILED)) { 1041 menu.add(0, MENU_EDIT_MESSAGE, 0, R.string.menu_edit) 1042 .setOnMenuItemClickListener(l); 1043 } 1044 1045 menu.add(0, MENU_COPY_MESSAGE_TEXT, 0, R.string.copy_message_text) 1046 .setOnMenuItemClickListener(l); 1047 } 1048 1049 addCallAndContactMenuItems(menu, l, msgItem); 1050 1051 // Forward is not available for undownloaded messages. 1052 if (msgItem.isDownloaded() && (msgItem.isSms() || isForwardable(msgId))) { 1053 menu.add(0, MENU_FORWARD_MESSAGE, 0, R.string.menu_forward) 1054 .setOnMenuItemClickListener(l); 1055 } 1056 1057 if (msgItem.isMms()) { 1058 switch (msgItem.mBoxId) { 1059 case Mms.MESSAGE_BOX_INBOX: 1060 break; 1061 case Mms.MESSAGE_BOX_OUTBOX: 1062 // Since we currently break outgoing messages to multiple 1063 // recipients into one message per recipient, only allow 1064 // editing a message for single-recipient conversations. 1065 if (getRecipients().size() == 1) { 1066 menu.add(0, MENU_EDIT_MESSAGE, 0, R.string.menu_edit) 1067 .setOnMenuItemClickListener(l); 1068 } 1069 break; 1070 } 1071 switch (msgItem.mAttachmentType) { 1072 case WorkingMessage.TEXT: 1073 break; 1074 case WorkingMessage.VIDEO: 1075 case WorkingMessage.IMAGE: 1076 if (haveSomethingToCopyToSDCard(msgItem.mMsgId)) { 1077 menu.add(0, MENU_COPY_TO_SDCARD, 0, R.string.copy_to_sdcard) 1078 .setOnMenuItemClickListener(l); 1079 } 1080 break; 1081 case WorkingMessage.SLIDESHOW: 1082 default: 1083 menu.add(0, MENU_VIEW_SLIDESHOW, 0, R.string.view_slideshow) 1084 .setOnMenuItemClickListener(l); 1085 if (haveSomethingToCopyToSDCard(msgItem.mMsgId)) { 1086 menu.add(0, MENU_COPY_TO_SDCARD, 0, R.string.copy_to_sdcard) 1087 .setOnMenuItemClickListener(l); 1088 } 1089 if (isDrmRingtoneWithRights(msgItem.mMsgId)) { 1090 menu.add(0, MENU_SAVE_RINGTONE, 0, 1091 getDrmMimeMenuStringRsrc(msgItem.mMsgId)) 1092 .setOnMenuItemClickListener(l); 1093 } 1094 break; 1095 } 1096 } 1097 1098 if (msgItem.mLocked) { 1099 menu.add(0, MENU_UNLOCK_MESSAGE, 0, R.string.menu_unlock) 1100 .setOnMenuItemClickListener(l); 1101 } else { 1102 menu.add(0, MENU_LOCK_MESSAGE, 0, R.string.menu_lock) 1103 .setOnMenuItemClickListener(l); 1104 } 1105 1106 menu.add(0, MENU_VIEW_MESSAGE_DETAILS, 0, R.string.view_message_details) 1107 .setOnMenuItemClickListener(l); 1108 1109 if (msgItem.mDeliveryStatus != MessageItem.DeliveryStatus.NONE || msgItem.mReadReport) { 1110 menu.add(0, MENU_DELIVERY_REPORT, 0, R.string.view_delivery_report) 1111 .setOnMenuItemClickListener(l); 1112 } 1113 1114 menu.add(0, MENU_DELETE_MESSAGE, 0, R.string.delete_message) 1115 .setOnMenuItemClickListener(l); 1116 } 1117 }; 1118 1119 private void editMessageItem(MessageItem msgItem) { 1120 if ("sms".equals(msgItem.mType)) { 1121 editSmsMessageItem(msgItem); 1122 } else { 1123 editMmsMessageItem(msgItem); 1124 } 1125 if (msgItem.isFailedMessage() && mMsgListAdapter.getCount() <= 1) { 1126 // For messages with bad addresses, let the user re-edit the recipients. 1127 initRecipientsEditor(); 1128 } 1129 } 1130 1131 private void editSmsMessageItem(MessageItem msgItem) { 1132 // When the message being edited is the only message in the conversation, the delete 1133 // below does something subtle. The trigger "delete_obsolete_threads_pdu" sees that a 1134 // thread contains no messages and silently deletes the thread. Meanwhile, the mConversation 1135 // object still holds onto the old thread_id and code thinks there's a backing thread in 1136 // the DB when it really has been deleted. Here we try and notice that situation and 1137 // clear out the thread_id. Later on, when Conversation.ensureThreadId() is called, we'll 1138 // create a new thread if necessary. 1139 synchronized(mConversation) { 1140 if (mConversation.getMessageCount() <= 1) { 1141 mConversation.clearThreadId(); 1142 MessagingNotification.setCurrentlyDisplayedThreadId( 1143 MessagingNotification.THREAD_NONE); 1144 } 1145 } 1146 // Delete the old undelivered SMS and load its content. 1147 Uri uri = ContentUris.withAppendedId(Sms.CONTENT_URI, msgItem.mMsgId); 1148 SqliteWrapper.delete(ComposeMessageActivity.this, 1149 mContentResolver, uri, null, null); 1150 1151 mWorkingMessage.setText(msgItem.mBody); 1152 } 1153 1154 private void editMmsMessageItem(MessageItem msgItem) { 1155 // Load the selected message in as the working message. 1156 WorkingMessage newWorkingMessage = WorkingMessage.load(this, msgItem.mMessageUri); 1157 if (newWorkingMessage == null) { 1158 return; 1159 } 1160 1161 // Discard the current message in progress. 1162 mWorkingMessage.discard(); 1163 1164 mWorkingMessage = newWorkingMessage; 1165 mWorkingMessage.setConversation(mConversation); 1166 1167 drawTopPanel(false); 1168 1169 // WorkingMessage.load() above only loads the slideshow. Set the 1170 // subject here because we already know what it is and avoid doing 1171 // another DB lookup in load() just to get it. 1172 mWorkingMessage.setSubject(msgItem.mSubject, false); 1173 1174 if (mWorkingMessage.hasSubject()) { 1175 showSubjectEditor(true); 1176 } 1177 } 1178 1179 private void copyToClipboard(String str) { 1180 ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 1181 clipboard.setPrimaryClip(ClipData.newPlainText(null, str)); 1182 } 1183 1184 private void forwardMessage(final MessageItem msgItem) { 1185 mTempThreadId = 0; 1186 // The user wants to forward the message. If the message is an mms message, we need to 1187 // persist the pdu to disk. This is done in a background task. 1188 // If the task takes longer than a half second, a progress dialog is displayed. 1189 // Once the PDU persisting is done, another runnable on the UI thread get executed to start 1190 // the ForwardMessageActivity. 1191 getAsyncDialog().runAsync(new Runnable() { 1192 @Override 1193 public void run() { 1194 // This runnable gets run in a background thread. 1195 if (msgItem.mType.equals("mms")) { 1196 SendReq sendReq = new SendReq(); 1197 String subject = getString(R.string.forward_prefix); 1198 if (msgItem.mSubject != null) { 1199 subject += msgItem.mSubject; 1200 } 1201 sendReq.setSubject(new EncodedStringValue(subject)); 1202 sendReq.setBody(msgItem.mSlideshow.makeCopy()); 1203 1204 mTempMmsUri = null; 1205 try { 1206 PduPersister persister = 1207 PduPersister.getPduPersister(ComposeMessageActivity.this); 1208 // Copy the parts of the message here. 1209 mTempMmsUri = persister.persist(sendReq, Mms.Draft.CONTENT_URI, true, 1210 MessagingPreferenceActivity 1211 .getIsGroupMmsEnabled(ComposeMessageActivity.this)); 1212 mTempThreadId = MessagingNotification.getThreadId( 1213 ComposeMessageActivity.this, mTempMmsUri); 1214 } catch (MmsException e) { 1215 Log.e(TAG, "Failed to copy message: " + msgItem.mMessageUri); 1216 Toast.makeText(ComposeMessageActivity.this, 1217 R.string.cannot_save_message, Toast.LENGTH_SHORT).show(); 1218 return; 1219 } 1220 } 1221 } 1222 }, new Runnable() { 1223 @Override 1224 public void run() { 1225 // Once the above background thread is complete, this runnable is run 1226 // on the UI thread. 1227 Intent intent = createIntent(ComposeMessageActivity.this, 0); 1228 1229 intent.putExtra("exit_on_sent", true); 1230 intent.putExtra("forwarded_message", true); 1231 if (mTempThreadId > 0) { 1232 intent.putExtra(THREAD_ID, mTempThreadId); 1233 } 1234 1235 if (msgItem.mType.equals("sms")) { 1236 intent.putExtra("sms_body", msgItem.mBody); 1237 } else { 1238 intent.putExtra("msg_uri", mTempMmsUri); 1239 String subject = getString(R.string.forward_prefix); 1240 if (msgItem.mSubject != null) { 1241 subject += msgItem.mSubject; 1242 } 1243 intent.putExtra("subject", subject); 1244 } 1245 // ForwardMessageActivity is simply an alias in the manifest for 1246 // ComposeMessageActivity. We have to make an alias because ComposeMessageActivity 1247 // launch flags specify singleTop. When we forward a message, we want to start a 1248 // separate ComposeMessageActivity. The only way to do that is to override the 1249 // singleTop flag, which is impossible to do in code. By creating an alias to the 1250 // activity, without the singleTop flag, we can launch a separate 1251 // ComposeMessageActivity to edit the forward message. 1252 intent.setClassName(ComposeMessageActivity.this, 1253 "com.android.mms.ui.ForwardMessageActivity"); 1254 startActivity(intent); 1255 } 1256 }, R.string.building_slideshow_title); 1257 } 1258 1259 /** 1260 * Context menu handlers for the message list view. 1261 */ 1262 private final class MsgListMenuClickListener implements MenuItem.OnMenuItemClickListener { 1263 private MessageItem mMsgItem; 1264 1265 public MsgListMenuClickListener(MessageItem msgItem) { 1266 mMsgItem = msgItem; 1267 } 1268 1269 @Override 1270 public boolean onMenuItemClick(MenuItem item) { 1271 if (mMsgItem == null) { 1272 return false; 1273 } 1274 1275 switch (item.getItemId()) { 1276 case MENU_EDIT_MESSAGE: 1277 editMessageItem(mMsgItem); 1278 drawBottomPanel(); 1279 return true; 1280 1281 case MENU_COPY_MESSAGE_TEXT: 1282 copyToClipboard(mMsgItem.mBody); 1283 return true; 1284 1285 case MENU_FORWARD_MESSAGE: 1286 forwardMessage(mMsgItem); 1287 return true; 1288 1289 case MENU_VIEW_SLIDESHOW: 1290 MessageUtils.viewMmsMessageAttachment(ComposeMessageActivity.this, 1291 ContentUris.withAppendedId(Mms.CONTENT_URI, mMsgItem.mMsgId), null, 1292 getAsyncDialog()); 1293 return true; 1294 1295 case MENU_VIEW_MESSAGE_DETAILS: 1296 return showMessageDetails(mMsgItem); 1297 1298 case MENU_DELETE_MESSAGE: { 1299 DeleteMessageListener l = new DeleteMessageListener(mMsgItem); 1300 confirmDeleteDialog(l, mMsgItem.mLocked); 1301 return true; 1302 } 1303 case MENU_DELIVERY_REPORT: 1304 showDeliveryReport(mMsgItem.mMsgId, mMsgItem.mType); 1305 return true; 1306 1307 case MENU_COPY_TO_SDCARD: { 1308 int resId = copyMedia(mMsgItem.mMsgId) ? R.string.copy_to_sdcard_success : 1309 R.string.copy_to_sdcard_fail; 1310 Toast.makeText(ComposeMessageActivity.this, resId, Toast.LENGTH_SHORT).show(); 1311 return true; 1312 } 1313 1314 case MENU_SAVE_RINGTONE: { 1315 int resId = getDrmMimeSavedStringRsrc(mMsgItem.mMsgId, 1316 saveRingtone(mMsgItem.mMsgId)); 1317 Toast.makeText(ComposeMessageActivity.this, resId, Toast.LENGTH_SHORT).show(); 1318 return true; 1319 } 1320 1321 case MENU_LOCK_MESSAGE: { 1322 lockMessage(mMsgItem, true); 1323 return true; 1324 } 1325 1326 case MENU_UNLOCK_MESSAGE: { 1327 lockMessage(mMsgItem, false); 1328 return true; 1329 } 1330 1331 default: 1332 return false; 1333 } 1334 } 1335 } 1336 1337 private void lockMessage(MessageItem msgItem, boolean locked) { 1338 Uri uri; 1339 if ("sms".equals(msgItem.mType)) { 1340 uri = Sms.CONTENT_URI; 1341 } else { 1342 uri = Mms.CONTENT_URI; 1343 } 1344 final Uri lockUri = ContentUris.withAppendedId(uri, msgItem.mMsgId); 1345 1346 final ContentValues values = new ContentValues(1); 1347 values.put("locked", locked ? 1 : 0); 1348 1349 new Thread(new Runnable() { 1350 @Override 1351 public void run() { 1352 getContentResolver().update(lockUri, 1353 values, null, null); 1354 } 1355 }, "ComposeMessageActivity.lockMessage").start(); 1356 } 1357 1358 /** 1359 * Looks to see if there are any valid parts of the attachment that can be copied to a SD card. 1360 * @param msgId 1361 */ 1362 private boolean haveSomethingToCopyToSDCard(long msgId) { 1363 PduBody body = null; 1364 try { 1365 body = SlideshowModel.getPduBody(this, 1366 ContentUris.withAppendedId(Mms.CONTENT_URI, msgId)); 1367 } catch (MmsException e) { 1368 Log.e(TAG, "haveSomethingToCopyToSDCard can't load pdu body: " + msgId); 1369 } 1370 if (body == null) { 1371 return false; 1372 } 1373 1374 boolean result = false; 1375 int partNum = body.getPartsNum(); 1376 for(int i = 0; i < partNum; i++) { 1377 PduPart part = body.getPart(i); 1378 String type = new String(part.getContentType()); 1379 1380 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 1381 log("[CMA] haveSomethingToCopyToSDCard: part[" + i + "] contentType=" + type); 1382 } 1383 1384 if (ContentType.isImageType(type) || ContentType.isVideoType(type) || 1385 ContentType.isAudioType(type) || DrmUtils.isDrmType(type)) { 1386 result = true; 1387 break; 1388 } 1389 } 1390 return result; 1391 } 1392 1393 /** 1394 * Copies media from an Mms to the DrmProvider 1395 * @param msgId 1396 */ 1397 private boolean saveRingtone(long msgId) { 1398 boolean result = true; 1399 PduBody body = null; 1400 try { 1401 body = SlideshowModel.getPduBody(this, 1402 ContentUris.withAppendedId(Mms.CONTENT_URI, msgId)); 1403 } catch (MmsException e) { 1404 Log.e(TAG, "copyToDrmProvider can't load pdu body: " + msgId); 1405 } 1406 if (body == null) { 1407 return false; 1408 } 1409 1410 int partNum = body.getPartsNum(); 1411 for(int i = 0; i < partNum; i++) { 1412 PduPart part = body.getPart(i); 1413 String type = new String(part.getContentType()); 1414 1415 if (DrmUtils.isDrmType(type)) { 1416 // All parts (but there's probably only a single one) have to be successful 1417 // for a valid result. 1418 result &= copyPart(part, Long.toHexString(msgId)); 1419 } 1420 } 1421 return result; 1422 } 1423 1424 /** 1425 * Returns true if any part is drm'd audio with ringtone rights. 1426 * @param msgId 1427 * @return true if one of the parts is drm'd audio with rights to save as a ringtone. 1428 */ 1429 private boolean isDrmRingtoneWithRights(long msgId) { 1430 PduBody body = null; 1431 try { 1432 body = SlideshowModel.getPduBody(this, 1433 ContentUris.withAppendedId(Mms.CONTENT_URI, msgId)); 1434 } catch (MmsException e) { 1435 Log.e(TAG, "isDrmRingtoneWithRights can't load pdu body: " + msgId); 1436 } 1437 if (body == null) { 1438 return false; 1439 } 1440 1441 int partNum = body.getPartsNum(); 1442 for (int i = 0; i < partNum; i++) { 1443 PduPart part = body.getPart(i); 1444 String type = new String(part.getContentType()); 1445 1446 if (DrmUtils.isDrmType(type)) { 1447 String mimeType = MmsApp.getApplication().getDrmManagerClient() 1448 .getOriginalMimeType(part.getDataUri()); 1449 if (ContentType.isAudioType(mimeType) && DrmUtils.haveRightsForAction(part.getDataUri(), 1450 DrmStore.Action.RINGTONE)) { 1451 return true; 1452 } 1453 } 1454 } 1455 return false; 1456 } 1457 1458 /** 1459 * Returns true if all drm'd parts are forwardable. 1460 * @param msgId 1461 * @return true if all drm'd parts are forwardable. 1462 */ 1463 private boolean isForwardable(long msgId) { 1464 PduBody body = null; 1465 try { 1466 body = SlideshowModel.getPduBody(this, 1467 ContentUris.withAppendedId(Mms.CONTENT_URI, msgId)); 1468 } catch (MmsException e) { 1469 Log.e(TAG, "getDrmMimeType can't load pdu body: " + msgId); 1470 } 1471 if (body == null) { 1472 return false; 1473 } 1474 1475 int partNum = body.getPartsNum(); 1476 for (int i = 0; i < partNum; i++) { 1477 PduPart part = body.getPart(i); 1478 String type = new String(part.getContentType()); 1479 1480 if (DrmUtils.isDrmType(type) && !DrmUtils.haveRightsForAction(part.getDataUri(), 1481 DrmStore.Action.TRANSFER)) { 1482 return false; 1483 } 1484 } 1485 return true; 1486 } 1487 1488 private int getDrmMimeMenuStringRsrc(long msgId) { 1489 if (isDrmRingtoneWithRights(msgId)) { 1490 return R.string.save_ringtone; 1491 } 1492 return 0; 1493 } 1494 1495 private int getDrmMimeSavedStringRsrc(long msgId, boolean success) { 1496 if (isDrmRingtoneWithRights(msgId)) { 1497 return success ? R.string.saved_ringtone : R.string.saved_ringtone_fail; 1498 } 1499 return 0; 1500 } 1501 1502 /** 1503 * Copies media from an Mms to the "download" directory on the SD card. If any of the parts 1504 * are audio types, drm'd or not, they're copied to the "Ringtones" directory. 1505 * @param msgId 1506 */ 1507 private boolean copyMedia(long msgId) { 1508 boolean result = true; 1509 PduBody body = null; 1510 try { 1511 body = SlideshowModel.getPduBody(this, 1512 ContentUris.withAppendedId(Mms.CONTENT_URI, msgId)); 1513 } catch (MmsException e) { 1514 Log.e(TAG, "copyMedia can't load pdu body: " + msgId); 1515 } 1516 if (body == null) { 1517 return false; 1518 } 1519 1520 int partNum = body.getPartsNum(); 1521 for(int i = 0; i < partNum; i++) { 1522 PduPart part = body.getPart(i); 1523 1524 // all parts have to be successful for a valid result. 1525 result &= copyPart(part, Long.toHexString(msgId)); 1526 } 1527 return result; 1528 } 1529 1530 private boolean copyPart(PduPart part, String fallback) { 1531 Uri uri = part.getDataUri(); 1532 String type = new String(part.getContentType()); 1533 boolean isDrm = DrmUtils.isDrmType(type); 1534 if (isDrm) { 1535 type = MmsApp.getApplication().getDrmManagerClient() 1536 .getOriginalMimeType(part.getDataUri()); 1537 } 1538 if (!ContentType.isImageType(type) && !ContentType.isVideoType(type) && 1539 !ContentType.isAudioType(type)) { 1540 return true; // we only save pictures, videos, and sounds. Skip the text parts, 1541 // the app (smil) parts, and other type that we can't handle. 1542 // Return true to pretend that we successfully saved the part so 1543 // the whole save process will be counted a success. 1544 } 1545 InputStream input = null; 1546 FileOutputStream fout = null; 1547 try { 1548 input = mContentResolver.openInputStream(uri); 1549 if (input instanceof FileInputStream) { 1550 FileInputStream fin = (FileInputStream) input; 1551 1552 byte[] location = part.getName(); 1553 if (location == null) { 1554 location = part.getFilename(); 1555 } 1556 if (location == null) { 1557 location = part.getContentLocation(); 1558 } 1559 1560 String fileName; 1561 if (location == null) { 1562 // Use fallback name. 1563 fileName = fallback; 1564 } else { 1565 // For locally captured videos, fileName can end up being something like this: 1566 // /mnt/sdcard/Android/data/com.android.mms/cache/.temp1.3gp 1567 fileName = new String(location); 1568 } 1569 File originalFile = new File(fileName); 1570 fileName = originalFile.getName(); // Strip the full path of where the "part" is 1571 // stored down to just the leaf filename. 1572 1573 // Depending on the location, there may be an 1574 // extension already on the name or not. If we've got audio, put the attachment 1575 // in the Ringtones directory. 1576 String dir = Environment.getExternalStorageDirectory() + "/" 1577 + (ContentType.isAudioType(type) ? Environment.DIRECTORY_RINGTONES : 1578 Environment.DIRECTORY_DOWNLOADS) + "/"; 1579 String extension; 1580 int index; 1581 if ((index = fileName.lastIndexOf('.')) == -1) { 1582 extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type); 1583 } else { 1584 extension = fileName.substring(index + 1, fileName.length()); 1585 fileName = fileName.substring(0, index); 1586 } 1587 if (isDrm) { 1588 extension += DrmUtils.getConvertExtension(type); 1589 } 1590 File file = getUniqueDestination(dir + fileName, extension); 1591 1592 // make sure the path is valid and directories created for this file. 1593 File parentFile = file.getParentFile(); 1594 if (!parentFile.exists() && !parentFile.mkdirs()) { 1595 Log.e(TAG, "[MMS] copyPart: mkdirs for " + parentFile.getPath() + " failed!"); 1596 return false; 1597 } 1598 1599 fout = new FileOutputStream(file); 1600 1601 byte[] buffer = new byte[8000]; 1602 int size = 0; 1603 while ((size=fin.read(buffer)) != -1) { 1604 fout.write(buffer, 0, size); 1605 } 1606 1607 // Notify other applications listening to scanner events 1608 // that a media file has been added to the sd card 1609 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 1610 Uri.fromFile(file))); 1611 } 1612 } catch (IOException e) { 1613 // Ignore 1614 Log.e(TAG, "IOException caught while opening or reading stream", e); 1615 return false; 1616 } finally { 1617 if (null != input) { 1618 try { 1619 input.close(); 1620 } catch (IOException e) { 1621 // Ignore 1622 Log.e(TAG, "IOException caught while closing stream", e); 1623 return false; 1624 } 1625 } 1626 if (null != fout) { 1627 try { 1628 fout.close(); 1629 } catch (IOException e) { 1630 // Ignore 1631 Log.e(TAG, "IOException caught while closing stream", e); 1632 return false; 1633 } 1634 } 1635 } 1636 return true; 1637 } 1638 1639 private File getUniqueDestination(String base, String extension) { 1640 File file = new File(base + "." + extension); 1641 1642 for (int i = 2; file.exists(); i++) { 1643 file = new File(base + "_" + i + "." + extension); 1644 } 1645 return file; 1646 } 1647 1648 private void showDeliveryReport(long messageId, String type) { 1649 Intent intent = new Intent(this, DeliveryReportActivity.class); 1650 intent.putExtra("message_id", messageId); 1651 intent.putExtra("message_type", type); 1652 1653 startActivity(intent); 1654 } 1655 1656 private final IntentFilter mHttpProgressFilter = new IntentFilter(PROGRESS_STATUS_ACTION); 1657 1658 private final BroadcastReceiver mHttpProgressReceiver = new BroadcastReceiver() { 1659 @Override 1660 public void onReceive(Context context, Intent intent) { 1661 if (PROGRESS_STATUS_ACTION.equals(intent.getAction())) { 1662 long token = intent.getLongExtra("token", 1663 SendingProgressTokenManager.NO_TOKEN); 1664 if (token != mConversation.getThreadId()) { 1665 return; 1666 } 1667 1668 int progress = intent.getIntExtra("progress", 0); 1669 switch (progress) { 1670 case PROGRESS_START: 1671 setProgressBarVisibility(true); 1672 break; 1673 case PROGRESS_ABORT: 1674 case PROGRESS_COMPLETE: 1675 setProgressBarVisibility(false); 1676 break; 1677 default: 1678 setProgress(100 * progress); 1679 } 1680 } 1681 } 1682 }; 1683 1684 private static ContactList sEmptyContactList; 1685 1686 private ContactList getRecipients() { 1687 // If the recipients editor is visible, the conversation has 1688 // not really officially 'started' yet. Recipients will be set 1689 // on the conversation once it has been saved or sent. In the 1690 // meantime, let anyone who needs the recipient list think it 1691 // is empty rather than giving them a stale one. 1692 if (isRecipientsEditorVisible()) { 1693 if (sEmptyContactList == null) { 1694 sEmptyContactList = new ContactList(); 1695 } 1696 return sEmptyContactList; 1697 } 1698 return mConversation.getRecipients(); 1699 } 1700 1701 private void updateTitle(ContactList list) { 1702 String title = null; 1703 String subTitle = null; 1704 int cnt = list.size(); 1705 switch (cnt) { 1706 case 0: { 1707 String recipient = null; 1708 if (mRecipientsEditor != null) { 1709 recipient = mRecipientsEditor.getText().toString(); 1710 } 1711 title = TextUtils.isEmpty(recipient) ? getString(R.string.new_message) : recipient; 1712 break; 1713 } 1714 case 1: { 1715 title = list.get(0).getName(); // get name returns the number if there's no 1716 // name available. 1717 String number = list.get(0).getNumber(); 1718 if (!title.equals(number)) { 1719 subTitle = PhoneNumberUtils.formatNumber(number, number, 1720 MmsApp.getApplication().getCurrentCountryIso()); 1721 } 1722 break; 1723 } 1724 default: { 1725 // Handle multiple recipients 1726 title = list.formatNames(", "); 1727 subTitle = getResources().getQuantityString(R.plurals.recipient_count, cnt, cnt); 1728 break; 1729 } 1730 } 1731 mDebugRecipients = list.serialize(); 1732 1733 ActionBar actionBar = getActionBar(); 1734 actionBar.setTitle(title); 1735 actionBar.setSubtitle(subTitle); 1736 } 1737 1738 // Get the recipients editor ready to be displayed onscreen. 1739 private void initRecipientsEditor() { 1740 if (isRecipientsEditorVisible()) { 1741 return; 1742 } 1743 // Must grab the recipients before the view is made visible because getRecipients() 1744 // returns empty recipients when the editor is visible. 1745 ContactList recipients = getRecipients(); 1746 1747 ViewStub stub = (ViewStub)findViewById(R.id.recipients_editor_stub); 1748 if (stub != null) { 1749 View stubView = stub.inflate(); 1750 mRecipientsEditor = (RecipientsEditor) stubView.findViewById(R.id.recipients_editor); 1751 mRecipientsPicker = (ImageButton) stubView.findViewById(R.id.recipients_picker); 1752 } else { 1753 mRecipientsEditor = (RecipientsEditor)findViewById(R.id.recipients_editor); 1754 mRecipientsEditor.setVisibility(View.VISIBLE); 1755 mRecipientsPicker = (ImageButton)findViewById(R.id.recipients_picker); 1756 } 1757 mRecipientsPicker.setOnClickListener(this); 1758 1759 mRecipientsEditor.setAdapter(new ChipsRecipientAdapter(this)); 1760 mRecipientsEditor.populate(recipients); 1761 mRecipientsEditor.setOnCreateContextMenuListener(mRecipientsMenuCreateListener); 1762 mRecipientsEditor.addTextChangedListener(mRecipientsWatcher); 1763 // TODO : Remove the max length limitation due to the multiple phone picker is added and the 1764 // user is able to select a large number of recipients from the Contacts. The coming 1765 // potential issue is that it is hard for user to edit a recipient from hundred of 1766 // recipients in the editor box. We may redesign the editor box UI for this use case. 1767 // mRecipientsEditor.setFilters(new InputFilter[] { 1768 // new InputFilter.LengthFilter(RECIPIENTS_MAX_LENGTH) }); 1769 1770 mRecipientsEditor.setOnSelectChipRunnable(new Runnable() { 1771 @Override 1772 public void run() { 1773 // After the user selects an item in the pop-up contacts list, move the 1774 // focus to the text editor if there is only one recipient. This helps 1775 // the common case of selecting one recipient and then typing a message, 1776 // but avoids annoying a user who is trying to add five recipients and 1777 // keeps having focus stolen away. 1778 if (mRecipientsEditor.getRecipientCount() == 1) { 1779 // if we're in extract mode then don't request focus 1780 final InputMethodManager inputManager = (InputMethodManager) 1781 getSystemService(Context.INPUT_METHOD_SERVICE); 1782 if (inputManager == null || !inputManager.isFullscreenMode()) { 1783 mTextEditor.requestFocus(); 1784 } 1785 } 1786 } 1787 }); 1788 1789 mRecipientsEditor.setOnFocusChangeListener(new View.OnFocusChangeListener() { 1790 @Override 1791 public void onFocusChange(View v, boolean hasFocus) { 1792 if (!hasFocus) { 1793 RecipientsEditor editor = (RecipientsEditor) v; 1794 ContactList contacts = editor.constructContactsFromInput(false); 1795 updateTitle(contacts); 1796 } 1797 } 1798 }); 1799 1800 PhoneNumberFormatter.setPhoneNumberFormattingTextWatcher(this, mRecipientsEditor); 1801 1802 mTopPanel.setVisibility(View.VISIBLE); 1803 } 1804 1805 //========================================================== 1806 // Activity methods 1807 //========================================================== 1808 1809 public static boolean cancelFailedToDeliverNotification(Intent intent, Context context) { 1810 if (MessagingNotification.isFailedToDeliver(intent)) { 1811 // Cancel any failed message notifications 1812 MessagingNotification.cancelNotification(context, 1813 MessagingNotification.MESSAGE_FAILED_NOTIFICATION_ID); 1814 return true; 1815 } 1816 return false; 1817 } 1818 1819 public static boolean cancelFailedDownloadNotification(Intent intent, Context context) { 1820 if (MessagingNotification.isFailedToDownload(intent)) { 1821 // Cancel any failed download notifications 1822 MessagingNotification.cancelNotification(context, 1823 MessagingNotification.DOWNLOAD_FAILED_NOTIFICATION_ID); 1824 return true; 1825 } 1826 return false; 1827 } 1828 1829 @Override 1830 protected void onCreate(Bundle savedInstanceState) { 1831 super.onCreate(savedInstanceState); 1832 1833 resetConfiguration(getResources().getConfiguration()); 1834 1835 setContentView(R.layout.compose_message_activity); 1836 setProgressBarVisibility(false); 1837 1838 // Initialize members for UI elements. 1839 initResourceRefs(); 1840 1841 mContentResolver = getContentResolver(); 1842 mBackgroundQueryHandler = new BackgroundQueryHandler(mContentResolver); 1843 1844 initialize(savedInstanceState, 0); 1845 1846 if (TRACE) { 1847 android.os.Debug.startMethodTracing("compose"); 1848 } 1849 } 1850 1851 private void showSubjectEditor(boolean show) { 1852 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 1853 log("" + show); 1854 } 1855 1856 if (mSubjectTextEditor == null) { 1857 // Don't bother to initialize the subject editor if 1858 // we're just going to hide it. 1859 if (show == false) { 1860 return; 1861 } 1862 mSubjectTextEditor = (EditText)findViewById(R.id.subject); 1863 mSubjectTextEditor.setFilters(new InputFilter[] { 1864 new LengthFilter(MmsConfig.getMaxSubjectLength())}); 1865 } 1866 1867 mSubjectTextEditor.setOnKeyListener(show ? mSubjectKeyListener : null); 1868 1869 if (show) { 1870 mSubjectTextEditor.addTextChangedListener(mSubjectEditorWatcher); 1871 } else { 1872 mSubjectTextEditor.removeTextChangedListener(mSubjectEditorWatcher); 1873 } 1874 1875 mSubjectTextEditor.setText(mWorkingMessage.getSubject()); 1876 mSubjectTextEditor.setVisibility(show ? View.VISIBLE : View.GONE); 1877 hideOrShowTopPanel(); 1878 } 1879 1880 private void hideOrShowTopPanel() { 1881 boolean anySubViewsVisible = (isSubjectEditorVisible() || isRecipientsEditorVisible()); 1882 mTopPanel.setVisibility(anySubViewsVisible ? View.VISIBLE : View.GONE); 1883 } 1884 1885 public void initialize(Bundle savedInstanceState, long originalThreadId) { 1886 // Create a new empty working message. 1887 mWorkingMessage = WorkingMessage.createEmpty(this); 1888 1889 // Read parameters or previously saved state of this activity. This will load a new 1890 // mConversation 1891 initActivityState(savedInstanceState); 1892 1893 if (LogTag.SEVERE_WARNING && originalThreadId != 0 && 1894 originalThreadId == mConversation.getThreadId()) { 1895 LogTag.warnPossibleRecipientMismatch("ComposeMessageActivity.initialize: " + 1896 " threadId didn't change from: " + originalThreadId, this); 1897 } 1898 1899 log("savedInstanceState = " + savedInstanceState + 1900 " intent = " + getIntent() + 1901 " mConversation = " + mConversation); 1902 1903 if (cancelFailedToDeliverNotification(getIntent(), this)) { 1904 // Show a pop-up dialog to inform user the message was 1905 // failed to deliver. 1906 undeliveredMessageDialog(getMessageDate(null)); 1907 } 1908 cancelFailedDownloadNotification(getIntent(), this); 1909 1910 // Set up the message history ListAdapter 1911 initMessageList(); 1912 1913 // Load the draft for this thread, if we aren't already handling 1914 // existing data, such as a shared picture or forwarded message. 1915 boolean isForwardedMessage = false; 1916 // We don't attempt to handle the Intent.ACTION_SEND when saveInstanceState is non-null. 1917 // saveInstanceState is non-null when this activity is killed. In that case, we already 1918 // handled the attachment or the send, so we don't try and parse the intent again. 1919 boolean intentHandled = savedInstanceState == null && 1920 (handleSendIntent() || handleForwardedMessage()); 1921 if (!intentHandled) { 1922 loadDraft(); 1923 } 1924 1925 // Let the working message know what conversation it belongs to 1926 mWorkingMessage.setConversation(mConversation); 1927 1928 // Show the recipients editor if we don't have a valid thread. Hide it otherwise. 1929 if (mConversation.getThreadId() <= 0) { 1930 // Hide the recipients editor so the call to initRecipientsEditor won't get 1931 // short-circuited. 1932 hideRecipientEditor(); 1933 initRecipientsEditor(); 1934 } else { 1935 hideRecipientEditor(); 1936 } 1937 1938 updateSendButtonState(); 1939 1940 drawTopPanel(false); 1941 if (intentHandled) { 1942 // We're not loading a draft, so we can draw the bottom panel immediately. 1943 drawBottomPanel(); 1944 } 1945 1946 onKeyboardStateChanged(mIsKeyboardOpen); 1947 1948 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 1949 log("update title, mConversation=" + mConversation.toString()); 1950 } 1951 1952 updateTitle(mConversation.getRecipients()); 1953 1954 if (isForwardedMessage && isRecipientsEditorVisible()) { 1955 // The user is forwarding the message to someone. Put the focus on the 1956 // recipient editor rather than in the message editor. 1957 mRecipientsEditor.requestFocus(); 1958 } 1959 1960 mMsgListAdapter.setIsGroupConversation(mConversation.getRecipients().size() > 1); 1961 } 1962 1963 @Override 1964 protected void onNewIntent(Intent intent) { 1965 super.onNewIntent(intent); 1966 1967 setIntent(intent); 1968 1969 Conversation conversation = null; 1970 mSentMessage = false; 1971 1972 // If we have been passed a thread_id, use that to find our 1973 // conversation. 1974 1975 // Note that originalThreadId might be zero but if this is a draft and we save the 1976 // draft, ensureThreadId gets called async from WorkingMessage.asyncUpdateDraftSmsMessage 1977 // the thread will get a threadId behind the UI thread's back. 1978 long originalThreadId = mConversation.getThreadId(); 1979 long threadId = intent.getLongExtra(THREAD_ID, 0); 1980 Uri intentUri = intent.getData(); 1981 1982 boolean sameThread = false; 1983 if (threadId > 0) { 1984 conversation = Conversation.get(this, threadId, false); 1985 } else { 1986 if (mConversation.getThreadId() == 0) { 1987 // We've got a draft. Make sure the working recipients are synched 1988 // to the conversation so when we compare conversations later in this function, 1989 // the compare will work. 1990 mWorkingMessage.syncWorkingRecipients(); 1991 } 1992 // Get the "real" conversation based on the intentUri. The intentUri might specify 1993 // the conversation by a phone number or by a thread id. We'll typically get a threadId 1994 // based uri when the user pulls down a notification while in ComposeMessageActivity and 1995 // we end up here in onNewIntent. mConversation can have a threadId of zero when we're 1996 // working on a draft. When a new message comes in for that same recipient, a 1997 // conversation will get created behind CMA's back when the message is inserted into 1998 // the database and the corresponding entry made in the threads table. The code should 1999 // use the real conversation as soon as it can rather than finding out the threadId 2000 // when sending with "ensureThreadId". 2001 conversation = Conversation.get(this, intentUri, false); 2002 } 2003 2004 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 2005 log("onNewIntent: data=" + intentUri + ", thread_id extra is " + threadId + 2006 ", new conversation=" + conversation + ", mConversation=" + mConversation); 2007 } 2008 2009 // this is probably paranoid to compare both thread_ids and recipient lists, 2010 // but we want to make double sure because this is a last minute fix for Froyo 2011 // and the previous code checked thread ids only. 2012 // (we cannot just compare thread ids because there is a case where mConversation 2013 // has a stale/obsolete thread id (=1) that could collide against the new thread_id(=1), 2014 // even though the recipient lists are different) 2015 sameThread = ((conversation.getThreadId() == mConversation.getThreadId() || 2016 mConversation.getThreadId() == 0) && 2017 conversation.equals(mConversation)); 2018 2019 if (sameThread) { 2020 log("onNewIntent: same conversation"); 2021 if (mConversation.getThreadId() == 0) { 2022 mConversation = conversation; 2023 mWorkingMessage.setConversation(mConversation); 2024 updateThreadIdIfRunning(); 2025 invalidateOptionsMenu(); 2026 } 2027 } else { 2028 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 2029 log("onNewIntent: different conversation"); 2030 } 2031 saveDraft(false); // if we've got a draft, save it first 2032 2033 initialize(null, originalThreadId); 2034 } 2035 loadMessageContent(); 2036 } 2037 2038 private void sanityCheckConversation() { 2039 if (mWorkingMessage.getConversation() != mConversation) { 2040 LogTag.warnPossibleRecipientMismatch( 2041 "ComposeMessageActivity: mWorkingMessage.mConversation=" + 2042 mWorkingMessage.getConversation() + ", mConversation=" + 2043 mConversation + ", MISMATCH!", this); 2044 } 2045 } 2046 2047 @Override 2048 protected void onRestart() { 2049 super.onRestart(); 2050 2051 if (mWorkingMessage.isDiscarded()) { 2052 // If the message isn't worth saving, don't resurrect it. Doing so can lead to 2053 // a situation where a new incoming message gets the old thread id of the discarded 2054 // draft. This activity can end up displaying the recipients of the old message with 2055 // the contents of the new message. Recognize that dangerous situation and bail out 2056 // to the ConversationList where the user can enter this in a clean manner. 2057 if (mWorkingMessage.isWorthSaving()) { 2058 if (LogTag.VERBOSE) { 2059 log("onRestart: mWorkingMessage.unDiscard()"); 2060 } 2061 mWorkingMessage.unDiscard(); // it was discarded in onStop(). 2062 2063 sanityCheckConversation(); 2064 } else if (isRecipientsEditorVisible()) { 2065 if (LogTag.VERBOSE) { 2066 log("onRestart: goToConversationList"); 2067 } 2068 goToConversationList(); 2069 } else { 2070 if (LogTag.VERBOSE) { 2071 log("onRestart: loadDraft"); 2072 } 2073 loadDraft(); 2074 mWorkingMessage.setConversation(mConversation); 2075 mAttachmentEditor.update(mWorkingMessage); 2076 } 2077 } 2078 } 2079 2080 @Override 2081 protected void onStart() { 2082 super.onStart(); 2083 2084 initFocus(); 2085 2086 // Register a BroadcastReceiver to listen on HTTP I/O process. 2087 registerReceiver(mHttpProgressReceiver, mHttpProgressFilter); 2088 2089 loadMessageContent(); 2090 2091 // Update the fasttrack info in case any of the recipients' contact info changed 2092 // while we were paused. This can happen, for example, if a user changes or adds 2093 // an avatar associated with a contact. 2094 mWorkingMessage.syncWorkingRecipients(); 2095 2096 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 2097 log("update title, mConversation=" + mConversation.toString()); 2098 } 2099 2100 updateTitle(mConversation.getRecipients()); 2101 2102 ActionBar actionBar = getActionBar(); 2103 actionBar.setDisplayHomeAsUpEnabled(true); 2104 } 2105 2106 public void loadMessageContent() { 2107 // Don't let any markAsRead DB updates occur before we've loaded the messages for 2108 // the thread. Unblocking occurs when we're done querying for the conversation 2109 // items. 2110 mConversation.blockMarkAsRead(true); 2111 mConversation.markAsRead(); // dismiss any notifications for this convo 2112 startMsgListQuery(); 2113 updateSendFailedNotification(); 2114 drawBottomPanel(); 2115 } 2116 2117 private void updateSendFailedNotification() { 2118 final long threadId = mConversation.getThreadId(); 2119 if (threadId <= 0) 2120 return; 2121 2122 // updateSendFailedNotificationForThread makes a database call, so do the work off 2123 // of the ui thread. 2124 new Thread(new Runnable() { 2125 @Override 2126 public void run() { 2127 MessagingNotification.updateSendFailedNotificationForThread( 2128 ComposeMessageActivity.this, threadId); 2129 } 2130 }, "ComposeMessageActivity.updateSendFailedNotification").start(); 2131 } 2132 2133 @Override 2134 public void onSaveInstanceState(Bundle outState) { 2135 super.onSaveInstanceState(outState); 2136 2137 outState.putString(RECIPIENTS, getRecipients().serialize()); 2138 2139 mWorkingMessage.writeStateToBundle(outState); 2140 2141 if (mExitOnSent) { 2142 outState.putBoolean("exit_on_sent", mExitOnSent); 2143 } 2144 } 2145 2146 @Override 2147 protected void onResume() { 2148 super.onResume(); 2149 2150 // OLD: get notified of presence updates to update the titlebar. 2151 // NEW: we are using ContactHeaderWidget which displays presence, but updating presence 2152 // there is out of our control. 2153 //Contact.startPresenceObserver(); 2154 2155 addRecipientsListeners(); 2156 2157 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 2158 log("update title, mConversation=" + mConversation.toString()); 2159 } 2160 2161 // There seems to be a bug in the framework such that setting the title 2162 // here gets overwritten to the original title. Do this delayed as a 2163 // workaround. 2164 mMessageListItemHandler.postDelayed(new Runnable() { 2165 @Override 2166 public void run() { 2167 ContactList recipients = isRecipientsEditorVisible() ? 2168 mRecipientsEditor.constructContactsFromInput(false) : getRecipients(); 2169 updateTitle(recipients); 2170 } 2171 }, 100); 2172 2173 mIsRunning = true; 2174 updateThreadIdIfRunning(); 2175 mConversation.markAsRead(); 2176 2177 int mode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; 2178 if (DraftCache.getInstance().hasDraft(mConversation.getThreadId())) { 2179 mode |= WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE; 2180 } else { 2181 mode |= WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN; 2182 } 2183 getWindow().setSoftInputMode(mode); 2184 } 2185 2186 @Override 2187 protected void onPause() { 2188 // HACK: fix for getting double callback for onSizeChanged() after re-entering this 2189 // activity. Wait 100ms until InputMethodManagerService handles hideSoftInput 2190 // This should really be fixed in WindowManagerService 2191 hideKeyboard(); 2192 try { 2193 Thread.sleep(100); 2194 } catch (InterruptedException ex) { 2195 } 2196 2197 super.onPause(); 2198 2199 if (DEBUG) { 2200 Log.v(TAG, "onPause: setCurrentlyDisplayedThreadId: " + 2201 MessagingNotification.THREAD_NONE); 2202 } 2203 MessagingNotification.setCurrentlyDisplayedThreadId(MessagingNotification.THREAD_NONE); 2204 2205 // OLD: stop getting notified of presence updates to update the titlebar. 2206 // NEW: we are using ContactHeaderWidget which displays presence, but updating presence 2207 // there is out of our control. 2208 //Contact.stopPresenceObserver(); 2209 2210 removeRecipientsListeners(); 2211 2212 // remove any callback to display a progress spinner 2213 if (mAsyncDialog != null) { 2214 mAsyncDialog.clearPendingProgressDialog(); 2215 } 2216 2217 // Remember whether the list is scrolled to the end when we're paused so we can rescroll 2218 // to the end when resumed. 2219 if (mMsgListAdapter != null && 2220 mMsgListView.getLastVisiblePosition() >= mMsgListAdapter.getCount() - 1) { 2221 mSavedScrollPosition = Integer.MAX_VALUE; 2222 } else { 2223 mSavedScrollPosition = mMsgListView.getFirstVisiblePosition(); 2224 } 2225 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 2226 Log.v(TAG, "onPause: mSavedScrollPosition=" + mSavedScrollPosition); 2227 } 2228 2229 mConversation.markAsRead(); 2230 mIsRunning = false; 2231 } 2232 2233 @Override 2234 protected void onStop() { 2235 super.onStop(); 2236 2237 // No need to do the querying when finished this activity 2238 mBackgroundQueryHandler.cancelOperation(MESSAGE_LIST_QUERY_TOKEN); 2239 2240 // Allow any blocked calls to update the thread's read status. 2241 mConversation.blockMarkAsRead(false); 2242 2243 if (mMsgListAdapter != null) { 2244 // Close the cursor in the ListAdapter if the activity stopped. 2245 Cursor cursor = mMsgListAdapter.getCursor(); 2246 2247 if (cursor != null && !cursor.isClosed()) { 2248 cursor.close(); 2249 } 2250 2251 mMsgListAdapter.changeCursor(null); 2252 mMsgListAdapter.cancelBackgroundLoading(); 2253 } 2254 2255 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 2256 log("save draft"); 2257 } 2258 saveDraft(true); 2259 2260 // Cleanup the BroadcastReceiver. 2261 unregisterReceiver(mHttpProgressReceiver); 2262 } 2263 2264 @Override 2265 protected void onDestroy() { 2266 if (TRACE) { 2267 android.os.Debug.stopMethodTracing(); 2268 } 2269 2270 super.onDestroy(); 2271 } 2272 2273 @Override 2274 public void onConfigurationChanged(Configuration newConfig) { 2275 super.onConfigurationChanged(newConfig); 2276 if (LOCAL_LOGV) { 2277 Log.v(TAG, "onConfigurationChanged: " + newConfig); 2278 } 2279 2280 if (resetConfiguration(newConfig)) { 2281 // Have to re-layout the attachment editor because we have different layouts 2282 // depending on whether we're portrait or landscape. 2283 drawTopPanel(isSubjectEditorVisible()); 2284 } 2285 onKeyboardStateChanged(mIsKeyboardOpen); 2286 } 2287 2288 // returns true if landscape/portrait configuration has changed 2289 private boolean resetConfiguration(Configuration config) { 2290 mIsKeyboardOpen = config.keyboardHidden == KEYBOARDHIDDEN_NO; 2291 boolean isLandscape = config.orientation == Configuration.ORIENTATION_LANDSCAPE; 2292 if (mIsLandscape != isLandscape) { 2293 mIsLandscape = isLandscape; 2294 return true; 2295 } 2296 return false; 2297 } 2298 2299 private void onKeyboardStateChanged(boolean isKeyboardOpen) { 2300 // If the keyboard is hidden, don't show focus highlights for 2301 // things that cannot receive input. 2302 if (isKeyboardOpen) { 2303 if (mRecipientsEditor != null) { 2304 mRecipientsEditor.setFocusableInTouchMode(true); 2305 } 2306 if (mSubjectTextEditor != null) { 2307 mSubjectTextEditor.setFocusableInTouchMode(true); 2308 } 2309 mTextEditor.setFocusableInTouchMode(true); 2310 mTextEditor.setHint(R.string.type_to_compose_text_enter_to_send); 2311 } else { 2312 if (mRecipientsEditor != null) { 2313 mRecipientsEditor.setFocusable(false); 2314 } 2315 if (mSubjectTextEditor != null) { 2316 mSubjectTextEditor.setFocusable(false); 2317 } 2318 mTextEditor.setFocusable(false); 2319 mTextEditor.setHint(R.string.open_keyboard_to_compose_message); 2320 } 2321 } 2322 2323 @Override 2324 public boolean onKeyDown(int keyCode, KeyEvent event) { 2325 switch (keyCode) { 2326 case KeyEvent.KEYCODE_DEL: 2327 if ((mMsgListAdapter != null) && mMsgListView.isFocused()) { 2328 Cursor cursor; 2329 try { 2330 cursor = (Cursor) mMsgListView.getSelectedItem(); 2331 } catch (ClassCastException e) { 2332 Log.e(TAG, "Unexpected ClassCastException.", e); 2333 return super.onKeyDown(keyCode, event); 2334 } 2335 2336 if (cursor != null) { 2337 String type = cursor.getString(COLUMN_MSG_TYPE); 2338 long msgId = cursor.getLong(COLUMN_ID); 2339 MessageItem msgItem = mMsgListAdapter.getCachedMessageItem(type, msgId, 2340 cursor); 2341 if (msgItem != null) { 2342 DeleteMessageListener l = new DeleteMessageListener(msgItem); 2343 confirmDeleteDialog(l, msgItem.mLocked); 2344 } 2345 return true; 2346 } 2347 } 2348 break; 2349 case KeyEvent.KEYCODE_DPAD_CENTER: 2350 case KeyEvent.KEYCODE_ENTER: 2351 if (isPreparedForSending()) { 2352 confirmSendMessageIfNeeded(); 2353 return true; 2354 } 2355 break; 2356 case KeyEvent.KEYCODE_BACK: 2357 exitComposeMessageActivity(new Runnable() { 2358 @Override 2359 public void run() { 2360 finish(); 2361 } 2362 }); 2363 return true; 2364 } 2365 2366 return super.onKeyDown(keyCode, event); 2367 } 2368 2369 private void exitComposeMessageActivity(final Runnable exit) { 2370 // If the message is empty, just quit -- finishing the 2371 // activity will cause an empty draft to be deleted. 2372 if (!mWorkingMessage.isWorthSaving()) { 2373 exit.run(); 2374 return; 2375 } 2376 2377 if (isRecipientsEditorVisible() && 2378 !mRecipientsEditor.hasValidRecipient(mWorkingMessage.requiresMms())) { 2379 MessageUtils.showDiscardDraftConfirmDialog(this, new DiscardDraftListener()); 2380 return; 2381 } 2382 2383 mToastForDraftSave = true; 2384 exit.run(); 2385 } 2386 2387 private void goToConversationList() { 2388 finish(); 2389 startActivity(new Intent(this, ConversationList.class)); 2390 } 2391 2392 private void hideRecipientEditor() { 2393 if (mRecipientsEditor != null) { 2394 mRecipientsEditor.removeTextChangedListener(mRecipientsWatcher); 2395 mRecipientsEditor.setVisibility(View.GONE); 2396 hideOrShowTopPanel(); 2397 } 2398 } 2399 2400 private boolean isRecipientsEditorVisible() { 2401 return (null != mRecipientsEditor) 2402 && (View.VISIBLE == mRecipientsEditor.getVisibility()); 2403 } 2404 2405 private boolean isSubjectEditorVisible() { 2406 return (null != mSubjectTextEditor) 2407 && (View.VISIBLE == mSubjectTextEditor.getVisibility()); 2408 } 2409 2410 @Override 2411 public void onAttachmentChanged() { 2412 // Have to make sure we're on the UI thread. This function can be called off of the UI 2413 // thread when we're adding multi-attachments 2414 runOnUiThread(new Runnable() { 2415 @Override 2416 public void run() { 2417 drawBottomPanel(); 2418 updateSendButtonState(); 2419 drawTopPanel(isSubjectEditorVisible()); 2420 } 2421 }); 2422 } 2423 2424 @Override 2425 public void onProtocolChanged(final boolean convertToMms) { 2426 // Have to make sure we're on the UI thread. This function can be called off of the UI 2427 // thread when we're adding multi-attachments 2428 runOnUiThread(new Runnable() { 2429 @Override 2430 public void run() { 2431 showSmsOrMmsSendButton(convertToMms); 2432 2433 if (convertToMms) { 2434 // In the case we went from a long sms with a counter to an mms because 2435 // the user added an attachment or a subject, hide the counter -- 2436 // it doesn't apply to mms. 2437 mTextCounter.setVisibility(View.GONE); 2438 2439 showConvertToMmsToast(); 2440 } 2441 } 2442 }); 2443 } 2444 2445 // Show or hide the Sms or Mms button as appropriate. Return the view so that the caller 2446 // can adjust the enableness and focusability. 2447 private View showSmsOrMmsSendButton(boolean isMms) { 2448 View showButton; 2449 View hideButton; 2450 if (isMms) { 2451 showButton = mSendButtonMms; 2452 hideButton = mSendButtonSms; 2453 } else { 2454 showButton = mSendButtonSms; 2455 hideButton = mSendButtonMms; 2456 } 2457 showButton.setVisibility(View.VISIBLE); 2458 hideButton.setVisibility(View.GONE); 2459 2460 return showButton; 2461 } 2462 2463 Runnable mResetMessageRunnable = new Runnable() { 2464 @Override 2465 public void run() { 2466 resetMessage(); 2467 } 2468 }; 2469 2470 @Override 2471 public void onPreMessageSent() { 2472 runOnUiThread(mResetMessageRunnable); 2473 } 2474 2475 @Override 2476 public void onMessageSent() { 2477 // This callback can come in on any thread; put it on the main thread to avoid 2478 // concurrency problems 2479 runOnUiThread(new Runnable() { 2480 @Override 2481 public void run() { 2482 // If we already have messages in the list adapter, it 2483 // will be auto-requerying; don't thrash another query in. 2484 // TODO: relying on auto-requerying seems unreliable when priming an MMS into the 2485 // outbox. Need to investigate. 2486// if (mMsgListAdapter.getCount() == 0) { 2487 if (LogTag.VERBOSE) { 2488 log("onMessageSent"); 2489 } 2490 startMsgListQuery(); 2491// } 2492 2493 // The thread ID could have changed if this is a new message that we just inserted 2494 // into the database (and looked up or created a thread for it) 2495 updateThreadIdIfRunning(); 2496 } 2497 }); 2498 } 2499 2500 @Override 2501 public void onMaxPendingMessagesReached() { 2502 saveDraft(false); 2503 2504 runOnUiThread(new Runnable() { 2505 @Override 2506 public void run() { 2507 Toast.makeText(ComposeMessageActivity.this, R.string.too_many_unsent_mms, 2508 Toast.LENGTH_LONG).show(); 2509 } 2510 }); 2511 } 2512 2513 @Override 2514 public void onAttachmentError(final int error) { 2515 runOnUiThread(new Runnable() { 2516 @Override 2517 public void run() { 2518 handleAddAttachmentError(error, R.string.type_picture); 2519 onMessageSent(); // now requery the list of messages 2520 } 2521 }); 2522 } 2523 2524 // We don't want to show the "call" option unless there is only one 2525 // recipient and it's a phone number. 2526 private boolean isRecipientCallable() { 2527 ContactList recipients = getRecipients(); 2528 return (recipients.size() == 1 && !recipients.containsEmail()); 2529 } 2530 2531 private void dialRecipient() { 2532 if (isRecipientCallable()) { 2533 String number = getRecipients().get(0).getNumber(); 2534 Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number)); 2535 startActivity(dialIntent); 2536 } 2537 } 2538 2539 @Override 2540 public boolean onPrepareOptionsMenu(Menu menu) { 2541 super.onPrepareOptionsMenu(menu) ; 2542 2543 menu.clear(); 2544 2545 if (isRecipientCallable()) { 2546 MenuItem item = menu.add(0, MENU_CALL_RECIPIENT, 0, R.string.menu_call) 2547 .setIcon(R.drawable.ic_menu_call) 2548 .setTitle(R.string.menu_call); 2549 if (!isRecipientsEditorVisible()) { 2550 // If we're not composing a new message, show the call icon in the actionbar 2551 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 2552 } 2553 } 2554 2555 if (MmsConfig.getMmsEnabled()) { 2556 if (!isSubjectEditorVisible()) { 2557 menu.add(0, MENU_ADD_SUBJECT, 0, R.string.add_subject).setIcon( 2558 R.drawable.ic_menu_edit); 2559 } 2560 menu.add(0, MENU_ADD_ATTACHMENT, 0, R.string.add_attachment) 2561 .setIcon(R.drawable.ic_menu_attachment) 2562 .setTitle(R.string.add_attachment) 2563 .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); // add to actionbar 2564 } 2565 2566 if (isPreparedForSending()) { 2567 menu.add(0, MENU_SEND, 0, R.string.send).setIcon(android.R.drawable.ic_menu_send); 2568 } 2569 2570 if (!mWorkingMessage.hasSlideshow()) { 2571 menu.add(0, MENU_INSERT_SMILEY, 0, R.string.menu_insert_smiley).setIcon( 2572 R.drawable.ic_menu_emoticons); 2573 } 2574 2575 if (getRecipients().size() > 1) { 2576 menu.add(0, MENU_GROUP_PARTICIPANTS, 0, R.string.menu_group_participants); 2577 } 2578 2579 if (mMsgListAdapter.getCount() > 0) { 2580 // Removed search as part of b/1205708 2581 //menu.add(0, MENU_SEARCH, 0, R.string.menu_search).setIcon( 2582 // R.drawable.ic_menu_search); 2583 Cursor cursor = mMsgListAdapter.getCursor(); 2584 if ((null != cursor) && (cursor.getCount() > 0)) { 2585 menu.add(0, MENU_DELETE_THREAD, 0, R.string.delete_thread).setIcon( 2586 android.R.drawable.ic_menu_delete); 2587 } 2588 } else { 2589 menu.add(0, MENU_DISCARD, 0, R.string.discard).setIcon(android.R.drawable.ic_menu_delete); 2590 } 2591 2592 buildAddAddressToContactMenuItem(menu); 2593 2594 menu.add(0, MENU_PREFERENCES, 0, R.string.menu_preferences).setIcon( 2595 android.R.drawable.ic_menu_preferences); 2596 2597 if (LogTag.DEBUG_DUMP) { 2598 menu.add(0, MENU_DEBUG_DUMP, 0, R.string.menu_debug_dump); 2599 } 2600 2601 return true; 2602 } 2603 2604 private void buildAddAddressToContactMenuItem(Menu menu) { 2605 // bug #7087793: for group of recipients, remove "Add to People" action. Rely on 2606 // individually creating contacts for unknown phone numbers by touching the individual 2607 // sender's avatars, one at a time 2608 ContactList contacts = getRecipients(); 2609 if (contacts.size() != 1) { 2610 return; 2611 } 2612 2613 // if we don't have a contact for the recipient, create a menu item to add the number 2614 // to contacts. 2615 Contact c = contacts.get(0); 2616 if (!c.existsInDatabase() && canAddToContacts(c)) { 2617 Intent intent = ConversationList.createAddContactIntent(c.getNumber()); 2618 menu.add(0, MENU_ADD_ADDRESS_TO_CONTACTS, 0, R.string.menu_add_to_contacts) 2619 .setIcon(android.R.drawable.ic_menu_add) 2620 .setIntent(intent); 2621 } 2622 } 2623 2624 @Override 2625 public boolean onOptionsItemSelected(MenuItem item) { 2626 switch (item.getItemId()) { 2627 case MENU_ADD_SUBJECT: 2628 showSubjectEditor(true); 2629 mWorkingMessage.setSubject("", true); 2630 updateSendButtonState(); 2631 mSubjectTextEditor.requestFocus(); 2632 break; 2633 case MENU_ADD_ATTACHMENT: 2634 // Launch the add-attachment list dialog 2635 showAddAttachmentDialog(false); 2636 break; 2637 case MENU_DISCARD: 2638 mWorkingMessage.discard(); 2639 finish(); 2640 break; 2641 case MENU_SEND: 2642 if (isPreparedForSending()) { 2643 confirmSendMessageIfNeeded(); 2644 } 2645 break; 2646 case MENU_SEARCH: 2647 onSearchRequested(); 2648 break; 2649 case MENU_DELETE_THREAD: 2650 confirmDeleteThread(mConversation.getThreadId()); 2651 break; 2652 2653 case android.R.id.home: 2654 case MENU_CONVERSATION_LIST: 2655 exitComposeMessageActivity(new Runnable() { 2656 @Override 2657 public void run() { 2658 goToConversationList(); 2659 } 2660 }); 2661 break; 2662 case MENU_CALL_RECIPIENT: 2663 dialRecipient(); 2664 break; 2665 case MENU_INSERT_SMILEY: 2666 showSmileyDialog(); 2667 break; 2668 case MENU_GROUP_PARTICIPANTS: 2669 { 2670 Intent intent = new Intent(this, RecipientListActivity.class); 2671 intent.putExtra(THREAD_ID, mConversation.getThreadId()); 2672 startActivity(intent); 2673 break; 2674 } 2675 case MENU_VIEW_CONTACT: { 2676 // View the contact for the first (and only) recipient. 2677 ContactList list = getRecipients(); 2678 if (list.size() == 1 && list.get(0).existsInDatabase()) { 2679 Uri contactUri = list.get(0).getUri(); 2680 Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); 2681 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 2682 startActivity(intent); 2683 } 2684 break; 2685 } 2686 case MENU_ADD_ADDRESS_TO_CONTACTS: 2687 mAddContactIntent = item.getIntent(); 2688 startActivityForResult(mAddContactIntent, REQUEST_CODE_ADD_CONTACT); 2689 break; 2690 case MENU_PREFERENCES: { 2691 Intent intent = new Intent(this, MessagingPreferenceActivity.class); 2692 startActivityIfNeeded(intent, -1); 2693 break; 2694 } 2695 case MENU_DEBUG_DUMP: 2696 mWorkingMessage.dump(); 2697 Conversation.dump(); 2698 LogTag.dumpInternalTables(this); 2699 break; 2700 } 2701 2702 return true; 2703 } 2704 2705 private void confirmDeleteThread(long threadId) { 2706 Conversation.startQueryHaveLockedMessages(mBackgroundQueryHandler, 2707 threadId, ConversationList.HAVE_LOCKED_MESSAGES_TOKEN); 2708 } 2709 2710// static class SystemProperties { // TODO, temp class to get unbundling working 2711// static int getInt(String s, int value) { 2712// return value; // just return the default value or now 2713// } 2714// } 2715 2716 private void addAttachment(int type, boolean replace) { 2717 // Calculate the size of the current slide if we're doing a replace so the 2718 // slide size can optionally be used in computing how much room is left for an attachment. 2719 int currentSlideSize = 0; 2720 SlideshowModel slideShow = mWorkingMessage.getSlideshow(); 2721 if (replace && slideShow != null) { 2722 WorkingMessage.removeThumbnailsFromCache(slideShow); 2723 SlideModel slide = slideShow.get(0); 2724 currentSlideSize = slide.getSlideSize(); 2725 } 2726 switch (type) { 2727 case AttachmentTypeSelectorAdapter.ADD_IMAGE: 2728 MessageUtils.selectImage(this, REQUEST_CODE_ATTACH_IMAGE); 2729 break; 2730 2731 case AttachmentTypeSelectorAdapter.TAKE_PICTURE: { 2732 MessageUtils.capturePicture(this, REQUEST_CODE_TAKE_PICTURE); 2733 break; 2734 } 2735 2736 case AttachmentTypeSelectorAdapter.ADD_VIDEO: 2737 MessageUtils.selectVideo(this, REQUEST_CODE_ATTACH_VIDEO); 2738 break; 2739 2740 case AttachmentTypeSelectorAdapter.RECORD_VIDEO: { 2741 long sizeLimit = computeAttachmentSizeLimit(slideShow, currentSlideSize); 2742 if (sizeLimit > 0) { 2743 MessageUtils.recordVideo(this, REQUEST_CODE_TAKE_VIDEO, sizeLimit); 2744 } else { 2745 Toast.makeText(this, 2746 getString(R.string.message_too_big_for_video), 2747 Toast.LENGTH_SHORT).show(); 2748 } 2749 } 2750 break; 2751 2752 case AttachmentTypeSelectorAdapter.ADD_SOUND: 2753 MessageUtils.selectAudio(this, REQUEST_CODE_ATTACH_SOUND); 2754 break; 2755 2756 case AttachmentTypeSelectorAdapter.RECORD_SOUND: 2757 long sizeLimit = computeAttachmentSizeLimit(slideShow, currentSlideSize); 2758 MessageUtils.recordSound(this, REQUEST_CODE_RECORD_SOUND, sizeLimit); 2759 break; 2760 2761 case AttachmentTypeSelectorAdapter.ADD_SLIDESHOW: 2762 editSlideshow(); 2763 break; 2764 2765 default: 2766 break; 2767 } 2768 } 2769 2770 public static long computeAttachmentSizeLimit(SlideshowModel slideShow, int currentSlideSize) { 2771 // Computer attachment size limit. Subtract 1K for some text. 2772 long sizeLimit = MmsConfig.getMaxMessageSize() - SlideshowModel.SLIDESHOW_SLOP; 2773 if (slideShow != null) { 2774 sizeLimit -= slideShow.getCurrentMessageSize(); 2775 2776 // We're about to ask the camera to capture some video (or the sound recorder 2777 // to record some audio) which will eventually replace the content on the current 2778 // slide. Since the current slide already has some content (which was subtracted 2779 // out just above) and that content is going to get replaced, we can add the size of the 2780 // current slide into the available space used to capture a video (or audio). 2781 sizeLimit += currentSlideSize; 2782 } 2783 return sizeLimit; 2784 } 2785 2786 private void showAddAttachmentDialog(final boolean replace) { 2787 AlertDialog.Builder builder = new AlertDialog.Builder(this); 2788 builder.setIcon(R.drawable.ic_dialog_attach); 2789 builder.setTitle(R.string.add_attachment); 2790 2791 if (mAttachmentTypeSelectorAdapter == null) { 2792 mAttachmentTypeSelectorAdapter = new AttachmentTypeSelectorAdapter( 2793 this, AttachmentTypeSelectorAdapter.MODE_WITH_SLIDESHOW); 2794 } 2795 builder.setAdapter(mAttachmentTypeSelectorAdapter, new DialogInterface.OnClickListener() { 2796 @Override 2797 public void onClick(DialogInterface dialog, int which) { 2798 addAttachment(mAttachmentTypeSelectorAdapter.buttonToCommand(which), replace); 2799 dialog.dismiss(); 2800 } 2801 }); 2802 2803 builder.show(); 2804 } 2805 2806 @Override 2807 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 2808 if (LogTag.VERBOSE) { 2809 log("requestCode=" + requestCode + ", resultCode=" + resultCode + ", data=" + data); 2810 } 2811 mWaitingForSubActivity = false; // We're back! 2812 if (mWorkingMessage.isFakeMmsForDraft()) { 2813 // We no longer have to fake the fact we're an Mms. At this point we are or we aren't, 2814 // based on attachments and other Mms attrs. 2815 mWorkingMessage.removeFakeMmsForDraft(); 2816 } 2817 2818 if (requestCode == REQUEST_CODE_PICK) { 2819 mWorkingMessage.asyncDeleteDraftSmsMessage(mConversation); 2820 } 2821 2822 if (requestCode == REQUEST_CODE_ADD_CONTACT) { 2823 // The user might have added a new contact. When we tell contacts to add a contact 2824 // and tap "Done", we're not returned to Messaging. If we back out to return to 2825 // messaging after adding a contact, the resultCode is RESULT_CANCELED. Therefore, 2826 // assume a contact was added and get the contact and force our cached contact to 2827 // get reloaded with the new info (such as contact name). After the 2828 // contact is reloaded, the function onUpdate() in this file will get called 2829 // and it will update the title bar, etc. 2830 if (mAddContactIntent != null) { 2831 String address = 2832 mAddContactIntent.getStringExtra(ContactsContract.Intents.Insert.EMAIL); 2833 if (address == null) { 2834 address = 2835 mAddContactIntent.getStringExtra(ContactsContract.Intents.Insert.PHONE); 2836 } 2837 if (address != null) { 2838 Contact contact = Contact.get(address, false); 2839 if (contact != null) { 2840 contact.reload(); 2841 } 2842 } 2843 } 2844 } 2845 2846 if (resultCode != RESULT_OK){ 2847 if (LogTag.VERBOSE) log("bail due to resultCode=" + resultCode); 2848 return; 2849 } 2850 2851 switch (requestCode) { 2852 case REQUEST_CODE_CREATE_SLIDESHOW: 2853 if (data != null) { 2854 WorkingMessage newMessage = WorkingMessage.load(this, data.getData()); 2855 if (newMessage != null) { 2856 mWorkingMessage = newMessage; 2857 mWorkingMessage.setConversation(mConversation); 2858 updateThreadIdIfRunning(); 2859 drawTopPanel(false); 2860 updateSendButtonState(); 2861 } 2862 } 2863 break; 2864 2865 case REQUEST_CODE_TAKE_PICTURE: { 2866 // create a file based uri and pass to addImage(). We want to read the JPEG 2867 // data directly from file (using UriImage) instead of decoding it into a Bitmap, 2868 // which takes up too much memory and could easily lead to OOM. 2869 File file = new File(TempFileProvider.getScrapPath(this)); 2870 Uri uri = Uri.fromFile(file); 2871 2872 // Remove the old captured picture's thumbnail from the cache 2873 MmsApp.getApplication().getThumbnailManager().removeThumbnail(uri); 2874 2875 addImageAsync(uri, false); 2876 break; 2877 } 2878 2879 case REQUEST_CODE_ATTACH_IMAGE: { 2880 if (data != null) { 2881 addImageAsync(data.getData(), false); 2882 } 2883 break; 2884 } 2885 2886 case REQUEST_CODE_TAKE_VIDEO: 2887 Uri videoUri = TempFileProvider.renameScrapFile(".3gp", null, this); 2888 // Remove the old captured video's thumbnail from the cache 2889 MmsApp.getApplication().getThumbnailManager().removeThumbnail(videoUri); 2890 2891 addVideoAsync(videoUri, false); // can handle null videoUri 2892 break; 2893 2894 case REQUEST_CODE_ATTACH_VIDEO: 2895 if (data != null) { 2896 addVideoAsync(data.getData(), false); 2897 } 2898 break; 2899 2900 case REQUEST_CODE_ATTACH_SOUND: { 2901 Uri uri = (Uri) data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 2902 if (Settings.System.DEFAULT_RINGTONE_URI.equals(uri)) { 2903 break; 2904 } 2905 addAudio(uri); 2906 break; 2907 } 2908 2909 case REQUEST_CODE_RECORD_SOUND: 2910 if (data != null) { 2911 addAudio(data.getData()); 2912 } 2913 break; 2914 2915 case REQUEST_CODE_ECM_EXIT_DIALOG: 2916 boolean outOfEmergencyMode = data.getBooleanExtra(EXIT_ECM_RESULT, false); 2917 if (outOfEmergencyMode) { 2918 sendMessage(false); 2919 } 2920 break; 2921 2922 case REQUEST_CODE_PICK: 2923 if (data != null) { 2924 processPickResult(data); 2925 } 2926 break; 2927 2928 default: 2929 if (LogTag.VERBOSE) log("bail due to unknown requestCode=" + requestCode); 2930 break; 2931 } 2932 } 2933 2934 private void processPickResult(final Intent data) { 2935 // The EXTRA_PHONE_URIS stores the phone's urls that were selected by user in the 2936 // multiple phone picker. 2937 final Parcelable[] uris = 2938 data.getParcelableArrayExtra(Intents.EXTRA_PHONE_URIS); 2939 2940 final int recipientCount = uris != null ? uris.length : 0; 2941 2942 final int recipientLimit = MmsConfig.getRecipientLimit(); 2943 if (recipientLimit != Integer.MAX_VALUE && recipientCount > recipientLimit) { 2944 new AlertDialog.Builder(this) 2945 .setMessage(getString(R.string.too_many_recipients, recipientCount, recipientLimit)) 2946 .setPositiveButton(android.R.string.ok, null) 2947 .create().show(); 2948 return; 2949 } 2950 2951 final Handler handler = new Handler(); 2952 final ProgressDialog progressDialog = new ProgressDialog(this); 2953 progressDialog.setTitle(getText(R.string.pick_too_many_recipients)); 2954 progressDialog.setMessage(getText(R.string.adding_recipients)); 2955 progressDialog.setIndeterminate(true); 2956 progressDialog.setCancelable(false); 2957 2958 final Runnable showProgress = new Runnable() { 2959 @Override 2960 public void run() { 2961 progressDialog.show(); 2962 } 2963 }; 2964 // Only show the progress dialog if we can not finish off parsing the return data in 1s, 2965 // otherwise the dialog could flicker. 2966 handler.postDelayed(showProgress, 1000); 2967 2968 new Thread(new Runnable() { 2969 @Override 2970 public void run() { 2971 final ContactList list; 2972 try { 2973 list = ContactList.blockingGetByUris(uris); 2974 } finally { 2975 handler.removeCallbacks(showProgress); 2976 progressDialog.dismiss(); 2977 } 2978 // TODO: there is already code to update the contact header widget and recipients 2979 // editor if the contacts change. we can re-use that code. 2980 final Runnable populateWorker = new Runnable() { 2981 @Override 2982 public void run() { 2983 mRecipientsEditor.populate(list); 2984 updateTitle(list); 2985 } 2986 }; 2987 handler.post(populateWorker); 2988 } 2989 }, "ComoseMessageActivity.processPickResult").start(); 2990 } 2991 2992 private final ResizeImageResultCallback mResizeImageCallback = new ResizeImageResultCallback() { 2993 // TODO: make this produce a Uri, that's what we want anyway 2994 @Override 2995 public void onResizeResult(PduPart part, boolean append) { 2996 if (part == null) { 2997 handleAddAttachmentError(WorkingMessage.UNKNOWN_ERROR, R.string.type_picture); 2998 return; 2999 } 3000 3001 Context context = ComposeMessageActivity.this; 3002 PduPersister persister = PduPersister.getPduPersister(context); 3003 int result; 3004 3005 Uri messageUri = mWorkingMessage.saveAsMms(true); 3006 if (messageUri == null) { 3007 result = WorkingMessage.UNKNOWN_ERROR; 3008 } else { 3009 try { 3010 Uri dataUri = persister.persistPart(part, ContentUris.parseId(messageUri)); 3011 result = mWorkingMessage.setAttachment(WorkingMessage.IMAGE, dataUri, append); 3012 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3013 log("ResizeImageResultCallback: dataUri=" + dataUri); 3014 } 3015 } catch (MmsException e) { 3016 result = WorkingMessage.UNKNOWN_ERROR; 3017 } 3018 } 3019 3020 handleAddAttachmentError(result, R.string.type_picture); 3021 } 3022 }; 3023 3024 private void handleAddAttachmentError(final int error, final int mediaTypeStringId) { 3025 if (error == WorkingMessage.OK) { 3026 return; 3027 } 3028 Log.d(TAG, "handleAddAttachmentError: " + error); 3029 3030 runOnUiThread(new Runnable() { 3031 @Override 3032 public void run() { 3033 Resources res = getResources(); 3034 String mediaType = res.getString(mediaTypeStringId); 3035 String title, message; 3036 3037 switch(error) { 3038 case WorkingMessage.UNKNOWN_ERROR: 3039 message = res.getString(R.string.failed_to_add_media, mediaType); 3040 Toast.makeText(ComposeMessageActivity.this, message, Toast.LENGTH_SHORT).show(); 3041 return; 3042 case WorkingMessage.UNSUPPORTED_TYPE: 3043 title = res.getString(R.string.unsupported_media_format, mediaType); 3044 message = res.getString(R.string.select_different_media, mediaType); 3045 break; 3046 case WorkingMessage.MESSAGE_SIZE_EXCEEDED: 3047 title = res.getString(R.string.exceed_message_size_limitation, mediaType); 3048 message = res.getString(R.string.failed_to_add_media, mediaType); 3049 break; 3050 case WorkingMessage.IMAGE_TOO_LARGE: 3051 title = res.getString(R.string.failed_to_resize_image); 3052 message = res.getString(R.string.resize_image_error_information); 3053 break; 3054 default: 3055 throw new IllegalArgumentException("unknown error " + error); 3056 } 3057 3058 MessageUtils.showErrorDialog(ComposeMessageActivity.this, title, message); 3059 } 3060 }); 3061 } 3062 3063 private void addImageAsync(final Uri uri, final boolean append) { 3064 getAsyncDialog().runAsync(new Runnable() { 3065 @Override 3066 public void run() { 3067 addImage(uri, append); 3068 } 3069 }, null, R.string.adding_attachments_title); 3070 } 3071 3072 private void addImage(Uri uri, boolean append) { 3073 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3074 log("append=" + append + ", uri=" + uri); 3075 } 3076 3077 int result = mWorkingMessage.setAttachment(WorkingMessage.IMAGE, uri, append); 3078 3079 if (result == WorkingMessage.IMAGE_TOO_LARGE || 3080 result == WorkingMessage.MESSAGE_SIZE_EXCEEDED) { 3081 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3082 log("resize image " + uri); 3083 } 3084 MessageUtils.resizeImageAsync(ComposeMessageActivity.this, 3085 uri, mAttachmentEditorHandler, mResizeImageCallback, append); 3086 return; 3087 } 3088 handleAddAttachmentError(result, R.string.type_picture); 3089 } 3090 3091 private void addVideoAsync(final Uri uri, final boolean append) { 3092 getAsyncDialog().runAsync(new Runnable() { 3093 @Override 3094 public void run() { 3095 addVideo(uri, append); 3096 } 3097 }, null, R.string.adding_attachments_title); 3098 } 3099 3100 private void addVideo(Uri uri, boolean append) { 3101 if (uri != null) { 3102 int result = mWorkingMessage.setAttachment(WorkingMessage.VIDEO, uri, append); 3103 handleAddAttachmentError(result, R.string.type_video); 3104 } 3105 } 3106 3107 private void addAudio(Uri uri) { 3108 int result = mWorkingMessage.setAttachment(WorkingMessage.AUDIO, uri, false); 3109 handleAddAttachmentError(result, R.string.type_audio); 3110 } 3111 3112 AsyncDialog getAsyncDialog() { 3113 if (mAsyncDialog == null) { 3114 mAsyncDialog = new AsyncDialog(this); 3115 } 3116 return mAsyncDialog; 3117 } 3118 3119 private boolean handleForwardedMessage() { 3120 Intent intent = getIntent(); 3121 3122 // If this is a forwarded message, it will have an Intent extra 3123 // indicating so. If not, bail out. 3124 if (intent.getBooleanExtra("forwarded_message", false) == false) { 3125 return false; 3126 } 3127 3128 Uri uri = intent.getParcelableExtra("msg_uri"); 3129 3130 if (Log.isLoggable(LogTag.APP, Log.DEBUG)) { 3131 log("" + uri); 3132 } 3133 3134 if (uri != null) { 3135 mWorkingMessage = WorkingMessage.load(this, uri); 3136 mWorkingMessage.setSubject(intent.getStringExtra("subject"), false); 3137 } else { 3138 mWorkingMessage.setText(intent.getStringExtra("sms_body")); 3139 } 3140 3141 // let's clear the message thread for forwarded messages 3142 mMsgListAdapter.changeCursor(null); 3143 3144 return true; 3145 } 3146 3147 // Handle send actions, where we're told to send a picture(s) or text. 3148 private boolean handleSendIntent() { 3149 Intent intent = getIntent(); 3150 Bundle extras = intent.getExtras(); 3151 if (extras == null) { 3152 return false; 3153 } 3154 3155 final String mimeType = intent.getType(); 3156 String action = intent.getAction(); 3157 if (Intent.ACTION_SEND.equals(action)) { 3158 if (extras.containsKey(Intent.EXTRA_STREAM)) { 3159 final Uri uri = (Uri)extras.getParcelable(Intent.EXTRA_STREAM); 3160 getAsyncDialog().runAsync(new Runnable() { 3161 @Override 3162 public void run() { 3163 addAttachment(mimeType, uri, false); 3164 } 3165 }, null, R.string.adding_attachments_title); 3166 return true; 3167 } else if (extras.containsKey(Intent.EXTRA_TEXT)) { 3168 mWorkingMessage.setText(extras.getString(Intent.EXTRA_TEXT)); 3169 return true; 3170 } 3171 } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && 3172 extras.containsKey(Intent.EXTRA_STREAM)) { 3173 SlideshowModel slideShow = mWorkingMessage.getSlideshow(); 3174 final ArrayList<Parcelable> uris = extras.getParcelableArrayList(Intent.EXTRA_STREAM); 3175 int currentSlideCount = slideShow != null ? slideShow.size() : 0; 3176 int importCount = uris.size(); 3177 if (importCount + currentSlideCount > SlideshowEditor.MAX_SLIDE_NUM) { 3178 importCount = Math.min(SlideshowEditor.MAX_SLIDE_NUM - currentSlideCount, 3179 importCount); 3180 Toast.makeText(ComposeMessageActivity.this, 3181 getString(R.string.too_many_attachments, 3182 SlideshowEditor.MAX_SLIDE_NUM, importCount), 3183 Toast.LENGTH_LONG).show(); 3184 } 3185 3186 // Attach all the pictures/videos asynchronously off of the UI thread. 3187 // Show a progress dialog if adding all the slides hasn't finished 3188 // within half a second. 3189 final int numberToImport = importCount; 3190 getAsyncDialog().runAsync(new Runnable() { 3191 @Override 3192 public void run() { 3193 for (int i = 0; i < numberToImport; i++) { 3194 Parcelable uri = uris.get(i); 3195 addAttachment(mimeType, (Uri) uri, true); 3196 } 3197 } 3198 }, null, R.string.adding_attachments_title); 3199 return true; 3200 } 3201 return false; 3202 } 3203 3204 // mVideoUri will look like this: content://media/external/video/media 3205 private static final String mVideoUri = Video.Media.getContentUri("external").toString(); 3206 // mImageUri will look like this: content://media/external/images/media 3207 private static final String mImageUri = Images.Media.getContentUri("external").toString(); 3208 3209 private void addAttachment(String type, Uri uri, boolean append) { 3210 if (uri != null) { 3211 // When we're handling Intent.ACTION_SEND_MULTIPLE, the passed in items can be 3212 // videos, and/or images, and/or some other unknown types we don't handle. When 3213 // a single attachment is "shared" the type will specify an image or video. When 3214 // there are multiple types, the type passed in is "*/*". In that case, we've got 3215 // to look at the uri to figure out if it is an image or video. 3216 boolean wildcard = "*/*".equals(type); 3217 if (type.startsWith("image/") || (wildcard && uri.toString().startsWith(mImageUri))) { 3218 addImage(uri, append); 3219 } else if (type.startsWith("video/") || 3220 (wildcard && uri.toString().startsWith(mVideoUri))) { 3221 addVideo(uri, append); 3222 } 3223 } 3224 } 3225 3226 private String getResourcesString(int id, String mediaName) { 3227 Resources r = getResources(); 3228 return r.getString(id, mediaName); 3229 } 3230 3231 private void drawBottomPanel() { 3232 // Reset the counter for text editor. 3233 resetCounter(); 3234 3235 if (mWorkingMessage.hasSlideshow()) { 3236 mBottomPanel.setVisibility(View.GONE); 3237 mAttachmentEditor.requestFocus(); 3238 return; 3239 } 3240 3241 mBottomPanel.setVisibility(View.VISIBLE); 3242 3243 CharSequence text = mWorkingMessage.getText(); 3244 3245 // TextView.setTextKeepState() doesn't like null input. 3246 if (text != null) { 3247 mTextEditor.setTextKeepState(text); 3248 } else { 3249 mTextEditor.setText(""); 3250 } 3251 } 3252 3253 private void drawTopPanel(boolean showSubjectEditor) { 3254 boolean showingAttachment = mAttachmentEditor.update(mWorkingMessage); 3255 mAttachmentEditorScrollView.setVisibility(showingAttachment ? View.VISIBLE : View.GONE); 3256 showSubjectEditor(showSubjectEditor || mWorkingMessage.hasSubject()); 3257 3258 invalidateOptionsMenu(); 3259 } 3260 3261 //========================================================== 3262 // Interface methods 3263 //========================================================== 3264 3265 @Override 3266 public void onClick(View v) { 3267 if ((v == mSendButtonSms || v == mSendButtonMms) && isPreparedForSending()) { 3268 confirmSendMessageIfNeeded(); 3269 } else if ((v == mRecipientsPicker)) { 3270 launchMultiplePhonePicker(); 3271 } 3272 } 3273 3274 private void launchMultiplePhonePicker() { 3275 Intent intent = new Intent(Intents.ACTION_GET_MULTIPLE_PHONES); 3276 intent.addCategory("android.intent.category.DEFAULT"); 3277 intent.setType(Phone.CONTENT_TYPE); 3278 // We have to wait for the constructing complete. 3279 ContactList contacts = mRecipientsEditor.constructContactsFromInput(true); 3280 int urisCount = 0; 3281 Uri[] uris = new Uri[contacts.size()]; 3282 urisCount = 0; 3283 for (Contact contact : contacts) { 3284 if (Contact.CONTACT_METHOD_TYPE_PHONE == contact.getContactMethodType()) { 3285 uris[urisCount++] = contact.getPhoneUri(); 3286 } 3287 } 3288 if (urisCount > 0) { 3289 intent.putExtra(Intents.EXTRA_PHONE_URIS, uris); 3290 } 3291 startActivityForResult(intent, REQUEST_CODE_PICK); 3292 } 3293 3294 @Override 3295 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 3296 if (event != null) { 3297 // if shift key is down, then we want to insert the '\n' char in the TextView; 3298 // otherwise, the default action is to send the message. 3299 if (!event.isShiftPressed() && event.getAction() == KeyEvent.ACTION_DOWN) { 3300 if (isPreparedForSending()) { 3301 confirmSendMessageIfNeeded(); 3302 } 3303 return true; 3304 } 3305 return false; 3306 } 3307 3308 if (isPreparedForSending()) { 3309 confirmSendMessageIfNeeded(); 3310 } 3311 return true; 3312 } 3313 3314 private final TextWatcher mTextEditorWatcher = new TextWatcher() { 3315 @Override 3316 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 3317 } 3318 3319 @Override 3320 public void onTextChanged(CharSequence s, int start, int before, int count) { 3321 // This is a workaround for bug 1609057. Since onUserInteraction() is 3322 // not called when the user touches the soft keyboard, we pretend it was 3323 // called when textfields changes. This should be removed when the bug 3324 // is fixed. 3325 onUserInteraction(); 3326 3327 mWorkingMessage.setText(s); 3328 3329 updateSendButtonState(); 3330 3331 updateCounter(s, start, before, count); 3332 3333 ensureCorrectButtonHeight(); 3334 } 3335 3336 @Override 3337 public void afterTextChanged(Editable s) { 3338 } 3339 }; 3340 3341 /** 3342 * Ensures that if the text edit box extends past two lines then the 3343 * button will be shifted up to allow enough space for the character 3344 * counter string to be placed beneath it. 3345 */ 3346 private void ensureCorrectButtonHeight() { 3347 int currentTextLines = mTextEditor.getLineCount(); 3348 if (currentTextLines <= 2) { 3349 mTextCounter.setVisibility(View.GONE); 3350 } 3351 else if (currentTextLines > 2 && mTextCounter.getVisibility() == View.GONE) { 3352 // Making the counter invisible ensures that it is used to correctly 3353 // calculate the position of the send button even if we choose not to 3354 // display the text. 3355 mTextCounter.setVisibility(View.INVISIBLE); 3356 } 3357 } 3358 3359 private final TextWatcher mSubjectEditorWatcher = new TextWatcher() { 3360 @Override 3361 public void beforeTextChanged(CharSequence s, int start, int count, int after) { } 3362 3363 @Override 3364 public void onTextChanged(CharSequence s, int start, int before, int count) { 3365 mWorkingMessage.setSubject(s, true); 3366 updateSendButtonState(); 3367 } 3368 3369 @Override 3370 public void afterTextChanged(Editable s) { } 3371 }; 3372 3373 //========================================================== 3374 // Private methods 3375 //========================================================== 3376 3377 /** 3378 * Initialize all UI elements from resources. 3379 */ 3380 private void initResourceRefs() { 3381 mMsgListView = (MessageListView) findViewById(R.id.history); 3382 mMsgListView.setDivider(null); // no divider so we look like IM conversation. 3383 3384 // called to enable us to show some padding between the message list and the 3385 // input field but when the message list is scrolled that padding area is filled 3386 // in with message content 3387 mMsgListView.setClipToPadding(false); 3388 3389 mMsgListView.setOnSizeChangedListener(new OnSizeChangedListener() { 3390 public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { 3391 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3392 Log.v(TAG, "##### onSizeChanged: w=" + width + " h=" + height + 3393 " oldw=" + oldWidth + " oldh=" + oldHeight); 3394 } 3395 3396 // The message list view changed size, most likely because the keyboard 3397 // appeared or disappeared or the user typed/deleted chars in the message 3398 // box causing it to change its height when expanding/collapsing to hold more 3399 // lines of text. 3400 smoothScrollToEnd(false, height - oldHeight); 3401 } 3402 }); 3403 3404 mBottomPanel = findViewById(R.id.bottom_panel); 3405 mTextEditor = (EditText) findViewById(R.id.embedded_text_editor); 3406 mTextEditor.setOnEditorActionListener(this); 3407 mTextEditor.addTextChangedListener(mTextEditorWatcher); 3408 mTextEditor.setFilters(new InputFilter[] { 3409 new LengthFilter(MmsConfig.getMaxTextLimit())}); 3410 mTextCounter = (TextView) findViewById(R.id.text_counter); 3411 mSendButtonMms = (TextView) findViewById(R.id.send_button_mms); 3412 mSendButtonSms = (ImageButton) findViewById(R.id.send_button_sms); 3413 mSendButtonMms.setOnClickListener(this); 3414 mSendButtonSms.setOnClickListener(this); 3415 mTopPanel = findViewById(R.id.recipients_subject_linear); 3416 mTopPanel.setFocusable(false); 3417 mAttachmentEditor = (AttachmentEditor) findViewById(R.id.attachment_editor); 3418 mAttachmentEditor.setHandler(mAttachmentEditorHandler); 3419 mAttachmentEditorScrollView = findViewById(R.id.attachment_editor_scroll_view); 3420 } 3421 3422 private void confirmDeleteDialog(OnClickListener listener, boolean locked) { 3423 AlertDialog.Builder builder = new AlertDialog.Builder(this); 3424 builder.setCancelable(true); 3425 builder.setMessage(locked ? R.string.confirm_delete_locked_message : 3426 R.string.confirm_delete_message); 3427 builder.setPositiveButton(R.string.delete, listener); 3428 builder.setNegativeButton(R.string.no, null); 3429 builder.show(); 3430 } 3431 3432 void undeliveredMessageDialog(long date) { 3433 String body; 3434 3435 if (date >= 0) { 3436 body = getString(R.string.undelivered_msg_dialog_body, 3437 MessageUtils.formatTimeStampString(this, date)); 3438 } else { 3439 // FIXME: we can not get sms retry time. 3440 body = getString(R.string.undelivered_sms_dialog_body); 3441 } 3442 3443 Toast.makeText(this, body, Toast.LENGTH_LONG).show(); 3444 } 3445 3446 private void startMsgListQuery() { 3447 startMsgListQuery(MESSAGE_LIST_QUERY_TOKEN); 3448 } 3449 3450 private void startMsgListQuery(int token) { 3451 Uri conversationUri = mConversation.getUri(); 3452 3453 if (conversationUri == null) { 3454 log("##### startMsgListQuery: conversationUri is null, bail!"); 3455 return; 3456 } 3457 3458 long threadId = mConversation.getThreadId(); 3459 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3460 log("startMsgListQuery for " + conversationUri + ", threadId=" + threadId + 3461 " token: " + token + " mConversation: " + mConversation); 3462 } 3463 3464 // Cancel any pending queries 3465 mBackgroundQueryHandler.cancelOperation(token); 3466 try { 3467 // Kick off the new query 3468 mBackgroundQueryHandler.startQuery( 3469 token, 3470 threadId /* cookie */, 3471 conversationUri, 3472 PROJECTION, 3473 null, null, null); 3474 } catch (SQLiteException e) { 3475 SqliteWrapper.checkSQLiteException(this, e); 3476 } 3477 } 3478 3479 private void initMessageList() { 3480 if (mMsgListAdapter != null) { 3481 return; 3482 } 3483 3484 String highlightString = getIntent().getStringExtra("highlight"); 3485 Pattern highlight = highlightString == null 3486 ? null 3487 : Pattern.compile("\\b" + Pattern.quote(highlightString), Pattern.CASE_INSENSITIVE); 3488 3489 // Initialize the list adapter with a null cursor. 3490 mMsgListAdapter = new MessageListAdapter(this, null, mMsgListView, true, highlight); 3491 mMsgListAdapter.setOnDataSetChangedListener(mDataSetChangedListener); 3492 mMsgListAdapter.setMsgListItemHandler(mMessageListItemHandler); 3493 mMsgListView.setAdapter(mMsgListAdapter); 3494 mMsgListView.setItemsCanFocus(false); 3495 mMsgListView.setVisibility(View.VISIBLE); 3496 mMsgListView.setOnCreateContextMenuListener(mMsgListMenuCreateListener); 3497 mMsgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 3498 @Override 3499 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 3500 if (view != null) { 3501 ((MessageListItem) view).onMessageListItemClick(); 3502 } 3503 } 3504 }); 3505 } 3506 3507 private void loadDraft() { 3508 if (mWorkingMessage.isWorthSaving()) { 3509 Log.w(TAG, "called with non-empty working message"); 3510 return; 3511 } 3512 3513 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3514 log("call WorkingMessage.loadDraft"); 3515 } 3516 3517 mWorkingMessage = WorkingMessage.loadDraft(this, mConversation, 3518 new Runnable() { 3519 @Override 3520 public void run() { 3521 drawTopPanel(false); 3522 drawBottomPanel(); 3523 updateSendButtonState(); 3524 } 3525 }); 3526 } 3527 3528 private void saveDraft(boolean isStopping) { 3529 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3530 LogTag.debug("saveDraft"); 3531 } 3532 // TODO: Do something better here. Maybe make discard() legal 3533 // to call twice and make isEmpty() return true if discarded 3534 // so it is caught in the clause above this one? 3535 if (mWorkingMessage.isDiscarded()) { 3536 return; 3537 } 3538 3539 if (!mWaitingForSubActivity && 3540 !mWorkingMessage.isWorthSaving() && 3541 (!isRecipientsEditorVisible() || recipientCount() == 0)) { 3542 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3543 log("not worth saving, discard WorkingMessage and bail"); 3544 } 3545 mWorkingMessage.discard(); 3546 return; 3547 } 3548 3549 mWorkingMessage.saveDraft(isStopping); 3550 3551 if (mToastForDraftSave) { 3552 Toast.makeText(this, R.string.message_saved_as_draft, 3553 Toast.LENGTH_SHORT).show(); 3554 } 3555 } 3556 3557 private boolean isPreparedForSending() { 3558 int recipientCount = recipientCount(); 3559 3560 return recipientCount > 0 && recipientCount <= MmsConfig.getRecipientLimit() && 3561 (mWorkingMessage.hasAttachment() || 3562 mWorkingMessage.hasText() || 3563 mWorkingMessage.hasSubject()); 3564 } 3565 3566 private int recipientCount() { 3567 int recipientCount; 3568 3569 // To avoid creating a bunch of invalid Contacts when the recipients 3570 // editor is in flux, we keep the recipients list empty. So if the 3571 // recipients editor is showing, see if there is anything in it rather 3572 // than consulting the empty recipient list. 3573 if (isRecipientsEditorVisible()) { 3574 recipientCount = mRecipientsEditor.getRecipientCount(); 3575 } else { 3576 recipientCount = getRecipients().size(); 3577 } 3578 return recipientCount; 3579 } 3580 3581 private void sendMessage(boolean bCheckEcmMode) { 3582 if (bCheckEcmMode) { 3583 // TODO: expose this in telephony layer for SDK build 3584 String inEcm = SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE); 3585 if (Boolean.parseBoolean(inEcm)) { 3586 try { 3587 startActivityForResult( 3588 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null), 3589 REQUEST_CODE_ECM_EXIT_DIALOG); 3590 return; 3591 } catch (ActivityNotFoundException e) { 3592 // continue to send message 3593 Log.e(TAG, "Cannot find EmergencyCallbackModeExitDialog", e); 3594 } 3595 } 3596 } 3597 3598 if (!mSendingMessage) { 3599 if (LogTag.SEVERE_WARNING) { 3600 String sendingRecipients = mConversation.getRecipients().serialize(); 3601 if (!sendingRecipients.equals(mDebugRecipients)) { 3602 String workingRecipients = mWorkingMessage.getWorkingRecipients(); 3603 if (!mDebugRecipients.equals(workingRecipients)) { 3604 LogTag.warnPossibleRecipientMismatch("ComposeMessageActivity.sendMessage" + 3605 " recipients in window: \"" + 3606 mDebugRecipients + "\" differ from recipients from conv: \"" + 3607 sendingRecipients + "\" and working recipients: " + 3608 workingRecipients, this); 3609 } 3610 } 3611 sanityCheckConversation(); 3612 } 3613 3614 // send can change the recipients. Make sure we remove the listeners first and then add 3615 // them back once the recipient list has settled. 3616 removeRecipientsListeners(); 3617 3618 mWorkingMessage.send(mDebugRecipients); 3619 3620 mSentMessage = true; 3621 mSendingMessage = true; 3622 addRecipientsListeners(); 3623 3624 mScrollOnSend = true; // in the next onQueryComplete, scroll the list to the end. 3625 } 3626 // But bail out if we are supposed to exit after the message is sent. 3627 if (mExitOnSent) { 3628 finish(); 3629 } 3630 } 3631 3632 private void resetMessage() { 3633 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3634 log(""); 3635 } 3636 3637 // Make the attachment editor hide its view. 3638 mAttachmentEditor.hideView(); 3639 mAttachmentEditorScrollView.setVisibility(View.GONE); 3640 3641 // Hide the subject editor. 3642 showSubjectEditor(false); 3643 3644 // Focus to the text editor. 3645 mTextEditor.requestFocus(); 3646 3647 // We have to remove the text change listener while the text editor gets cleared and 3648 // we subsequently turn the message back into SMS. When the listener is listening while 3649 // doing the clearing, it's fighting to update its counts and itself try and turn 3650 // the message one way or the other. 3651 mTextEditor.removeTextChangedListener(mTextEditorWatcher); 3652 3653 // Clear the text box. 3654 TextKeyListener.clear(mTextEditor.getText()); 3655 3656 mWorkingMessage.clearConversation(mConversation, false); 3657 mWorkingMessage = WorkingMessage.createEmpty(this); 3658 mWorkingMessage.setConversation(mConversation); 3659 3660 hideRecipientEditor(); 3661 drawBottomPanel(); 3662 3663 // "Or not", in this case. 3664 updateSendButtonState(); 3665 3666 // Our changes are done. Let the listener respond to text changes once again. 3667 mTextEditor.addTextChangedListener(mTextEditorWatcher); 3668 3669 // Close the soft on-screen keyboard if we're in landscape mode so the user can see the 3670 // conversation. 3671 if (mIsLandscape) { 3672 hideKeyboard(); 3673 } 3674 3675 mLastRecipientCount = 0; 3676 mSendingMessage = false; 3677 invalidateOptionsMenu(); 3678 } 3679 3680 private void hideKeyboard() { 3681 InputMethodManager inputMethodManager = 3682 (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 3683 inputMethodManager.hideSoftInputFromWindow(mTextEditor.getWindowToken(), 0); 3684 } 3685 3686 private void updateSendButtonState() { 3687 boolean enable = false; 3688 if (isPreparedForSending()) { 3689 // When the type of attachment is slideshow, we should 3690 // also hide the 'Send' button since the slideshow view 3691 // already has a 'Send' button embedded. 3692 if (!mWorkingMessage.hasSlideshow()) { 3693 enable = true; 3694 } else { 3695 mAttachmentEditor.setCanSend(true); 3696 } 3697 } else if (null != mAttachmentEditor){ 3698 mAttachmentEditor.setCanSend(false); 3699 } 3700 3701 boolean requiresMms = mWorkingMessage.requiresMms(); 3702 View sendButton = showSmsOrMmsSendButton(requiresMms); 3703 sendButton.setEnabled(enable); 3704 sendButton.setFocusable(enable); 3705 } 3706 3707 private long getMessageDate(Uri uri) { 3708 if (uri != null) { 3709 Cursor cursor = SqliteWrapper.query(this, mContentResolver, 3710 uri, new String[] { Mms.DATE }, null, null, null); 3711 if (cursor != null) { 3712 try { 3713 if ((cursor.getCount() == 1) && cursor.moveToFirst()) { 3714 return cursor.getLong(0) * 1000L; 3715 } 3716 } finally { 3717 cursor.close(); 3718 } 3719 } 3720 } 3721 return NO_DATE_FOR_DIALOG; 3722 } 3723 3724 private void initActivityState(Bundle bundle) { 3725 Intent intent = getIntent(); 3726 if (bundle != null) { 3727 setIntent(getIntent().setAction(Intent.ACTION_VIEW)); 3728 String recipients = bundle.getString(RECIPIENTS); 3729 if (LogTag.VERBOSE) log("get mConversation by recipients " + recipients); 3730 mConversation = Conversation.get(this, 3731 ContactList.getByNumbers(recipients, 3732 false /* don't block */, true /* replace number */), false); 3733 addRecipientsListeners(); 3734 mExitOnSent = bundle.getBoolean("exit_on_sent", false); 3735 mWorkingMessage.readStateFromBundle(bundle); 3736 3737 return; 3738 } 3739 3740 // If we have been passed a thread_id, use that to find our conversation. 3741 long threadId = intent.getLongExtra(THREAD_ID, 0); 3742 if (threadId > 0) { 3743 if (LogTag.VERBOSE) log("get mConversation by threadId " + threadId); 3744 mConversation = Conversation.get(this, threadId, false); 3745 } else { 3746 Uri intentData = intent.getData(); 3747 if (intentData != null) { 3748 // try to get a conversation based on the data URI passed to our intent. 3749 if (LogTag.VERBOSE) log("get mConversation by intentData " + intentData); 3750 mConversation = Conversation.get(this, intentData, false); 3751 mWorkingMessage.setText(getBody(intentData)); 3752 } else { 3753 // special intent extra parameter to specify the address 3754 String address = intent.getStringExtra("address"); 3755 if (!TextUtils.isEmpty(address)) { 3756 if (LogTag.VERBOSE) log("get mConversation by address " + address); 3757 mConversation = Conversation.get(this, ContactList.getByNumbers(address, 3758 false /* don't block */, true /* replace number */), false); 3759 } else { 3760 if (LogTag.VERBOSE) log("create new conversation"); 3761 mConversation = Conversation.createNew(this); 3762 } 3763 } 3764 } 3765 addRecipientsListeners(); 3766 updateThreadIdIfRunning(); 3767 3768 mExitOnSent = intent.getBooleanExtra("exit_on_sent", false); 3769 if (intent.hasExtra("sms_body")) { 3770 mWorkingMessage.setText(intent.getStringExtra("sms_body")); 3771 } 3772 mWorkingMessage.setSubject(intent.getStringExtra("subject"), false); 3773 } 3774 3775 private void initFocus() { 3776 if (!mIsKeyboardOpen) { 3777 return; 3778 } 3779 3780 // If the recipients editor is visible, there is nothing in it, 3781 // and the text editor is not already focused, focus the 3782 // recipients editor. 3783 if (isRecipientsEditorVisible() 3784 && TextUtils.isEmpty(mRecipientsEditor.getText()) 3785 && !mTextEditor.isFocused()) { 3786 mRecipientsEditor.requestFocus(); 3787 return; 3788 } 3789 3790 // If we decided not to focus the recipients editor, focus the text editor. 3791 mTextEditor.requestFocus(); 3792 } 3793 3794 private final MessageListAdapter.OnDataSetChangedListener 3795 mDataSetChangedListener = new MessageListAdapter.OnDataSetChangedListener() { 3796 @Override 3797 public void onDataSetChanged(MessageListAdapter adapter) { 3798 } 3799 3800 @Override 3801 public void onContentChanged(MessageListAdapter adapter) { 3802 startMsgListQuery(); 3803 } 3804 }; 3805 3806 /** 3807 * smoothScrollToEnd will scroll the message list to the bottom if the list is already near 3808 * the bottom. Typically this is called to smooth scroll a newly received message into view. 3809 * It's also called when sending to scroll the list to the bottom, regardless of where it is, 3810 * so the user can see the just sent message. This function is also called when the message 3811 * list view changes size because the keyboard state changed or the compose message field grew. 3812 * 3813 * @param force always scroll to the bottom regardless of current list position 3814 * @param listSizeChange the amount the message list view size has vertically changed 3815 */ 3816 private void smoothScrollToEnd(boolean force, int listSizeChange) { 3817 int last = mMsgListView.getLastVisiblePosition(); 3818 if (last <= 0) { 3819 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3820 Log.v(TAG, "smoothScrollToEnd: last=" + last + ", mMsgListView not ready"); 3821 } 3822 return; 3823 } 3824 3825 View lastChild = mMsgListView.getChildAt(last - mMsgListView.getFirstVisiblePosition()); 3826 int bottom = 0; 3827 if (lastChild != null) { 3828 bottom = lastChild.getBottom(); 3829 } 3830 3831 int newPosition = mMsgListAdapter.getCount() - 1; 3832 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3833 Log.v(TAG, "smoothScrollToEnd newPosition: " + newPosition + 3834 " mLastSmoothScrollPosition: " + mLastSmoothScrollPosition + 3835 " first: " + mMsgListView.getFirstVisiblePosition() + 3836 " last: " + last + 3837 " bottom: " + bottom + 3838 " bottom + listSizeChange: " + (bottom + listSizeChange) + 3839 " mMsgListView.getHeight() - mMsgListView.getPaddingBottom(): " + 3840 (mMsgListView.getHeight() - mMsgListView.getPaddingBottom()) + 3841 " listSizeChange: " + listSizeChange); 3842 } 3843 // Only scroll if the list if we're responding to a newly sent message (force == true) or 3844 // the list is already scrolled to the end. This code also has to handle the case where 3845 // the listview has changed size (from the keyboard coming up or down or the message entry 3846 // field growing/shrinking) and it uses that grow/shrink factor in listSizeChange to 3847 // compute whether the list was at the end before the resize took place. 3848 // For example, when the keyboard comes up, listSizeChange will be negative, something 3849 // like -524. The lastChild listitem's bottom value will be the old value before the 3850 // keyboard became visible but the size of the list will have changed. The test below 3851 // add listSizeChange to bottom to figure out if the old position was already scrolled 3852 // to the bottom. 3853 if (force || ((listSizeChange != 0 || newPosition != mLastSmoothScrollPosition) && 3854 bottom + listSizeChange <= 3855 mMsgListView.getHeight() - mMsgListView.getPaddingBottom())) { 3856 if (Math.abs(listSizeChange) > SMOOTH_SCROLL_THRESHOLD) { 3857 // When the keyboard comes up, the window manager initiates a cross fade 3858 // animation that conflicts with smooth scroll. Handle that case by jumping the 3859 // list directly to the end. 3860 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3861 Log.v(TAG, "keyboard state changed. setSelection=" + newPosition); 3862 } 3863 mMsgListView.setSelection(newPosition); 3864 } else if (newPosition - last > MAX_ITEMS_TO_INVOKE_SCROLL_SHORTCUT) { 3865 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3866 Log.v(TAG, "too many to scroll, setSelection=" + newPosition); 3867 } 3868 mMsgListView.setSelection(newPosition); 3869 } else { 3870 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3871 Log.v(TAG, "smooth scroll to " + newPosition); 3872 } 3873 mMsgListView.smoothScrollToPosition(newPosition); 3874 mLastSmoothScrollPosition = newPosition; 3875 } 3876 } 3877 } 3878 3879 private final class BackgroundQueryHandler extends ConversationQueryHandler { 3880 public BackgroundQueryHandler(ContentResolver contentResolver) { 3881 super(contentResolver); 3882 } 3883 3884 @Override 3885 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 3886 switch(token) { 3887 case MESSAGE_LIST_QUERY_TOKEN: 3888 mConversation.blockMarkAsRead(false); 3889 3890 // check consistency between the query result and 'mConversation' 3891 long tid = (Long) cookie; 3892 3893 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 3894 log("##### onQueryComplete: msg history result for threadId " + tid); 3895 } 3896 if (tid != mConversation.getThreadId()) { 3897 log("onQueryComplete: msg history query result is for threadId " + 3898 tid + ", but mConversation has threadId " + 3899 mConversation.getThreadId() + " starting a new query"); 3900 if (cursor != null) { 3901 cursor.close(); 3902 } 3903 startMsgListQuery(); 3904 return; 3905 } 3906 3907 // check consistency b/t mConversation & mWorkingMessage.mConversation 3908 ComposeMessageActivity.this.sanityCheckConversation(); 3909 3910 int newSelectionPos = -1; 3911 long targetMsgId = getIntent().getLongExtra("select_id", -1); 3912 if (targetMsgId != -1) { 3913 cursor.moveToPosition(-1); 3914 while (cursor.moveToNext()) { 3915 long msgId = cursor.getLong(COLUMN_ID); 3916 if (msgId == targetMsgId) { 3917 newSelectionPos = cursor.getPosition(); 3918 break; 3919 } 3920 } 3921 } else if (mSavedScrollPosition != -1) { 3922 // mSavedScrollPosition is set when this activity pauses. If equals maxint, 3923 // it means the message list was scrolled to the end. Meanwhile, messages 3924 // could have been received. When the activity resumes and we were 3925 // previously scrolled to the end, jump the list so any new messages are 3926 // visible. 3927 if (mSavedScrollPosition == Integer.MAX_VALUE) { 3928 int cnt = mMsgListAdapter.getCount(); 3929 if (cnt > 0) { 3930 // Have to wait until the adapter is loaded before jumping to 3931 // the end. 3932 newSelectionPos = cnt - 1; 3933 mSavedScrollPosition = -1; 3934 } 3935 } else { 3936 // remember the saved scroll position before the activity is paused. 3937 // reset it after the message list query is done 3938 newSelectionPos = mSavedScrollPosition; 3939 mSavedScrollPosition = -1; 3940 } 3941 } 3942 3943 mMsgListAdapter.changeCursor(cursor); 3944 3945 if (newSelectionPos != -1) { 3946 mMsgListView.setSelection(newSelectionPos); // jump the list to the pos 3947 } else { 3948 int count = mMsgListAdapter.getCount(); 3949 long lastMsgId = 0; 3950 if (count > 0) { 3951 cursor.moveToLast(); 3952 lastMsgId = cursor.getLong(COLUMN_ID); 3953 } 3954 // mScrollOnSend is set when we send a message. We always want to scroll 3955 // the message list to the end when we send a message, but have to wait 3956 // until the DB has changed. We also want to scroll the list when a 3957 // new message has arrived. 3958 smoothScrollToEnd(mScrollOnSend || lastMsgId != mLastMessageId, 0); 3959 mLastMessageId = lastMsgId; 3960 mScrollOnSend = false; 3961 } 3962 // Adjust the conversation's message count to match reality. The 3963 // conversation's message count is eventually used in 3964 // WorkingMessage.clearConversation to determine whether to delete 3965 // the conversation or not. 3966 mConversation.setMessageCount(mMsgListAdapter.getCount()); 3967 3968 // Once we have completed the query for the message history, if 3969 // there is nothing in the cursor and we are not composing a new 3970 // message, we must be editing a draft in a new conversation (unless 3971 // mSentMessage is true). 3972 // Show the recipients editor to give the user a chance to add 3973 // more people before the conversation begins. 3974 if (cursor.getCount() == 0 && !isRecipientsEditorVisible() && !mSentMessage) { 3975 initRecipientsEditor(); 3976 } 3977 3978 // FIXME: freshing layout changes the focused view to an unexpected 3979 // one, set it back to TextEditor forcely. 3980 mTextEditor.requestFocus(); 3981 3982 invalidateOptionsMenu(); // some menu items depend on the adapter's count 3983 return; 3984 3985 case ConversationList.HAVE_LOCKED_MESSAGES_TOKEN: 3986 @SuppressWarnings("unchecked") 3987 ArrayList<Long> threadIds = (ArrayList<Long>)cookie; 3988 ConversationList.confirmDeleteThreadDialog( 3989 new ConversationList.DeleteThreadListener(threadIds, 3990 mBackgroundQueryHandler, ComposeMessageActivity.this), 3991 threadIds, 3992 cursor != null && cursor.getCount() > 0, 3993 ComposeMessageActivity.this); 3994 if (cursor != null) { 3995 cursor.close(); 3996 } 3997 break; 3998 3999 case MESSAGE_LIST_QUERY_AFTER_DELETE_TOKEN: 4000 // check consistency between the query result and 'mConversation' 4001 tid = (Long) cookie; 4002 4003 if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 4004 log("##### onQueryComplete (after delete): msg history result for threadId " 4005 + tid); 4006 } 4007 if (cursor == null) { 4008 return; 4009 } 4010 if (tid > 0 && cursor.getCount() == 0) { 4011 // We just deleted the last message and the thread will get deleted 4012 // by a trigger in the database. Clear the threadId so next time we 4013 // need the threadId a new thread will get created. 4014 log("##### MESSAGE_LIST_QUERY_AFTER_DELETE_TOKEN clearing thread id: " 4015 + tid); 4016 Conversation conv = Conversation.get(ComposeMessageActivity.this, tid, 4017 false); 4018 if (conv != null) { 4019 conv.clearThreadId(); 4020 conv.setDraftState(false); 4021 } 4022 } 4023 cursor.close(); 4024 } 4025 } 4026 4027 @Override 4028 protected void onDeleteComplete(int token, Object cookie, int result) { 4029 super.onDeleteComplete(token, cookie, result); 4030 switch(token) { 4031 case ConversationList.DELETE_CONVERSATION_TOKEN: 4032 mConversation.setMessageCount(0); 4033 // fall through 4034 case DELETE_MESSAGE_TOKEN: 4035 if (cookie instanceof Boolean && ((Boolean)cookie).booleanValue()) { 4036 // If we just deleted the last message, reset the saved id. 4037 mLastMessageId = 0; 4038 } 4039 // Update the notification for new messages since they 4040 // may be deleted. 4041 MessagingNotification.nonBlockingUpdateNewMessageIndicator( 4042 ComposeMessageActivity.this, MessagingNotification.THREAD_NONE, false); 4043 // Update the notification for failed messages since they 4044 // may be deleted. 4045 updateSendFailedNotification(); 4046 break; 4047 } 4048 // If we're deleting the whole conversation, throw away 4049 // our current working message and bail. 4050 if (token == ConversationList.DELETE_CONVERSATION_TOKEN) { 4051 ContactList recipients = mConversation.getRecipients(); 4052 mWorkingMessage.discard(); 4053 4054 // Remove any recipients referenced by this single thread from the 4055 // contacts cache. It's possible for two or more threads to reference 4056 // the same contact. That's ok if we remove it. We'll recreate that contact 4057 // when we init all Conversations below. 4058 if (recipients != null) { 4059 for (Contact contact : recipients) { 4060 contact.removeFromCache(); 4061 } 4062 } 4063 4064 // Make sure the conversation cache reflects the threads in the DB. 4065 Conversation.init(ComposeMessageActivity.this); 4066 finish(); 4067 } else if (token == DELETE_MESSAGE_TOKEN) { 4068 // Check to see if we just deleted the last message 4069 startMsgListQuery(MESSAGE_LIST_QUERY_AFTER_DELETE_TOKEN); 4070 } 4071 4072 MmsWidgetProvider.notifyDatasetChanged(getApplicationContext()); 4073 } 4074 } 4075 4076 private void showSmileyDialog() { 4077 if (mSmileyDialog == null) { 4078 int[] icons = SmileyParser.DEFAULT_SMILEY_RES_IDS; 4079 String[] names = getResources().getStringArray( 4080 SmileyParser.DEFAULT_SMILEY_NAMES); 4081 final String[] texts = getResources().getStringArray( 4082 SmileyParser.DEFAULT_SMILEY_TEXTS); 4083 4084 final int N = names.length; 4085 4086 List<Map<String, ?>> entries = new ArrayList<Map<String, ?>>(); 4087 for (int i = 0; i < N; i++) { 4088 // We might have different ASCII for the same icon, skip it if 4089 // the icon is already added. 4090 boolean added = false; 4091 for (int j = 0; j < i; j++) { 4092 if (icons[i] == icons[j]) { 4093 added = true; 4094 break; 4095 } 4096 } 4097 if (!added) { 4098 HashMap<String, Object> entry = new HashMap<String, Object>(); 4099 4100 entry. put("icon", icons[i]); 4101 entry. put("name", names[i]); 4102 entry.put("text", texts[i]); 4103 4104 entries.add(entry); 4105 } 4106 } 4107 4108 final SimpleAdapter a = new SimpleAdapter( 4109 this, 4110 entries, 4111 R.layout.smiley_menu_item, 4112 new String[] {"icon", "name", "text"}, 4113 new int[] {R.id.smiley_icon, R.id.smiley_name, R.id.smiley_text}); 4114 SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { 4115 @Override 4116 public boolean setViewValue(View view, Object data, String textRepresentation) { 4117 if (view instanceof ImageView) { 4118 Drawable img = getResources().getDrawable((Integer)data); 4119 ((ImageView)view).setImageDrawable(img); 4120 return true; 4121 } 4122 return false; 4123 } 4124 }; 4125 a.setViewBinder(viewBinder); 4126 4127 AlertDialog.Builder b = new AlertDialog.Builder(this); 4128 4129 b.setTitle(getString(R.string.menu_insert_smiley)); 4130 4131 b.setCancelable(true); 4132 b.setAdapter(a, new DialogInterface.OnClickListener() { 4133 @Override 4134 @SuppressWarnings("unchecked") 4135 public final void onClick(DialogInterface dialog, int which) { 4136 HashMap<String, Object> item = (HashMap<String, Object>) a.getItem(which); 4137 4138 String smiley = (String)item.get("text"); 4139 if (mSubjectTextEditor != null && mSubjectTextEditor.hasFocus()) { 4140 mSubjectTextEditor.append(smiley); 4141 } else { 4142 mTextEditor.append(smiley); 4143 } 4144 4145 dialog.dismiss(); 4146 } 4147 }); 4148 4149 mSmileyDialog = b.create(); 4150 } 4151 4152 mSmileyDialog.show(); 4153 } 4154 4155 @Override 4156 public void onUpdate(final Contact updated) { 4157 // Using an existing handler for the post, rather than conjuring up a new one. 4158 mMessageListItemHandler.post(new Runnable() { 4159 @Override 4160 public void run() { 4161 ContactList recipients = isRecipientsEditorVisible() ? 4162 mRecipientsEditor.constructContactsFromInput(false) : getRecipients(); 4163 if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { 4164 log("[CMA] onUpdate contact updated: " + updated); 4165 log("[CMA] onUpdate recipients: " + recipients); 4166 } 4167 updateTitle(recipients); 4168 4169 // The contact information for one (or more) of the recipients has changed. 4170 // Rebuild the message list so each MessageItem will get the last contact info. 4171 ComposeMessageActivity.this.mMsgListAdapter.notifyDataSetChanged(); 4172 4173 // Don't do this anymore. When we're showing chips, we don't want to switch from 4174 // chips to text. 4175// if (mRecipientsEditor != null) { 4176// mRecipientsEditor.populate(recipients); 4177// } 4178 } 4179 }); 4180 } 4181 4182 private void addRecipientsListeners() { 4183 Contact.addListener(this); 4184 } 4185 4186 private void removeRecipientsListeners() { 4187 Contact.removeListener(this); 4188 } 4189 4190 public static Intent createIntent(Context context, long threadId) { 4191 Intent intent = new Intent(context, ComposeMessageActivity.class); 4192 4193 if (threadId > 0) { 4194 intent.setData(Conversation.getUri(threadId)); 4195 } 4196 4197 return intent; 4198 } 4199 4200 private String getBody(Uri uri) { 4201 if (uri == null) { 4202 return null; 4203 } 4204 String urlStr = uri.getSchemeSpecificPart(); 4205 if (!urlStr.contains("?")) { 4206 return null; 4207 } 4208 urlStr = urlStr.substring(urlStr.indexOf('?') + 1); 4209 String[] params = urlStr.split("&"); 4210 for (String p : params) { 4211 if (p.startsWith("body=")) { 4212 try { 4213 return URLDecoder.decode(p.substring(5), "UTF-8"); 4214 } catch (UnsupportedEncodingException e) { } 4215 } 4216 } 4217 return null; 4218 } 4219 4220 private void updateThreadIdIfRunning() { 4221 if (mIsRunning && mConversation != null) { 4222 if (DEBUG) { 4223 Log.v(TAG, "updateThreadIdIfRunning: threadId: " + 4224 mConversation.getThreadId()); 4225 } 4226 MessagingNotification.setCurrentlyDisplayedThreadId(mConversation.getThreadId()); 4227 } else { 4228 if (DEBUG) { 4229 Log.v(TAG, "updateThreadIdIfRunning: mIsRunning: " + mIsRunning + 4230 " mConversation: " + mConversation); 4231 } 4232 } 4233 // If we're not running, but resume later, the current thread ID will be set in onResume() 4234 } 4235} 4236