How does Android contact linking work? I'd like to understand how contact linking in Android works - or to be specific I'd like to understand it pretty deeply, almost from an algorithmic point of view.

What I've observed on my phone is that some contacts were linked automatically, some were not (even though when you go to manual linking screen Android will suggest pretty good matches) and I'm confused about this.

So my specific questions are:


What is the trigger for automatic contact linking? When does it occur?
Say that the answer to 1) is "when new contact is added, either manually or via account sync". So when this new contact is added what will happen? Will Android go through all existing contacts and try to find out if the new contact is just a linked contact?
When will Android recognize the new contact as a linked contact? When names match? Phone numbers? Emails? Some combination of those?


Any insight would help clearing this up for me, thanks.
See the section on Automatic Aggregation of Contact Data in the Using the Contacts API from the technical resources on the Android Developers site.

As the article explains:


  When a raw contact is added or modified, the system looks for matching (overlapping) raw contacts with which to aggregate it.  It may not find any matching raw contacts, in which case it will create an aggregate contact that contains just the original raw contact.  If it finds a single match, it creates a new contact that contains the two raw contacts.  And it may even find multiple similar raw contacts, in which case it chooses the closest match.
  
  Two raw contacts are considered to be a match if at least one of these conditions is met:
  
  
  They have matching names.
  Their names consist of the same words but in different order (for example, "Bob Parr" and "Parr, Bob")
  One of them has a common short name for the other (for example, "Bob Parr" and "Robert Parr")
  One of them has just a first or last name and it matches the other raw contact. This rule is less reliable, so it only applies if the two raw contacts are also sharing some other data like a phone number, an email address or a nickname (for example, Helen ["elastigirl"] = Helen Parr ["elastigirl"])
  At least one of the two raw contacts is missing the name altogether and they are sharing a phone number, an email address or a nickname (for example, Bob Parr [incredible@android.com] = incredible@android.com).
  
  
  When comparing names, the system ignores upper/lower case differences (Bob=BOB=bob) and diacritical marks (Hélène=Helene). When comparing two phone numbers the system ignores special characters such as "*", "#", "(", ")", and whitespace. Also if the only difference between two numbers is that one has a country code and the other does not, then the system considers those to be a match (except for numbers in the Japan country code)
  
  Automatic aggregation is not permanent; any change of a constituent raw contact may create a new aggregate or break up an existing one.


The article then goes on to explain the various modes that an application can assign to the raw contacts that it creates.  Depending on the mode, the system will either:


Automatically aggregate the contact as similarities are found with other contacts.  Where similarities no longer exist, these aggregations will be removed.
Never aggregate the contact with any other.
Perform no further aggregation on a contact, even if that contact changes such that it would be taken out of an aggregate group.


Aside from this automatic aggregation, applications are also allowed to manually set up aggregate contacts.  In these cases, the algorithm depends on the application.

Finally, you may manually join contacts in the People app by editing one of a pair of contacts, and selecting Join from the overflow menu.