2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.contacts;
19 import com.google.android.collect.Lists;
21 import static com.android.contacts.ContactEntryAdapter.CONTACT_CUSTOM_RINGTONE_COLUMN;
22 import static com.android.contacts.ContactEntryAdapter.CONTACT_NAME_COLUMN;
23 import static com.android.contacts.ContactEntryAdapter.CONTACT_NOTES_COLUMN;
24 import static com.android.contacts.ContactEntryAdapter.CONTACT_PROJECTION;
25 import static com.android.contacts.ContactEntryAdapter.CONTACT_SEND_TO_VOICEMAIL_COLUMN;
26 import static com.android.contacts.ContactEntryAdapter.CONTACT_PHONETIC_NAME_COLUMN;
27 import static com.android.contacts.ContactEntryAdapter.METHODS_AUX_DATA_COLUMN;
28 import static com.android.contacts.ContactEntryAdapter.METHODS_DATA_COLUMN;
29 import static com.android.contacts.ContactEntryAdapter.METHODS_ID_COLUMN;
30 import static com.android.contacts.ContactEntryAdapter.METHODS_ISPRIMARY_COLUMN;
31 import static com.android.contacts.ContactEntryAdapter.METHODS_KIND_COLUMN;
32 import static com.android.contacts.ContactEntryAdapter.METHODS_LABEL_COLUMN;
33 import static com.android.contacts.ContactEntryAdapter.METHODS_PROJECTION;
34 import static com.android.contacts.ContactEntryAdapter.METHODS_TYPE_COLUMN;
35 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_COMPANY_COLUMN;
36 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_ID_COLUMN;
37 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_ISPRIMARY_COLUMN;
38 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_LABEL_COLUMN;
39 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_PROJECTION;
40 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_TITLE_COLUMN;
41 import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_TYPE_COLUMN;
42 import static com.android.contacts.ContactEntryAdapter.PHONES_ID_COLUMN;
43 import static com.android.contacts.ContactEntryAdapter.PHONES_ISPRIMARY_COLUMN;
44 import static com.android.contacts.ContactEntryAdapter.PHONES_LABEL_COLUMN;
45 import static com.android.contacts.ContactEntryAdapter.PHONES_NUMBER_COLUMN;
46 import static com.android.contacts.ContactEntryAdapter.PHONES_PROJECTION;
47 import static com.android.contacts.ContactEntryAdapter.PHONES_TYPE_COLUMN;
49 import android.app.Activity;
50 import android.app.AlertDialog;
51 import android.app.Dialog;
52 import android.content.ActivityNotFoundException;
53 import android.content.ContentResolver;
54 import android.content.ContentUris;
55 import android.content.ContentValues;
56 import android.content.Context;
57 import android.content.DialogInterface;
58 import android.content.Intent;
59 import android.content.SharedPreferences;
60 import android.content.res.Resources;
61 import android.database.Cursor;
62 import android.graphics.Bitmap;
63 import android.media.Ringtone;
64 import android.media.RingtoneManager;
65 import android.net.Uri;
66 import android.os.Bundle;
67 import android.os.Parcel;
68 import android.os.Parcelable;
69 import android.preference.PreferenceManager;
70 import android.provider.Contacts;
71 import android.provider.Contacts.ContactMethods;
72 import android.provider.Contacts.Intents.Insert;
73 import android.provider.Contacts.Organizations;
74 import android.provider.Contacts.People;
75 import android.provider.Contacts.Phones;
76 import android.telephony.PhoneNumberFormattingTextWatcher;
77 import android.text.Editable;
78 import android.text.TextUtils;
79 import android.text.TextWatcher;
80 import android.text.method.DialerKeyListener;
81 import android.text.method.TextKeyListener;
82 import android.text.method.TextKeyListener.Capitalize;
83 import android.util.Log;
84 import android.view.ContextThemeWrapper;
85 import android.view.KeyEvent;
86 import android.view.LayoutInflater;
87 import android.view.Menu;
88 import android.view.MenuItem;
89 import android.view.View;
90 import android.view.ViewGroup;
91 import android.view.ViewParent;
92 import android.view.inputmethod.EditorInfo;
93 import android.widget.Button;
94 import android.widget.CheckBox;
95 import android.widget.CompoundButton;
96 import android.widget.EditText;
97 import android.widget.ExpandableListView;
98 import android.widget.ImageView;
99 import android.widget.LinearLayout;
100 import android.widget.SimpleExpandableListAdapter;
101 import android.widget.TextView;
102 import android.widget.Toast;
104 import java.io.ByteArrayOutputStream;
105 import java.util.ArrayList;
106 import java.util.HashMap;
107 import java.util.List;
108 import java.util.Locale;
109 import java.util.Map;
112 * Activity for editing or inserting a contact. Note that if the contact data changes in the
113 * background while this activity is running, the updates will be overwritten.
115 public final class EditContactActivity extends Activity implements View.OnClickListener,
116 ExpandableListView.OnChildClickListener, TextWatcher, CheckBox.OnCheckedChangeListener {
117 private static final String TAG = "EditContactActivity";
119 private static final int STATE_UNKNOWN = 0;
120 /** Editing an existing contact */
121 private static final int STATE_EDIT = 1;
122 /** The full insert mode */
123 private static final int STATE_INSERT = 2;
125 /** The launch code when picking a photo and the raw data is returned */
126 private static final int PHOTO_PICKED_WITH_DATA = 3021;
128 /** The launch code when picking a ringtone */
129 private static final int RINGTONE_PICKED = 3023;
131 // Label picker position info
132 final static int LABEL_PICKER_PHONES_POSITION = 0;
133 final static int LABEL_PICKER_EMAIL_POSITION = 1;
134 final static int LABEL_PICKER_IM_POSITION = 2;
135 final static int LABEL_PICKER_POSTAL_POSITION = 3;
136 final static int LABEL_PICKER_OTHER_POSITION = 4;
138 // These correspond to the string array in resources for picker "other" items
139 final static int OTHER_ORGANIZATION = 0;
140 final static int OTHER_NOTE = 1;
143 final static int LABEL_PICKER_ALL_TYPES_DIALOG = 1;
144 final static int DELETE_CONFIRMATION_DIALOG = 2;
147 public static final int MENU_ITEM_SAVE = 1;
148 public static final int MENU_ITEM_DONT_SAVE = 2;
149 public static final int MENU_ITEM_DELETE = 3;
150 public static final int MENU_ITEM_ADD = 5;
151 public static final int MENU_ITEM_PHOTO = 6;
153 /** Used to represent an invalid type for a contact entry */
154 private static final int INVALID_TYPE = -1;
156 /** The default type for a phone that is added via an intent */
157 private static final int DEFAULT_PHONE_TYPE = Phones.TYPE_MOBILE;
159 /** The default type for an email that is added via an intent */
160 private static final int DEFAULT_EMAIL_TYPE = ContactMethods.TYPE_HOME;
162 /** The default type for a postal address that is added via an intent */
163 private static final int DEFAULT_POSTAL_TYPE = ContactMethods.TYPE_HOME;
165 private int mState; // saved across instances
166 private boolean mInsert; // saved across instances
167 private Uri mUri; // saved across instances
168 /** In insert mode this is the photo */
169 private Bitmap mPhoto; // saved across instances
170 private boolean mPhotoChanged = false; // saved across instances
172 private EditText mNameView;
173 private ImageView mPhotoImageView;
174 private View mPhotoButton;
175 private CheckBox mSendToVoicemailCheckBox;
176 private LinearLayout mLayout;
177 private LayoutInflater mInflater;
178 private MenuItem mPhotoMenuItem;
179 private boolean mPhotoPresent = false;
180 private EditText mPhoneticNameView;
181 private LinearLayout mPhoneticNameLayout;
183 /** Flag marking this contact as changed, meaning we should write changes back. */
184 private boolean mContactChanged = false;
186 // These are accessed by inner classes. They're package scoped to make access more efficient.
187 /* package */ ContentResolver mResolver;
188 /* package */ ArrayList<EditEntry> mPhoneEntries = new ArrayList<EditEntry>();
189 /* package */ ArrayList<EditEntry> mEmailEntries = new ArrayList<EditEntry>();
190 /* package */ ArrayList<EditEntry> mImEntries = new ArrayList<EditEntry>();
191 /* package */ ArrayList<EditEntry> mPostalEntries = new ArrayList<EditEntry>();
192 /* package */ ArrayList<EditEntry> mOtherEntries = new ArrayList<EditEntry>();
193 /* package */ ArrayList<ArrayList<EditEntry>> mSections = new ArrayList<ArrayList<EditEntry>>();
195 /* package */ static final int MSG_DELETE = 1;
196 /* package */ static final int MSG_CHANGE_LABEL = 2;
197 /* package */ static final int MSG_ADD_PHONE = 3;
198 /* package */ static final int MSG_ADD_EMAIL = 4;
199 /* package */ static final int MSG_ADD_POSTAL = 5;
201 public void onClick(View v) {
203 case R.id.photoButton:
204 case R.id.photoImage: {
213 case R.id.saveButton:
217 case R.id.discardButton:
223 EditEntry entry = findEntryForView(v);
225 // Clear the text and hide the view so it gets saved properly
226 ((TextView) entry.view.findViewById(R.id.data)).setText(null);
227 entry.view.setVisibility(View.GONE);
228 entry.isDeleted = true;
234 EditEntry entry = findEntryForView(v);
236 String[] labels = getLabelsForKind(this, entry.kind);
237 LabelPickedListener listener = new LabelPickedListener(entry, labels);
238 new AlertDialog.Builder(EditContactActivity.this)
239 .setItems(labels, listener)
240 .setTitle(R.string.selectLabel)
247 EditEntry entry = findEntryForView(v);
248 if (isRingtoneEntry(entry)) {
249 doPickRingtone(entry);
256 private void setPhotoPresent(boolean present) {
257 mPhotoImageView.setVisibility(present ? View.VISIBLE : View.GONE);
258 mPhotoButton.setVisibility(present ? View.GONE : View.VISIBLE);
259 mPhotoPresent = present;
260 if (mPhotoMenuItem != null) {
262 mPhotoMenuItem.setTitle(R.string.removePicture);
263 mPhotoMenuItem.setIcon(android.R.drawable.ic_menu_delete);
265 mPhotoMenuItem.setTitle(R.string.addPicture);
266 mPhotoMenuItem.setIcon(R.drawable.ic_menu_add_picture);
271 private EditEntry findEntryForView(View v) {
272 // Try to find the entry for this view
273 EditEntry entry = null;
275 Object tag = v.getTag();
276 if (tag != null && tag instanceof EditEntry) {
277 entry = (EditEntry) tag;
280 ViewParent parent = v.getParent();
281 if (parent != null && parent instanceof View) {
291 private DialogInterface.OnClickListener mDeleteContactDialogListener =
292 new DialogInterface.OnClickListener() {
293 public void onClick(DialogInterface dialog, int button) {
294 mResolver.delete(mUri, null, null);
299 private boolean mMobilePhoneAdded = false;
300 private boolean mPrimaryEmailAdded = false;
303 protected void onCreate(Bundle icicle) {
304 super.onCreate(icicle);
306 mResolver = getContentResolver();
308 // Build the list of sections
312 setContentView(R.layout.edit_contact);
313 mLayout = (LinearLayout) findViewById(R.id.list);
314 mNameView = (EditText) findViewById(R.id.name);
315 mPhotoImageView = (ImageView) findViewById(R.id.photoImage);
316 mPhotoImageView.setOnClickListener(this);
317 mPhotoImageView.setVisibility(View.GONE);
318 mPhotoButton = findViewById(R.id.photoButton);
319 mPhotoButton.setOnClickListener(this);
320 mSendToVoicemailCheckBox = (CheckBox) findViewById(R.id.send_to_voicemail);
321 mPhoneticNameView = (EditText) findViewById(R.id.phonetic_name);
322 mPhoneticNameLayout = (LinearLayout) findViewById(R.id.phonetic_name_layout);
324 // Setup phonetic name field. mPhoneticNameLayout is GONE by default.
325 // TODO: Don't do this here in Java; instead do it purely using
326 // resources, by having mPhoneticNameLayout come from an XML
327 // <include> file that contains the real UI in layout-ja, but is
328 // empty in layout-finger...
329 String language = Locale.getDefault().getLanguage();
330 if (language != null && language.equals("ja")) {
331 mPhoneticNameLayout.setVisibility(View.VISIBLE);
334 // Setup the bottom buttons
335 View view = findViewById(R.id.addMore);
336 view.setOnClickListener(this);
337 view = findViewById(R.id.saveButton);
338 view.setOnClickListener(this);
339 view = findViewById(R.id.discardButton);
340 view.setOnClickListener(this);
342 mInflater = getLayoutInflater();
344 // Resolve the intent
345 mState = STATE_UNKNOWN;
346 Intent intent = getIntent();
347 String action = intent.getAction();
348 mUri = intent.getData();
350 if (action.equals(Intent.ACTION_EDIT)) {
351 if (icicle == null) {
352 // Build the entries & views
353 buildEntriesForEdit(getIntent().getExtras());
357 } else if (action.equals(Intent.ACTION_INSERT)) {
358 if (icicle == null) {
359 // Build the entries & views
360 buildEntriesForInsert(getIntent().getExtras());
363 mState = STATE_INSERT;
368 if (mState == STATE_UNKNOWN) {
369 Log.e(TAG, "Cannot resolve intent: " + intent);
374 if (mState == STATE_EDIT) {
375 setTitle(getResources().getText(R.string.editContact_title_edit));
377 setTitle(getResources().getText(R.string.editContact_title_insert));
381 private void setupSections() {
382 mSections.add(mPhoneEntries);
383 mSections.add(mEmailEntries);
384 mSections.add(mImEntries);
385 mSections.add(mPostalEntries);
386 mSections.add(mOtherEntries);
390 protected void onSaveInstanceState(Bundle outState) {
391 outState.putParcelableArrayList("phoneEntries", mPhoneEntries);
392 outState.putParcelableArrayList("emailEntries", mEmailEntries);
393 outState.putParcelableArrayList("imEntries", mImEntries);
394 outState.putParcelableArrayList("postalEntries", mPostalEntries);
395 outState.putParcelableArrayList("otherEntries", mOtherEntries);
396 outState.putInt("state", mState);
397 outState.putBoolean("insert", mInsert);
398 outState.putParcelable("uri", mUri);
399 outState.putString("name", mNameView.getText().toString());
400 outState.putParcelable("photo", mPhoto);
401 outState.putBoolean("photoChanged", mPhotoChanged);
402 outState.putBoolean("sendToVoicemail", mSendToVoicemailCheckBox.isChecked());
403 outState.putString("phoneticName", mPhoneticNameView.getText().toString());
404 outState.putBoolean("contactChanged", mContactChanged);
408 protected void onRestoreInstanceState(Bundle inState) {
409 mPhoneEntries = inState.getParcelableArrayList("phoneEntries");
410 mEmailEntries = inState.getParcelableArrayList("emailEntries");
411 mImEntries = inState.getParcelableArrayList("imEntries");
412 mPostalEntries = inState.getParcelableArrayList("postalEntries");
413 mOtherEntries = inState.getParcelableArrayList("otherEntries");
416 mState = inState.getInt("state");
417 mInsert = inState.getBoolean("insert");
418 mUri = inState.getParcelable("uri");
419 mNameView.setText(inState.getString("name"));
420 mPhoto = inState.getParcelable("photo");
421 if (mPhoto != null) {
422 mPhotoImageView.setImageBitmap(mPhoto);
423 setPhotoPresent(true);
425 mPhotoImageView.setImageResource(R.drawable.ic_contact_picture);
426 setPhotoPresent(false);
428 mPhotoChanged = inState.getBoolean("photoChanged");
429 mSendToVoicemailCheckBox.setChecked(inState.getBoolean("sendToVoicemail"));
430 mPhoneticNameView.setText(inState.getString("phoneticName"));
431 mContactChanged = inState.getBoolean("contactChanged");
433 // Now that everything is restored, build the view
438 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
439 if (resultCode != RESULT_OK) {
443 switch (requestCode) {
444 case PHOTO_PICKED_WITH_DATA: {
445 final Bundle extras = data.getExtras();
446 if (extras != null) {
447 Bitmap photo = extras.getParcelable("data");
449 mPhotoChanged = true;
450 mPhotoImageView.setImageBitmap(photo);
451 setPhotoPresent(true);
456 case RINGTONE_PICKED: {
457 Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
458 handleRingtonePicked(pickedUri);
459 mContactChanged = true;
466 public boolean onKeyDown(int keyCode, KeyEvent event) {
468 case KeyEvent.KEYCODE_BACK: {
473 return super.onKeyDown(keyCode, event);
477 public boolean onCreateOptionsMenu(Menu menu) {
478 super.onCreateOptionsMenu(menu);
479 menu.add(0, MENU_ITEM_SAVE, 0, R.string.menu_done)
480 .setIcon(android.R.drawable.ic_menu_save)
481 .setAlphabeticShortcut('\n');
482 menu.add(0, MENU_ITEM_DONT_SAVE, 0, R.string.menu_doNotSave)
483 .setIcon(android.R.drawable.ic_menu_close_clear_cancel)
484 .setAlphabeticShortcut('q');
486 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
487 .setIcon(android.R.drawable.ic_menu_delete);
490 menu.add(0, MENU_ITEM_ADD, 0, R.string.menu_addItem)
491 .setIcon(android.R.drawable.ic_menu_add)
492 .setAlphabeticShortcut('n');
494 mPhotoMenuItem = menu.add(0, MENU_ITEM_PHOTO, 0, null);
495 // Updates the state of the menu item
496 setPhotoPresent(mPhotoPresent);
502 public boolean onOptionsItemSelected(MenuItem item) {
503 switch (item.getItemId()) {
508 case MENU_ITEM_DONT_SAVE:
512 case MENU_ITEM_DELETE:
514 showDialog(DELETE_CONFIRMATION_DIALOG);
521 case MENU_ITEM_PHOTO:
522 if (!mPhotoPresent) {
525 doRemovePhotoAction();
533 private void doAddAction() {
534 showDialog(LABEL_PICKER_ALL_TYPES_DIALOG);
537 private void doRevertAction() {
541 private void doPickPhotoAction() {
542 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
543 // TODO: get these values from constants somewhere
544 intent.setType("image/*");
545 intent.putExtra("crop", "true");
546 intent.putExtra("aspectX", 1);
547 intent.putExtra("aspectY", 1);
548 intent.putExtra("outputX", 96);
549 intent.putExtra("outputY", 96);
551 intent.putExtra("return-data", true);
552 startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
553 } catch (ActivityNotFoundException e) {
554 new AlertDialog.Builder(EditContactActivity.this)
555 .setTitle(R.string.errorDialogTitle)
556 .setMessage(R.string.photoPickerNotFoundText)
557 .setPositiveButton(android.R.string.ok, null)
562 private void doRemovePhotoAction() {
564 mPhotoChanged = true;
565 mPhotoImageView.setImageResource(R.drawable.ic_contact_picture);
566 setPhotoPresent(false);
569 private void doPickRingtone(EditEntry entry) {
570 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
571 // Allow user to pick 'Default'
572 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
573 // Show only ringtones
574 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
575 // Don't show 'Silent'
576 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
579 if (entry.data != null) {
580 ringtoneUri = Uri.parse(entry.data);
582 // Otherwise pick default ringtone Uri so that something is selected.
583 ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
586 // Put checkmark next to the current ringtone for this contact
587 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
589 startActivityForResult(intent, RINGTONE_PICKED);
592 private void handleRingtonePicked(Uri pickedUri) {
593 EditEntry entry = getRingtoneEntry();
595 Log.w(TAG, "Ringtone picked but could not find ringtone entry");
599 if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
602 entry.data = pickedUri.toString();
605 updateRingtoneView(entry);
608 private void updateRingtoneView(EditEntry entry) {
609 if (entry.data == null) {
610 updateDataView(entry, getString(R.string.default_ringtone));
612 Uri ringtoneUri = Uri.parse(entry.data);
613 Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
614 if (ringtone == null) {
615 Log.w(TAG, "ringtone's URI doesn't resolve to a Ringtone");
618 updateDataView(entry, ringtone.getTitle(this));
622 private void updateDataView(EditEntry entry, String text) {
623 TextView dataView = (TextView) entry.view.findViewById(R.id.data);
624 dataView.setText(text);
628 protected Dialog onCreateDialog(int id) {
630 case LABEL_PICKER_ALL_TYPES_DIALOG:
631 return createAllTypesPicker();
633 case DELETE_CONFIRMATION_DIALOG:
634 return new AlertDialog.Builder(EditContactActivity.this)
635 .setTitle(R.string.deleteConfirmation_title)
636 .setIcon(android.R.drawable.ic_dialog_alert)
637 .setMessage(R.string.deleteConfirmation)
638 .setNegativeButton(android.R.string.cancel, null)
639 .setPositiveButton(android.R.string.ok, mDeleteContactDialogListener)
640 .setCancelable(false)
643 return super.onCreateDialog(id);
646 static String[] getLabelsForKind(Context context, int kind) {
647 final Resources resources = context.getResources();
649 case Contacts.KIND_PHONE:
650 return resources.getStringArray(android.R.array.phoneTypes);
651 case Contacts.KIND_EMAIL:
652 return resources.getStringArray(android.R.array.emailAddressTypes);
653 case Contacts.KIND_POSTAL:
654 return resources.getStringArray(android.R.array.postalAddressTypes);
655 case Contacts.KIND_IM:
656 return resources.getStringArray(android.R.array.imProtocols);
657 case Contacts.KIND_ORGANIZATION:
658 return resources.getStringArray(android.R.array.organizationTypes);
659 case EditEntry.KIND_CONTACT:
660 return resources.getStringArray(R.array.otherLabels);
665 int getTypeFromLabelPosition(CharSequence[] labels, int labelPosition) {
666 // In the UI Custom... comes last, but it is uses the constant 0
667 // so it is in the same location across the various kinds. Fix up the
668 // position to a valid type here.
669 if (labelPosition == labels.length - 1) {
670 return ContactMethods.TYPE_CUSTOM;
672 return labelPosition + 1;
676 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
677 int childPosition, long id) {
678 EditEntry entry = null;
680 // Make the dialog go away
681 dismissDialog(LABEL_PICKER_ALL_TYPES_DIALOG);
683 // Create the new entry
684 switch (groupPosition) {
685 case LABEL_PICKER_PHONES_POSITION: {
686 String[] labels = getLabelsForKind(this, Contacts.KIND_PHONE);
687 final int type = getTypeFromLabelPosition(labels, childPosition);
688 entry = EditEntry.newPhoneEntry(EditContactActivity.this,
689 labels[childPosition], type,
690 null, Uri.withAppendedPath(mUri, People.Phones.CONTENT_DIRECTORY), 0);
691 if (type == Phones.TYPE_CUSTOM) {
692 createCustomPicker(entry, mPhoneEntries);
695 mPhoneEntries.add(entry);
700 case LABEL_PICKER_EMAIL_POSITION: {
701 String[] labels = getLabelsForKind(this, Contacts.KIND_EMAIL);
702 final int type = getTypeFromLabelPosition(labels, childPosition);
703 entry = EditEntry.newEmailEntry(EditContactActivity.this,
704 labels[childPosition], type, null,
705 Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY), 0);
706 if (type == ContactMethods.TYPE_CUSTOM) {
707 createCustomPicker(entry, mEmailEntries);
710 mEmailEntries.add(entry);
715 case LABEL_PICKER_IM_POSITION: {
716 String[] labels = getLabelsForKind(this, Contacts.KIND_IM);
717 entry = EditEntry.newImEntry(EditContactActivity.this,
718 labels[childPosition], childPosition, null,
719 Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY), 0);
720 mImEntries.add(entry);
724 case LABEL_PICKER_POSTAL_POSITION: {
725 String[] labels = getLabelsForKind(this, Contacts.KIND_POSTAL);
726 final int type = getTypeFromLabelPosition(labels, childPosition);
727 entry = EditEntry.newPostalEntry(EditContactActivity.this,
728 labels[childPosition], type, null,
729 Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY), 0);
730 if (type == ContactMethods.TYPE_CUSTOM) {
731 createCustomPicker(entry, mPostalEntries);
734 mPostalEntries.add(entry);
739 case LABEL_PICKER_OTHER_POSITION: {
740 switch (childPosition) {
741 case OTHER_ORGANIZATION:
742 entry = EditEntry.newOrganizationEntry(EditContactActivity.this,
743 Uri.withAppendedPath(mUri, Organizations.CONTENT_DIRECTORY),
744 Organizations.TYPE_WORK);
745 mOtherEntries.add(entry);
749 entry = EditEntry.newNotesEntry(EditContactActivity.this, null, mUri);
750 mOtherEntries.add(entry);
763 // Rebuild the views if needed
766 mContactChanged = true;
768 View dataView = entry.view.findViewById(R.id.data);
769 if (dataView == null) {
770 entry.view.requestFocus();
772 dataView.requestFocus();
778 private EditEntry getRingtoneEntry() {
779 for (int i = mOtherEntries.size() - 1; i >= 0; i--) {
780 EditEntry entry = mOtherEntries.get(i);
781 if (isRingtoneEntry(entry)) {
788 private static boolean isRingtoneEntry(EditEntry entry) {
789 return entry != null && entry.column != null && entry.column.equals(People.CUSTOM_RINGTONE);
792 private Dialog createAllTypesPicker() {
794 List<Map<String, ?>> groupData = Lists.newArrayList();
795 List<List<Map<String, ?>>> childData = Lists.newArrayList();
796 List<Map<String, ?>> children;
797 HashMap<String, CharSequence> curGroupMap;
798 CharSequence[] labels;
802 curGroupMap = new HashMap<String, CharSequence>();
803 groupData.add(curGroupMap);
804 curGroupMap.put("data", getText(R.string.phoneLabelsGroup));
806 labels = getLabelsForKind(this, Contacts.KIND_PHONE);
807 labelsSize = labels.length;
808 children = Lists.newArrayList();
809 for (int i = 0; i < labelsSize; i++) {
810 HashMap<String, CharSequence> curChildMap = new HashMap<String, CharSequence>();
811 children.add(curChildMap);
812 curChildMap.put("data", labels[i]);
814 childData.add(LABEL_PICKER_PHONES_POSITION, children);
817 curGroupMap = new HashMap<String, CharSequence>();
818 groupData.add(curGroupMap);
819 curGroupMap.put("data", getText(R.string.emailLabelsGroup));
821 labels = getLabelsForKind(this, Contacts.KIND_EMAIL);
822 labelsSize = labels.length;
823 children = Lists.newArrayList();
824 for (int i = 0; i < labelsSize; i++) {
825 HashMap<String, CharSequence> curChildMap = new HashMap<String, CharSequence>();
826 children.add(curChildMap);
827 curChildMap.put("data", labels[i]);
829 childData.add(LABEL_PICKER_EMAIL_POSITION, children);
832 curGroupMap = new HashMap<String, CharSequence>();
833 groupData.add(curGroupMap);
834 curGroupMap.put("data", getText(R.string.imLabelsGroup));
836 labels = getLabelsForKind(this, Contacts.KIND_IM);
837 labelsSize = labels.length;
838 children = Lists.newArrayList();
839 for (int i = 0; i < labelsSize; i++) {
840 HashMap<String, CharSequence> curChildMap = new HashMap<String, CharSequence>();
841 children.add(curChildMap);
842 curChildMap.put("data", labels[i]);
844 childData.add(LABEL_PICKER_IM_POSITION, children);
847 curGroupMap = new HashMap<String, CharSequence>();
848 groupData.add(curGroupMap);
849 curGroupMap.put("data", getText(R.string.postalLabelsGroup));
851 labels = getLabelsForKind(this, Contacts.KIND_POSTAL);
852 labelsSize = labels.length;
853 children = Lists.newArrayList();
854 for (int i = 0; i < labelsSize; i++) {
855 HashMap<String, CharSequence> curChildMap = new HashMap<String, CharSequence>();
856 children.add(curChildMap);
857 curChildMap.put("data", labels[i]);
859 childData.add(LABEL_PICKER_POSTAL_POSITION, children);
862 curGroupMap = new HashMap<String, CharSequence>();
863 groupData.add(curGroupMap);
864 curGroupMap.put("data", getText(R.string.otherLabelsGroup));
866 labels = getLabelsForKind(this, EditEntry.KIND_CONTACT);
867 labelsSize = labels.length;
868 children = Lists.newArrayList();
869 for (int i = 0; i < labelsSize; i++) {
870 HashMap<String, CharSequence> curChildMap = new HashMap<String, CharSequence>();
871 children.add(curChildMap);
872 curChildMap.put("data", labels[i]);
874 childData.add(LABEL_PICKER_OTHER_POSITION, children);
876 // Create the expandable list view
877 ExpandableListView list = new ExpandableListView(new ContextThemeWrapper(this,
878 android.R.style.Theme_Light));
879 list.setOnChildClickListener(this);
880 list.setAdapter(new SimpleExpandableListAdapter(
881 new ContextThemeWrapper(this, android.R.style.Theme_Light),
883 android.R.layout.simple_expandable_list_item_1,
884 new String[] { "data" },
885 new int[] { android.R.id.text1 },
887 android.R.layout.simple_expandable_list_item_1,
888 new String[] { "data" },
889 new int[] { android.R.id.text1 }
891 // This list shouldn't have a color hint since the dialog may be transparent
892 list.setCacheColorHint(0);
895 return new AlertDialog.Builder(this).setView(list).setInverseBackgroundForced(true)
896 .setTitle(R.string.selectLabel).create();
899 private void createCustomPicker(final EditEntry entry, final ArrayList<EditEntry> addTo) {
900 final EditText label = new EditText(this);
901 label.setKeyListener(TextKeyListener.getInstance(false, Capitalize.WORDS));
902 label.requestFocus();
903 new AlertDialog.Builder(this)
905 .setTitle(R.string.customLabelPickerTitle)
906 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
907 public void onClick(DialogInterface dialog, int which) {
908 entry.setLabel(EditContactActivity.this, ContactMethods.TYPE_CUSTOM,
909 label.getText().toString());
910 mContactChanged = true;
915 entry.view.requestFocus(View.FOCUS_DOWN);
919 .setNegativeButton(android.R.string.cancel, null)
924 * Saves or creates the contact based on the mode, and if sucessful finishes the activity.
926 private void doSaveAction() {
927 // Save or create the contact if needed
938 Log.e(TAG, "Unknown state in doSaveOrCreate: " + mState);
945 * Save the various fields to the existing contact.
947 private void save() {
948 ContentValues values = new ContentValues();
952 // Handle the name and send to voicemail specially
953 final String name = mNameView.getText().toString();
954 if (name != null && TextUtils.isGraphic(name)) {
957 values.put(People.NAME, name);
958 values.put(People.PHONETIC_NAME, mPhoneticNameView.getText().toString());
959 values.put(People.SEND_TO_VOICEMAIL, mSendToVoicemailCheckBox.isChecked() ? 1 : 0);
960 mResolver.update(mUri, values, null, null);
963 // Only write the photo if it's changed, since we don't initially load mPhoto
964 if (mPhoto != null) {
965 ByteArrayOutputStream stream = new ByteArrayOutputStream();
966 mPhoto.compress(Bitmap.CompressFormat.JPEG, 75, stream);
967 Contacts.People.setPhotoData(mResolver, mUri, stream.toByteArray());
969 Contacts.People.setPhotoData(mResolver, mUri, null);
973 int entryCount = ContactEntryAdapter.countEntries(mSections, false);
974 for (int i = 0; i < entryCount; i++) {
975 EditEntry entry = ContactEntryAdapter.getEntry(mSections, i, false);
976 int kind = entry.kind;
977 data = entry.getData();
978 boolean empty = data == null || !TextUtils.isGraphic(data);
979 if (kind == EditEntry.KIND_CONTACT) {
982 values.put(entry.column, data);
983 mResolver.update(entry.uri, values, null, null);
986 values.put(entry.column, (String) null);
987 mResolver.update(entry.uri, values, null, null);
992 entry.toValues(values);
994 mResolver.update(entry.uri, values, null, null);
996 mResolver.insert(entry.uri, values);
999 } else if (entry.id != 0) {
1000 mResolver.delete(entry.uri, null, null);
1005 if (numValues == 0) {
1006 // The contact is completely empty, delete it
1007 mResolver.delete(mUri, null, null);
1009 setResult(RESULT_CANCELED);
1011 // Add the entry to the my contacts group if it isn't there already
1012 People.addToMyContactsGroup(mResolver, ContentUris.parseId(mUri));
1013 setResult(RESULT_OK, new Intent().setData(mUri));
1015 // Only notify user if we actually changed contact
1016 if (mContactChanged || mPhotoChanged) {
1017 Toast.makeText(this, R.string.contactSavedToast, Toast.LENGTH_SHORT).show();
1023 * Takes the entered data and saves it to a new contact.
1025 private void create() {
1026 ContentValues values = new ContentValues();
1030 // Create the contact itself
1031 final String name = mNameView.getText().toString();
1032 if (name != null && TextUtils.isGraphic(name)) {
1035 values.put(People.NAME, name);
1036 values.put(People.PHONETIC_NAME, mPhoneticNameView.getText().toString());
1037 values.put(People.SEND_TO_VOICEMAIL, mSendToVoicemailCheckBox.isChecked() ? 1 : 0);
1039 // Add the contact to the My Contacts group
1040 Uri contactUri = People.createPersonInMyContactsGroup(mResolver, values);
1042 // Add the contact to the group that is being displayed in the contact list
1043 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
1044 int displayType = prefs.getInt(ContactsListActivity.PREF_DISPLAY_TYPE,
1045 ContactsListActivity.DISPLAY_TYPE_UNKNOWN);
1046 if (displayType == ContactsListActivity.DISPLAY_TYPE_USER_GROUP) {
1047 String displayGroup = prefs.getString(ContactsListActivity.PREF_DISPLAY_INFO,
1049 if (!TextUtils.isEmpty(displayGroup)) {
1050 People.addToGroup(mResolver, ContentUris.parseId(contactUri), displayGroup);
1055 if (mPhoto != null) {
1056 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1057 mPhoto.compress(Bitmap.CompressFormat.JPEG, 75, stream);
1058 Contacts.People.setPhotoData(getContentResolver(), contactUri, stream.toByteArray());
1061 // Create the contact methods
1062 int entryCount = ContactEntryAdapter.countEntries(mSections, false);
1063 for (int i = 0; i < entryCount; i++) {
1064 EditEntry entry = ContactEntryAdapter.getEntry(mSections, i, false);
1065 if (entry.kind != EditEntry.KIND_CONTACT) {
1067 if (entry.toValues(values)) {
1068 // Only create the entry if there is data
1069 entry.uri = mResolver.insert(
1070 Uri.withAppendedPath(contactUri, entry.contentDirectory), values);
1071 entry.id = ContentUris.parseId(entry.uri);
1075 // Update the contact with any straggling data, like notes
1076 data = entry.getData();
1078 if (data != null && TextUtils.isGraphic(data)) {
1079 values.put(entry.column, data);
1080 mResolver.update(contactUri, values, null, null);
1086 if (numValues == 0) {
1087 mResolver.delete(contactUri, null, null);
1088 setResult(RESULT_CANCELED);
1091 Intent resultIntent = new Intent()
1093 .putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
1094 setResult(RESULT_OK, resultIntent);
1095 Toast.makeText(this, R.string.contactCreatedToast, Toast.LENGTH_SHORT).show();
1100 * Build up the entries to display on the screen.
1102 * @param extras the extras used to start this activity, may be null
1104 private void buildEntriesForEdit(Bundle extras) {
1105 Cursor personCursor = mResolver.query(mUri, CONTACT_PROJECTION, null, null, null);
1106 if (personCursor == null) {
1107 Log.e(TAG, "invalid contact uri: " + mUri);
1110 } else if (!personCursor.moveToFirst()) {
1111 Log.e(TAG, "invalid contact uri: " + mUri);
1113 personCursor.close();
1117 // Clear out the old entries
1118 int numSections = mSections.size();
1119 for (int i = 0; i < numSections; i++) {
1120 mSections.get(i).clear();
1126 mNameView.setText(personCursor.getString(CONTACT_NAME_COLUMN));
1127 mNameView.addTextChangedListener(this);
1130 mPhoto = People.loadContactPhoto(this, mUri, 0, null);
1131 if (mPhoto == null) {
1132 setPhotoPresent(false);
1134 setPhotoPresent(true);
1135 mPhotoImageView.setImageBitmap(mPhoto);
1138 // Send to voicemail
1139 mSendToVoicemailCheckBox
1140 .setChecked(personCursor.getInt(CONTACT_SEND_TO_VOICEMAIL_COLUMN) == 1);
1141 mSendToVoicemailCheckBox
1142 .setOnCheckedChangeListener(this);
1145 Uri organizationsUri = Uri.withAppendedPath(mUri, Organizations.CONTENT_DIRECTORY);
1146 Cursor organizationsCursor = mResolver.query(organizationsUri, ORGANIZATIONS_PROJECTION,
1149 if (organizationsCursor != null) {
1150 while (organizationsCursor.moveToNext()) {
1151 int type = organizationsCursor.getInt(ORGANIZATIONS_TYPE_COLUMN);
1152 String label = organizationsCursor.getString(ORGANIZATIONS_LABEL_COLUMN);
1153 String company = organizationsCursor.getString(ORGANIZATIONS_COMPANY_COLUMN);
1154 String title = organizationsCursor.getString(ORGANIZATIONS_TITLE_COLUMN);
1155 long id = organizationsCursor.getLong(ORGANIZATIONS_ID_COLUMN);
1156 Uri uri = ContentUris.withAppendedId(Organizations.CONTENT_URI, id);
1158 // Add an organization entry
1159 entry = EditEntry.newOrganizationEntry(this, label, type, company, title, uri, id);
1160 entry.isPrimary = organizationsCursor.getLong(ORGANIZATIONS_ISPRIMARY_COLUMN) != 0;
1161 mOtherEntries.add(entry);
1163 organizationsCursor.close();
1167 if (!personCursor.isNull(CONTACT_NOTES_COLUMN)) {
1168 entry = EditEntry.newNotesEntry(this, personCursor.getString(CONTACT_NOTES_COLUMN),
1170 mOtherEntries.add(entry);
1174 entry = EditEntry.newRingtoneEntry(this,
1175 personCursor.getString(CONTACT_CUSTOM_RINGTONE_COLUMN), mUri);
1176 mOtherEntries.add(entry);
1179 mPhoneticNameView.setText(personCursor.getString(CONTACT_PHONETIC_NAME_COLUMN));
1180 mPhoneticNameView.addTextChangedListener(this);
1182 personCursor.close();
1184 // Build up the phone entries
1185 Uri phonesUri = Uri.withAppendedPath(mUri, People.Phones.CONTENT_DIRECTORY);
1186 Cursor phonesCursor = mResolver.query(phonesUri, PHONES_PROJECTION,
1189 if (phonesCursor != null) {
1190 while (phonesCursor.moveToNext()) {
1191 int type = phonesCursor.getInt(PHONES_TYPE_COLUMN);
1192 String label = phonesCursor.getString(PHONES_LABEL_COLUMN);
1193 String number = phonesCursor.getString(PHONES_NUMBER_COLUMN);
1194 long id = phonesCursor.getLong(PHONES_ID_COLUMN);
1195 boolean isPrimary = phonesCursor.getLong(PHONES_ISPRIMARY_COLUMN) != 0;
1196 Uri uri = ContentUris.withAppendedId(phonesUri, id);
1198 // Add a phone number entry
1199 entry = EditEntry.newPhoneEntry(this, label, type, number, uri, id);
1200 entry.isPrimary = isPrimary;
1201 mPhoneEntries.add(entry);
1203 // Keep track of which primary types have been added
1204 if (type == Phones.TYPE_MOBILE) {
1205 mMobilePhoneAdded = true;
1209 phonesCursor.close();
1212 // Build the contact method entries
1213 Uri methodsUri = Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY);
1214 Cursor methodsCursor = mResolver.query(methodsUri, METHODS_PROJECTION, null, null, null);
1216 if (methodsCursor != null) {
1217 while (methodsCursor.moveToNext()) {
1218 int kind = methodsCursor.getInt(METHODS_KIND_COLUMN);
1219 String label = methodsCursor.getString(METHODS_LABEL_COLUMN);
1220 String data = methodsCursor.getString(METHODS_DATA_COLUMN);
1221 String auxData = methodsCursor.getString(METHODS_AUX_DATA_COLUMN);
1222 int type = methodsCursor.getInt(METHODS_TYPE_COLUMN);
1223 long id = methodsCursor.getLong(METHODS_ID_COLUMN);
1224 boolean isPrimary = methodsCursor.getLong(METHODS_ISPRIMARY_COLUMN) != 0;
1225 Uri uri = ContentUris.withAppendedId(methodsUri, id);
1228 case Contacts.KIND_EMAIL: {
1229 entry = EditEntry.newEmailEntry(this, label, type, data, uri, id);
1230 entry.isPrimary = isPrimary;
1231 mEmailEntries.add(entry);
1234 mPrimaryEmailAdded = true;
1239 case Contacts.KIND_POSTAL: {
1240 entry = EditEntry.newPostalEntry(this, label, type, data, uri, id);
1241 entry.isPrimary = isPrimary;
1242 mPostalEntries.add(entry);
1246 case Contacts.KIND_IM: {
1247 Object protocolObj = ContactMethods.decodeImProtocol(auxData);
1248 if (protocolObj == null) {
1249 // Invalid IM protocol, log it then ignore.
1250 Log.e(TAG, "Couldn't decode IM protocol: " + auxData);
1253 if (protocolObj instanceof Number) {
1254 int protocol = ((Number) protocolObj).intValue();
1255 entry = EditEntry.newImEntry(this,
1256 getLabelsForKind(this, Contacts.KIND_IM)[protocol], protocol,
1259 entry = EditEntry.newImEntry(this, protocolObj.toString(), -1, data,
1262 mImEntries.add(entry);
1269 methodsCursor.close();
1272 // Add values from the extras, if there are any
1273 if (extras != null) {
1274 addFromExtras(extras, phonesUri, methodsUri);
1277 // Add the base types if needed
1278 if (!mMobilePhoneAdded) {
1279 entry = EditEntry.newPhoneEntry(this,
1280 Uri.withAppendedPath(mUri, People.Phones.CONTENT_DIRECTORY),
1281 DEFAULT_PHONE_TYPE);
1282 mPhoneEntries.add(entry);
1285 if (!mPrimaryEmailAdded) {
1286 entry = EditEntry.newEmailEntry(this,
1287 Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY),
1288 DEFAULT_EMAIL_TYPE);
1289 entry.isPrimary = true;
1290 mEmailEntries.add(entry);
1293 mContactChanged = false;
1297 * Build the list of EditEntries for full mode insertions.
1299 * @param extras the extras used to start this activity, may be null
1301 private void buildEntriesForInsert(Bundle extras) {
1302 // Clear out the old entries
1303 int numSections = mSections.size();
1304 for (int i = 0; i < numSections; i++) {
1305 mSections.get(i).clear();
1310 // Check the intent extras
1311 if (extras != null) {
1312 addFromExtras(extras, null, null);
1316 mPhotoImageView.setImageResource(R.drawable.ic_contact_picture);
1318 // Add the base entries if they're not already present
1319 if (!mMobilePhoneAdded) {
1320 entry = EditEntry.newPhoneEntry(this, null, Phones.TYPE_MOBILE);
1321 entry.isPrimary = true;
1322 mPhoneEntries.add(entry);
1325 if (!mPrimaryEmailAdded) {
1326 entry = EditEntry.newEmailEntry(this, null, DEFAULT_EMAIL_TYPE);
1327 entry.isPrimary = true;
1328 mEmailEntries.add(entry);
1332 entry = EditEntry.newRingtoneEntry(this, null, mUri);
1333 mOtherEntries.add(entry);
1336 private void addFromExtras(Bundle extras, Uri phonesUri, Uri methodsUri) {
1339 // Read the name from the bundle
1340 CharSequence name = extras.getCharSequence(Insert.NAME);
1341 if (name != null && TextUtils.isGraphic(name)) {
1342 mNameView.setText(name);
1345 // Read the phonetic name from the bundle
1346 CharSequence phoneticName = extras.getCharSequence(Insert.PHONETIC_NAME);
1347 if (!TextUtils.isEmpty(phoneticName)) {
1348 mPhoneticNameView.setText(phoneticName);
1351 // Postal entries from extras
1352 CharSequence postal = extras.getCharSequence(Insert.POSTAL);
1353 int postalType = extras.getInt(Insert.POSTAL_TYPE, INVALID_TYPE);
1354 if (!TextUtils.isEmpty(postal) && postalType == INVALID_TYPE) {
1355 postalType = DEFAULT_POSTAL_TYPE;
1358 if (postalType != INVALID_TYPE) {
1359 entry = EditEntry.newPostalEntry(this, null, postalType, postal.toString(),
1361 entry.isPrimary = extras.getBoolean(Insert.POSTAL_ISPRIMARY);
1362 mPostalEntries.add(entry);
1365 // Email entries from extras
1366 addEmailFromExtras(extras, methodsUri, Insert.EMAIL, Insert.EMAIL_TYPE,
1367 Insert.EMAIL_ISPRIMARY);
1368 addEmailFromExtras(extras, methodsUri, Insert.SECONDARY_EMAIL, Insert.SECONDARY_EMAIL_TYPE,
1370 addEmailFromExtras(extras, methodsUri, Insert.TERTIARY_EMAIL, Insert.TERTIARY_EMAIL_TYPE,
1373 // Phone entries from extras
1374 addPhoneFromExtras(extras, phonesUri, Insert.PHONE, Insert.PHONE_TYPE,
1375 Insert.PHONE_ISPRIMARY);
1376 addPhoneFromExtras(extras, phonesUri, Insert.SECONDARY_PHONE, Insert.SECONDARY_PHONE_TYPE,
1378 addPhoneFromExtras(extras, phonesUri, Insert.TERTIARY_PHONE, Insert.TERTIARY_PHONE_TYPE,
1381 // IM entries from extras
1382 CharSequence imHandle = extras.getCharSequence(Insert.IM_HANDLE);
1383 CharSequence imProtocol = extras.getCharSequence(Insert.IM_PROTOCOL);
1385 if (imHandle != null && imProtocol != null) {
1386 Object protocolObj = ContactMethods.decodeImProtocol(imProtocol.toString());
1387 if (protocolObj instanceof Number) {
1388 int protocol = ((Number) protocolObj).intValue();
1389 entry = EditEntry.newImEntry(this,
1390 getLabelsForKind(this, Contacts.KIND_IM)[protocol], protocol,
1391 imHandle.toString(), null, 0);
1393 entry = EditEntry.newImEntry(this, protocolObj.toString(), -1, imHandle.toString(),
1396 entry.isPrimary = extras.getBoolean(Insert.IM_ISPRIMARY);
1397 mImEntries.add(entry);
1401 private void addEmailFromExtras(Bundle extras, Uri methodsUri, String emailField,
1402 String typeField, String primaryField) {
1403 CharSequence email = extras.getCharSequence(emailField);
1405 // Correctly handle String in typeField as TYPE_CUSTOM
1406 int emailType = INVALID_TYPE;
1407 String customLabel = null;
1408 if(extras.get(typeField) instanceof String) {
1409 emailType = ContactMethods.TYPE_CUSTOM;
1410 customLabel = extras.getString(typeField);
1412 emailType = extras.getInt(typeField, INVALID_TYPE);
1415 if (!TextUtils.isEmpty(email) && emailType == INVALID_TYPE) {
1416 emailType = DEFAULT_EMAIL_TYPE;
1417 mPrimaryEmailAdded = true;
1420 if (emailType != INVALID_TYPE) {
1421 EditEntry entry = EditEntry.newEmailEntry(this, customLabel, emailType, email.toString(),
1423 entry.isPrimary = (primaryField == null) ? false : extras.getBoolean(primaryField);
1424 mEmailEntries.add(entry);
1426 // Keep track of which primary types have been added
1427 if (entry.isPrimary) {
1428 mPrimaryEmailAdded = true;
1433 private void addPhoneFromExtras(Bundle extras, Uri phonesUri, String phoneField,
1434 String typeField, String primaryField) {
1435 CharSequence phoneNumber = extras.getCharSequence(phoneField);
1437 // Correctly handle String in typeField as TYPE_CUSTOM
1438 int phoneType = INVALID_TYPE;
1439 String customLabel = null;
1440 if(extras.get(typeField) instanceof String) {
1441 phoneType = Phones.TYPE_CUSTOM;
1442 customLabel = extras.getString(typeField);
1444 phoneType = extras.getInt(typeField, INVALID_TYPE);
1447 if (!TextUtils.isEmpty(phoneNumber) && phoneType == INVALID_TYPE) {
1448 phoneType = DEFAULT_PHONE_TYPE;
1451 if (phoneType != INVALID_TYPE) {
1452 EditEntry entry = EditEntry.newPhoneEntry(this, customLabel, phoneType,
1453 phoneNumber.toString(), phonesUri, 0);
1454 entry.isPrimary = (primaryField == null) ? false : extras.getBoolean(primaryField);
1455 mPhoneEntries.add(entry);
1457 // Keep track of which primary types have been added
1458 if (phoneType == Phones.TYPE_MOBILE) {
1459 mMobilePhoneAdded = true;
1465 * Removes all existing views, builds new ones for all the entries, and adds them.
1467 private void buildViews() {
1468 // Remove existing views
1469 final LinearLayout layout = mLayout;
1470 layout.removeAllViews();
1472 buildViewsForSection(layout, mPhoneEntries, R.string.listSeparatorCallNumber);
1473 buildViewsForSection(layout, mEmailEntries, R.string.listSeparatorSendEmail);
1474 buildViewsForSection(layout, mImEntries, R.string.listSeparatorSendIm);
1475 buildViewsForSection(layout, mPostalEntries, R.string.listSeparatorMapAddress);
1476 buildViewsForSection(layout, mOtherEntries, R.string.listSeparatorOtherInformation);
1481 * Builds the views for a specific section.
1483 * @param layout the container
1484 * @param section the section to build the views for
1486 private void buildViewsForSection(final LinearLayout layout, ArrayList<EditEntry> section,
1487 int separatorResource) {
1488 // Build the separator if the section isn't empty
1489 if (section.size() > 0) {
1490 View separator = mInflater.inflate(R.layout.edit_separator, layout, false);
1491 TextView text = (TextView) separator.findViewById(R.id.text);
1492 text.setText(getText(separatorResource));
1493 layout.addView(separator);
1496 // Build views for the current section
1497 for (EditEntry entry : section) {
1498 entry.activity = this; // this could be null from when the state is restored
1499 if (!entry.isDeleted) {
1500 View view = buildViewForEntry(entry);
1501 layout.addView(view);
1507 * Builds a view to display an EditEntry.
1509 * @param entry the entry to display
1510 * @return a view that will display the given entry
1512 /* package */ View buildViewForEntry(final EditEntry entry) {
1513 // Look for any existing entered text, and save it if found
1514 if (entry.view != null && entry.syncDataWithView) {
1515 String enteredText = ((TextView) entry.view.findViewById(R.id.data))
1516 .getText().toString();
1517 if (!TextUtils.isEmpty(enteredText)) {
1518 entry.data = enteredText;
1523 final ViewGroup parent = mLayout;
1526 if (entry.kind == Contacts.KIND_ORGANIZATION) {
1527 view = mInflater.inflate(R.layout.edit_contact_entry_org, parent, false);
1528 } else if (isRingtoneEntry(entry)) {
1529 view = mInflater.inflate(R.layout.edit_contact_entry_ringtone, parent, false);
1530 } else if (!entry.isStaticLabel) {
1531 view = mInflater.inflate(R.layout.edit_contact_entry, parent, false);
1533 view = mInflater.inflate(R.layout.edit_contact_entry_static_label, parent, false);
1537 // Set the entry as the tag so we can find it again later given just the view
1541 entry.bindLabel(this);
1544 TextView data = (TextView) view.findViewById(R.id.data);
1545 TextView data2 = (TextView) view.findViewById(R.id.data2);
1547 if (data instanceof Button) {
1548 data.setOnClickListener(this);
1550 if (data.length() == 0) {
1551 if (entry.syncDataWithView) {
1552 // If there is already data entered don't overwrite it
1553 data.setText(entry.data);
1555 fillViewData(entry);
1558 if (data2 != null && data2.length() == 0) {
1559 // If there is already data entered don't overwrite it
1560 data2.setText(entry.data2);
1562 data.setHint(entry.hint);
1563 if (data2 != null) data2.setHint(entry.hint2);
1564 if (entry.lines > 1) {
1565 data.setLines(entry.lines);
1566 data.setMaxLines(entry.maxLines);
1567 if (data2 != null) {
1568 data2.setLines(entry.lines);
1569 data2.setMaxLines(entry.maxLines);
1572 int contentType = entry.contentType;
1573 if (contentType != EditorInfo.TYPE_NULL) {
1574 data.setInputType(contentType);
1575 if (data2 != null) {
1576 data2.setInputType(contentType);
1578 if ((contentType&EditorInfo.TYPE_MASK_CLASS)
1579 == EditorInfo.TYPE_CLASS_PHONE) {
1580 data.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
1581 if (data2 != null) {
1582 data2.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
1587 // Connect listeners up to watch for changed values.
1588 if (data instanceof EditText) {
1589 data.addTextChangedListener(this);
1591 if (data2 instanceof EditText) {
1592 data2.addTextChangedListener(this);
1595 // Hook up the delete button
1596 View delete = view.findViewById(R.id.delete);
1597 if (delete != null) delete.setOnClickListener(this);
1598 View delete2 = view.findViewById(R.id.delete2);
1599 if (delete2 != null) delete2.setOnClickListener(this);
1604 private void fillViewData(final EditEntry entry) {
1605 if (isRingtoneEntry(entry)) {
1606 updateRingtoneView(entry);
1611 * Handles the results from the label change picker.
1613 private final class LabelPickedListener implements DialogInterface.OnClickListener {
1617 public LabelPickedListener(EditEntry entry, String[] labels) {
1622 public void onClick(DialogInterface dialog, int which) {
1623 // TODO: Use a managed dialog
1624 if (mEntry.kind != Contacts.KIND_IM) {
1625 final int type = getTypeFromLabelPosition(mLabels, which);
1626 if (type == ContactMethods.TYPE_CUSTOM) {
1627 createCustomPicker(mEntry, null);
1629 mEntry.setLabel(EditContactActivity.this, type, mLabels[which]);
1630 mContactChanged = true;
1633 mEntry.setLabel(EditContactActivity.this, which, mLabels[which]);
1634 mContactChanged = true;
1640 * A basic structure with the data for a contact entry in the list.
1642 private static final class EditEntry extends ContactEntryAdapter.Entry implements Parcelable {
1643 // These aren't stuffed into the parcel
1644 public EditContactActivity activity;
1647 // These are stuffed into the parcel
1649 public String hint2;
1650 public String column;
1651 public String contentDirectory;
1652 public String data2;
1653 public int contentType;
1656 * If 0 or 1, setSingleLine will be called. If negative, setSingleLine
1657 * will not be called.
1659 public int lines = 1;
1660 public boolean isPrimary;
1661 public boolean isDeleted = false;
1662 public boolean isStaticLabel = false;
1663 public boolean syncDataWithView = true;
1665 private EditEntry() {
1666 // only used by CREATOR
1669 public EditEntry(EditContactActivity activity) {
1670 this.activity = activity;
1673 public EditEntry(EditContactActivity activity, String label,
1674 int type, String data, Uri uri, long id) {
1675 this.activity = activity;
1676 this.isPrimary = false;
1684 public int describeContents() {
1688 public void writeToParcel(Parcel parcel, int flags) {
1689 // Make sure to read data from the input field, if anything is entered
1692 // Write in our own fields.
1693 parcel.writeString(hint);
1694 parcel.writeString(hint2);
1695 parcel.writeString(column);
1696 parcel.writeString(contentDirectory);
1697 parcel.writeString(data2);
1698 parcel.writeInt(contentType);
1699 parcel.writeInt(type);
1700 parcel.writeInt(lines);
1701 parcel.writeInt(isPrimary ? 1 : 0);
1702 parcel.writeInt(isDeleted ? 1 : 0);
1703 parcel.writeInt(isStaticLabel ? 1 : 0);
1704 parcel.writeInt(syncDataWithView ? 1 : 0);
1706 // Write in the fields from Entry
1707 super.writeToParcel(parcel);
1710 public static final Parcelable.Creator<EditEntry> CREATOR =
1711 new Parcelable.Creator<EditEntry>() {
1712 public EditEntry createFromParcel(Parcel in) {
1713 EditEntry entry = new EditEntry();
1715 // Read out our own fields
1716 entry.hint = in.readString();
1717 entry.hint2 = in.readString();
1718 entry.column = in.readString();
1719 entry.contentDirectory = in.readString();
1720 entry.data2 = in.readString();
1721 entry.contentType = in.readInt();
1722 entry.type = in.readInt();
1723 entry.lines = in.readInt();
1724 entry.isPrimary = in.readInt() == 1;
1725 entry.isDeleted = in.readInt() == 1;
1726 entry.isStaticLabel = in.readInt() == 1;
1727 entry.syncDataWithView = in.readInt() == 1;
1729 // Read out the fields from Entry
1730 entry.readFromParcel(in);
1735 public EditEntry[] newArray(int size) {
1736 return new EditEntry[size];
1740 public void setLabel(Context context, int typeIn, String labelIn) {
1748 public void bindLabel(Context context) {
1749 TextView v = (TextView) view.findViewById(R.id.label);
1750 if (isStaticLabel) {
1756 case Contacts.KIND_PHONE: {
1757 v.setText(Phones.getDisplayLabel(context, type, label));
1761 case Contacts.KIND_IM: {
1762 v.setText(getLabelsForKind(activity, kind)[type]);
1766 case Contacts.KIND_ORGANIZATION: {
1767 v.setText(Organizations.getDisplayLabel(activity, type, label));
1772 v.setText(Contacts.ContactMethods.getDisplayLabel(context, kind, type, label));
1773 if (kind == Contacts.KIND_POSTAL) {
1779 v.setOnClickListener(activity);
1783 * Returns the data for the entry
1784 * @return the data for the entry
1786 public String getData() {
1787 if (view != null && syncDataWithView) {
1788 CharSequence text = ((TextView) view.findViewById(R.id.data)).getText();
1790 return text.toString();
1795 return data.toString();
1802 * Dumps the entry into a HashMap suitable for passing to the database.
1804 * @param values the HashMap to fill in.
1805 * @return true if the value should be saved, false otherwise
1807 public boolean toValues(ContentValues values) {
1808 boolean success = false;
1809 String labelString = null;
1810 // Save the type and label
1812 // Read the possibly updated label from the text field
1813 labelString = ((TextView) view.findViewById(R.id.label)).getText().toString();
1816 case Contacts.KIND_PHONE:
1817 if (type != Phones.TYPE_CUSTOM) {
1820 values.put(Phones.LABEL, labelString);
1821 values.put(Phones.TYPE, type);
1824 case Contacts.KIND_EMAIL:
1825 if (type != ContactMethods.TYPE_CUSTOM) {
1828 values.put(ContactMethods.LABEL, labelString);
1829 values.put(ContactMethods.KIND, kind);
1830 values.put(ContactMethods.TYPE, type);
1833 case Contacts.KIND_IM:
1834 values.put(ContactMethods.KIND, kind);
1835 values.put(ContactMethods.TYPE, ContactMethods.TYPE_OTHER);
1836 values.putNull(ContactMethods.LABEL);
1838 values.put(ContactMethods.AUX_DATA,
1839 ContactMethods.encodePredefinedImProtocol(type));
1841 values.put(ContactMethods.AUX_DATA,
1842 ContactMethods.encodeCustomImProtocol(label.toString()));
1846 case Contacts.KIND_POSTAL:
1847 if (type != ContactMethods.TYPE_CUSTOM) {
1850 values.put(ContactMethods.LABEL, labelString);
1851 values.put(ContactMethods.KIND, kind);
1852 values.put(ContactMethods.TYPE, type);
1855 case Contacts.KIND_ORGANIZATION:
1856 if (type != ContactMethods.TYPE_CUSTOM) {
1859 values.put(ContactMethods.LABEL, labelString);
1860 values.put(ContactMethods.TYPE, type);
1863 // Read the possibly updated data from the text field
1864 data2 = ((TextView) view.findViewById(R.id.data2)).getText().toString();
1866 if (!TextUtils.isGraphic(data2)) {
1867 values.putNull(Organizations.TITLE);
1869 values.put(Organizations.TITLE, data2.toString());
1875 Log.w(TAG, "unknown kind " + kind);
1876 values.put(ContactMethods.LABEL, labelString);
1877 values.put(ContactMethods.KIND, kind);
1878 values.put(ContactMethods.TYPE, type);
1882 // Only set the ISPRIMARY flag if part of the incoming data. This is because the
1883 // ContentProvider will try finding a new primary when setting to false, meaning
1884 // it's possible to lose primary altogether as we walk down the list. If this editor
1885 // implements editing of primaries in the future, this will need to be revisited.
1887 values.put(ContactMethods.ISPRIMARY, 1);
1891 if (view != null && syncDataWithView) {
1892 // Read the possibly updated data from the text field
1893 data = ((TextView) view.findViewById(R.id.data)).getText().toString();
1895 if (!TextUtils.isGraphic(data)) {
1896 values.putNull(column);
1899 values.put(column, data.toString());
1905 * Create a new empty organization entry
1907 public static final EditEntry newOrganizationEntry(EditContactActivity activity,
1908 Uri uri, int type) {
1909 return newOrganizationEntry(activity, null, type, null, null, uri, 0);
1913 * Create a new company entry with the given data.
1915 public static final EditEntry newOrganizationEntry(EditContactActivity activity,
1916 String label, int type, String company, String title, Uri uri, long id) {
1917 EditEntry entry = new EditEntry(activity, label, type, company, uri, id);
1918 entry.hint = activity.getString(R.string.ghostData_company);
1919 entry.hint2 = activity.getString(R.string.ghostData_title);
1920 entry.data2 = title;
1921 entry.column = Organizations.COMPANY;
1922 entry.contentDirectory = Organizations.CONTENT_DIRECTORY;
1923 entry.kind = Contacts.KIND_ORGANIZATION;
1924 entry.contentType = EditorInfo.TYPE_CLASS_TEXT
1925 | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS;
1930 * Create a new notes entry with the given data.
1932 public static final EditEntry newNotesEntry(EditContactActivity activity,
1933 String data, Uri uri) {
1934 EditEntry entry = new EditEntry(activity);
1935 entry.label = activity.getString(R.string.label_notes);
1936 entry.hint = activity.getString(R.string.ghostData_notes);
1939 entry.column = People.NOTES;
1940 entry.maxLines = 10;
1943 entry.kind = KIND_CONTACT;
1944 entry.contentType = EditorInfo.TYPE_CLASS_TEXT
1945 | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES
1946 | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
1947 entry.isStaticLabel = true;
1952 * Create a new ringtone entry with the given data.
1954 public static final EditEntry newRingtoneEntry(EditContactActivity activity,
1955 String data, Uri uri) {
1956 EditEntry entry = new EditEntry(activity);
1957 entry.label = activity.getString(R.string.label_ringtone);
1960 entry.column = People.CUSTOM_RINGTONE;
1961 entry.kind = KIND_CONTACT;
1962 entry.isStaticLabel = true;
1963 entry.syncDataWithView = false;
1969 * Create a new empty email entry
1971 public static final EditEntry newPhoneEntry(EditContactActivity activity,
1972 Uri uri, int type) {
1973 return newPhoneEntry(activity, null, type, null, uri, 0);
1977 * Create a new phone entry with the given data.
1979 public static final EditEntry newPhoneEntry(EditContactActivity activity,
1980 String label, int type, String data, Uri uri,
1982 EditEntry entry = new EditEntry(activity, label, type, data, uri, id);
1983 entry.hint = activity.getString(R.string.ghostData_phone);
1984 entry.column = People.Phones.NUMBER;
1985 entry.contentDirectory = People.Phones.CONTENT_DIRECTORY;
1986 entry.kind = Contacts.KIND_PHONE;
1987 entry.contentType = EditorInfo.TYPE_CLASS_PHONE;
1992 * Create a new empty email entry
1994 public static final EditEntry newEmailEntry(EditContactActivity activity,
1995 Uri uri, int type) {
1996 return newEmailEntry(activity, null, type, null, uri, 0);
2000 * Create a new email entry with the given data.
2002 public static final EditEntry newEmailEntry(EditContactActivity activity,
2003 String label, int type, String data, Uri uri,
2005 EditEntry entry = new EditEntry(activity, label, type, data, uri, id);
2006 entry.hint = activity.getString(R.string.ghostData_email);
2007 entry.column = ContactMethods.DATA;
2008 entry.contentDirectory = People.ContactMethods.CONTENT_DIRECTORY;
2009 entry.kind = Contacts.KIND_EMAIL;
2010 entry.contentType = EditorInfo.TYPE_CLASS_TEXT
2011 | EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
2016 * Create a new empty postal address entry
2018 public static final EditEntry newPostalEntry(EditContactActivity activity,
2019 Uri uri, int type) {
2020 return newPostalEntry(activity, null, type, null, uri, 0);
2024 * Create a new postal address entry with the given data.
2026 * @param label label for the item, from the db not the display label
2027 * @param type the type of postal address
2028 * @param data the starting data for the entry, may be null
2029 * @param uri the uri for the entry if it already exists, may be null
2030 * @param id the id for the entry if it already exists, 0 it it doesn't
2031 * @return the new EditEntry
2033 public static final EditEntry newPostalEntry(EditContactActivity activity,
2034 String label, int type, String data, Uri uri, long id) {
2035 EditEntry entry = new EditEntry(activity, label, type, data, uri, id);
2036 entry.hint = activity.getString(R.string.ghostData_postal);
2037 entry.column = ContactMethods.DATA;
2038 entry.contentDirectory = People.ContactMethods.CONTENT_DIRECTORY;
2039 entry.kind = Contacts.KIND_POSTAL;
2040 entry.contentType = EditorInfo.TYPE_CLASS_TEXT
2041 | EditorInfo.TYPE_TEXT_VARIATION_POSTAL_ADDRESS
2042 | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS
2043 | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
2050 * Create a new postal address entry with the given data.
2052 * @param label label for the item, from the db not the display label
2053 * @param protocol the type used
2054 * @param data the starting data for the entry, may be null
2055 * @param uri the uri for the entry if it already exists, may be null
2056 * @param id the id for the entry if it already exists, 0 it it doesn't
2057 * @return the new EditEntry
2059 public static final EditEntry newImEntry(EditContactActivity activity,
2060 String label, int protocol, String data, Uri uri, long id) {
2061 EditEntry entry = new EditEntry(activity, label, protocol, data, uri, id);
2062 entry.hint = activity.getString(R.string.ghostData_im);
2063 entry.column = ContactMethods.DATA;
2064 entry.contentDirectory = People.ContactMethods.CONTENT_DIRECTORY;
2065 entry.kind = Contacts.KIND_IM;
2066 entry.contentType = EditorInfo.TYPE_CLASS_TEXT;
2071 public void afterTextChanged(Editable s) {
2072 // Someone edited a text field, so assume this contact is dirty.
2073 mContactChanged = true;
2076 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
2077 // Do nothing; editing handled by afterTextChanged()
2080 public void onTextChanged(CharSequence s, int start, int before, int count) {
2081 // Do nothing; editing handled by afterTextChanged()
2084 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2085 // Someone changed a checkbox, so assume this contact is dirty.
2086 mContactChanged = true;