Email5.0 code structure and email5.0 code structure

Source: Internet
Author: User
Tags imap

Email5.0 code structure and email5.0 code structure

Email5.0 code structure

1 core interface IEmailService

IEmailService is the core interface of Email. Defines the basic functions of Email. Such as sendmail, loadAttachment, sync, and searchMessages. EmailServiceStub implements the IEmailService interface. It mainly implements some common methods for POP3 and IMAP mailboxes. EmailServiceStub is an abstract class and has two subclasses. These two sub-classes are POP3 mail and IMAP mail respectively to implement this interface. These two subclasses rewrite some methods of the parent class for POP3 and IMAP protocols. If the POP3 protocol does not support downloading attachments separately, the loadAttachment of the parent class is rewritten in the POP3 subclass. These two subclasses are respectively the anonymous internal classes of Pop3Service and ImapService. The IEmailService Implementation of Exchange Protocol is located in the anonymous internal class of EasService.

1.1 Main interface functions

① Sendmail sends an email.

POP3/IMAP mailboxes use the SMTP protocol to send mails. the sendmail function is implemented in EmailServiceStub.

Exchange mail sends an email over HTTP. The email content is in XML format. Upload the file to the server using the POST method. Exchange Mailbox does not implement sendmail, but uses the syncOutbox function to send mail.

② LoadAttachment: Download the attachment.

POP3 protocol does not support downloading attachments separately. When downloading attachments, you must download all emails. IMAP and Exchange protocols can be used to download attachments separately.

 

③ SearchMessages

SearchMessages does not search for emails in the local database, but the mail content in the mail server. The Email client sends a search command to the server. The server returns the Email containing the keyword to the client. POP3 protocol does not have a search command, so POP3 mailbox does not have a search function.

④ Sync

The synchronization function mainly synchronizes three items.

1. synchronization server folder list

2. Synchronizing emails in outbox is actually sending emails in outbox.

3. Synchronize emails in inbox, the most important synchronization operation.

1.2 C/S structure

Exchange is located in an independent module (I don't know why Exchange is independent. One possible reason is that the calendar and contact modules also use the Exchange function ). Email and Exchange are different processes. Exchange is only responsible for function implementation and does not display the interface. The mail content is displayed in the Email module, and Email and Exchange communicate through the Binder Mechanism. To be consistent with the Exchange Code, the POP3 and IMAP Email Code also adopts the C/S structure (Email is not the same structure before 4.4 ). The code is divided into two parts: Proxy (client) and Stub (Service. Only POP3 and IMAP services are in the Email process, rather than cross-process communication. The Service of Exchange is located in the Exchange process and is a cross-process communication.

The IEmailService interface is also implemented on the proxy. The implementation class is EmailServiceProxy (the proxy class only transfers the request to Stub ). The upper-Layer Code only interacts with EmailServiceProxy.

Bind. The biggest advantage of this structure is that the upper-Layer Code only needs to be programmed for the IEmailService interface, so there is no need to care about the differences between the three protocols.

When an email operation is required, an EmailServiceProxy object is created. Then, call the corresponding function for execution. For example, execute the following statement when downloading the attachment:

New EmailServiceProxy (context, class). loadAttachment (attachmentId, callback)

Bind different services according to the parameter class and execute the download code for different protocols. POP mailbox is bound to Pop3Service, IMAP mailbox is bound to ImapService, and Exchange Mailbox is bound to EasService. Pop3Service, ImapService, and EasService will return the implementation of the IEmailService interface in the onbind function. EmailServiceProxy creates an asynchronous task and requests the server to download the attachment in the asynchronous task. After the attachment is downloaded, the agent is disconnected from the server. Therefore, an EmailServiceProxy object can only perform mail operations once.

2. synchronization framework

Since the 4.4 platform, Email adopts the Android synchronization framework to synchronize emails. The POP3 mailbox is used as an example to describe the mail synchronization process by the synchronization framework.

2.1 synchronization framework Introduction

To enable the Framework layer to call the Synchronous Code of Email, Email must provide a Bound Service class. This class is returned to the framework layer in the onbind function as a binder reference for synchronous operations. When synchronization is required, the synchronization framework remotely calls the ondomainmsync function through this reference to perform synchronization. The binder is the key to connecting the framework layer and our applications. So how to create this Bind object?

First, this object must be a Bind object, and the on‑msync function must be implemented. Google provides the AbstractThreadedSyncAdapter class. We only need to inherit AbstractThreadedSyncAdapter to override ondomainmsync.

The code for creating the binder in POP3 mailbox is as follows:

Private static class SyncAdapterImpl extends actthreadedsyncadapter {

Public SyncAdapterImpl (Context context ){

Super (context, true/* autoInitialize */);

}

@ Override

Public void onPerformSync (android. accounts. Account account Account, Bundle extras,

String authority, ContentProviderClient provider, SyncResult syncResult ){

PopImapSyncAdapterService. javasmsync (getContext (), account, extras, provider,

SyncResult );

}

}

Generally, this binder reference is created in the onCreate function of the Bound Service. Call getSyncAdapterBinder in the onBinder function to return the binder. The onCreate and onBind functions of the Bound Service are as follows:

@ Override

Public void onCreate (){

Super. onCreate ();

MSyncAdapter = new SyncAdapterImpl (getApplicationContext ());

}

@ Override

Public IBinder onBind (Intent intent ){

Return mSyncAdapter. getSyncAdapterBinder ();

}

 

In addition to a special binder, The bound Service also has some special features during declaration. Android. content. SyncAdapter action must be added to intent-filter so that the framework layer can issue intent binding to the service. You also need to add metadata files to meta-data. This file is located in the/res/xml/directory. The file describes the Account type to be synchronized, the authority of the content provider used by the application, and synchronization control. For details, see the official documentation.

Adt-bundle-windows-x86-20140702 \ sdk \ docs \ training \ sync-adapters \ index.html

The bound Service Statement for POP3 mailbox synchronization in Email is as follows:

<Service

Android: name = "com. android. email. service. Pop3SyncAdapterService"

Android: exported = "true">

<Intent-filter>

<Action

Android: name = "android. content. SyncAdapter"/>

</Intent-filter>

<Meta-data android: name = "android. content. SyncAdapter"

Android: resource = "@ xml/syncadapter_pop3"/>

</Service>

The metadata syncadapter_pop3 is as follows:

<Sync-adapter xmlns: android = "http://schemas.android.com/apk/res/android"

Android: contentAuthority = "@ string/authority_email_provider"

Android: accountType = "@ string/account_manager_type_pop3"

Android: supportsUploading = "true"

Android: allowParallelSyncs = "true"

/>

 

AllowParallelSyncs indicates that multiple accounts of the same type can be created, and synchronous operations can be performed concurrently. For example, you can create two Pop3 mailboxes, which can be synchronized at the same time.

Android: supportsUploading: sets whether the yychange notification is required for synchronization.

Defaults to true and if true an upload-only sync will be requested for all syncadapters associated with an authority whenever that authority's content provider does a policychange (android.net. uri, android. database. contentObserver, boolean) with syncToNetwork set to true.

If supportsUploading is set to true, synchronization is performed when ContentResolver. policychange is executed. POP3/IMAP mailbox supportsUploading = true. I didn't know about this option before, which caused a lot of trouble. When the email star adding operation is performed, it is immediately synchronized to the server. The Code cannot be synchronized until then. Later we found that the synchronization was executed when ContentResolver. policychange was executed. Exchange Mailbox supportsUploading = false

When the mail status is changed, you must manually request the framework to perform synchronization.

In Email, the synchronization service used for POP3 mailbox is Pop3SyncAdapterService, IMAP mailbox is LegacyImapSyncAdapterService, and Exchange Mailbox is EmailSyncAdapterService. Exchange mail can also synchronize contacts and calendars. The synchronization services are ContactsSyncAdapterService and CalendarSyncAdapterService.

2.2 Use the synchronization framework for synchronization

There are two ways to perform the synchronization operation in Email. One is to manually refresh and execute synchronization, and the other is to execute synchronization periodically. Using the synchronization framework can be very simple to implement these two operations. During manual synchronization, you only need to execute ContentResolver. requestSync (). The framework layer calls back the ondomainmsync function to perform the synchronization operation.

Call ContentResolver. addPeriodicSync () during periodic synchronization, and specify the synchronization period in the parameter to periodically execute the synchronization operation. You can specify

Using the synchronization framework to execute synchronization operations has many advantages. First, it is easier to write the synchronization code. You only need to provide an interface for calling at the framework layer. Previously, periodic Email synchronization was implemented using AlarmManager, and the code was much more complicated than the synchronization framework. Second, the synchronization framework checks network connections when performing synchronization. If there is no network link, the synchronization operation is not performed. The re-synchronization framework also provides a user verification mechanism.

3 protocol layer interface (Exchange part)

The Exchange Protocol interacts with the server through the HTTP protocol. Both the sent command and returned result are in XML format.

Executing the Exchange command is actually sending a POST request to the server and parsing the response result. The command contains many parameters, which are placed in the corresponding tag of the xml file. The Exchange Protocol provides a detailed description of the role and parameters of each command. For details, refer to the Microsoft documentation.

Https://msdn.microsoft.com/en-us/library/ee200913 (v = exchg.80). aspx

To execute the Exchange command, send a POST request to handle common errors and parse the returned results.

The define moperation method of Exchange in the EasOperation class defines this set of procedures. The procedure moperation is a template method that calls many virtual functions to send requests and parse results. EasOperation has many subclasses. Each class corresponds to an Exchange operation. Subclass only needs to override some virtual functions to implement the defined operation.

① EasMoveItems

② EasLoadAttachment

③ EasFullSyncOperation

④ EasFolderSync synchronizes the folder list.

⑤ EasOutboxSync enables email sending

⑥ EasSearch supports searching emails

7. EasSync implements the mail synchronization function for a folder (upload)

⑧ EasSyncBase implements the mail synchronization function for a folder (download)

EasSync and EasSyncBase work together to synchronize folders

The implementation of the EasFullSyncOperation class does not correspond to a specific Exchange command. Instead, it calls other classes in performOperation to synchronize the entire mailbox.



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.