Android Phonebook contact UI loading and contact storage process (6)

Source: Internet
Author: User

At 11:18:08 on February 7, Baidu moved the items in the space.

1. Save contact

We have already written four articles, made a lot of preparations, and finally reached this step. The time to witness the miracle has finally arrived.

After the user adds all the information to be added, click "Done" to save the newly created Contact. Then, the user clicks "Done" Button to start analysis.

As mentioned above, the "Done" Processing Event is set in contacteditgateti.pdf as follows:

 View saveMenuItem = saveMenuItem.setOnClickListener(          });

Let's go to ContactEditorFragment and check his call logic. doSaveAction () --> save (SaveMode. CLOSE). Focus on the save () method:

   save(           Intent intent =       }

We can see that the mState of Qianli tracking is treated as a parameter. Like other objects, we enter ContactSaveService. This ContactSaveService inherits from IntentService and calls a callback method after entering ContactSaveService, as shown below:

        String action =               CallerInfoCacheUtils.sendUpdateCallerInfoCacheIntent(     }

To view the saveContact (intent), this method is the ultimate method for saving the contact:

       RawContactDeltaList state =       isProfile = intent.getBooleanExtra(EXTRA_SAVE_IS_PROFILE,      Bundle updatedPhotos =        AccountTypeManager accountTypes = AccountTypeManager.getInstance(       String sourceId =       isNewAdnContact =      RawContactDelta entityDelta = state.get(0      AccountType type =          Uri lookupUri =         ContentResolver resolver =      succeeded =         insertedRawContactId = -1        tries = 0      (tries++ <          ArrayList<ContentProviderOperation> diff =           ContentProviderResult[] results =           (!             results =            rawContactId =          (rawContactId == -1          insertedRawContactId =          Uri rawContactUri =         lookupUri =         succeeded =             (isAdnContact && isNewAdnContact && lookupUri !=               contactId =              newRawContactId =                 }

The above method is not a complete method. I just truncated the key code and looked at the while loop. It indicates that the contact will be saved three times, instead of once. State = intent. getParcelableExtra (EXTRA_CONTACT_STATE); at the same time, the account information is also retrieved from the intent, and the most critical code is in the while loop. First, Check final ArrayList <ContentProviderOperation> diff = state. buildDiff (); we know that when saving a contact, it will typically encapsulate all the information in ContentProviderOperation (you can refer to the http://www.cnblogs.com/wlrhnh/p/3477216.html and http://www.cnblogs.com/wlrhnh/p/3477252.html), and then execute resolver. applyBatch, the problem now is how to encapsulate ContentProviderOperation. We know that the state is RawContactDeltaList, and according to the previous analysis, it adds a RawContactDelta object. Next we will enter the state of RawContactDeltaList. buildDiff () method:

  ArrayList<ContentProviderOperation>      ArrayList<ContentProviderOperation> diff =        (RawContactDelta delta :            firstBatch =           isInsert =         backRefs[rawContactIndex++] = isInsert ? firstBatch : -1               (mIsUnlinkingRawContact)             (rawContactId != -1              Builder builder =              }   (firstInsertRow == -1             firstInsertRow =           }               Builder builder =           }

First, the RawContactDelta object is retrieved in the for loop. Generally, there is only one object, and then delta is called. buildDiff (diff), it seems that you have to enter the buildDiff () method of RawContactDelta. the diff passed in here is an ArrayList <ContentProviderOperation> object:

  (ArrayList<ValuesDelta>                                 builder =           }                builder =   }

Looking at the for loop of the outer layer, mEntries is a HashMap <String, ArrayList <ValuesDelta> object. Remember the saveValue (String column, String value) method analyzed in the previous article, as shown below:

       Log.d("David", "column = " +     Log.d("David", "value = " +      Log.d("David", "mState = " + }

Here, mEntry is a ValuesDelta object, and there are ValuesDelta entry: mState. getMimeEntries (mKind. mimeType) in KindSectionView. java.

Analyze the inner loop, retrieve each ValuesDelta object, call child. buildDiff (Data. CONTENT_URI), and enter ValuesDelta:

      Builder builder =                builder =        }

Now, let's look at these cycles. I have logged them under each loop:

 RawContactDeltaList:  (RawContactDelta delta :         Log.d("D1", "delta = " +   RawContactDelta:  (ArrayList<ValuesDelta>        Log.d("D1", "==================================="        Log.d("D1", "mimeEntries = " +   RawContactDelta:         Log.d("D1", "child = " +           Log.d("D1", "mAfter = " + mAfter);

When I enter Name = Lucky, PhoneNumber = 18611112222, and click Save contact, the log result is as follows:

There is only one RawContactDelta that contains information entered by all users. It can be seen that RawContactDelta is an object that contains information of all contacts;

Each RawContactDelta contains several ArrayList <ValuesDelta>. In fact, each ArrayList has only one ValuesDelta object. Then, a ValuesDelta contains information about an Item, such as Name, Phone, and Email. Then, each ValuesDelta performs the buildDiff () operation, builder. withValues (mAfter), and mAfter is a ContentValues object, as shown in mAfter.

Okay, now return to the initial position. The ContactSaveService. java and saveContact () methods have

  ArrayList<ContentProviderOperation> diff =   ContentProviderResult[] results =   (!     results = }

Resolver. applyBatch operation. Let's see what the diff is returned?

      Log.d("D2", "ContentProviderOperation = " +  results = resolver.applyBatch(ContactsContract.AUTHORITY, diff);

The log is as follows:

You can see that each ContentProviderOperation has corresponding values, and these values are stored in the database by the resolver. applyBatch () method.

Now, the new contact UI and save the contact analysis are complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.