Merge change 26264 into eclair

* changes:
  Assoicates make primary logic with the remember this choice checkbox in fasttrack window.
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 5746232..1af6162 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -39,6 +39,7 @@
     <item type="id" name="dialog_select_multiple_vcard" />
     <item type="id" name="dialog_reading_vcard" />
     <item type="id" name="dialog_io_exception" />
+    <item type="id" name="dialog_error_with_message" />
 
     <!-- For ExportVCard -->
     <item type="id" name="dialog_confirm_export_vcard" />
diff --git a/src/com/android/contacts/ImportVCardActivity.java b/src/com/android/contacts/ImportVCardActivity.java
index 027c70e..e77a3e8 100644
--- a/src/com/android/contacts/ImportVCardActivity.java
+++ b/src/com/android/contacts/ImportVCardActivity.java
@@ -104,6 +104,25 @@
     private VCardReadThread mVCardReadThread;
     private ProgressDialog mProgressDialogForReadVCard;
 
+    private String mErrorMessage;
+
+    private class DialogDisplayer implements Runnable {
+        private final int mResId;
+        public DialogDisplayer(int resId) {
+            mResId = resId;
+        }
+        public DialogDisplayer(String errorMessage) {
+            mResId = R.id.dialog_error_with_message;
+            mErrorMessage = errorMessage;
+        }
+        public void run() {
+            // Show the Dialog only when the parent Activity is still alive.
+            if (!ImportVCardActivity.this.isFinishing()) {
+                showDialog(mResId);
+            }
+        }
+    }
+
     private class CancelListener
         implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
         public void onClick(DialogInterface dialog, int which) {
@@ -117,27 +136,6 @@
 
     private CancelListener mCancelListener = new CancelListener();
 
-    private class ErrorDisplayer implements Runnable {
-        private String mErrorMessage;
-
-        public ErrorDisplayer(String errorMessage) {
-            mErrorMessage = errorMessage;
-        }
-
-        public void run() {
-            String message =
-                getString(R.string.reading_vcard_failed_message, mErrorMessage);
-            AlertDialog.Builder builder =
-                new AlertDialog.Builder(ImportVCardActivity.this)
-                    .setTitle(getString(R.string.reading_vcard_failed_title))
-                    .setIcon(android.R.drawable.ic_dialog_alert)
-                    .setMessage(message)
-                    .setOnCancelListener(mCancelListener)
-                    .setPositiveButton(android.R.string.ok, mCancelListener);
-            builder.show();
-        }
-    }
-
     private class VCardReadThread extends Thread
             implements DialogInterface.OnCancelListener {
         private ContentResolver mResolver;
@@ -263,7 +261,7 @@
             } finally {
                 mWakeLock.release();
                 mProgressDialogForReadVCard.dismiss();
-                // finish() is called via ErrorDisplayer() on failure.
+                // finish() is called via mCancelListener, which is used in DialogDisplayer.
                 if (shouldCallFinish && !isFinishing()) {
                     if (mErrorFileNameList == null || mErrorFileNameList.isEmpty()) {
                         finish();
@@ -279,7 +277,7 @@
                             builder.append(fileName);
                         }
                         
-                        mHandler.post(new ErrorDisplayer(
+                        mHandler.post(new DialogDisplayer(
                                 getString(R.string.fail_reason_failed_to_read_files,
                                         builder.toString())));
                     }
@@ -359,7 +357,7 @@
                 if (errorFileNameList != null) {
                     errorFileNameList.add(canonicalPath);
                 } else {
-                    mHandler.post(new ErrorDisplayer(
+                    mHandler.post(new DialogDisplayer(
                             getString(R.string.fail_reason_io_error,
                                     e.getMessage())));
                 }
@@ -371,7 +369,7 @@
                 if (errorFileNameList != null) {
                     errorFileNameList.add(canonicalPath);
                 } else {
-                    mHandler.post(new ErrorDisplayer(
+                    mHandler.post(new DialogDisplayer(
                             getString(R.string.fail_reason_vcard_not_supported_error) +
                             " (" + e.getMessage() + ")"));
                 }
@@ -380,7 +378,7 @@
                 if (errorFileNameList != null) {
                     errorFileNameList.add(canonicalPath);
                 } else {
-                    mHandler.post(new ErrorDisplayer(
+                    mHandler.post(new DialogDisplayer(
                             getString(R.string.fail_reason_vcard_parse_error) +
                             " (" + e.getMessage() + ")"));
                 }
@@ -602,17 +600,9 @@
         } else if (size == 1) {
             importOneVCardFromSDCard(mAllVCardFileList.get(0).getCanonicalPath());
         } else if (getResources().getBoolean(R.bool.config_allow_users_select_all_vcard_import)) {
-            mHandler.post(new Runnable() {
-                public void run() {
-                    showDialog(R.id.dialog_select_import_type);
-                }
-            });
+            mHandler.post(new DialogDisplayer(R.id.dialog_select_import_type));
         } else {
-            mHandler.post(new Runnable() {
-                public void run() {
-                    showDialog(R.id.dialog_select_one_vcard);
-                }
-            });
+            mHandler.post(new DialogDisplayer(R.id.dialog_select_one_vcard));
         }
     }
     
@@ -779,6 +769,20 @@
                     .setPositiveButton(android.R.string.ok, mCancelListener);
                 return builder.create();
             }
+            case R.id.dialog_error_with_message: {
+                String message = mErrorMessage;
+                if (TextUtils.isEmpty(message)) {
+                    Log.e(LOG_TAG, "Error message is null while it must not.");
+                    message = getString(R.string.fail_reason_unknown);
+                }
+                AlertDialog.Builder builder = new AlertDialog.Builder(this)
+                    .setTitle(getString(R.string.reading_vcard_failed_title))
+                    .setIcon(android.R.drawable.ic_dialog_alert)
+                    .setMessage(message)
+                    .setOnCancelListener(mCancelListener)
+                    .setPositiveButton(android.R.string.ok, mCancelListener);
+                return builder.create();
+            }
         }
 
         return super.onCreateDialog(resId);
diff --git a/src/com/android/contacts/model/ExchangeSource.java b/src/com/android/contacts/model/ExchangeSource.java
index c017037..5c2d024 100644
--- a/src/com/android/contacts/model/ExchangeSource.java
+++ b/src/com/android/contacts/model/ExchangeSource.java
@@ -212,6 +212,8 @@
         final DataKind kind = super.inflateOrganization(ContactsSource.LEVEL_MIMETYPES);
 
         if (inflateLevel >= ContactsSource.LEVEL_CONSTRAINTS) {
+            kind.typeOverallMax = 1;
+
             kind.typeColumn = Organization.TYPE;
             kind.typeList = Lists.newArrayList();
             kind.typeList.add(buildOrgType(Organization.TYPE_WORK).setSpecificMax(1));
diff --git a/src/com/android/contacts/model/FallbackSource.java b/src/com/android/contacts/model/FallbackSource.java
index eee6f71..fa545be 100644
--- a/src/com/android/contacts/model/FallbackSource.java
+++ b/src/com/android/contacts/model/FallbackSource.java
@@ -140,6 +140,9 @@
         }
 
         if (inflateLevel >= ContactsSource.LEVEL_CONSTRAINTS) {
+            kind.defaultValues = new ContentValues();
+            kind.defaultValues.put(Nickname.TYPE, Nickname.TYPE_DEFAULT);
+
             kind.fieldList = Lists.newArrayList();
             kind.fieldList.add(new EditField(Nickname.NAME, R.string.nicknameLabelsGroup,
                     FLAGS_PERSON_NAME));
@@ -345,7 +348,6 @@
         }
 
         if (inflateLevel >= ContactsSource.LEVEL_CONSTRAINTS) {
-
             kind.fieldList = Lists.newArrayList();
             kind.fieldList.add(new EditField(Note.NOTE, R.string.label_notes, FLAGS_NOTE));
         }
@@ -364,6 +366,9 @@
         }
 
         if (inflateLevel >= ContactsSource.LEVEL_CONSTRAINTS) {
+            kind.defaultValues = new ContentValues();
+            kind.defaultValues.put(Website.TYPE, Website.TYPE_OTHER);
+
             kind.fieldList = Lists.newArrayList();
             kind.fieldList.add(new EditField(Website.URL, R.string.websiteLabelsGroup, FLAGS_WEBSITE));
         }
diff --git a/src/com/android/contacts/ui/EditContactActivity.java b/src/com/android/contacts/ui/EditContactActivity.java
index c59c1d1..c3a3935 100644
--- a/src/com/android/contacts/ui/EditContactActivity.java
+++ b/src/com/android/contacts/ui/EditContactActivity.java
@@ -279,8 +279,7 @@
      */
     protected EntityDelta getSelectedEntityDelta() {
         final Long rawContactId = getSelectedRawContactId();
-        final int stateIndex = mState.indexOfRawContactId(rawContactId);
-        return mState.get(stateIndex);
+        return mState.getByRawContactId(rawContactId);
     }
 
     /**
@@ -391,9 +390,10 @@
 
         // Find entity and source for selected tab
         final EntityDelta entity = this.getSelectedEntityDelta();
-        final String accountType = entity.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
+        if (entity == null) return;
 
         final Sources sources = Sources.getInstance(this);
+        final String accountType = entity.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
         final ContactsSource source = sources.getInflatedSource(accountType,
                 ContactsSource.LEVEL_CONSTRAINTS);
 
@@ -885,6 +885,7 @@
             public void onClick(DialogInterface dialog, int which) {
                 // Mark the currently selected contact for deletion
                 final EntityDelta delta = getSelectedEntityDelta();
+                if (delta == null) return;
                 delta.markDeleted();
 
                 bindTabs();