SoFunction
Updated on 2025-03-11

Android Mms: Application Analysis of Contact Management

Contact contact is very important to Mms, because each conversation recipient is a contact person. When creating new information, you can enter any information of the contact person, such as a number or name, and Mms can send the information to the corresponding person. The class Contact in Mms is used to represent a contact, which contains contact information, such as name, number, contact ID, whether it exists in the contact database, etc. At the same time, Contact also provides some methods to obtain the Contact object, () method to obtain the Contact object. Contact keeps the data synchronized with the contact database, and has an interface updateContact() to notify the contact information that has changed.

There is also a cache inside Contact to save the recently used contacts.Because when using the () method to get Contact, it usually only passes to one number and expects to get Contact.

The Cache inside Contact is created and managed by the ContactCache class.It also has a TaskStack inside it for managing some Runnables. Because each Contact loading from the database is a separate thread, this TaskStack is specifically used to manage Runnable and manage it in the Stack mode, that is, to run Runnable tasks in the FILO order.

There are many overloaded get() methods in ContactCache to get Contact object, all of which are a Boolean parameter canBlock. Is this meant to load Contact in the way of blocking the caller or asynchronously? Another parameter is the contact number.The get() method will first call the internalGet() method, and the internalGet() will first try to get the Contact from the internal CachemContactHash. If it does not exist, create a new Contact with the incoming number. In short, it will definitely return a contact. After that, the updateContact() action will be performed. updateContact() is placed in a Runnable thread. If the caller is blockable, run the Runnable immediately to updateContact. If it is an asynchronous method, put the Runnable in TaskStack and run it later. UpdateContact will call getContactInfo to get the contact information, getContactInfo will call getContactInfoForSelf(), getContactInfoForPhoneNumber, getContactInfoForEmailAddress() to get the specific contact information. If this number is the contact person on the mobile phone, you will get the relevant information of the machine; if the number is an email address or a short number, or a character number, then use the number as an email address to query, that is, use it as the contact's Email field to query; in other cases, that is, the number is a phone number, match it with the contact's phone number field to query. After the query is completed, updateContact will call interface() to tell the listener that the contact has been updated. Because the contact acquisition process is asynchronous, the interface is called to notify after the update is completed.

The internal Cache data structure is a HashMap<String,ArrayList<Contact>>, and the Key is generated from the data inside Contact. InvalidateCache, the data inside mContactsHash will not be removed, but it is marked as Stale. When the next time you get this Contact, updateContact() will be called to update the Contact.