Preventing a crash when joining an empty contact

Bug: 3205806
Change-Id: Ibed4f9ac48a96bb0d0b44eb85b77c70bd16c286d
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 31492f0..ddc1537 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1447,4 +1447,10 @@
       (Contacts themselves will not be deleted.)
     </string>
 
+    <!-- Toast displayed when the user creates a new contact and attempts to join it
+      with another before entering any data  [CHAR LIMIT=256] -->
+    <string name="toast_join_with_empty_contact">Please enter contact name before joining
+      with another contact.
+    </string>
+
 </resources>
diff --git a/src/com/android/contacts/views/editor/ContactEditorFragment.java b/src/com/android/contacts/views/editor/ContactEditorFragment.java
index ec5ef60..4bdd22c 100644
--- a/src/com/android/contacts/views/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/views/editor/ContactEditorFragment.java
@@ -604,6 +604,22 @@
     }
 
     private boolean doJoinContactAction() {
+        if (!hasValidState()) {
+            return false;
+        }
+
+        // If we just started creating a new contact and haven't added any data, it's too
+        // early to do a join
+        if (mState.size() == 1 && mState.get(0).isContactInsert()) {
+            final AccountTypes sources = AccountTypes.getInstance(mContext);
+            EntityModifier.trimEmpty(mState, sources);
+            if (mState.buildDiff().isEmpty()) {
+                Toast.makeText(getActivity(), R.string.toast_join_with_empty_contact,
+                                Toast.LENGTH_LONG).show();
+                return true;
+            }
+        }
+
         return doSaveAction(SaveMode.JOIN);
     }